Skip to content

Commit

Permalink
Sort keys by name
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed May 2, 2024
1 parent 5b4685d commit 5cfde38
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/cli/command/project/delete-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewDeleteKeyCmd() *cobra.Command {
}

if options.name == "" {
msg.FailMsg("Name of the key is required")
msg.FailMsg("Name of the key is required. Pass it with the %s flag", style.Code("--name"))
exit.ErrorMsg("Name of the key is required")
}

Expand Down
31 changes: 28 additions & 3 deletions internal/pkg/cli/command/project/list-keys.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package project

import (
"sort"
"strings"

"github.com/pinecone-io/cli/internal/pkg/dashboard"
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/state"
"github.com/pinecone-io/cli/internal/pkg/utils/exit"
"github.com/pinecone-io/cli/internal/pkg/utils/help"
"github.com/pinecone-io/cli/internal/pkg/utils/msg"
Expand Down Expand Up @@ -43,27 +45,45 @@ func NewListKeysCmd() *cobra.Command {
exit.Error(err)
}

// Sort keys alphabetically
sortedKeys := keysResponse.Keys
sort.Slice(sortedKeys, func(i, j int) bool {
return sortedKeys[i].UserLabel < sortedKeys[j].UserLabel
})

// Unless --reveal, redact secret key values
var keysToShow []dashboard.Key = []dashboard.Key{}
if !options.reveal {
for _, key := range keysResponse.Keys {
for _, key := range sortedKeys {
keysToShow = append(keysToShow, dashboard.Key{
Id: key.Id,
UserLabel: key.UserLabel,
Value: "REDACTED",
})
}
} else {
keysToShow = keysResponse.Keys
keysToShow = sortedKeys
}

// Display output
if options.json {
presentedKeys := []PresentedKey{}
for _, key := range keysToShow {
presentedKeys = append(presentedKeys, presentKey(key))
}
text.PrettyPrintJSON(presentedKeys)
} else {
pcio.Printf("org: %s\n", style.Emphasis(state.TargetOrg.Get().Name))
pcio.Printf("project: %s\n", style.Emphasis(state.TargetProj.Get().Name))
pcio.Println()
pcio.Println(style.Heading("API Keys"))
if !options.reveal {
msg.HintMsg("To see the key values, add the %s flag", style.Code("--reveal"))
}

pcio.Println()
printKeysTable(keysToShow)

}
},
}
Expand All @@ -81,7 +101,12 @@ func printKeysTable(keys []dashboard.Key) {
pcio.Fprint(w, header)

for _, key := range keys {
values := []string{key.UserLabel, key.Value}
var values []string
if key.Value == "REDACTED" {
values = []string{key.UserLabel, style.StatusRed(key.Value)}
} else {
values = []string{key.UserLabel, key.Value}
}
pcio.Fprintf(w, strings.Join(values, "\t")+"\n")
}

Expand Down

0 comments on commit 5cfde38

Please sign in to comment.