-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
265 lines (212 loc) · 6.17 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
// varuh - command line encryption program written in Go.
package main
import (
"fmt"
"github.com/pythonhacker/argparse"
"os"
"strings"
)
const VERSION = 0.4
const APP = "varuh"
const AUTHOR_INFO = `
AUTHORS
Copyright (C) 2022 Anand B Pillai <abpillai@gmail.com>
`
type actionFunc func(string) error
type actionFunc2 func(string) (error, string)
type voidFunc func() error
type voidFunc2 func() (error, string)
type settingFunc func(string)
// Structure to keep the options data
type CmdOption struct {
Short string
Long string
Help string
Path string
Default string
}
// Print the program's usage string and exit
func printUsage() error {
// getopt.Usage()
os.Exit(0)
return nil
}
// Print the program's version info and exit
func printVersionInfo() error {
fmt.Printf("%s version %.2f\n", APP, VERSION)
os.Exit(0)
return nil
}
// Command-line wrapper to generateRandomPassword
func genPass() (error, string) {
var err error
var passwd string
err, passwd = generateStrongPassword()
if err != nil {
fmt.Printf("Error generating password - \"%s\"\n", err.Error())
return err, ""
}
fmt.Println(passwd)
if settingsRider.CopyPassword {
copyPasswordToClipboard(passwd)
fmt.Println("Password copied to clipboard")
}
return nil, passwd
}
// // Perform an action by using the command line options map
func performAction(optMap map[string]interface{}) {
var flag bool
boolActionsMap := map[string]voidFunc{
"add": WrapperMaxKryptVoidFunc(addNewEntry),
"version": printVersionInfo,
"help": printUsage,
"path": showActiveDatabasePath,
"list-all": WrapperMaxKryptVoidFunc(listAllEntries),
"encrypt": encryptActiveDatabase,
}
stringActionsMap := map[string]actionFunc{
"edit": WrapperMaxKryptStringFunc(editCurrentEntry),
"init": initNewDatabase,
"list-entry": WrapperMaxKryptStringFunc(listCurrentEntry),
"remove": WrapperMaxKryptStringFunc(removeCurrentEntry),
"clone": WrapperMaxKryptStringFunc(copyCurrentEntry),
"use-db": setActiveDatabasePath,
"export": exportToFile,
"migrate": migrateDatabase,
}
stringListActionsMap := map[string]actionFunc{
"find": WrapperMaxKryptStringFunc(findCurrentEntry),
}
stringActions2Map := map[string]actionFunc2{
"decrypt": decryptDatabase,
}
flagsActions2Map := map[string]voidFunc2{
"genpass": genPass,
}
flagsActionsMap := map[string]voidFunc{
"show": setShowPasswords,
"copy": setCopyPasswordToClipboard,
"assume-yes": setAssumeYes,
}
flagsSettingsMap := map[string]settingFunc{
"type": setType,
}
// Flag actions - always done
for key, mappedFunc := range flagsActionsMap {
if *optMap[key].(*bool) {
mappedFunc()
}
}
// Flag 2 actions
for key, mappedFunc := range flagsActions2Map {
if *optMap[key].(*bool) {
mappedFunc()
flag = true
break
}
}
// Settings
for key, mappedFunc := range flagsSettingsMap {
if *optMap[key].(*string) != "" {
var val = *(optMap[key].(*string))
mappedFunc(val)
}
}
// One of bool or string actions
for key, mappedFunc := range boolActionsMap {
if *optMap[key].(*bool) {
mappedFunc()
flag = true
break
}
}
if flag {
return
}
for key, mappedFunc := range stringActionsMap {
if *optMap[key].(*string) != "" {
var val = *(optMap[key].(*string))
mappedFunc(val)
flag = true
break
}
}
for key, mappedFunc := range stringListActionsMap {
if len(*optMap[key].(*[]string)) > 0 {
var vals = *(optMap[key].(*[]string))
// Convert to single string
var singleVal = strings.Join(vals, " ")
mappedFunc(singleVal)
flag = true
break
}
}
if flag {
return
}
for key, mappedFunc := range stringActions2Map {
if *optMap[key].(*string) != "" {
var val = *(optMap[key].(*string))
mappedFunc(val)
break
}
}
}
func initializeCmdLine(parser *argparse.Parser) map[string]interface{} {
var optMap map[string]interface{}
optMap = make(map[string]interface{})
stringOptions := []CmdOption{
{"I", "init", "Initialize a new database", "<path>", ""},
{"d", "decrypt", "Decrypt password database", "<path>", ""},
{"C", "clone", "Clone an entry with <id>", "<id>", ""},
{"R", "remove", "Remove an entry with <id> or <id-range>", "<id>", ""},
{"U", "use-db", "Set <path> as active database", "<path>", ""},
{"E", "edit", "Edit entry by <id>", "<id>", ""},
{"l", "list-entry", "List entry by <id>", "<id>", ""},
{"x", "export", "Export all entries to <filename>", "<filename>", ""},
{"m", "migrate", "Migrate a database to latest schema", "<path>", ""},
{"t", "type", "Specify type when adding a new entry", "<type>", ""},
}
for _, opt := range stringOptions {
optMap[opt.Long] = parser.String(opt.Short, opt.Long, &argparse.Options{Help: opt.Help, Path: opt.Path})
}
stringListOptions := []CmdOption{
{"f", "find", "Search entries with terms", "<t1> <t2> ...", ""},
}
for _, opt := range stringListOptions {
optMap[opt.Long] = parser.StringList(opt.Short, opt.Long, &argparse.Options{Help: opt.Help, Path: opt.Path})
}
boolOptions := []CmdOption{
{"e", "encrypt", "Encrypt the current database", "", ""},
{"A", "add", "Add a new entry", "", ""},
{"p", "path", "Show current database path", "", ""},
{"a", "list-all", "List all entries in current database", "", ""},
{"g", "genpass", "Generate a strong password (length: 12 - 16)", "", ""},
{"s", "show", "Show passwords when listing entries", "", ""},
{"c", "copy", "Copy password to clipboard", "", ""},
{"y", "assume-yes", "Assume yes to actions requiring confirmation", "", ""},
{"v", "version", "Show version information and exit", "", ""},
{"h", "help", "Print this help message and exit", "", ""},
}
for _, opt := range boolOptions {
optMap[opt.Long] = parser.Flag(string(opt.Short), opt.Long, &argparse.Options{Help: opt.Help})
}
return optMap
}
// Main routine
func main() {
if len(os.Args) == 1 {
os.Args = append(os.Args, "-h")
}
parser := argparse.NewParser("varuh",
"Password manager for the command line for Unix like operating systems",
AUTHOR_INFO,
)
optMap := initializeCmdLine(parser)
err := parser.Parse(os.Args)
if err != nil {
fmt.Println(parser.Usage(err))
}
getOrCreateLocalConfig(APP)
performAction(optMap)
}