Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
fix: make 'duffle cred list' output in predictive order (#265)
Browse files Browse the repository at this point in the history
Also, this adds a '--long' option to bring it into parity with 'duffle list'
  • Loading branch information
technosophos authored Oct 10, 2018
1 parent ba8a306 commit 6260634
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
35 changes: 30 additions & 5 deletions cmd/duffle/credential_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"

"github.com/gosuri/uitable"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

Expand All @@ -16,6 +17,7 @@ import (
type credentialListCmd struct {
out io.Writer
home home.Home
long bool
}

func newCredentialListCmd(w io.Writer) *cobra.Command {
Expand All @@ -31,21 +33,44 @@ func newCredentialListCmd(w io.Writer) *cobra.Command {
return list.run()
},
}

f := cmd.Flags()
f.BoolVarP(&list.long, "long", "l", false, "output longer listing format")

return cmd
}

func (ls *credentialListCmd) run() error {
credentialPath := ls.home.Credentials()
creds := findCredentialSets(credentialPath)

for name := range creds {
fmt.Fprintln(ls.out, name)
if ls.long {
table := uitable.New()
table.MaxColWidth = 80
table.Wrap = true

table.AddRow("NAME", "PATH")
for _, cred := range creds {
table.AddRow(cred.name, cred.path)
}

fmt.Fprintln(ls.out, table)
return nil
}

for _, item := range creds {
fmt.Fprintln(ls.out, item.name)
}
return nil
}

func findCredentialSets(dir string) map[string]string {
creds := map[string]string{} // name: path
type credListItem struct {
name string
path string
}

func findCredentialSets(dir string) []credListItem {
creds := []credListItem{}

log.Debugf("Traversing credentials directory (%s) for credential sets", dir)

Expand All @@ -63,7 +88,7 @@ func findCredentialSets(dir string) map[string]string {
}

log.Debugf("Successfully loaded credential set %s from %s", credSet.Name, path)
creds[credSet.Name] = path
creds = append(creds, credListItem{name: credSet.Name, path: path})
}
return nil
})
Expand Down
8 changes: 7 additions & 1 deletion cmd/duffle/credential_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ func (rm *credentialRemoveCmd) run() error {
var notFound []string
credentialSets := findCredentialSets(rm.home.Credentials())

// Put this in a map to minimize lookup time.
pathMap := map[string]string{}
for _, cred := range credentialSets {
pathMap[cred.name] = cred.path
}

for _, name := range rm.names {
if path, ok := credentialSets[name]; ok {
if path, ok := pathMap[name]; ok {
if err := removeCredentialSet(path); err != nil {
removeErrors = append(removeErrors, fmt.Sprintf("Failed to remove credential set %s: %v", name, err))
} else {
Expand Down

0 comments on commit 6260634

Please sign in to comment.