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

Feature/open_issue #358

Merged
merged 6 commits into from
Jan 4, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ BrbXNW2RUYLe
If an instance has a public IP address configured, you can display it using `civo instance public-ip ID/hostname`:

```sh
$ civo instance public-ip api-demo.test -o custom -f public_ip
$ civo instance show api-demo.test -o custom -f public_ip
74.220.21.246
```

Expand Down
3 changes: 1 addition & 2 deletions cmd/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apikey
import (
"errors"
"fmt"
"strings"

"github.com/civo/cli/config"
"github.com/spf13/cobra"
Expand All @@ -29,7 +28,7 @@ between them when required.`,
func apiKeyFind(search string) (string, error) {
var result string
for key, value := range config.Current.APIKeys {
if strings.Contains(key, search) || strings.Contains(value, search) {
if key == search || value == search {
result = key
}
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/apikey/apikey_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ var apikeyRemoveCmd = &cobra.Command{
os.Exit(1)
}

// Check if the requested API key is the current one
if index == config.Current.Meta.CurrentAPIKey {
utility.Warning("The API key %q is the current one, please change it before removing it", args[0])
os.Exit(1)
}

if utility.UserConfirmedDeletion("api key", common.DefaultYes, args[0]) {
numKeys := len(config.Current.APIKeys)
delete(config.Current.APIKeys, index)
Expand Down
3 changes: 1 addition & 2 deletions cmd/objectstore/objectstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"github.com/spf13/cobra"
)

var credentialSize int
var accessKey, secretAccessKey string

//ObjectStoreCmd manages Civo Object Store
// ObjectStoreCmd manages Civo Object Store
var ObjectStoreCmd = &cobra.Command{
Use: "objectstore",
Aliases: []string{"bucket", "buckets", "object", "objects"},
Expand Down
1 change: 0 additions & 1 deletion cmd/objectstore/objectstore_credential_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

var credAccessKey, credSecretAccessKey string
var credSuspended bool

var objectStoreCredentialUpdateCmd = &cobra.Command{
Use: "update",
Expand Down
9 changes: 9 additions & 0 deletions cmd/objectstore/objectstore_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var objectStoreShowCmd = &cobra.Command{
os.Exit(1)
}

// Get the credentials for the object store
var creds *civogo.ObjectStoreCredential
if objectStore.OwnerInfo.Name != "" {
creds, err = client.FindObjectStoreCredential(objectStore.OwnerInfo.Name)
Expand All @@ -47,6 +48,13 @@ var objectStoreShowCmd = &cobra.Command{
}
}

// Get the stats for the object store
stats, err := client.GetObjectStoreStats(objectStore.ID)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}

ow := utility.NewOutputWriter()

ow.StartLine()
Expand All @@ -58,6 +66,7 @@ var objectStoreShowCmd = &cobra.Command{
ow.AppendDataWithLabel("region", client.Region, "Region")
ow.AppendDataWithLabel("accesskey", creds.AccessKeyID, "Access Key")
ow.AppendDataWithLabel("status", objectStore.Status, "Status")
ow.AppendDataWithLabel("stats", fmt.Sprintf("Objects: %d, Size: %s MB, Size Used: %d", stats.NumObjects, strconv.Itoa(objectStore.MaxSize), stats.SizeKBUtilised), "Stats")

switch common.OutputFormat {
case "json":
Expand Down
3 changes: 2 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/civo/cli/utility"
"github.com/kierdavis/ansi"

gogithub "github.com/google/go-github/v57/github"
"github.com/spf13/cobra"
"github.com/tj/go-update"
"github.com/tj/go-update/progress"
Expand Down Expand Up @@ -39,7 +40,7 @@ var (
// fetch the new releases
releases, err := m.LatestReleases()
if err != nil {
if common.IsGHError(err) != nil {
if _, ok := err.(*gogithub.AbuseRateLimitError); ok {
os.Exit(1)
}
utility.Error("error fetching releases: %s", err)
Expand Down
64 changes: 18 additions & 46 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package common

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/google/go-github/github"
"github.com/pkg/errors"
"github.com/google/go-github/v57/github"
"github.com/savioxavier/termlink"
"github.com/tcnksm/go-latest"
)

var (
Expand All @@ -30,60 +27,35 @@ var (
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() {
res, skip := VersionCheck()
ghClient := GithubClient()
res, skip := VersionCheck(ghClient)
if !skip {
if res.Outdated {
fmt.Printf("A newer version (v%s) is available, please upgrade with \"civo update\"\n", res.Current)
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", "italic green")
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() (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 IsGHError(err) != nil {
return nil, true
}
fmt.Printf("Checking for a newer version failed with %s \n", err)
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
}
return res, false
}

// IsGHError checks if any error from github is returned
func IsGHError(err error) error {
ghErr, ok := err.(*github.RateLimitError)
if ok {
if ghErr.Response.StatusCode >= 400 && ghErr.Response.StatusCode < 500 {
return errors.Wrap(err, `Failed to query the GitHub API for updates.
This is most likely due to GitHub rate-limiting on unauthenticated requests.
To have the civo-cli make authenticated requests please:
1. Generate a token at https://github.com/settings/tokens
2. Set the token by either adding it to your ~/.gitconfig
setting the GITHUB_TOKEN environment variable.
Instructions for generating a token can be found at:
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
We call the github releases API to look for new releases.
More information about that API can be found here: https://developer.github.com/v3/repos/releases/`)
}
if ghErr.Response.StatusCode == http.StatusUnauthorized {
return errors.Wrap(err, "Your Github token is invalid. Check the [github] section in ~/.gitconfig")
}
return errors.Wrap(err, "error finding latest release")
}
return nil
return release, false
}
41 changes: 26 additions & 15 deletions common/common_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
package common

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/google/go-github/github"
"github.com/google/go-github/v57/github"
)

func TestGithubError(t *testing.T) {
errorResponse := &github.ErrorResponse{Response: nil}
twoFactor := (*github.TwoFactorAuthError)(errorResponse)
if IsGHError(twoFactor) != nil {
t.Fail()
}
rateLimit := github.RateLimitError{}
if IsGHError(&rateLimit) != nil {
t.Fail()
}
abuseRateLimit := github.AbuseRateLimitError{}
if IsGHError(&abuseRateLimit) != nil {
t.Fail()
}
// FILEPATH: /Users/alejandrojnm/Project/go/src/github.com/civo/cli/common/common_test.go

func TestVersionCheck(t *testing.T) {
// Test when the GitHub API returns a successful response
t.Run("successful response", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`{"tag_name": "v1.0.0"}`))
}))
defer server.Close()

client := github.NewClient(nil)
client.BaseURL, _ = url.Parse(fmt.Sprintf("%s/", server.URL))

release, skip := VersionCheck(client)
if skip {
t.Errorf("Expected skip to be false, got true")
}
if release.TagName == nil || *release.TagName != "v1.0.0" {
t.Errorf("Expected release version to be v1.0.0, got %s", *release.TagName)
}
})
}
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ func CivoAPIClient() (*civogo.Client, error) {
}

var version string
res, skip := common.VersionCheck()
res, skip := common.VersionCheck(common.GithubClient())
if !skip {
version = res.Current
version = *res.TagName
} else {
version = "0.0.0"
}
Expand Down
29 changes: 14 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.20
go 1.21

module github.com/civo/cli

Expand All @@ -9,24 +9,22 @@ require (
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/briandowns/spinner v1.11.1
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect
github.com/civo/civogo v0.3.55
github.com/civo/civogo v0.3.56
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
github.com/google/go-github v17.0.0+incompatible // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0
github.com/gookit/color v1.3.8
github.com/gosuri/uilive v0.0.4 // indirect
github.com/gosuri/uiprogress v0.0.1 // indirect
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 // indirect
github.com/kierdavis/ansi v0.0.0-20180105022324-90d93b0fcae2
github.com/klauspost/pgzip v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.4
github.com/spf13/cobra v1.1.1
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
github.com/tj/go-update v2.2.4+incompatible
github.com/ulikunitz/xz v0.5.10 // indirect
golang.org/x/crypto v0.17.0
Expand All @@ -35,7 +33,7 @@ require (

require (
github.com/adhocore/gronx v1.6.5
github.com/pkg/errors v0.9.1
github.com/google/go-github/v57 v57.0.0
github.com/savioxavier/termlink v1.2.1
)

Expand All @@ -46,10 +44,10 @@ require (
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dlclark/regexp2 v1.1.6 // indirect
github.com/eliukblau/pixterm/pkg/ansimage v0.0.0-20191210081756-9fb6cf8c2f75 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gomarkdown/markdown v0.0.0-20191123064959-2c17d62f5098 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jwalton/go-supportscolor v1.1.0 // indirect
Expand All @@ -61,17 +59,18 @@ require (
github.com/mattn/go-runewidth v0.0.8 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/image v0.11.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.27.1 // indirect
k8s.io/apimachinery v0.27.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
k8s.io/api v0.29.0 // indirect
k8s.io/apimachinery v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Loading