Merge pull request #184 from harmony-one/version-check

[cli] Check for updating version on error
pull/188/head v1.0.5
Daniel Van Der Maden 5 years ago committed by GitHub
commit 80a4f42df3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      Makefile
  2. 22
      cmd/subcommands/root.go

@ -9,7 +9,9 @@ ldflags := -X main.version=v${version} -X main.commit=${commit}
ldflags += -X main.builtAt=${built_at} -X main.builtBy=${built_by}
cli := ./dist/hmy
upload-path-darwin := 's3://pub.harmony.one/release/darwin-x86_64/mainnet/hmy'
upload-path-darwin-version := 's3://pub.harmony.one/release/darwin-x86_64/mainnet/hmy_version'
upload-path-linux := 's3://pub.harmony.one/release/linux-x86_64/mainnet/hmy'
upload-path-linux-version := 's3://pub.harmony.one/release/linux-x86_64/mainnet/hmy_version'
env := GO111MODULE=on
@ -44,9 +46,13 @@ test-rpc:
# Notice assumes you have correct uploading credentials
upload-darwin:all
aws --profile upload s3 cp ./hmy ${upload-path-darwin}
./hmy version &> ./hmy_version
aws --profile upload s3 cp ./hmy_version ${upload-path-darwin-version}
upload-linux:static
aws --profile upload s3 cp ./hmy ${upload-path-linux}
./hmy version &> ./hmy_version
aws --profile upload s3 cp ./hmy_version ${upload-path-linux-version}
.PHONY:clean run-tests upload-darwin upload-linux

@ -1,8 +1,10 @@
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"regexp"
@ -158,14 +160,30 @@ var (
// VersionWrapDump meant to be set from main.go
VersionWrapDump = ""
cookbook = color.GreenString("hmy cookbook")
versionLink = "https://harmony.one/hmycli_ver"
versionFormat = regexp.MustCompile("v[0-9]+-[0-9]{6}")
)
// Execute kicks off the hmy CLI
func Execute() {
RootCmd.SilenceErrors = true
if err := RootCmd.Execute(); err != nil {
fmt.Println(errors.Wrapf(err, "commit: %s, error", VersionWrapDump).Error())
fmt.Println("check " + cookbook + " for valid examples or try adding a `--help` flag")
resp, _ := http.Get(versionLink)
defer resp.Body.Close()
// If error, no op
if resp != nil && resp.StatusCode == 200{
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
currentVersion := versionFormat.FindAllString(buf.String(), 1)
if currentVersion != nil && currentVersion[0] != VersionWrapDump {
warnMsg := fmt.Sprintf("Warning: Using outdated version. Redownload to upgrade to %s\n", currentVersion[0])
fmt.Fprintf(os.Stderr, color.RedString(warnMsg))
}
}
errMsg := errors.Wrapf(err, "commit: %s, error", VersionWrapDump).Error()
fmt.Fprintf(os.Stderr, errMsg + "\n")
fmt.Fprintf(os.Stderr, "check " + cookbook + " for valid examples or try adding a `--help` flag\n")
os.Exit(1)
}
}

Loading…
Cancel
Save