Skip to content

Commit

Permalink
Merge pull request #184 from harmony-one/version-check
Browse files Browse the repository at this point in the history
[cli] Check for updating version on error
  • Loading branch information
Daniel-VDM authored Mar 21, 2020
2 parents 73ea354 + 44f8d30 commit 80a4f42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions cmd/subcommands/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"regexp"
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 80a4f42

Please sign in to comment.