forked from jessfraz/pony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (223 loc) · 5.82 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"text/tabwriter"
"github.com/Sirupsen/logrus"
"github.com/atotto/clipboard"
"github.com/codegangsta/cli"
"github.com/docker/docker/pkg/homedir"
"github.com/docker/docker/pkg/term"
)
const (
defaultFilestore string = ".pony"
defaultGPGPath string = ".gnupg/"
// VERSION is the command version.
VERSION = "v0.1.0"
// BANNER is the commands banner.
BANNER = ` _ __ ___ _ __ _ _
| '_ \ / _ \| '_ \| | | |
| |_) | (_) | | | | |_| |
| .__/ \___/|_| |_|\__, |
|_| |___/
`
)
var (
defaultGPGKey string
filestore string
gpgPath string
publicKeyring string
secretKeyring string
s SecretFile
debug bool
version bool
)
// preload initializes any global options and configuration
// before the main or sub commands are run
func preload(c *cli.Context) (err error) {
if c.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
defaultGPGKey = c.GlobalString("keyid")
home := homedir.Get()
homeShort := homedir.GetShortcutString()
// set the filestore variable
filestore = strings.Replace(c.GlobalString("file"), homeShort, home, 1)
// set gpg path variables
gpgPath = strings.Replace(c.GlobalString("gpgpath"), homeShort, home, 1)
publicKeyring = filepath.Join(gpgPath, "pubring.gpg")
secretKeyring = filepath.Join(gpgPath, "secring.gpg")
// if they passed an arguement, run the prechecks
// TODO(jfrazelle): this will run even if the command they issue
// does not exist, which is kinda shitty
if len(c.Args()) > 0 {
preChecks()
}
// we need to read the secrets file for all commands
// might as well be dry about it
s, err = readSecretsFile(filestore)
if err != nil {
logrus.Fatal(err)
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "pony"
app.Version = VERSION
app.Author = "@jfrazelle"
app.Email = "[email protected]"
app.Usage = "Local File-Based Password, API Key, Secret, Recovery Code Store Backed By GPG"
app.Before = preload
app.EnableBashCompletion = true
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "run in debug mode",
},
cli.StringFlag{
Name: "file, f",
Value: fmt.Sprintf("%s/%s", homedir.GetShortcutString(), defaultFilestore),
Usage: "file to use for saving encrypted secrets",
},
cli.StringFlag{
Name: "gpgpath",
Value: fmt.Sprintf("%s/%s", homedir.GetShortcutString(), defaultGPGPath),
Usage: "filepath used for gpg keys",
},
cli.StringFlag{
Name: "keyid",
Usage: "optionally set specific gpg keyid/fingerprint to use for encryption & decryption",
EnvVar: fmt.Sprintf("%s_KEYID", strings.ToUpper(app.Name)),
},
}
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"save"},
Usage: "Add a new secret",
Action: func(c *cli.Context) {
args := c.Args()
if len(args) < 2 {
logrus.Errorf("You need to pass a key and value to the command. ex: %s %s com.example.apikey EUSJCLLAWE", app.Name, c.Command.Name)
cli.ShowSubcommandHelp(c)
return
}
// add the key value pair to secrets
key, value := args[0], args[1]
s.setKeyValue(key, value, false)
fmt.Printf("Added %s %s to secrets", key, value)
},
},
{
Name: "delete",
Aliases: []string{"rm"},
Usage: "Delete a secret",
Action: func(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
cli.ShowSubcommandHelp(c)
return
}
key := args[0]
if _, ok := s.Secrets[key]; !ok {
logrus.Fatalf("Secret for (%s) does not exist", key)
}
delete(s.Secrets, key)
if err := writeSecretsFile(filestore, s); err != nil {
logrus.Fatal(err)
}
fmt.Printf("Secret %q deleted successfully", key)
},
},
{
Name: "get",
Usage: "Get the value of a secret",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "copy, c",
Usage: "copy the secret to your clipboard",
},
},
Action: func(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
cli.ShowSubcommandHelp(c)
return
}
// add the key value pair to secrets
key := args[0]
if _, ok := s.Secrets[key]; !ok {
logrus.Fatalf("Secret for (%s) does not exist", key)
}
fmt.Println(s.Secrets[key])
// copy to clipboard
if c.Bool("copy") {
if err := clipboard.WriteAll(s.Secrets[key]); err != nil {
logrus.Fatalf("Clipboard copy failed: %v", err)
}
fmt.Println("Copied to clipboard!")
}
},
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List all secrets",
Flags: []cli.Flag{
cli.StringFlag{
Name: "filter, f",
Usage: "filter secrets keys by a regular expression",
},
},
Action: func(c *cli.Context) {
_, stdout, _ := term.StdStreams()
w := tabwriter.NewWriter(stdout, 20, 1, 3, ' ', 0)
// print header
fmt.Fprintln(w, "KEY\tVALUE")
// print the keys alphabetically
printSorted := func(m map[string]string) {
mk := make([]string, len(m))
i := 0
for k := range m {
mk[i] = k
i++
}
sort.Strings(mk)
for _, key := range mk {
filter := c.String("filter")
if filter != "" {
if ok, _ := regexp.MatchString(c.String("filter"), key); !ok {
continue
}
}
fmt.Fprintf(w, "%s\t%s\n", key, m[key])
}
}
printSorted(s.Secrets)
w.Flush()
},
},
{
Name: "update",
Usage: "Update a secret",
Action: func(c *cli.Context) {
args := c.Args()
if len(args) < 2 {
logrus.Errorf("You need to pass a key and value to the command. ex: %s %s com.example.apikey EUSJCLLAWE", app.Name, c.Command.Name)
cli.ShowSubcommandHelp(c)
return
}
// add the key value pair to secrets
key, value := args[0], args[1]
s.setKeyValue(key, value, true)
fmt.Printf("Updated secret %s to %s", key, value)
},
},
}
app.Run(os.Args)
}