-
Notifications
You must be signed in to change notification settings - Fork 84
/
common.go
69 lines (61 loc) · 2.26 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package common
import (
"context"
"fmt"
"github.com/google/go-github/v57/github"
"github.com/savioxavier/termlink"
)
var (
// OutputFields for custom format output
OutputFields string
// OutputFormat for custom format output
OutputFormat string
// RegionSet picks the region to connect to, if you use this option it will use it over the default region
RegionSet string
// DefaultYes : automatic yes to prompts; assume \"yes\" as answer to all prompts and run non-interactively
DefaultYes bool
// PrettySet : Prints the json output in pretty format
PrettySet bool
// VersionCli is set from outside using ldflags
VersionCli = "0.0.0"
// CommitCli is set from outside using ldflags
CommitCli = "none"
// DateCli is set from outside using ldflags
DateCli = "unknown"
)
// GithubClient Create a Github client
func GithubClient() *github.Client {
return github.NewClient(nil)
}
// CheckVersionUpdate checks if there's an update to be done
func CheckVersionUpdate() {
ghClient := GithubClient()
res, skip := VersionCheck(ghClient)
if skip {
return
}
// Check if the version is different from the one in the binary
if res.TagName != nil && *res.TagName != fmt.Sprintf("v%s", VersionCli) {
if res.TagName != nil && *res.TagName != VersionCli {
fmt.Printf("A newer version (%s) is available, please upgrade with \"civo update\"\n", *res.TagName)
}
}
}
// IssueMessage is the message to be displayed when an error is returned
func IssueMessage() {
gitIssueLink := termlink.ColorLink("GitHub issue", "https://github.com/civo/cli/issues", "green")
fmt.Printf("Please check if you are using the latest version of CLI and retry the command \nIf you are still facing issues, please report it on our community slack or open a %s \n", gitIssueLink)
}
// VersionCheck checks if there is a new version of the CLI
func VersionCheck(client *github.Client) (res *github.RepositoryRelease, skip bool) {
// Get the last release from GitHub API
release, _, err := client.Repositories.GetLatestRelease(context.Background(), "civo", "cli")
if _, ok := err.(*github.AbuseRateLimitError); ok {
fmt.Printf("hit secondary rate limit try again in %s minute", err.(*github.AbuseRateLimitError).RetryAfter)
return nil, true
}
if err != nil {
return nil, true
}
return release, false
}