-
Notifications
You must be signed in to change notification settings - Fork 786
/
get_secret.go
97 lines (82 loc) · 2.06 KB
/
get_secret.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
package cmd
import (
"io"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/vault"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
type GetSecretOptions struct {
GetOptions
Namespace string
Name string
}
func (o *GetSecretOptions) VaultName() string {
return o.Name
}
func (o *GetSecretOptions) VaultNamespace() string {
return o.Namespace
}
var (
getSecretLong = templates.LongDesc(`
Display one or more Vault Secrets
`)
getSecretExample = templates.Examples(`
# List all secrets
jx get secrets
`)
)
// NewCmdGetSecret creates a new command for 'jx get secrets'
func NewCmdGetSecret(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GetSecretOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "secrets",
Short: "Display one or more Secrets",
Long: getSecretLong,
Example: getSecretExample,
Run: func(c *cobra.Command, args []string) {
options.Cmd = c
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addGetFlags(cmd)
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace from where to list the secrets")
cmd.Flags().StringVarP(&options.Name, "name", "m", "", "The name of the Vault to use")
return cmd
}
// Run implements the command
func (o *GetSecretOptions) Run() error {
var vaultClient vault.Client
var err error
if o.Name != "" && o.Namespace != "" {
vaultClient, err = o.Factory.GetVaultClient(o.Name, o.Namespace)
} else {
vaultClient, err = o.Factory.GetSystemVaultClient()
}
if err != nil {
return errors.Wrap(err, "retrieving the vault client")
}
secrets, err := vaultClient.List("")
if err != nil {
return errors.Wrap(err, "listing all secrets in vault")
}
table := o.CreateTable()
table.AddRow("KEY")
for _, secret := range secrets {
table.AddRow(secret)
}
table.Render()
return nil
}