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 wrong apikey issue: #464 #467

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 0 deletions cmd/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
31 changes: 31 additions & 0 deletions cmd/apikey/apikey_add.go
Original file line number Diff line number Diff line change
@@ -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))
},
}
31 changes: 31 additions & 0 deletions cmd/apikey/apikey_update.go
Original file line number Diff line number Diff line change
@@ -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))
},
}