forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
421 lines (406 loc) · 10.3 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"fmt"
"io"
"log"
"os"
"runtime"
"time"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/action"
"github.com/urfave/cli"
)
const (
name = "gopass"
)
var (
// Version is the released version of gopass
Version string
// BuildTime is the time the binary was built
BuildTime string
// Commit is the git hash the binary was built from
Commit string
)
type errorWriter struct {
out io.Writer
}
func (e errorWriter) Write(p []byte) (int, error) {
return e.out.Write([]byte("\n" + color.RedString("Error: "+string(p))))
}
func main() {
cli.ErrWriter = errorWriter{
out: os.Stderr,
}
cli.VersionPrinter = func(c *cli.Context) {
buildtime := ""
if bt, err := time.Parse("2006-01-02T15:04:05-0700", BuildTime); err == nil {
buildtime = bt.Format("2006-01-02 15:04:05")
}
if Version == "" {
Version = "HEAD"
}
if Commit == "" {
Commit = "n/a"
}
fmt.Printf("%s %s (%s %s) %s\n",
name,
Version,
Commit,
buildtime,
runtime.Version(),
)
}
action := action.New(Version)
app := cli.NewApp()
app.Name = name
app.Version = Version
app.Usage = "The standard unix password manager - rewritten in Go"
app.EnableBashCompletion = true
app.BashComplete = func(c *cli.Context) {
cli.DefaultAppComplete(c)
action.Complete(c)
}
app.Action = func(c *cli.Context) error {
if err := action.Initialized(c); err != nil {
return err
}
if c.Args().Present() {
return action.Show(c)
}
return action.List(c)
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "clip, c",
Usage: "Copy the secret into the clipboard",
},
}
app.Commands = []cli.Command{
{
Name: "clone",
Usage: "Clone a new store",
Description: "To clone a remote repo",
Action: action.Clone,
Flags: []cli.Flag{
cli.StringFlag{
Name: "path",
Usage: "Path to clone the repo to",
},
},
},
{
Name: "completion",
Usage: "Source the output with bash or zsh to get auto completion",
Subcommands: []cli.Command{{
Name: "bash",
Usage: "Source for auto completion in bash",
Action: action.CompletionBash,
}, {
Name: "zsh",
Usage: "Source for auto completion in zsh",
Action: action.CompletionZSH,
}},
},
{
Name: "config",
Usage: "Edit configuration",
Description: "To manipulate the gopass configuration",
Action: action.Config,
},
{
Name: "copy",
Aliases: []string{"cp"},
Usage: "Copies old-path to new-path, optionally forcefully, selectively reencrypting.",
Before: action.Initialized,
Action: action.Copy,
BashComplete: action.Complete,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Force to copy the secret and overwrite existing one",
},
},
},
{
Name: "delete",
Usage: "Remove existing secret or directory, optionally forcefully.",
Aliases: []string{"remove", "rm"},
Before: action.Initialized,
Action: action.Delete,
BashComplete: action.Complete,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "recursive, r",
Usage: "f",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Force to delete the secret",
},
},
},
{
Name: "edit",
Usage: "Insert a new secret or edit an existing secret using $EDITOR.",
Before: action.Initialized,
Action: action.Edit,
BashComplete: action.Complete,
},
{
Name: "find",
Usage: "List secrets that match the search term.",
Before: action.Initialized,
Action: action.Find,
BashComplete: action.Complete,
},
{
Name: "fsck",
Usage: "Check store integrity",
Description: "Check integrity of all stores",
Before: action.Initialized,
Action: action.Fsck,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "check, c",
Usage: "Only report",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Auto-correct any errors, do not ask",
},
},
},
{
Name: "generate",
Usage: "Generate a new password of the specified length with optionally no symbols.",
Description: "" +
"Generate a new password of the specified length with optionally no symbols. " +
"Optionally put it on the clipboard and clear board after 45 seconds. " +
"Prompt before overwriting existing password unless forced. " +
"Optionally replace only the first line of an existing file with a new password.",
Before: action.Initialized,
Action: action.Generate,
BashComplete: action.Complete,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "clip, c",
Usage: "Copy the password into the clipboard",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Force to overwrite existing password",
},
cli.BoolFlag{
Name: "no-symbols, n",
Usage: "Don't use symbols in the password",
},
},
},
{
Name: "git",
Usage: "Do git things",
Description: "If the password store is a git repository, execute a git command specified by git-command-args.",
Before: action.Initialized,
Action: action.Git,
Flags: []cli.Flag{
cli.StringFlag{
Name: "store",
Usage: "Store to operate on",
},
},
Subcommands: []cli.Command{
{
Name: "init",
Usage: "Init git repo",
Description: "Create and initialize a new git repo in the store",
Before: action.Initialized,
Action: action.GitInit,
Flags: []cli.Flag{
cli.StringFlag{
Name: "store",
Usage: "Store to operate on",
},
cli.StringFlag{
Name: "sign-key",
Usage: "GPG Key to sign commits",
},
},
},
},
},
{
Name: "grep",
Usage: "Search for secrets files containing search-string when decrypted.",
Before: action.Initialized,
Action: action.Grep,
},
{
Name: "init",
Usage: "Initialize new password storage and use gpg-id for encryption.",
Description: "" +
"Initialize new password storage and use gpg-id for encryption.",
Action: action.Init,
Flags: []cli.Flag{
cli.StringFlag{
Name: "store, s",
Usage: "Set the sub store to operate on",
},
cli.BoolFlag{
Name: "nogit",
Usage: "Do not init git repo",
},
},
},
{
Name: "insert",
Usage: "Insert new secret",
Description: "" +
"Insert new secret. Optionally, echo the secret back to the console during entry. " +
"Or, optionally, the entry may be multiline. " +
"Prompt before overwriting existing secret unless forced.",
Before: action.Initialized,
Action: action.Insert,
BashComplete: action.Complete,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "echo, e",
Usage: "Display secret while typing",
},
cli.BoolFlag{
Name: "multiline, m",
Usage: "Insert using $EDITOR",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Overwrite any existing secret",
},
},
},
{
Name: "list",
Usage: "List secrets.",
Aliases: []string{"ls"},
Before: action.Initialized,
Action: action.List,
BashComplete: action.Complete,
},
{
Name: "move",
Aliases: []string{"mv"},
Usage: "Renames or moves old-path to new-path, optionally forcefully, selectively reencrypting.",
Before: action.Initialized,
Action: action.Move,
BashComplete: action.Complete,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Force to move the secret and overwrite existing one",
},
},
},
{
Name: "mounts",
Usage: "Edit mounts",
Description: "To manipulate gopass mounts",
Before: action.Initialized,
Action: action.MountsPrint,
Subcommands: []cli.Command{
{
Name: "add",
Usage: "Add mount",
Description: "To add a new mounted sub store",
Before: action.Initialized,
Action: action.MountAdd,
Flags: []cli.Flag{
cli.StringFlag{
Name: "init, i",
Usage: "Init the store with the given recpient key",
},
},
},
{
Name: "remove",
Usage: "Remove mount",
Description: "To remove a mounted sub store",
Before: action.Initialized,
Action: action.MountRemove,
BashComplete: action.MountsComplete,
},
},
},
{
Name: "recipients",
Usage: "List Recipients",
Description: "To show all recipients",
Before: action.Initialized,
Action: action.RecipientsPrint,
Subcommands: []cli.Command{
{
Name: "add",
Usage: "Add any number of Recipients",
Description: "To add any number of recipients to a store",
Before: action.Initialized,
Action: action.RecipientsAdd,
Flags: []cli.Flag{
cli.StringFlag{
Name: "store",
Usage: "Store to operate on",
},
},
},
{
Name: "remove",
Usage: "Remove any number of Recipients",
Description: "To remove any number of recipients from a store",
Before: action.Initialized,
Action: action.RecipientsRemove,
BashComplete: action.RecipientsComplete,
Flags: []cli.Flag{
cli.StringFlag{
Name: "store",
Usage: "Store to operate on",
},
},
},
},
},
{
Name: "show",
Usage: "Show existing secret and optionally put it on the clipboard.",
Description: "" +
"Show existing secret and optionally put it on the clipboard. " +
"If put on the clipboard, it will be cleared in 45 seconds.",
Before: action.Initialized,
Action: action.Show,
BashComplete: action.Complete,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "clip, c",
Usage: "Copy the secret into the clipboard",
},
},
},
{
Name: "unclip",
Usage: "Internal command to clear clipboard",
Description: "Clear the clipboard if the content matches the checksum",
Action: action.Unclip,
Hidden: true,
Flags: []cli.Flag{
cli.IntFlag{
Name: "timeout",
Usage: "Time to wait",
},
},
},
{
Name: "version",
Usage: "Print gopass version",
Description: "Display version and build time information",
Action: action.Version,
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}