Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit 6dc142a

Browse files
committed
Merge pull request #15 from pavanka/supported_versions
check if version is supported
2 parents fb751be + c411347 commit 6dc142a

File tree

5 files changed

+122
-93
lines changed

5 files changed

+122
-93
lines changed

main.go

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net/url"
88
"os"
9-
"os/exec"
109
"os/signal"
1110
"path/filepath"
1211
"runtime"
@@ -26,10 +25,7 @@ const (
2625
defaultBaseURL = "https://api.parse.com/1/"
2726
)
2827

29-
var (
30-
autoUpdate = false
31-
userAgent = fmt.Sprintf("parse-cli-%s-%s", runtime.GOOS, version)
32-
)
28+
var userAgent = fmt.Sprintf("parse-cli-%s-%s", runtime.GOOS, version)
3329

3430
type versionCmd struct{}
3531

@@ -377,39 +373,13 @@ func main() {
377373
}
378374
e.Client = client
379375

380-
// autoUpdate is false for all non-production builds
381-
// so we never auto update
382-
// for production builds autoUpdate is true but
383-
// we suppress auto-update iff PARSE_NOUPDATE is not set
384-
if autoUpdate && os.Getenv("PARSE_NOUPDATE") == "" {
385-
// Perform a best effort update
386-
updated, err := (&updateCmd{}).updateCLI(&e)
387-
if err != nil {
388-
cmd := exec.Command(os.Args[0], "version")
389-
if err := cmd.Run(); err != nil {
390-
fmt.Fprintf(e.Out, `parse cli corrupted during update.
391-
Please follow instructions at:
392-
https://parse.com/apps/quickstart#cloud_code
393-
to install a new cli.`)
394-
os.Exit(1)
395-
}
396-
}
397-
398-
if updated {
399-
// Re-run the command with the updated CLI
400-
cmd := exec.Cmd{
401-
Path: os.Args[0],
402-
Args: os.Args,
403-
Stdin: os.Stdin,
404-
Stdout: os.Stdout,
405-
Stderr: os.Stderr,
406-
}
407-
408-
if err := cmd.Run(); err != nil {
409-
os.Exit(1)
410-
}
411-
return
412-
}
376+
message, err := checkIfSupported(&e, version)
377+
if err != nil {
378+
fmt.Fprintln(e.Err, err)
379+
os.Exit(1)
380+
}
381+
if message != "" {
382+
fmt.Fprintln(e.Err, message)
413383
}
414384

415385
if err := rootCmd(&e).Execute(); err != nil {

update_cmd.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"net/url"
7-
"path"
87
"runtime"
9-
"strings"
108

119
"github.com/facebookgo/stackerr"
1210
"github.com/inconshreveable/go-update"
@@ -15,50 +13,60 @@ import (
1513
)
1614

1715
const (
18-
macCliDownloadURL = "https://parse.com/downloads/cloud_code/cli/parse-osx/latest"
19-
unixCliDownloadURL = "https://parse.com/downloads/cloud_code/cli/parse-linux/latest"
20-
windowsCliDownloadURL = "https://parse.com/downloads/cloud_code/cli/parse-windows/latest"
16+
macDownload = "parse"
17+
windowsDownload = "parse.exe"
18+
linuxDownload = "parse_linux"
19+
linuxArmDownload = "parse_linux_arm"
20+
downloadURLFormat = "https://github.com/ParsePlatform/parse-cli/releases/download/release_%s/%s"
2121
)
2222

2323
type updateCmd struct{}
2424

25-
func (u *updateCmd) latestVersion(e *env, downloadURL string) (string, error) {
26-
dURL, err := url.Parse(downloadURL)
27-
if err != nil {
28-
return "", stackerr.Wrap(err)
25+
func (u *updateCmd) latestVersion(e *env) (string, error) {
26+
v := make(url.Values)
27+
v.Set("version", "latest")
28+
req := &http.Request{
29+
Method: "GET",
30+
URL: &url.URL{Path: "supported", RawQuery: v.Encode()},
2931
}
3032

31-
resp, err := e.Client.Do(&http.Request{Method: "HEAD", URL: dURL}, nil, nil)
32-
if err != nil {
33-
return "", nil // if unable to fetch latest cli version, do not abort!
33+
var res struct {
34+
Version string `json:"version"`
3435
}
3536

36-
base := path.Base(resp.Header.Get("Location"))
37-
base = strings.TrimSuffix(base, ".exe")
38-
//parse-os-2.0.2
39-
splits := strings.Split(base, "-")
37+
if _, err := e.Client.Do(req, nil, &res); err != nil {
38+
return "", stackerr.Wrap(err)
39+
}
4040

41-
return splits[len(splits)-1], nil
41+
return res.Version, nil
4242
}
4343

4444
func (u *updateCmd) updateCLI(e *env) (bool, error) {
45-
downloadURL := unixCliDownloadURL
46-
switch runtime.GOOS {
47-
case "windows":
48-
downloadURL = windowsCliDownloadURL
49-
case "darwin":
50-
downloadURL = macCliDownloadURL
51-
}
45+
ostype := runtime.GOOS
46+
arch := runtime.GOARCH
5247

53-
latestVersion, err := u.latestVersion(e, downloadURL)
48+
latestVersion, err := u.latestVersion(e)
5449
if err != nil {
55-
return false, stackerr.Wrap(err)
50+
return false, err
5651
}
57-
5852
if latestVersion == "" || latestVersion == version {
5953
return false, nil
6054
}
6155

56+
var downloadURL string
57+
switch ostype {
58+
case "darwin":
59+
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, macDownload)
60+
case "windows":
61+
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, windowsDownload)
62+
case "linux":
63+
if arch == "arm" {
64+
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, linuxArmDownload)
65+
} else {
66+
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, linuxDownload)
67+
}
68+
}
69+
6270
exec, err := osext.Executable()
6371
if err != nil {
6472
return false, stackerr.Wrap(err)

update_cmd_test.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package main
33
import (
44
"io/ioutil"
55
"net/http"
6-
"strings"
76
"testing"
87

98
"github.com/facebookgo/ensure"
9+
"github.com/facebookgo/jsonpipe"
1010
"github.com/facebookgo/parse"
1111
)
1212

@@ -17,37 +17,20 @@ func TestLatestVersion(t *testing.T) {
1717
defer h.Stop()
1818

1919
ht := transportFunc(func(r *http.Request) (*http.Response, error) {
20-
var result string
21-
switch r.URL.String() {
22-
case windowsCliDownloadURL:
23-
result = "http://parse-cli.aws.com/hash/parse-windows-2.0.2.exe"
24-
case unixCliDownloadURL:
25-
result = "http://parse-cli.aws.com/hash/parse-linux-2.0.2"
26-
case macCliDownloadURL:
27-
result = "http://parse-cli.aws.com/hash/parse-osx-2.0.2"
28-
}
29-
resp := http.Response{
20+
ensure.DeepEqual(t, r.URL.Path, "/1/supported")
21+
return &http.Response{
3022
StatusCode: http.StatusOK,
31-
Body: ioutil.NopCloser(strings.NewReader("Success!")),
32-
}
33-
if resp.Header == nil {
34-
resp.Header = make(http.Header)
35-
}
36-
resp.Header.Set("Location", result)
37-
return &resp, nil
23+
Body: ioutil.NopCloser(
24+
jsonpipe.Encode(
25+
map[string]string{"version": "2.0.2"},
26+
),
27+
),
28+
}, nil
3829
})
3930
h.env.Client = &Client{client: &parse.Client{Transport: ht}}
4031
u := new(updateCmd)
4132

42-
version, err := u.latestVersion(h.env, unixCliDownloadURL)
33+
latestVersion, err := u.latestVersion(h.env)
4334
ensure.Nil(t, err)
44-
ensure.DeepEqual(t, version, "2.0.2")
45-
46-
version, err = u.latestVersion(h.env, macCliDownloadURL)
47-
ensure.Nil(t, err)
48-
ensure.DeepEqual(t, version, "2.0.2")
49-
50-
version, err = u.latestVersion(h.env, windowsCliDownloadURL)
51-
ensure.Nil(t, err)
52-
ensure.DeepEqual(t, version, "2.0.2")
35+
ensure.DeepEqual(t, latestVersion, "2.0.2")
5336
}

utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"net/http"
67
"net/url"
78
"regexp"
89
"strconv"
@@ -104,3 +105,21 @@ func getHostFromURL(urlStr string) (string, error) {
104105
}
105106
return server, nil
106107
}
108+
109+
func checkIfSupported(e *env, version string) (string, error) {
110+
v := make(url.Values)
111+
v.Set("version", version)
112+
req := &http.Request{
113+
Method: "GET",
114+
URL: &url.URL{Path: "supported", RawQuery: v.Encode()},
115+
}
116+
117+
var res struct {
118+
Warning string `json:"warning"`
119+
}
120+
121+
if _, err := e.Client.Do(req, nil, &res); err != nil {
122+
return "", stackerr.Wrap(err)
123+
}
124+
return res.Warning, nil
125+
}

utils_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package main
22

33
import (
44
"crypto/rand"
5+
"io/ioutil"
6+
"net/http"
57
"regexp"
68
"testing"
79

810
"github.com/facebookgo/ensure"
11+
"github.com/facebookgo/jsonpipe"
12+
"github.com/facebookgo/parse"
913
)
1014

1115
func TestBothEmpty(t *testing.T) {
@@ -83,3 +87,48 @@ func TestGetHostFromURL(t *testing.T) {
8387
host, err = getHostFromURL(urlStr)
8488
ensure.Err(t, err, regexp.MustCompile("not a valid url"))
8589
}
90+
91+
func TestIsSupportedWarning(t *testing.T) {
92+
t.Parallel()
93+
94+
h := newHarness(t)
95+
defer h.Stop()
96+
97+
ht := transportFunc(func(r *http.Request) (*http.Response, error) {
98+
ensure.DeepEqual(t, r.URL.Path, "/1/supported")
99+
return &http.Response{
100+
StatusCode: http.StatusOK,
101+
Body: ioutil.NopCloser(
102+
jsonpipe.Encode(
103+
map[string]string{"warning": "please update"},
104+
),
105+
),
106+
}, nil
107+
})
108+
h.env.Client = &Client{client: &parse.Client{Transport: ht}}
109+
message, err := checkIfSupported(h.env, "2.0.2")
110+
ensure.Nil(t, err)
111+
ensure.DeepEqual(t, message, "please update")
112+
}
113+
114+
func TestIsSupportedError(t *testing.T) {
115+
t.Parallel()
116+
117+
h := newHarness(t)
118+
defer h.Stop()
119+
120+
ht := transportFunc(func(r *http.Request) (*http.Response, error) {
121+
ensure.DeepEqual(t, r.URL.Path, "/1/supported")
122+
return &http.Response{
123+
StatusCode: http.StatusBadRequest,
124+
Body: ioutil.NopCloser(
125+
jsonpipe.Encode(
126+
map[string]string{"error": "not supported"},
127+
),
128+
),
129+
}, nil
130+
})
131+
h.env.Client = &Client{client: &parse.Client{Transport: ht}}
132+
_, err := checkIfSupported(h.env, "2.0.2")
133+
ensure.Err(t, err, regexp.MustCompile("not supported"))
134+
}

0 commit comments

Comments
 (0)