Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion cmd/setec/setec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"errors"
"expvar"
"fmt"
Expand Down Expand Up @@ -192,6 +193,7 @@ var serverArgs struct {

var clientArgs struct {
Server string `flag:"s,default=$SETEC_SERVER,Server address"`
JSON bool `flag:"json,Write output in JSON format"`
}

func runServer(env *command.Env) error {
Expand Down Expand Up @@ -361,6 +363,13 @@ func runList(env *command.Env) error {
if err != nil {
return fmt.Errorf("failed to list secrets: %v", err)
}
if clientArgs.JSON {
enc := json.NewEncoder(os.Stdout)
for _, s := range secrets {
enc.Encode(s)
}
return nil
}

tw := newTabWriter(os.Stdout)
io.WriteString(tw, "NAME\tACTIVE\tVERSIONS\tLAST ACCESSED\n")
Expand All @@ -373,11 +382,24 @@ func runList(env *command.Env) error {
if !s.LastAccess.IsZero() {
lastAccess = s.LastAccess.Format(time.RFC3339)
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", s.Name, s.ActiveVersion, strings.Join(vers, ","), lastAccess)
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", s.Name, s.ActiveVersion, formatVersions(vers), lastAccess)
}
return tw.Flush()
}

func formatVersions(vs []string) string {
i, n := len(vs)-1, 0
for i > 0 && n < 50 {
n += len(vs[i]) + 1
i--
}
s := strings.Join(vs[i:], ",")
if i > 0 {
return "...," + s
}
return s
}

func runInfo(env *command.Env, name string) error {
c, err := newClient()
if err != nil {
Expand All @@ -388,6 +410,9 @@ func runInfo(env *command.Env, name string) error {
if err != nil {
return fmt.Errorf("failed to get secret info: %v", err)
}
if clientArgs.JSON {
return json.NewEncoder(os.Stdout).Encode(info)
}
vers := make([]string, 0, len(info.Versions))
for _, v := range info.Versions {
vers = append(vers, v.String())
Expand Down Expand Up @@ -425,6 +450,10 @@ func runGet(env *command.Env, name string) error {
return fmt.Errorf("failed to get secret: %v", err)
}

if clientArgs.JSON {
return json.NewEncoder(os.Stdout).Encode(val)
}

// Print with a newline if a human's going to look at it,
// otherwise output just the secret bytes.
if term.IsTerminal(int(os.Stdout.Fd())) {
Expand Down
Loading