-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (109 loc) · 2.14 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
package main
import (
"log"
"os"
"path/filepath"
"github.com/kanywst/rapg/internal/out"
"github.com/kanywst/rapg/pkg/rapg/api"
"github.com/urfave/cli"
)
var (
homePath, _ = os.UserHomeDir()
keyPath = filepath.Join(homePath, ".rapg", ".key_store")
)
func main() {
if _, err := os.Stat(filepath.Join(homePath, ".rapg")); os.IsNotExist(err) {
os.Mkdir(filepath.Join(homePath, ".rapg"), 0755)
}
app := cli.NewApp()
app.Name = "Rapg"
app.Usage = "rapg is a tool for generating and managing random, strong passwords."
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "len, l",
Value: 24,
Usage: "password length",
},
}
app.Action = func(c *cli.Context) error {
mrp, err := api.MakeRandomPassword(c.Int("len"))
if err != nil {
log.Fatal(err)
}
out.Green(mrp)
return nil
}
app.Commands = []cli.Command{
{
Name: "add",
Usage: "add password",
Action: func(c *cli.Context) error {
if !checkKeyStore() {
out.Green("At first, rapg init")
} else {
api.AddPassword(c.Args().First(), c.Int("len"))
}
return nil
},
Flags: []cli.Flag{
cli.IntFlag{
Name: "len, l",
Value: 24,
},
},
},
{
Name: "init",
Usage: "initialize",
Action: func(c *cli.Context) error {
api.CreateKey()
return nil
},
},
{
Name: "show",
Aliases: []string{"s"},
Usage: "show password",
Action: func(c *cli.Context) error {
if !checkKeyStore() {
out.Red("At first, rapg init")
} else {
api.ShowPassword(c.Args().First())
}
return nil
},
},
{
Name: "list",
Usage: "list password",
Action: func(c *cli.Context) error {
api.ShowList()
return nil
},
},
{
Name: "remove",
Aliases: []string{"rm"},
Usage: "remove password",
Action: func(c *cli.Context) error {
if !checkKeyStore() {
out.Red("At first, rapg init")
} else {
api.RemovePassword(c.Args().First())
}
return nil
},
},
}
app.Run(os.Args)
}
func checkKeyStore() bool {
_, err := os.OpenFile(keyPath, os.O_RDONLY, 0)
if err != nil {
if os.IsNotExist(err) {
return false
}
panic(err)
}
return true
}