forked from lightninglabs/lndinit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_load_secret.go
68 lines (58 loc) · 1.65 KB
/
cmd_load_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
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
)
type loadSecretCommand struct {
Source string `long:"source" short:"s" description:"Secret storage source" choice:"k8s"`
K8s *k8sSecretOptions `group:"Flags for looking up the secret as a value inside a Kubernetes Secret (use when --source=k8s)" namespace:"k8s"`
Output string `long:"output" short:"o" description:"Output format" choice:"raw" choice:"json"`
}
func newLoadSecretCommand() *loadSecretCommand {
return &loadSecretCommand{
Source: storageK8s,
K8s: &k8sSecretOptions{
Namespace: defaultK8sNamespace,
},
Output: outputFormatRaw,
}
}
func (x *loadSecretCommand) Register(parser *flags.Parser) error {
_, err := parser.AddCommand(
"load-secret",
"Load a secret from external secrets storage",
"Load a secret from the selected external secrets storage and "+
"print it to stdout, either as raw text or formatted "+
"as JSON",
x,
)
return err
}
func (x *loadSecretCommand) Execute(_ []string) error {
switch x.Source {
case storageK8s:
content, secret, err := readK8s(x.K8s)
if err != nil {
return fmt.Errorf("error reading secret %s in "+
"namespace %s: %v", x.K8s.SecretName,
x.K8s.Namespace, err)
}
if x.Output == outputFormatJSON {
content, err = asJSON(&struct {
*jsonK8sObject `json:",inline"`
Value string `json:"value"`
}{
jsonK8sObject: secret,
Value: content,
})
if err != nil {
return fmt.Errorf("error encoding as JSON: %v",
err)
}
}
fmt.Printf("%s\n", content)
return nil
default:
return fmt.Errorf("invalid secret storage source %s", x.Source)
}
}