/
main.go
93 lines (83 loc) · 1.89 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
// Copyright (c) 2017, David Url
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"time"
)
type command int
const (
addCmd command = iota
getCmd
delCmd
filterCmd
changeKeyCmd
quitCmd
noCmd
)
func main() {
cmd, app := parseCmd()
// setup:
if isFile(app.filename) {
app.Init()
} else {
app.InitEmpty()
}
app.Import()
loop(app, cmd)
}
func parseCmd() (command, termApp) {
user, err := user.Current()
if err != nil {
quitErr(err)
}
defaultFilename := filepath.Join(user.HomeDir, ".passmgr_store")
// cmd parsing:
filename := flag.String("file", defaultFilename, "specify the passmgr store")
appTTL := flag.Int("appTTL", 120, "time in seconds after which the application quits if there is no user interaction")
clipboardTTL := flag.Int("clipboardTTL", 15, "time in seconds after which the clipboard is reset")
importFilename := flag.String("import", "", "file to import credentials from")
changeKey := flag.Bool("change-key", false, "change the master passphrase")
flag.Parse()
cmd := noCmd
if *changeKey {
cmd = changeKeyCmd
}
return cmd, termApp{filename: calcFilename(*filename), clipboardTTL: *clipboardTTL, appTTL: *appTTL, importFilename: *importFilename}
}
func loop(app termApp, cmd command) {
app.PrintTable()
success := false
for {
timer := time.AfterFunc(time.Duration(app.appTTL)*time.Second, func() {
fmt.Println("\n[passmgr] Exited due to inactivity.")
os.Exit(1)
})
switch cmd {
case getCmd:
success = app.Get()
case addCmd:
success = app.Add()
case delCmd:
success = app.Delete()
case filterCmd:
success = app.Filter()
case changeKeyCmd:
success = app.ChangeKey()
case quitCmd:
return
default:
cmd = askCommand()
}
if success {
app.PrintTable()
cmd = askCommand()
}
timer.Stop()
}
}