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

Commit

Permalink
fix: make formatting consistent in duffle key list (#359)
Browse files Browse the repository at this point in the history
Closes #325
  • Loading branch information
technosophos authored Nov 6, 2018
1 parent 076c286 commit a78ed2d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cmd/duffle/key_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func newKeyListCmd(w io.Writer) *cobra.Command {
var (
privateOnly bool
publicOnly bool
long bool
)
cmd := &cobra.Command{
Use: "list",
Expand All @@ -44,21 +45,32 @@ func newKeyListCmd(w io.Writer) *cobra.Command {
if publicOnly {
rings = []string{h.PublicKeyRing()}
}
return listKeys(cmd.OutOrStdout(), rings...)
return listKeys(cmd.OutOrStdout(), long, rings...)
},
}
cmd.Flags().BoolVarP(&privateOnly, "signing", "s", false, "show private (sign-or-verify) keys")
cmd.Flags().BoolVarP(&publicOnly, "verify-only", "p", false, "show public (verify-only) keys")
cmd.Flags().BoolVarP(&long, "long", "l", false, "show additional details")

return cmd
}

func listKeys(out io.Writer, rings ...string) error {
func listKeys(out io.Writer, long bool, rings ...string) error {
kr, err := signature.LoadKeyRings(rings...)
if err != nil {
return err
}

if !long {
for _, k := range kr.Keys() {
name, err := k.UserID()
if err != nil {
fmt.Fprintln(out, "[anonymous key]")
}
fmt.Fprintln(out, name)
}
return nil
}
table := uitable.New()
table.MaxColWidth = 80
table.Wrap = true
Expand Down
31 changes: 31 additions & 0 deletions cmd/duffle/key_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListKeys(t *testing.T) {
out := bytes.NewBuffer(nil)
ring := "../../pkg/signature/testdata/public.gpg"
if err := listKeys(out, false, ring); err != nil {
t.Fatal(err)
}

is := assert.New(t)
lines := strings.Split(out.String(), "\n")
is.Len(lines, 3)
is.Contains(out.String(), "[email protected]")

out.Reset()
if err := listKeys(out, true, ring); err != nil {
t.Fatal(err)
}
lines = strings.Split(out.String(), "\n")
is.Len(lines, 4)
is.Contains(out.String(), "[email protected]")
is.Contains(out.String(), "FINGERPRINT")
}

0 comments on commit a78ed2d

Please sign in to comment.