-
Notifications
You must be signed in to change notification settings - Fork 66
fix: enable self updating CLI #1509
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ca71151
fix: enable self updating CLI
wtrocki 24e374b
fix: reorder self update method
wtrocki 7c888a3
fix: remove fmt
wtrocki 00ae50e
fix: add update confirmation
wtrocki 7ac26d3
fix: remove temp check
wtrocki 5a939fc
fix: add comment
wtrocki 566f9cf
fix: return if not possible to determine update
wtrocki 2c6fa89
fix: use our own package until we get upstream fixed
wtrocki 377a634
fix: demete submodule
wtrocki bf1312c
fix: update message to include version
wtrocki a129d76
fix: update package with fix
wtrocki 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,86 @@ | ||
| package cmdutil | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/AlecAivazis/survey/v2" | ||
| "github.com/blang/semver" | ||
| "github.com/redhat-developer/app-services-cli/internal/build" | ||
| "github.com/redhat-developer/app-services-cli/pkg/core/localize" | ||
| "github.com/redhat-developer/app-services-cli/pkg/shared/factory" | ||
| "github.com/wtrocki/go-github-selfupdate/selfupdate" | ||
| ) | ||
|
|
||
| // DoSelfUpdate checks for updates and prompts the user to update if there is a newer version available | ||
| func DoSelfUpdate(f *factory.Factory) (bool, error) { | ||
| version := build.Version | ||
|
|
||
| v := semver.MustParse(version) | ||
| versionToUpdate, found, err := selfupdate.DefaultUpdater().DetectLatest(version) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if found && versionToUpdate.Version.Equals(v) { | ||
| // latest version is the same as current version. It means current binary is up to date. | ||
| f.Logger.Debug("Current binary is the latest version", version) | ||
| return false, nil | ||
| } | ||
|
|
||
| promptConfirmName := &survey.Confirm{ | ||
| Message: f.Localizer.MustLocalize("common.selfupdate.confirm"), | ||
| } | ||
|
|
||
| var confirmUpdate bool | ||
| err = survey.AskOne(promptConfirmName, &confirmUpdate) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| if !confirmUpdate { | ||
| return false, nil | ||
| } | ||
|
|
||
| latest, err := selfupdate.UpdateSelf(v, build.RepositoryOwner+"/"+build.RepositoryName) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| f.Logger.Info(f.Localizer.MustLocalize("common.selfupdate.success", localize.NewEntry("Version", latest.Version))) | ||
| return true, err | ||
|
|
||
| } | ||
|
|
||
| // DoSelfUpdate checks for updates once per day and prompts the user to update if there is a newer version available | ||
| func DoSelfUpdateOnceADay(f *factory.Factory) (bool, error) { | ||
| if !f.IOStreams.CanPrompt() { | ||
| // Do not prompt if we are not in interactive mode | ||
| return false, nil | ||
| } | ||
|
|
||
| if build.IsDevBuild() { | ||
| return false, nil | ||
| } | ||
|
|
||
| cfg, err := f.Config.Load() | ||
|
|
||
| logger := f.Logger | ||
|
|
||
| if err != nil { | ||
| return false, err | ||
| } | ||
| logger.Debug("Checking for updates. Last check was done:", cfg.LastUpdated) | ||
|
|
||
| if cfg.LastUpdated < time.Now().AddDate(0, 0, -1).UnixMilli() { | ||
| updated, err := DoSelfUpdate(f) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| cfg.LastUpdated = time.Now().UnixMilli() | ||
| err = f.Config.Save(cfg) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return updated, nil | ||
| } | ||
| return false, nil | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.