Skip to content

Commit

Permalink
Merge pull request #51 from DopplerHQ/tom_fix_version
Browse files Browse the repository at this point in the history
Fix version check printing unnecessarily
  • Loading branch information
Piccirello authored Dec 30, 2019
2 parents 444ede0 + 245b986 commit 53c6595
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
48 changes: 33 additions & 15 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd
import (
"fmt"
"os"
"time"

"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/http"
Expand All @@ -41,27 +42,44 @@ var rootCmd = &cobra.Command{
fmt.Println("")
}

silent := utils.GetBoolFlagIfChanged(cmd, "silent", false)
plain := utils.GetBoolFlagIfChanged(cmd, "plain", false)
canPrintResults := utils.Debug || (!silent && !plain && !utils.OutputJSON)

// disable version checking on the "run" command
if version.PerformVersionCheck && canPrintResults && cmd.CalledAs() != "run" {
versionCheck := http.CheckCLIVersion(configuration.VersionCheck(), silent, utils.OutputJSON, utils.Debug)
if versionCheck != (models.VersionCheck{}) {
if version.ProgramVersion != versionCheck.LatestVersion {
fmt.Printf("Doppler CLI version %s is now available\n", versionCheck.LatestVersion)
}

configuration.SetVersionCheck(versionCheck)
}
}
silent := utils.GetBoolFlagIfChanged(cmd, "silent", false)
plain := utils.GetBoolFlagIfChanged(cmd, "plain", false)
canPrintResults := utils.Debug || (!silent && !plain && !utils.OutputJSON)
checkVersion(cmd.CalledAs(), silent, plain, canPrintResults)
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}

func checkVersion(command string, silent bool, plain bool, print bool) {
// disable version checking on the "run" command
if command == "run" {
return
}

if !version.PerformVersionCheck || !print {
return
}

prevVersionCheck := configuration.VersionCheck()
// don't check more often than every 24 hours
if !time.Now().After(prevVersionCheck.CheckedAt.Add(24 * time.Hour)) {
return
}

versionCheck := http.CheckCLIVersion(prevVersionCheck, silent, utils.OutputJSON, utils.Debug)
if versionCheck == (models.VersionCheck{}) {
return
}

if version.ProgramVersion != versionCheck.LatestVersion {
fmt.Printf("Doppler CLI %s is now available\n", versionCheck.LatestVersion)
}

configuration.SetVersionCheck(versionCheck)
}

func loadFlags(cmd *cobra.Command) {
configuration.UserConfigPath = utils.GetFlagIfChanged(cmd, "configuration", configuration.UserConfigPath)
http.TimeoutDuration = utils.GetDurationFlagIfChanged(cmd, "timeout", http.TimeoutDuration)
Expand Down
21 changes: 13 additions & 8 deletions pkg/http/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/DopplerHQ/cli/pkg/models"
Expand Down Expand Up @@ -50,12 +51,6 @@ func getLatestVersion() (string, error) {

// CheckCLIVersion check for updates to the CLI
func CheckCLIVersion(versionCheck models.VersionCheck, silent bool, json bool, debug bool) models.VersionCheck {
now := time.Now()
// only check version if more than a day since last check
if !now.After(versionCheck.CheckedAt.Add(24 * time.Hour)) {
return models.VersionCheck{}
}

utils.LogDebug("Checking for latest version of the CLI")
tag, err := getLatestVersion()
if err != nil {
Expand All @@ -68,10 +63,20 @@ func CheckCLIVersion(versionCheck models.VersionCheck, silent bool, json bool, d
return models.VersionCheck{}
}

versionCheck.CheckedAt = now
if versionCheck.LatestVersion != tag {
versionCheck.CheckedAt = time.Now()
tag = normalizeVersion(tag)
if normalizeVersion(versionCheck.LatestVersion) != tag {
versionCheck.LatestVersion = tag
}

return versionCheck
}

func normalizeVersion(version string) string {
version = strings.TrimSpace(version)
if !strings.HasPrefix(version, "v") {
return "v" + version
}

return version
}

0 comments on commit 53c6595

Please sign in to comment.