Skip to content

Commit

Permalink
Merge pull request #421 from civo/api-key-del
Browse files Browse the repository at this point in the history
Api key del
  • Loading branch information
uzaxirr authored May 17, 2024
2 parents a0cd8fc + 730db18 commit 413890c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ func init() {

// 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)")
apikeyRemoveCmd.Flags().BoolVar(&forceFlag, "force", false, "Force removal of the current API key without confirmation")
}
14 changes: 10 additions & 4 deletions cmd/apikey/apikey_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/spf13/cobra"
)

var forceFlag bool

var apikeyRemoveCmd = &cobra.Command{
Use: "remove",
Aliases: []string{"delete", "rm"},
Expand All @@ -19,17 +21,21 @@ var apikeyRemoveCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
index, err := apiKeyFind(args[0])
if err != nil {
utility.Error("Unable find the API key %s", err.Error())
utility.Error("Unable to find the API key %s", err.Error())
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 forceFlag {
utility.Warning("The API key %q is the current one. You are using the --force flag, so it will be deleted.", args[0])
} else {
utility.Warning("The API key %q is the current one. If you remove it, you will need to set another API key as the current one to continue using the CLI.", args[0])
}
}

if utility.UserConfirmedDeletion("api key", common.DefaultYes, args[0]) {
// Confirm deletion of the API key
if forceFlag || utility.UserConfirmedDeletion("API key", common.DefaultYes, args[0]) {
numKeys := len(config.Current.APIKeys)
delete(config.Current.APIKeys, index)
config.SaveConfig()
Expand Down

0 comments on commit 413890c

Please sign in to comment.