-
Notifications
You must be signed in to change notification settings - Fork 20
/
list.go
60 lines (49 loc) · 1.27 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"context"
"flag"
"fmt"
"os"
"regexp"
"sort"
"text/tabwriter"
)
const listHelp = `List secrets.`
func (cmd *listCommand) Name() string { return "ls" }
func (cmd *listCommand) Args() string { return "" }
func (cmd *listCommand) ShortHelp() string { return listHelp }
func (cmd *listCommand) LongHelp() string { return listHelp }
func (cmd *listCommand) Hidden() bool { return false }
func (cmd *listCommand) Register(fs *flag.FlagSet) {
fs.StringVar(&cmd.filter, "f", "", "filter secrets keys by a regular expression")
fs.StringVar(&cmd.filter, "filter", "", "filter secrets keys by a regular expression")
}
type listCommand struct {
filter string
}
func (cmd *listCommand) Run(ctx context.Context, args []string) error {
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
// print header
fmt.Fprintln(w, "KEY\tVALUE")
// print the keys alphabetically
printSorted := func(m map[string]string) {
mk := make([]string, len(m))
i := 0
for k := range m {
mk[i] = k
i++
}
sort.Strings(mk)
for _, key := range mk {
if len(cmd.filter) > 0 {
if ok, _ := regexp.MatchString(cmd.filter, key); !ok {
continue
}
}
fmt.Fprintf(w, "%s\t%s\n", key, m[key])
}
}
printSorted(s.Secrets)
w.Flush()
return nil
}