From ac0befd86428d3cfa7bda3bd9c9114a304875603 Mon Sep 17 00:00:00 2001 From: Sundaram Kumar Jha Date: Fri, 20 Sep 2024 23:36:12 +0530 Subject: [PATCH 1/3] added apikey add feature --- cmd/apikey/apikey_add.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cmd/apikey/apikey_add.go diff --git a/cmd/apikey/apikey_add.go b/cmd/apikey/apikey_add.go new file mode 100644 index 00000000..4ae68f06 --- /dev/null +++ b/cmd/apikey/apikey_add.go @@ -0,0 +1,31 @@ +package apikey + +import ( + "fmt" + "os" + + "github.com/civo/cli/config" + "github.com/civo/cli/utility" + "github.com/spf13/cobra" +) + +var apikeyAddCmd = &cobra.Command{ + Use: "add", + Aliases: []string{"create", "new"}, + Short: "Add a new API key", + Args: cobra.ExactArgs(2), // Expecting exactly two arguments: key name and key value + Example: "civo apikey add NAME API_KEY", + Run: func(cmd *cobra.Command, args []string) { + keyName := args[0] + keyValue := args[1] + + if _, exists := config.Current.APIKeys[keyName]; exists { + utility.Error("API key with name %q already exists. Choose a different name or update the existing key.", keyName) + os.Exit(1) + } + + config.Current.APIKeys[keyName] = keyValue + config.SaveConfig() + fmt.Printf("Added new API Key %s\n", utility.Green(keyName)) + }, +} From 1a77af22a132f957f654098d0a14bca67b4f3c7f Mon Sep 17 00:00:00 2001 From: Sundaram Kumar Jha Date: Fri, 20 Sep 2024 23:36:43 +0530 Subject: [PATCH 2/3] added apikey update an apikey feature --- cmd/apikey/apikey_update.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cmd/apikey/apikey_update.go diff --git a/cmd/apikey/apikey_update.go b/cmd/apikey/apikey_update.go new file mode 100644 index 00000000..e5a87af8 --- /dev/null +++ b/cmd/apikey/apikey_update.go @@ -0,0 +1,31 @@ +package apikey + +import ( + "fmt" + "os" + + "github.com/civo/cli/config" + "github.com/civo/cli/utility" + "github.com/spf13/cobra" +) + +var apikeyUpdateCmd = &cobra.Command{ + Use: "update", + Aliases: []string{"set", "edit"}, + Short: "Update an existing API key", + Args: cobra.ExactArgs(2), // Expecting exactly two arguments: key name and new key value + Example: "civo apikey update NAME NEW_API_KEY", + Run: func(cmd *cobra.Command, args []string) { + keyName := args[0] + newKeyValue := args[1] + + if _, exists := config.Current.APIKeys[keyName]; !exists { + utility.Error("No API key with name %q found. Use add command to insert new key.", keyName) + os.Exit(1) + } + + config.Current.APIKeys[keyName] = newKeyValue + config.SaveConfig() + fmt.Printf("Updated API Key %s\n", utility.Green(keyName)) + }, +} From 152c9d10496d3236c1ff037f7d0e1c1f32dfa464 Mon Sep 17 00:00:00 2001 From: Sundaram Kumar Jha Date: Fri, 20 Sep 2024 23:37:18 +0530 Subject: [PATCH 3/3] added add , update command --- cmd/apikey/apikey.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/apikey/apikey.go b/cmd/apikey/apikey.go index e35ca787..6a0fa26a 100644 --- a/cmd/apikey/apikey.go +++ b/cmd/apikey/apikey.go @@ -47,6 +47,8 @@ func init() { APIKeyCmd.AddCommand(apikeyRemoveCmd) APIKeyCmd.AddCommand(apikeyCurrentCmd) APIKeyCmd.AddCommand(apikeyShowCmd) + APIKeyCmd.AddCommand(apikeyAddCmd) + APIKeyCmd.AddCommand(apikeyUpdateCmd) // Flags for "civo apikey save" command apikeySaveCmd.Flags().BoolVar(&loadAPIKeyFromEnv, "load-from-env", false, "When set, the name and key will be taken from environment variables (see notes above)")