-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.go
150 lines (129 loc) · 2.54 KB
/
cli.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
package main
import (
"os"
"github.com/urfave/cli"
)
const (
cliName = "sekret"
cliDesc = "Work with encrypted Kubernetes Secrets"
decodeBase64FlagName = "decode-base64"
editorFlagName = "editor"
keyFlagName = "key"
)
func newCLI() *cli.App {
app := cli.NewApp()
app.Name = cliName
app.Usage = cliDesc
app.Version = cliVer
keyFlag := cli.StringFlag{
Name: keyFlagName,
Usage: "Encryption key (16 or 32 bytes)",
EnvVar: "ENCRYPTION_KEY",
}
decodeBase64Flag := cli.BoolTFlag{
Name: decodeBase64FlagName,
Usage: "Decode base64 data",
}
editorFlag := cli.StringFlag{
Name: editorFlagName,
Usage: "Editor",
EnvVar: "EDITOR",
}
app.Flags = []cli.Flag{keyFlag}
app.Commands = []cli.Command{
{
Name: "edit",
Usage: "Edit secret YAML as plain text",
ArgsUsage: "file",
Flags: []cli.Flag{
keyFlag,
decodeBase64Flag,
editorFlag,
},
Action: editAction,
},
{
Name: "new",
Usage: "Create new encrypted secret YAML and edit it",
ArgsUsage: "file",
Flags: []cli.Flag{
keyFlag,
decodeBase64Flag,
editorFlag,
},
Action: newAction,
},
{
Name: "show",
Usage: "Show decrypted secret YAML",
ArgsUsage: "file",
Flags: []cli.Flag{
keyFlag,
decodeBase64Flag,
},
Action: showAction,
},
{
Name: "encrypt",
ShortName: "enc",
Usage: "Encrypt file",
ArgsUsage: "file",
Flags: []cli.Flag{keyFlag},
Action: encryptAction,
},
{
Name: "decrypt",
ShortName: "dec",
Usage: "Decrypt encrypted file",
ArgsUsage: "file",
Flags: []cli.Flag{keyFlag},
Action: decryptAction,
},
}
return app
}
func editAction(c *cli.Context) error {
cmd, err := editCommandFromContext(c)
if err != nil {
return err
}
return cmd.run()
}
func newAction(c *cli.Context) error {
cmd, err := newCommandFromContext(c)
if err != nil {
return err
}
return cmd.run()
}
func showAction(c *cli.Context) error {
cmd, err := showCommandFromContext(c)
if err != nil {
return err
}
return cmd.run()
}
func encryptAction(c *cli.Context) error {
cmd, err := encryptCommandFromContext(c)
if err != nil {
return err
}
in, err := os.Open(cmd.filename)
if err != nil {
return err
}
defer in.Close()
return cmd.run(in, os.Stdout)
}
func decryptAction(c *cli.Context) error {
cmd, err := decryptCommandFromContext(c)
if err != nil {
return err
}
in, err := os.Open(cmd.filename)
if err != nil {
return err
}
defer in.Close()
return cmd.run(in, os.Stdout)
}