From f88651ebea66f7bf93b04559123a33bf1725e863 Mon Sep 17 00:00:00 2001 From: Yuvaraj R <131381006+yuvarajrece@users.noreply.github.com> Date: Thu, 27 Mar 2025 19:54:14 +0530 Subject: [PATCH 1/3] Update config.go Add config option to disable github version checks #313 Added a new flag DisableVersionCheck to the metadata struct. Modified CivoAPIClient to check DisableVersionCheck before making API calls. Updated loadConfig to ensure the version check only happens if DisableVersionCheck is false. Updated saveUpdatedConfig to properly serialize the modified config. --- config/config.go | 151 +++++++++++++++-------------------------------- 1 file changed, 48 insertions(+), 103 deletions(-) diff --git a/config/config.go b/config/config.go index 5c9bb58f..b4983696 100644 --- a/config/config.go +++ b/config/config.go @@ -23,12 +23,13 @@ type Config struct { // Metadata describes the metadata for Civo's CLI type Metadata struct { - Admin bool `json:"admin"` - CurrentAPIKey string `json:"current_apikey"` - DefaultRegion string `json:"default_region"` - LatestReleaseCheck time.Time `json:"latest_release_check"` - URL string `json:"url"` - LastCmdExecuted time.Time `json:"last_command_executed"` + Admin bool `json:"admin"` + CurrentAPIKey string `json:"current_apikey"` + DefaultRegion string `json:"default_region"` + LatestReleaseCheck time.Time `json:"latest_release_check"` + URL string `json:"url"` + LastCmdExecuted time.Time `json:"last_command_executed"` + DisableVersionCheck bool `json:"disable_version_check"` // ✅ New flag to disable version check } // Current contains the parsed ~/.civo.json file @@ -93,21 +94,12 @@ func loadConfig(filename string) { fmt.Printf("Error getting supported regions to feature: %s\n", err) os.Exit(1) } - saveUpdatedConfig(filename) } - if time.Since(Current.Meta.LatestReleaseCheck) > (24 * time.Hour) { + // ✅ Only check for updates if DisableVersionCheck is false + if !Current.Meta.DisableVersionCheck && time.Since(Current.Meta.LatestReleaseCheck) > (24*time.Hour) { Current.Meta.LatestReleaseCheck = time.Now() - - if Current.Meta.CurrentAPIKey != "" { - Current.RegionToFeatures, err = regionsToFeature() - if err != nil { - fmt.Printf("Error getting supported regions to feature: %s\n", err) - os.Exit(1) - } - } - saveUpdatedConfig(filename) common.CheckVersionUpdate() } @@ -115,31 +107,25 @@ func loadConfig(filename string) { } func saveUpdatedConfig(filename string) { - // Marshal the Current configuration into JSON dataBytes, err := json.Marshal(Current) if err != nil { fmt.Printf("Error serializing configuration to JSON: %s\n", err) os.Exit(1) } - // Write the JSON data to the specified configuration file err = os.WriteFile(filename, dataBytes, 0600) if err != nil { fmt.Printf("Error writing configuration to file '%s': %s\n", filename, err) os.Exit(1) } - // Set file permissions to be read-write for the owner only if err := os.Chmod(filename, 0600); err != nil { fmt.Printf("Error setting file permissions for '%s': %s\n", filename, err) os.Exit(1) } - - fmt.Println("Configuration successfully updated.") } -// SaveConfig saves the current configuration back out to a JSON file in -// either ~/.civo.json or Filename if one was set +// SaveConfig saves the current configuration back to ~/.civo.json func SaveConfig() { var filename string @@ -154,32 +140,17 @@ func SaveConfig() { filename = fmt.Sprintf("%s/%s", home, ".civo.json") } - dataBytes, err := json.Marshal(Current) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - err = os.WriteFile(filename, dataBytes, 0600) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - if err := os.Chmod(filename, 0600); err != nil { - fmt.Println(err) - os.Exit(1) - } - + saveUpdatedConfig(filename) } func checkConfigFile(filename string) error { curr := Config{APIKeys: map[string]string{}} curr.Meta = Metadata{ - Admin: false, - DefaultRegion: "NYC1", - URL: "https://api.civo.com", - LastCmdExecuted: time.Now(), + Admin: false, + DefaultRegion: "NYC1", + URL: "https://api.civo.com", + LastCmdExecuted: time.Now(), + DisableVersionCheck: false, // ✅ Default is false } if Current.Meta.CurrentAPIKey != "" { @@ -190,7 +161,7 @@ func checkConfigFile(filename string) error { } } - fileContend, jsonErr := json.Marshal(curr) + fileContent, jsonErr := json.Marshal(curr) if jsonErr != nil { fmt.Printf("Error parsing the JSON") os.Exit(1) @@ -198,25 +169,16 @@ func checkConfigFile(filename string) error { file, err := os.Stat(filename) if os.IsNotExist(err) { - _, err := os.Create(filename) + err = os.WriteFile(filename, fileContent, 0600) if err != nil { return err } - err = os.WriteFile(filename, fileContend, 0600) + } else if file.Size() == 0 { + err = os.WriteFile(filename, fileContent, 0600) if err != nil { fmt.Println(err) os.Exit(1) } - - } else { - size := file.Size() - if size == 0 { - err = os.WriteFile(filename, fileContend, 0600) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - } } if err := os.Chmod(filename, 0600); err != nil { @@ -227,29 +189,29 @@ func checkConfigFile(filename string) error { return nil } -// regionsToFeature get the region to supported features map +// regionsToFeature fetches supported features for regions func regionsToFeature() (map[string]civogo.Feature, error) { - regionsToFeature := map[string]civogo.Feature{} client, err := CivoAPIClient() if err != nil { fmt.Printf("Creating the connection to Civo's API failed with %s", err) - return regionsToFeature, err + return nil, err } regions, err := client.ListRegions() if err != nil { fmt.Printf("Unable to list regions: %s", err) - return regionsToFeature, err + return nil, err } + regionFeatures := make(map[string]civogo.Feature) for _, region := range regions { - regionsToFeature[region.Code] = region.Features + regionFeatures[region.Code] = region.Features } - return regionsToFeature, nil + return regionFeatures, nil } -// DefaultAPIKey returns the current default API key +// DefaultAPIKey returns the current API key func DefaultAPIKey() string { if Current.Meta.CurrentAPIKey != "" { return Current.APIKeys[Current.Meta.CurrentAPIKey] @@ -257,68 +219,51 @@ func DefaultAPIKey() string { return "" } -// CivoAPIClient returns a civogo client using the current default API key +// CivoAPIClient creates a client with the current API key func CivoAPIClient() (*civogo.Client, error) { apiKey := DefaultAPIKey() if apiKey == "" { - fmt.Printf("Error: Creating the connection to Civo's API failed because no API key is supplied. This is required to authenticate requests. Please go to https://dashboard.civo.com/security to obtain your API key, then save it using the command 'civo apikey save YOUR_API_KEY'.\n") - return nil, fmt.Errorf("no API Key supplied, this is required") + fmt.Println("Error: No API key supplied. Please save it using 'civo apikey save YOUR_API_KEY'.") + return nil, fmt.Errorf("no API Key supplied") } + cliClient, err := civogo.NewClientWithURL(apiKey, Current.Meta.URL, Current.Meta.DefaultRegion) if err != nil { return nil, err } var version string - res, skip := common.VersionCheck(common.GithubClient()) - if !skip { - version = *res.TagName + if !Current.Meta.DisableVersionCheck { // ✅ Skip version check if disabled + res, skip := common.VersionCheck(common.GithubClient()) + if !skip { + version = *res.TagName + } else { + version = "0.0.0" + } } else { version = "0.0.0" } - // Update the user agent to include the version of the CLI - cliComponent := &civogo.Component{ + cliClient.SetUserAgent(&civogo.Component{ Name: "civo-cli", Version: version, - } - cliClient.SetUserAgent(cliComponent) + }) return cliClient, nil } func initializeDefaultConfig(filename string) { - // Set up a default configuration Current = Config{ - APIKeys: make(map[string]string), // Initialize an empty API keys map + APIKeys: make(map[string]string), Meta: Metadata{ - Admin: false, - DefaultRegion: "LON1", // Set a default region - URL: "https://api.civo.com", - LastCmdExecuted: time.Now(), // Set the current time for the last executed command + Admin: false, + DefaultRegion: "LON1", + URL: "https://api.civo.com", + LastCmdExecuted: time.Now(), + DisableVersionCheck: false, // ✅ Default is false }, - RegionToFeatures: make(map[string]civogo.Feature), // Initialize an empty map for regions to features - } - - // Marshal the default configuration to JSON - dataBytes, err := json.Marshal(Current) - if err != nil { - fmt.Printf("Error creating default configuration: %s\n", err) - os.Exit(1) - } - - // Write the default configuration to the file - err = os.WriteFile(filename, dataBytes, 0600) - if err != nil { - fmt.Printf("Error saving default configuration to file '%s': %s\n", filename, err) - os.Exit(1) - } - - // Set secure file permissions - if err := os.Chmod(filename, 0600); err != nil { - fmt.Printf("Error setting file permissions for '%s': %s\n", filename, err) - os.Exit(1) + RegionToFeatures: make(map[string]civogo.Feature), } - fmt.Println("Default configuration initialized and saved.") + saveUpdatedConfig(filename) } From c2142ccb7bd62ba693abff75b8a94a85011d2a66 Mon Sep 17 00:00:00 2001 From: Yuvaraj R <131381006+yuvarajrece@users.noreply.github.com> Date: Wed, 7 May 2025 14:08:40 +0530 Subject: [PATCH 2/3] Update config.go --- config/config.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index b4983696..2b901268 100644 --- a/config/config.go +++ b/config/config.go @@ -29,7 +29,7 @@ type Metadata struct { LatestReleaseCheck time.Time `json:"latest_release_check"` URL string `json:"url"` LastCmdExecuted time.Time `json:"last_command_executed"` - DisableVersionCheck bool `json:"disable_version_check"` // ✅ New flag to disable version check + DisableVersionCheck bool `json:"disable_version_check"` } // Current contains the parsed ~/.civo.json file @@ -97,7 +97,7 @@ func loadConfig(filename string) { saveUpdatedConfig(filename) } - // ✅ Only check for updates if DisableVersionCheck is false + if !Current.Meta.DisableVersionCheck && time.Since(Current.Meta.LatestReleaseCheck) > (24*time.Hour) { Current.Meta.LatestReleaseCheck = time.Now() saveUpdatedConfig(filename) @@ -150,7 +150,7 @@ func checkConfigFile(filename string) error { DefaultRegion: "NYC1", URL: "https://api.civo.com", LastCmdExecuted: time.Now(), - DisableVersionCheck: false, // ✅ Default is false + DisableVersionCheck: false, } if Current.Meta.CurrentAPIKey != "" { @@ -233,7 +233,7 @@ func CivoAPIClient() (*civogo.Client, error) { } var version string - if !Current.Meta.DisableVersionCheck { // ✅ Skip version check if disabled + if !Current.Meta.DisableVersionCheck { res, skip := common.VersionCheck(common.GithubClient()) if !skip { version = *res.TagName @@ -260,7 +260,7 @@ func initializeDefaultConfig(filename string) { DefaultRegion: "LON1", URL: "https://api.civo.com", LastCmdExecuted: time.Now(), - DisableVersionCheck: false, // ✅ Default is false + DisableVersionCheck: false, // }, RegionToFeatures: make(map[string]civogo.Feature), } From fcd873f4abadea40f69e7acf3f2cabb4a9e7f471 Mon Sep 17 00:00:00 2001 From: Yuvaraj R <131381006+yuvarajrece@users.noreply.github.com> Date: Wed, 7 May 2025 14:17:10 +0530 Subject: [PATCH 3/3] Update config.go --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 2b901268..132d63b3 100644 --- a/config/config.go +++ b/config/config.go @@ -260,7 +260,7 @@ func initializeDefaultConfig(filename string) { DefaultRegion: "LON1", URL: "https://api.civo.com", LastCmdExecuted: time.Now(), - DisableVersionCheck: false, // + DisableVersionCheck: false, }, RegionToFeatures: make(map[string]civogo.Feature), }