/
main.go
118 lines (110 loc) · 2.44 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
package main
import (
"bufio"
"fmt"
"github.com/erasche/go-otp/cmds"
"github.com/urfave/cli"
"os"
"os/user"
"path"
)
var (
version string
builddate string
)
func promptForPasscode() string {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Passcode: ")
text, _ := reader.ReadString('\n')
return text
}
func main() {
app := cli.NewApp()
app.Name = "go-otp"
app.Usage = "Simple TOTP tool"
app.Version = fmt.Sprintf("%s (%s)", version, builddate)
user, err := user.Current()
var defaultDbPath string
if err != nil {
defaultDbPath = "auth.db"
} else {
defaultDbPath = path.Join(user.HomeDir, ".go-otp.db")
}
app.Flags = []cli.Flag{
cli.StringFlag{Name: "dbPath", Value: defaultDbPath, EnvVar: "GOTP_DB_PATH"},
cli.StringFlag{Name: "password", EnvVar: "GOTP_DB_PASS"},
}
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a TOTP secret",
Flags: []cli.Flag{
cli.StringFlag{Name: "account", Usage: "Account Name (e.g. user@host)"},
cli.StringFlag{Name: "issuer", Usage: "Issuer (e.g. example.com)"},
cli.StringFlag{Name: "secretKey", Usage: "TOTP secret key"},
cli.BoolFlag{Name: "overwrite"},
},
Action: func(c *cli.Context) error {
cmds.AddCode(
c.GlobalString("dbPath"),
c.GlobalString("password"),
c.GlobalString("account"),
c.GlobalString("issuer"),
c.GlobalString("secretKey"),
c.GlobalBool("overwrite"),
)
return nil
},
},
{
Name: "export",
Aliases: []string{"e"},
Usage: "export all TOTP secrets",
Action: func(c *cli.Context) error {
cmds.ExportSecrets(
c.GlobalString("dbPath"),
c.GlobalString("password"),
)
return nil
},
},
{
Name: "gen",
Aliases: []string{"g"},
Usage: "Show current TOTP tokens",
Action: func(c *cli.Context) error {
cmds.GenerateCodes(
c.GlobalString("dbPath"),
c.GlobalString("password"),
)
return nil
},
},
{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize database",
Action: func(c *cli.Context) error {
cmds.InitDb(
c.GlobalString("dbPath"),
c.GlobalString("password"),
)
return nil
},
},
{
Name: "qr",
Aliases: []string{"q"},
Usage: "Dump QR codes as PNG files",
Action: func(c *cli.Context) error {
cmds.QrCodes(
c.GlobalString("dbPath"),
c.GlobalString("password"),
)
return nil
},
},
}
app.Run(os.Args)
}