-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_list.go
76 lines (70 loc) · 2.12 KB
/
secret_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
package rove
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type DockerSecretLsJson struct {
CreatedAt string `json:"CreatedAt"`
Id string `json:"ID"`
Name string `json:"Name"`
UpdatedAt string `json:"UpdatedAt"`
}
type SecretListCommand struct {
ConfigFile string `flag:"" name:"config" help:"Config file." type:"path" default:".rove"`
Json bool `flag:"" name:"json" help:"Output as JSON."`
Machine string `flag:"" name:"machine" help:"Name of machine." default:""`
}
type SecretListJson struct {
Secrets []SecretJson `json:"secrets"`
}
type SecretJson struct {
CreatedAt string `json:"created_at"`
Id string `json:"id"`
Name string `json:"name"`
UpdatedAt string `json:"updated_at"`
}
func (cmd *SecretListCommand) Run() error {
return Database(cmd.ConfigFile, func() error {
return SshMachineByName(cmd.Machine, func(conn SshRunner, stdin io.Reader) error {
return conn.
Run("docker secret ls --format json --filter label=rove", func(res string) error {
output := make([]DockerSecretLsJson, 0)
for _, line := range strings.Split(strings.ReplaceAll(res, "\r\n", "\n"), "\n") {
if line != "" {
var dockerSecretLs DockerSecretLsJson
if err := json.Unmarshal([]byte(line), &dockerSecretLs); err != nil {
fmt.Println("🚫 Could not parse docker secret ls JSON:\n", line)
return err
}
output = append(output, dockerSecretLs)
}
}
if cmd.Json {
var t SecretListJson
for _, secret := range output {
t.Secrets = append(t.Secrets, SecretJson{
CreatedAt: secret.CreatedAt,
Id: secret.Id,
Name: secret.Name,
UpdatedAt: secret.UpdatedAt,
})
}
out, err := json.MarshalIndent(t, "", " ")
if err != nil {
fmt.Println("🚫 Could not format JSON:\n", t)
return err
}
fmt.Println(string(out))
} else {
for _, dockerSecretLs := range output {
fmt.Println(dockerSecretLs.Id, dockerSecretLs.Name, dockerSecretLs.CreatedAt, dockerSecretLs.UpdatedAt)
}
}
return nil
}).
Error()
})
})
}