From ffbfd832f7b411bfee565890f82907826bb54761 Mon Sep 17 00:00:00 2001 From: Haardik Dharma Date: Mon, 26 Sep 2022 15:02:52 +0530 Subject: [PATCH 1/2] Add message for users to redownload their kubeconfig is cluster > 1 year old --- cmd/apikey/apikey_save.go | 22 ++++++++++++++++++++++ cmd/kubernetes/kubernetes_config.go | 2 ++ cmd/kubernetes/kubernetes_list.go | 7 +++++++ config/config.go | 5 +++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cmd/apikey/apikey_save.go b/cmd/apikey/apikey_save.go index 40035a11..a3360722 100644 --- a/cmd/apikey/apikey_save.go +++ b/cmd/apikey/apikey_save.go @@ -7,6 +7,7 @@ import ( "runtime" "strings" "syscall" + "time" "github.com/civo/civogo" "github.com/civo/cli/common" @@ -132,6 +133,27 @@ var apikeySaveCmd = &cobra.Command{ config.SaveConfig() } + if config.Current.Clusters == nil { + client, err := civogo.NewClientWithURL(apiKey, config.Current.Meta.URL, config.Current.Meta.DefaultRegion) + if err != nil { + utility.Error("Unable to create a Civo API client, please report this at https://github.com/civo/cli") + os.Exit(1) + } + clusters, err := client.ListKubernetesClusters() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + config.Current.Clusters = make(map[string]bool) + for _, cluster := range clusters.Items { + config.Current.Clusters[cluster.ID] = true + if time.Since(cluster.BuiltAt) > (365 * 24 * time.Hour) { + config.Current.Clusters[cluster.ID] = false + } + } + } + config.SaveConfig() + ow := utility.NewOutputWriterWithMap(map[string]string{"name": name, "key": apiKey}) switch common.OutputFormat { diff --git a/cmd/kubernetes/kubernetes_config.go b/cmd/kubernetes/kubernetes_config.go index 2e197990..34a1116b 100644 --- a/cmd/kubernetes/kubernetes_config.go +++ b/cmd/kubernetes/kubernetes_config.go @@ -89,6 +89,7 @@ If you wish to use a custom format, the available fields are: utility.Error("Saving the cluster config failed with %s", err) os.Exit(1) } + config.Current.Clusters[kube.ID] = true } else { fmt.Println("Operation aborted.") os.Exit(1) @@ -100,6 +101,7 @@ If you wish to use a custom format, the available fields are: utility.Error("Saving the cluster config failed with %s", err) os.Exit(1) } + config.Current.Clusters[kube.ID] = true } } diff --git a/cmd/kubernetes/kubernetes_list.go b/cmd/kubernetes/kubernetes_list.go index f3538ef7..1891f237 100644 --- a/cmd/kubernetes/kubernetes_list.go +++ b/cmd/kubernetes/kubernetes_list.go @@ -1,6 +1,8 @@ package kubernetes import ( + "fmt" + "github.com/civo/cli/common" "github.com/civo/cli/config" "github.com/civo/cli/utility" @@ -60,6 +62,9 @@ If you wish to use a custom format, the available fields are: ow.AppendDataWithLabel("status", cluster.Status, "Status") } + if config.Current.Clusters[cluster.ID] == false { + ow.AppendDataWithLabel("name", cluster.Name+" *", "Name") + } } switch common.OutputFormat { @@ -70,5 +75,7 @@ If you wish to use a custom format, the available fields are: default: ow.WriteTable() } + fmt.Println() + utility.Info("Cluster names marked with * are clusters which are over 1 year old. You might want to consider redownloading the config for these clusters. Ignore if already downloaded.") }, } diff --git a/config/config.go b/config/config.go index 463f80bc..58a99648 100644 --- a/config/config.go +++ b/config/config.go @@ -14,8 +14,9 @@ import ( // Config describes the configuration for Civo's CLI type Config struct { - APIKeys map[string]string `json:"apikeys"` - Meta Metadata `json:"meta"` + APIKeys map[string]string `json:"apikeys"` + Meta Metadata `json:"meta"` + Clusters map[string]bool `json:"clusters"` } // Metadata describes the metadata for Civo's CLI From 481881983696dbc33b368ecf7845a1acde835b69 Mon Sep 17 00:00:00 2001 From: Haardik Dharma Date: Tue, 27 Sep 2022 18:04:23 +0530 Subject: [PATCH 2/2] Address comments --- cmd/apikey/apikey_save.go | 22 ---------------------- config/config.go | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/cmd/apikey/apikey_save.go b/cmd/apikey/apikey_save.go index a3360722..40035a11 100644 --- a/cmd/apikey/apikey_save.go +++ b/cmd/apikey/apikey_save.go @@ -7,7 +7,6 @@ import ( "runtime" "strings" "syscall" - "time" "github.com/civo/civogo" "github.com/civo/cli/common" @@ -133,27 +132,6 @@ var apikeySaveCmd = &cobra.Command{ config.SaveConfig() } - if config.Current.Clusters == nil { - client, err := civogo.NewClientWithURL(apiKey, config.Current.Meta.URL, config.Current.Meta.DefaultRegion) - if err != nil { - utility.Error("Unable to create a Civo API client, please report this at https://github.com/civo/cli") - os.Exit(1) - } - clusters, err := client.ListKubernetesClusters() - if err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - config.Current.Clusters = make(map[string]bool) - for _, cluster := range clusters.Items { - config.Current.Clusters[cluster.ID] = true - if time.Since(cluster.BuiltAt) > (365 * 24 * time.Hour) { - config.Current.Clusters[cluster.ID] = false - } - } - } - config.SaveConfig() - ow := utility.NewOutputWriterWithMap(map[string]string{"name": name, "key": apiKey}) switch common.OutputFormat { diff --git a/config/config.go b/config/config.go index 58a99648..d6b95e4b 100644 --- a/config/config.go +++ b/config/config.go @@ -99,6 +99,26 @@ func loadConfig(filename string) { } if time.Since(Current.Meta.LatestReleaseCheck) > (24 * time.Hour) { + if len(Current.APIKeys) > 0 { + client, err := CivoAPIClient() + if err != nil { + fmt.Println("Unable to create a Civo API client, please report this at https://github.com/civo/cli") + os.Exit(1) + } + clusters, err := client.ListKubernetesClusters() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + Current.Clusters = make(map[string]bool) + for _, cluster := range clusters.Items { + Current.Clusters[cluster.ID] = true + timeSince := int(time.Since(cluster.BuiltAt).Hours()) % 8670 + if timeSince > 0 { + Current.Clusters[cluster.ID] = false + } + } + } Current.Meta.LatestReleaseCheck = time.Now() dataBytes, err := json.Marshal(Current) if err != nil {