-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of state export log
.
#2886
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package export | ||
|
||
import ( | ||
"path/filepath" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
|
||
"github.com/ActiveState/cli/internal/errs" | ||
"github.com/ActiveState/cli/internal/fileutils" | ||
"github.com/ActiveState/cli/internal/logging" | ||
"github.com/ActiveState/cli/internal/output" | ||
) | ||
|
||
type Log struct { | ||
output.Outputer | ||
} | ||
|
||
func NewLog(prime primeable) *Log { | ||
return &Log{prime.Output()} | ||
} | ||
|
||
type LogParams struct { | ||
Prefix string | ||
Index int | ||
} | ||
|
||
type logFile struct { | ||
Name string | ||
Timestamp int | ||
} | ||
|
||
var ErrInvalidLogIndex = errs.New("invalid index") | ||
var ErrInvalidLogPrefix = errs.New("invalid log prefix") | ||
var ErrLogNotFound = errs.New("log not found") | ||
|
||
func (l *Log) Run(params *LogParams) (rerr error) { | ||
defer rationalizeError(&rerr) | ||
|
||
if params.Index < 0 { | ||
return ErrInvalidLogIndex | ||
} | ||
if params.Prefix == "" { | ||
params.Prefix = "state" | ||
} | ||
|
||
// Fetch list of log files. | ||
logDir := filepath.Dir(logging.FilePath()) | ||
logFiles := fileutils.ListDirSimple(logDir, false) | ||
|
||
// Filter down the list based on the given prefix. | ||
filteredLogFiles := []*logFile{} | ||
regex, err := regexp.Compile(params.Prefix + `-\d+-(\d+)\.log`) | ||
if err != nil { | ||
return ErrInvalidLogPrefix | ||
} | ||
for _, file := range logFiles { | ||
if regex.MatchString(file) { | ||
timestamp, err := strconv.Atoi(regex.FindStringSubmatch(file)[1]) | ||
if err != nil { | ||
continue | ||
} | ||
filteredLogFiles = append(filteredLogFiles, &logFile{file, timestamp}) | ||
} | ||
} | ||
|
||
// Sort logs in ascending order by name (which include timestamp), not modification time. | ||
sort.SliceStable(filteredLogFiles, func(i, j int) bool { | ||
return filteredLogFiles[i].Timestamp > filteredLogFiles[j].Timestamp // sort ascending, not descending | ||
}) | ||
|
||
if params.Index >= len(filteredLogFiles) { | ||
return ErrLogNotFound | ||
} | ||
|
||
l.Outputer.Print(output.Prepare( | ||
filteredLogFiles[params.Index].Name, | ||
&struct { | ||
LogFile string `json:"logFile"` | ||
}{filteredLogFiles[params.Index].Name}, | ||
)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package export | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/ActiveState/cli/internal/errs" | ||
"github.com/ActiveState/cli/internal/locale" | ||
) | ||
|
||
func rationalizeError(err *error) { | ||
switch { | ||
// export log with invalid --index. | ||
case errors.Is(*err, ErrInvalidLogIndex): | ||
*err = errs.WrapUserFacing(*err, | ||
locale.Tl("err_export_log_invalid_index", "Index must be >= 0"), | ||
errs.SetInput(), | ||
) | ||
|
||
// export log <prefix> with invalid <prefix>. | ||
case errors.Is(*err, ErrInvalidLogPrefix): | ||
*err = errs.WrapUserFacing(*err, | ||
locale.Tl("err_export_log_invalid_prefix", "Invalid log prefix"), | ||
errs.SetInput(), | ||
errs.SetTips( | ||
locale.Tl("export_log_prefix_tip", "Try a prefix like 'state' or 'state-svc'"), | ||
), | ||
) | ||
|
||
// export log does not turn up a log file. | ||
case errors.Is(*err, ErrLogNotFound): | ||
*err = errs.WrapUserFacing(*err, | ||
locale.Tl("err_export_log_out_of_bounds", "Log file not found"), | ||
errs.SetInput(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the
regex.MatchString
guarantee that we will find a second group? If not the indexing of the slice here is a potential panic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if there is a match, the capture from the pattern guarantees a valid index.