-
Notifications
You must be signed in to change notification settings - Fork 42
/
passgo.go
168 lines (163 loc) · 4.52 KB
/
passgo.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
package main
import (
"fmt"
"runtime/debug"
"strconv"
"github.com/ejcx/passgo/v2/edit"
"github.com/ejcx/passgo/v2/generate"
"github.com/ejcx/passgo/v2/initialize"
"github.com/ejcx/passgo/v2/insert"
"github.com/ejcx/passgo/v2/pio"
"github.com/ejcx/passgo/v2/show"
"github.com/spf13/cobra"
)
var (
copyPass bool
RootCmd = &cobra.Command{
Use: "passgo",
Short: "Print the contents of the vault.",
Long: `Print the contents of the vault. If you have
not yet initialized your vault, it is necessary to run
the init subcommand in order to create your passgo
directory, and initialize your cryptographic keys.`,
Run: func(cmd *cobra.Command, args []string) {
if exists, _ := pio.PassFileDirExists(); exists {
show.ListAll()
} else {
cmd.Help()
}
},
}
versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of your passgo binary.",
Run: func(cmd *cobra.Command, args []string) {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("(unknown)")
return
}
fmt.Println(info.Main.Version)
},
}
initCmd = &cobra.Command{
Use: "init",
Short: "Initialize your passgo vault",
Long: "Initialize the .passgo directory, and generate your secret keys",
Run: func(cmd *cobra.Command, args []string) {
initialize.Init()
},
}
insertCmd = &cobra.Command{
Use: "insert",
Short: "Insert a file or password in to your vault",
Example: "passgo insert money/bank.com",
Args: cobra.RangeArgs(1, 2),
Long: `Add a site to your password store. This site can optionally be a part
of a group by prepending a group name and slash to the site name.
Will prompt for confirmation when a site path is not unique.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 2 {
path := args[0]
filename := args[1]
insert.File(path, filename)
} else {
pathName := args[0]
insert.Password(pathName)
}
},
}
showCmd = &cobra.Command{
Use: "show",
Example: "passgo show money/bank.com",
Short: "Print the password of a passgo entry.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
show.Site(path, copyPass)
},
}
generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate a secure password",
Example: "passgo generate",
Long: `Prints a randomly generated password. The length of this password defaults
to 24. If a password length is specified as greater than 2048 then generate
will fail.`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
pwlen := -1
if len(args) != 0 {
pwlenStr := args[0]
pwlenint, err := strconv.Atoi(pwlenStr)
if err != nil {
pwlen = -1
} else {
pwlen = pwlenint
}
}
pass := generate.Generate(pwlen)
fmt.Println(pass)
},
}
findCmd = &cobra.Command{
Use: "find",
Aliases: []string{"ls"},
Example: "passgo find bank.com",
Short: "Find a site that contains the site-path.",
Long: `Prints all sites that contain the site-path. Used to print just
one group or all sites that contain a certain word in the group or name`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
show.Find(path)
},
}
renameCmd = &cobra.Command{
Use: "rename",
Short: "Rename an entry in the password vault",
Example: "passgo rename money/bank.com",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
edit.Rename(path)
},
}
editCmd = &cobra.Command{
Use: "edit",
Aliases: []string{"update"},
Short: "Change the password of a site in the vault.",
Example: "passgo edit money/bank.com",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
edit.Edit(path)
},
}
removeCmd = &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Example: "passgo remove money/bank.com",
Short: "Remove a site from the password vault by specifying the entire site-path.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
edit.RemovePassword(path)
},
}
)
func init() {
showCmd.PersistentFlags().BoolVarP(©Pass, "copy", "c", false, "Copy your password to the clipboard")
RootCmd.AddCommand(findCmd)
RootCmd.AddCommand(generateCmd)
RootCmd.AddCommand(initCmd)
RootCmd.AddCommand(insertCmd)
RootCmd.AddCommand(removeCmd)
RootCmd.AddCommand(editCmd)
RootCmd.AddCommand(renameCmd)
RootCmd.AddCommand(showCmd)
RootCmd.AddCommand(versionCmd)
}
func main() {
RootCmd.Execute()
}