-
Notifications
You must be signed in to change notification settings - Fork 787
/
get_vault_config.go
93 lines (77 loc) · 2.27 KB
/
get_vault_config.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
package cmd
import (
"fmt"
"runtime"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
)
type GetVaultConfigOptions struct {
GetOptions
Namespace string
Name string
terminal string
}
func (o *GetVaultConfigOptions) VaultName() string {
return o.Name
}
func (o *GetVaultConfigOptions) VaultNamespace() string {
return o.Namespace
}
var (
getVaultConfigLong = templates.LongDesc(`
Echoes the configuration required for connecting to a vault using the official vault CLI client
`)
getVaultConfigExample = templates.Examples(`
# Gets vault config
jx get vault-config
`)
)
// NewCmdGetVaultConfig creates a new command for 'jx get secrets'
func NewCmdGetVaultConfig(commonOpts *CommonOptions) *cobra.Command {
options := &GetVaultConfigOptions{
GetOptions: GetOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "vault-config",
Short: "Gets the configuration for using the official vault CLI client",
Long: getVaultConfigLong,
Example: getVaultConfigExample,
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 get the vault config")
cmd.Flags().StringVarP(&options.Name, "name", "m", "", "Name of the vault to get the config for")
cmd.Flags().StringVarP(&options.terminal, "terminal", "t", "", "terminal type output override. Values: ['sh', 'cmd'].")
return cmd
}
// Run implements the command
func (o *GetVaultConfigOptions) Run() error {
vaultClient, err := o.VaultClient(o.Name, o.Namespace) // Will use defaults if empty strings specified
if err != nil {
return err
}
// Install the vault CLI for the user
o.installVaultCli()
url, token, err := vaultClient.Config()
// Echo the client config out to the command line to be piped into bash
if o.terminal == "" {
if runtime.GOOS == "windows" {
o.terminal = "cmd"
} else {
o.terminal = "sh"
}
}
if o.terminal == "cmd" {
_, _ = fmt.Fprintf(o.Out, "set VAULT_ADDR=%s\nset VAULT_TOKEN=%s\n", url.String(), token)
} else {
_, _ = fmt.Fprintf(o.Out, "export VAULT_ADDR=%s\nexport VAULT_TOKEN=%s\n", url.String(), token)
}
return err
}