forked from raystack/optimus
/
list.go
154 lines (131 loc) · 3.73 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package secret
import (
"bytes"
"context"
"errors"
"fmt"
"sort"
"time"
"github.com/goto/salt/log"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/goto/optimus/client/cmd/internal"
"github.com/goto/optimus/client/cmd/internal/connection"
"github.com/goto/optimus/client/cmd/internal/logger"
"github.com/goto/optimus/client/cmd/internal/progressbar"
"github.com/goto/optimus/config"
pb "github.com/goto/optimus/protos/gotocompany/optimus/core/v1beta1"
)
type listCommand struct {
logger log.Logger
connection connection.Connection
configFilePath string
projectName string
host string
}
// NewListCommand initializes command for listing secret
func NewListCommand() *cobra.Command {
list := &listCommand{
logger: logger.NewClientLogger(),
}
cmd := &cobra.Command{
Use: "list",
Short: "Show all the secrets registered with optimus",
Example: "optimus secret list",
Long: `This operation shows the secrets for project.`,
RunE: list.RunE,
PreRunE: list.PreRunE,
}
list.injectFlags(cmd)
return cmd
}
func (l *listCommand) injectFlags(cmd *cobra.Command) {
// Config filepath flag
cmd.Flags().StringVarP(&l.configFilePath, "config", "c", config.EmptyPath, "File path for client configuration")
// Mandatory flags if config is not set
cmd.Flags().StringVarP(&l.projectName, "project-name", "p", "", "Name of the optimus project")
cmd.Flags().StringVar(&l.host, "host", "", "Optimus service endpoint url")
}
func (l *listCommand) PreRunE(cmd *cobra.Command, _ []string) error {
// Load config
conf, err := internal.LoadOptionalConfig(l.configFilePath)
if err != nil {
return err
}
if conf == nil {
internal.MarkFlagsRequired(cmd, []string{"project-name", "host"})
return nil
}
if l.projectName == "" {
l.projectName = conf.Project.Name
}
if l.host == "" {
l.host = conf.Host
}
l.connection = connection.New(l.logger, conf)
return nil
}
func (l *listCommand) RunE(_ *cobra.Command, _ []string) error {
updateSecretRequest := &pb.ListSecretsRequest{
ProjectName: l.projectName,
}
return l.listSecret(updateSecretRequest)
}
func (l *listCommand) listSecret(req *pb.ListSecretsRequest) error {
conn, err := l.connection.Create(l.host)
if err != nil {
return err
}
defer conn.Close()
spinner := progressbar.NewProgressBar()
spinner.Start("please wait...")
secret := pb.NewSecretServiceClient(conn)
ctx, cancelFunc := context.WithTimeout(context.Background(), secretTimeout)
defer cancelFunc()
listSecretsResponse, err := secret.ListSecrets(ctx, req)
spinner.Stop()
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
l.logger.Error("Secret listing took too long, timing out")
}
return fmt.Errorf("%w: request failed for listing secrets", err)
}
if len(listSecretsResponse.Secrets) == 0 {
l.logger.Info("No secrets were found in %s project.", req.ProjectName)
} else {
result := l.stringifyListOfSecrets(listSecretsResponse)
l.logger.Info("Secrets for project: %s", l.projectName)
l.logger.Info(result)
}
return nil
}
func (*listCommand) stringifyListOfSecrets(listSecretsResponse *pb.ListSecretsResponse) string {
buff := &bytes.Buffer{}
table := tablewriter.NewWriter(buff)
table.SetBorder(false)
table.SetHeader([]string{
"Name",
"Digest",
"Namespace",
"Date",
})
table.SetAlignment(tablewriter.ALIGN_LEFT)
secrets := listSecretsResponse.Secrets
sort.Slice(secrets, func(i, j int) bool {
return secrets[i].Name < secrets[j].Name
})
for _, secret := range secrets {
namespace := "*"
if secret.Namespace != "" {
namespace = secret.Namespace
}
table.Append([]string{
secret.Name,
secret.Digest,
namespace,
secret.UpdatedAt.AsTime().Format(time.RFC3339),
})
}
table.Render()
return buff.String()
}