Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore github rate limit errors #282

Merged
merged 5 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ var (
// fetch the new releases
releases, err := m.LatestReleases()
if err != nil {
if common.IsGHRatelimitError(err) {
options := "1. Wait a few minutes and try again\n 2. Try switching to a different network (e.g. mobile data, VPN, etc)\n 3. Download latest CLI from https://github.com/civo/cli/releases"
fmt.Println("You have reached the github rate limit:\n " + options)
os.Exit(1)
}
utility.Error("error fetching releases: %s", err)
os.Exit(1)
}
Expand Down
18 changes: 14 additions & 4 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package common

import (
"fmt"
"os"
"strings"

"github.com/google/go-github/github"
"github.com/tcnksm/go-latest"
)

Expand All @@ -27,16 +27,26 @@ var (
DateCli = "unknown"
)

func VersionCheck() *latest.CheckResponse {
// VersionCheck checks if there is a new version of the CLI
func VersionCheck() (res *latest.CheckResponse, skip bool) {
githubTag := &latest.GithubTag{
Owner: "civo",
Repository: "cli",
FixVersionStrFunc: latest.DeleteFrontV(),
}
res, err := latest.Check(githubTag, strings.Replace(VersionCli, "v", "", 1))
if err != nil {
if IsGHRatelimitError(err) {
return nil, true
}
fmt.Printf("Checking for a newer version failed with %s \n", err)
os.Exit(1)
return nil, true
}
return res
return res, false
}

// IsGHRatelimitError checks if the error is a github rate limit error
func IsGHRatelimitError(err error) bool {
_, ok := err.(*github.RateLimitError)
return ok
}
22 changes: 16 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ func loadConfig(filename string) {
fmt.Println(err)
os.Exit(1)
}
res := common.VersionCheck()
if res.Outdated {
msg := "A newer version (v%s) is available, please upgrade with \"civo update\"\n"
fmt.Fprintf(os.Stderr, "%s: %s", color.Red.Sprintf("IMPORTANT"), fmt.Sprintf(msg, res.Current))
res, skip := common.VersionCheck()
if !skip {
if res.Outdated {
msg := "A newer version (v%s) is available, please upgrade with \"civo update\"\n"
fmt.Fprintf(os.Stderr, "%s: %s", color.Red.Sprintf("IMPORTANT"), fmt.Sprintf(msg, res.Current))
}
}
}

Expand Down Expand Up @@ -159,7 +161,7 @@ func checkConfigFile(filename string) error {
curr := Config{APIKeys: map[string]string{}}
curr.Meta = Metadata{
Admin: false,
DefaultRegion: "LON1",
DefaultRegion: "NYC1",
URL: "https://api.civo.com",
LastCmdExecuted: time.Now(),
}
Expand Down Expand Up @@ -214,10 +216,18 @@ func CivoAPIClient() (*civogo.Client, error) {
return nil, err
}

var version string
res, skip := common.VersionCheck()
if !skip {
version = res.Current
} else {
version = "0.0.0"
}

// Update the user agent to include the version of the CLI
cliComponent := &civogo.Component{
Name: "civo-cli",
Version: common.VersionCheck().Current,
Version: version,
}
cliClient.SetUserAgent(cliComponent)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/civo/civogo v0.3.13
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/google/go-github v17.0.0+incompatible // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/uuid v1.2.0
github.com/gookit/color v1.3.8
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main

import (
"fmt"
"os"

"github.com/civo/cli/cmd"
"github.com/civo/cli/common"
Expand All @@ -26,7 +27,13 @@ import (
func main() {
defer func() {
if err := recover(); err != nil {
res := common.VersionCheck().Current
resp, skip := common.VersionCheck()
if skip == true {
fmt.Println("An error occurred fetching the CLI tag, please try again later")
os.Exit(1)
}
res := resp.Current

updateCmd := "civo update"
gitIssueLink := termlink.ColorLink("GitHub issue", "https://github.com/civo/cli/issues", "italic green")
fmt.Printf("panic : %s \nYour CLI Version : %s \nPlease, run %q and retry the command \nIf you are still facing issues, please report it on our community slack or open a %s \n", err, res, updateCmd, gitIssueLink)
Expand Down
9 changes: 6 additions & 3 deletions utility/color_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ func Red(value string) string {
return newColor(value)
}

// CheckVersionUpdate checks if there's an update to be done
func CheckVersionUpdate() {
res := common.VersionCheck()
if res.Outdated {
fmt.Printf("A newer version (v%s) is available, please upgrade with \"civo update\"\n", res.Current)
res, skip := common.VersionCheck()
if !skip {
if res.Outdated {
fmt.Printf("A newer version (v%s) is available, please upgrade with \"civo update\"\n", res.Current)
}
}
}

Expand Down