Skip to content

Commit

Permalink
Refactor API key search and add validation for removing current API k…
Browse files Browse the repository at this point in the history
…ey close Civo apikey issue #355

Signed-off-by: Alejandro J. Nuñez Madrazo <[email protected]>
  • Loading branch information
alejandrojnm committed Jan 4, 2024
1 parent 7de256f commit 461a223
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 1 addition & 2 deletions cmd/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apikey
import (
"errors"
"fmt"
"strings"

"github.com/civo/cli/config"
"github.com/spf13/cobra"
Expand All @@ -29,7 +28,7 @@ between them when required.`,
func apiKeyFind(search string) (string, error) {
var result string
for key, value := range config.Current.APIKeys {
if strings.Contains(key, search) || strings.Contains(value, search) {
if key == search || value == search {
result = key
}
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/apikey/apikey_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ var apikeyRemoveCmd = &cobra.Command{
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 utility.UserConfirmedDeletion("api key", common.DefaultYes, args[0]) {
numKeys := len(config.Current.APIKeys)
delete(config.Current.APIKeys, index)
Expand Down

0 comments on commit 461a223

Please sign in to comment.