Skip to content

Commit 00ae50e

Browse files
committed
fix: add update confirmation
1 parent 7c888a3 commit 00ae50e

3 files changed

Lines changed: 60 additions & 22 deletions

File tree

cmd/rhoas/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func executeCommandWithTelemetry(rootCmd *cobra.Command, cmdFactory *factory.Fac
179179
if cmd.Runnable() && !cmd.Hidden {
180180
commandPath = cmd.CommandPath()
181181
}
182-
_, err1 := cmdutil.DoSelfUpdateOnceADay(cmdFactory.Logger, cmdFactory.Config)
182+
_, err1 := cmdutil.DoSelfUpdateOnceADay(cmdFactory)
183183
if err1 != nil {
184184
cmdFactory.Logger.Errorf(cmdFactory.Localizer.MustLocalize("main.update.error", localize.NewEntry("Error", err)))
185185
}

pkg/core/cmdutil/selfupdate.go

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,81 @@ package cmdutil
33
import (
44
"time"
55

6+
"github.com/AlecAivazis/survey/v2"
67
"github.com/blang/semver"
78
"github.com/redhat-developer/app-services-cli/internal/build"
8-
"github.com/redhat-developer/app-services-cli/pkg/core/config"
9-
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
9+
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
10+
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
1011
"github.com/rhysd/go-github-selfupdate/selfupdate"
1112
)
1213

13-
func DoSelfUpdate(logger logging.Logger) (bool, error) {
14+
// DoSelfUpdate checks for updates and prompts the user to update if there is a newer version available
15+
func DoSelfUpdate(f *factory.Factory) (bool, error) {
1416
version := build.Version
17+
// TODO temp
1518
if build.IsDevBuild() {
16-
// TODO uncomment later
17-
//return false, nil
1819
version = "0.0.0"
1920
}
2021

2122
v := semver.MustParse(version)
22-
latest, err := selfupdate.UpdateSelf(v, build.RepositoryOwner+"/"+build.RepositoryName)
23+
versionToUpdate, found, err := selfupdate.DefaultUpdater().DetectLatest(version)
24+
25+
if found && versionToUpdate.Version.Equals(v) {
26+
// latest version is the same as current version. It means current binary is up to date.
27+
f.Logger.Debug("Current binary is the latest version", version)
28+
return false, nil
29+
}
30+
31+
promptConfirmName := &survey.Confirm{
32+
Message: f.Localizer.MustLocalize("common.selfupdate.confirm"),
33+
}
34+
35+
var confirmUpdate bool
36+
err = survey.AskOne(promptConfirmName, &confirmUpdate)
2337
if err != nil {
2438
return false, err
2539
}
26-
if latest.Version.Equals(v) {
27-
// latest version is the same as current version. It means current binary is up to date.
28-
logger.Debug("Current binary is the latest version", version)
29-
} else {
30-
logger.Info("Successfully updated RHOAS CLI to latest version", latest.Version)
31-
logger.Info("Release notes:\n", latest.ReleaseNotes)
32-
return true, err
40+
41+
if !confirmUpdate {
42+
return false, nil
3343
}
3444

35-
return false, nil
45+
latest, err := selfupdate.UpdateSelf(v, build.RepositoryOwner+"/"+build.RepositoryName)
46+
if err != nil {
47+
return false, err
48+
}
49+
50+
f.Logger.Info(f.Localizer.MustLocalize("common.selfupdate.success", localize.NewEntry("Version", latest.Version)))
51+
return true, err
52+
3653
}
3754

38-
func DoSelfUpdateOnceADay(logger logging.Logger, loader config.IConfig) (bool, error) {
39-
cfg, err := loader.Load()
55+
func DoSelfUpdateOnceADay(f *factory.Factory) (bool, error) {
56+
if !f.IOStreams.CanPrompt() {
57+
// Do not prompt if we are not in interactive mode
58+
return false, nil
59+
}
60+
61+
if build.IsDevBuild() {
62+
return false, nil
63+
}
64+
65+
cfg, err := f.Config.Load()
66+
67+
logger := f.Logger
68+
4069
if err != nil {
4170
return false, err
4271
}
43-
logger.Debug("Last updated cli", cfg.LastUpdated)
72+
logger.Debug("Checking for updates. Last check was done:", cfg.LastUpdated)
4473

4574
if cfg.LastUpdated < time.Now().AddDate(0, 0, -1).UnixMilli() {
46-
logger.Debug("Updating CLI")
47-
48-
updated, err := DoSelfUpdate(logger)
75+
updated, err := DoSelfUpdate(f)
4976
if err != nil {
5077
return false, err
5178
}
5279
cfg.LastUpdated = time.Now().UnixMilli()
53-
err = loader.Save(cfg)
80+
err = f.Config.Save(cfg)
5481
if err != nil {
5582
return false, err
5683
}

pkg/core/localize/locales/en/common.en.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ You can also disable telemetry by setting the "RHOAS_TELEMETRY" environment vari
2121

2222
[common.telemetry.question]
2323
one = 'Do you agree to send anonymous data'
24+
25+
[common.selfupdate.confirm]
26+
one = '''
27+
New version of the RHOAS CLI detected.
28+
Do you want CLI to be updated to the latest version now?
29+
30+
NOTE: You might need to run the CLI as root to make sure that binary file can be updated.
31+
'''
32+
33+
[common.selfupdate.success]
34+
one = 'RHOAS CLI updated to version {{.Version}}'

0 commit comments

Comments
 (0)