Skip to content

Commit

Permalink
Fix optional version checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
camerondurham committed May 28, 2021
1 parent b2696fc commit e3dca2c
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var rootCmd = &cobra.Command{

func Execute() {

checkCliVersion()
optionalVersionCheck()

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -68,25 +68,33 @@ func initConfig() {
}
}

func checkCliVersion() {
func optionalVersionCheck() {
opts, err := util.GetConfigOpts()

if err != nil {
util.DebugPrint(fmt.Sprintf("error reading environment file: %v", err))
} else if v, ok := opts["version_check"]; !ok || v == "true" {
latestVersion, err := util.GetLatestVersion(util.GetRequest)
if err != nil {
util.DebugPrint(fmt.Sprintf("ignoring version check since error occured when retrieving latest version: %v\n", err))
} else if version.PkgVersion != "" && latestVersion != version.PkgVersion {
fmt.Printf(
"\tA new version of ch is available!\n"+
"\tYou are running version %s but the latest version is %s.\n"+
"\tSee %s instructions on upgrading.\n",
version.PkgVersion,
latestVersion,
RepositoryUrl)
} else {
util.DebugPrint(fmt.Sprintf("local package version: %s\nlatest version: %s\nversion_check: %s", version.PkgVersion, latestVersion, v))
}
checkLatestVersion()
}
if v, ok := opts["version_check"]; !ok || v == "true" {
checkLatestVersion()
} else {
util.DebugPrint(fmt.Sprintf("local package version: %s\nlatest version: %s\n", version.PkgVersion, v))
}
}

func checkLatestVersion() {
latestVersion, err := util.GetLatestVersion(util.GetRequest)
if err != nil {
util.DebugPrint(fmt.Sprintf("ignoring version check since error occured when retrieving latest version: %v\n", err))
} else if version.PkgVersion != "" && latestVersion != version.PkgVersion {
fmt.Printf(
"\tA new version of ch is available!\n"+
"\tYou are running version %s but the latest version is %s.\n"+
"\tSee %s instructions on upgrading.\n",
version.PkgVersion,
latestVersion,
RepositoryUrl)
} else {
util.DebugPrint(fmt.Sprintf("local package version: %s\nlatest version: %s\n", version.PkgVersion, latestVersion))
}
}

0 comments on commit e3dca2c

Please sign in to comment.