-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_list.go
122 lines (104 loc) · 2.64 KB
/
cmd_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2021 Ronny Bangsund
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"text/tabwriter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/grimdork/opt"
)
// ListCmd options.
type ListCmd struct {
opt.DefaultHelp
Filter string `placeholder:"FILTER" help:"Lists all keys starting with this." default:"/"`
Output string `short:"o" help:"Output format." choices:"table,compact,json" default:"table"`
}
func (cmd *ListCmd) Run(in []string) error {
if cmd.Help {
return opt.ErrUsage
}
client, err := getClient()
if err != nil {
return nil
}
filter := types.ParameterStringFilter{
Key: aws.String("Name"),
Option: aws.String("BeginsWith"),
Values: []string{validKey(cmd.Filter)},
}
input := &ssm.DescribeParametersInput{
MaxResults: 50,
ParameterFilters: []types.ParameterStringFilter{filter},
}
w := &tabwriter.Writer{}
jsonout := jsonSecrets{}
switch cmd.Output {
case "json":
case "compact":
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "Secret,Last modified")
default:
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "Secret\tLast modified\tDescription")
}
loop := true
for loop {
po, err := client.DescribeParameters(context.Background(), input)
if err != nil {
return err
}
for _, p := range po.Parameters {
if p.Description == nil {
p.Description = aws.String("")
}
s := fmt.Sprintf("%s\t%s\t%s\n", *p.Name, p.LastModifiedDate.String(), *p.Description)
switch cmd.Output {
case "json":
e := jsonSecret{
Name: *p.Name,
LastMod: p.LastModifiedDate.String(),
Description: *p.Description,
}
jsonout.Secrets = append(jsonout.Secrets, e)
case "compact":
t := p.LastModifiedDate
date := fmt.Sprintf("%04d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
fmt.Fprintf(w, "%s,%s\n", *p.Name, date)
default:
fmt.Fprint(w, s)
}
}
if po.NextToken == nil {
loop = false
} else {
input.NextToken = po.NextToken
}
}
switch cmd.Output {
case "json":
data, err := json.MarshalIndent(jsonout, "", "\t")
if err != nil {
return err
}
fmt.Printf("%s\n", data)
default:
w.Flush()
}
return nil
}
type jsonSecrets struct {
Secrets []jsonSecret `json:"secrets"`
}
type jsonSecret struct {
Name string `json:"secret"`
LastMod string `json:"last_modified"`
Description string `json:"description,omitempty"`
Contents string `json:"contents,omitempty"`
}