Skip to content

Commit

Permalink
feat(update): represent states better in update cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhao authored and elldritch committed Feb 23, 2018
1 parent 95e446e commit 7ce1571
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/fossa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func defaultCmd(c *cli.Context) {
mainLogger.Fatalf("Could not load configuration: %s", err.Error())
}

if err := checkUpdate(); err != nil {
if ok, err := checkUpdate(); ok && err != nil {
mainLogger.Infof("An update is available for this CLI; run `fossa update` to get the latest version.")
}

Expand Down
16 changes: 10 additions & 6 deletions cmd/fossa/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ func getSemver() string {
return version[1:]
}

func checkUpdate() error {
func checkUpdate() (bool, error) {
latest, found, err := selfupdate.DetectLatest(updateEndpoint)
if err != nil {
return err // unable to update due to GH error
return false, err // unable to update due to GH error
}

parsedVersion := getSemver()
updateLogger.Debugf("checking version for updates (%s -> %s)", version, parsedVersion)
v, err := semver.Parse(parsedVersion)
if err != nil {
return fmt.Errorf("invalid version; you may be using a development binary")
return false, fmt.Errorf("invalid version; you may be using a development binary")
}
if !found || latest.Version.Equals(v) {
return fmt.Errorf("current binary is latest version")
return false, nil
}
return nil
return true, nil
}

func doSelfUpdate() error {
Expand All @@ -57,9 +57,13 @@ func doSelfUpdate() error {
}

func updateCmd(c *cli.Context) {
if err := checkUpdate(); err != nil {
ok, err := checkUpdate()
if err != nil {
updateLogger.Fatalf("Unable to update: %s", err.Error())
}
if !ok {
updateLogger.Fatalf("No updates available")
}

if err := doSelfUpdate(); err != nil {
updateLogger.Fatalf("Update failed: %s", err.Error())
Expand Down

0 comments on commit 7ce1571

Please sign in to comment.