Repository navigation
Implement "roxie update" command for self-updating #323
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/creativeprojects/go-selfupdate" | ||
| "github.com/spf13/cobra" | ||
| log "github.com/stackrox/roxie/internal/logger" | ||
| ) | ||
|
|
||
| const ( | ||
| updateRepoOwner = "stackrox" | ||
| updateRepoName = "roxie" | ||
| ) | ||
|
|
||
| func newUpdateCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "update", | ||
| Short: "Update roxie to the latest version", | ||
| Long: `Download and install the latest roxie release from GitHub, replacing the current binary in-place.`, | ||
| RunE: runUpdate, | ||
| } | ||
| } | ||
|
|
||
| func runUpdate(cmd *cobra.Command, _ []string) error { | ||
| if version == "dev" { | ||
| return fmt.Errorf("cannot self-update a development build (version %q); install a released binary first", version) | ||
| } | ||
|
|
||
| updater, err := selfupdate.NewUpdater(selfupdate.Config{ | ||
| Validator: &selfupdate.ChecksumValidator{UniqueFilename: "checksums.txt"}, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("initializing updater: %w", err) | ||
| } | ||
|
|
||
| log.Infof("Checking for updates (current version: %s)...", version) | ||
|
|
||
| latest, found, err := updater.DetectLatest(cmd.Context(), selfupdate.ParseSlug(updateRepoOwner+"/"+updateRepoName)) | ||
| if err != nil { | ||
| return fmt.Errorf("detecting latest version: %w", err) | ||
| } | ||
| if !found { | ||
| return fmt.Errorf("no releases found for %s/%s", updateRepoOwner, updateRepoName) | ||
| } | ||
|
|
||
| if latest.LessOrEqual(version) { | ||
| log.Infof("Already up to date (latest: %s).", latest.Version()) | ||
| return nil | ||
| } | ||
|
|
||
| log.Infof("Updating to %s...", latest.Version()) | ||
|
|
||
| exe, err := os.Executable() | ||
| if err != nil { | ||
| return fmt.Errorf("locating current executable: %w", err) | ||
| } | ||
|
|
||
| if err := updater.UpdateTo(cmd.Context(), latest, exe); err != nil { | ||
| return fmt.Errorf("updating binary: %w", err) | ||
| } | ||
|
Comment on lines
+55
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve symlinks before you replace the executable.
Resolve the path with 🐛 Proposed fix
|
||
|
|
||
| log.Infof("Updated roxie: %s → %s", version, latest.Version()) | ||
| return nil | ||
| } | ||
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.
Script executed:
Repository: stackrox/roxie
Length of output: 152
Script executed:
Repository: stackrox/roxie
Length of output: 1019
Script executed:
Repository: stackrox/roxie
Length of output: 7950
Reject non-semver build versions before calling
LessOrEqual.Release.LessOrEqualcallssemver.MustParse(other). A non-"dev"version that is not valid semver can therefore panic at the comparison instead of returning a clear error.Makefilecan pass rawgit describe --tags --always --dirtyoutput toversion.Suggested fix