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

Fix error in the CLI and the GitHub API #298

Merged
merged 1 commit into from
Feb 1, 2023
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
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ $(BINARY_WINDOWS): buildprep
buildprep:
git fetch --tags -f
mkdir -p dest
# $(eval VERSION_CLI=$(shell git describe --tags | cut -d "v" -f 2 | cut -d "-" -f 1))
$(eval VERSION_CLI=$(shell git describe --tags | cut -d "v" -f 2 | cut -d "-" -f 1))
$(eval COMMIT_CLI=$(shell git log --format="%H" -n 1))

Expand Down
5 changes: 2 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"runtime"

"github.com/civo/cli/common"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
)

Expand All @@ -24,11 +23,11 @@ var (
fmt.Printf("Build date (client): %s\n", common.DateCli)
fmt.Printf("Git commit (client): %s\n", common.CommitCli)
fmt.Printf("OS/Arch (client): %s/%s\n", runtime.GOOS, runtime.GOARCH)
utility.CheckVersionUpdate()
common.CheckVersionUpdate()
case quiet:
fmt.Printf("v%s\n", common.VersionCli)
default:
utility.CheckVersionUpdate()
common.CheckVersionUpdate()
fmt.Printf("Civo CLI v%s\n", common.VersionCli)
}
},
Expand Down
17 changes: 17 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/go-github/github"
"github.com/pkg/errors"
"github.com/tcnksm/go-latest"
"github.com/savioxavier/termlink"
)

var (
Expand All @@ -29,6 +30,22 @@ var (
DateCli = "unknown"
)

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

// 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", "italic 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() (res *latest.CheckResponse, skip bool) {
githubTag := &latest.GithubTag{
Expand Down
9 changes: 1 addition & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/gookit/color"
"github.com/mitchellh/go-homedir"
)

Expand Down Expand Up @@ -110,13 +109,7 @@ func loadConfig(filename string) {
fmt.Println(err)
os.Exit(1)
}
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))
}
}
common.CheckVersionUpdate()
}

}
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.23
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.1.0 // indirect
github.com/google/uuid v1.2.0
github.com/gookit/color v1.3.8
Expand Down
20 changes: 3 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,13 @@ limitations under the License.
package main

import (
"fmt"
"os"
// "fmt"
// "os"

"github.com/civo/cli/cmd"
"github.com/civo/cli/common"
"github.com/savioxavier/termlink"
// "github.com/civo/cli/common"
)

func main() {
defer func() {
if err := recover(); err != nil {
resp, skip := common.VersionCheck()
gitIssueLink := termlink.ColorLink("GitHub issue", "https://github.com/civo/cli/issues", "italic green")
if skip == true {
fmt.Printf("panic : %s \nPlease 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", err, gitIssueLink)
os.Exit(1)
}
res := resp.Current
updateCmd := "civo update"
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)
}
}()
cmd.Execute()
}
12 changes: 1 addition & 11 deletions utility/color_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,9 @@ func Red(value string) string {
return newColor(value)
}

// CheckVersionUpdate checks if there's an update to be done
func CheckVersionUpdate() {
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)
}
}
}

// Error is the function to handler all error in the Cli
func Error(msg string, args ...interface{}) {
CheckVersionUpdate()
common.IssueMessage()
fmt.Fprintf(os.Stderr, "%s: %s\n", color.Red.Sprintf("Error"), fmt.Sprintf(msg, args...))
}

Expand Down