@@ -4,25 +4,29 @@ package main
4
4
5
5
import (
6
6
"fmt"
7
- "strconv"
8
- // getopt "github.com/pborman/getopt/v2"
9
- "github.com/akamensky/argparse"
7
+ "github.com/pythonhacker/argparse"
10
8
"os"
9
+ "strconv"
11
10
)
12
11
13
- const VERSION = 0.2
12
+ const VERSION = 0.3
14
13
const APP = "varuh"
15
- const AUTHOR_EMAIL = "Anand B Pillai <[email protected] >"
14
+
15
+ const AUTHOR_INFO = `
16
+ AUTHORS
17
+ Copyright (C) 2021 Anand B Pillai <[email protected] >
18
+ `
16
19
17
20
type actionFunc func (string ) error
18
21
type actionFunc2 func (string ) (error , string )
19
22
type voidFunc func () error
20
23
21
24
// Structure to keep the options data
22
25
type CmdOption struct {
23
- Short string
24
- Long string
25
- Help string
26
+ Short string
27
+ Long string
28
+ Help string
29
+ Path string
26
30
Default string
27
31
}
28
32
@@ -43,13 +47,13 @@ func printVersionInfo() error {
43
47
}
44
48
45
49
// Command-line wrapper to generateRandomPassword
46
- func generatePassword (length string ) (error , string ) {
50
+ func genPass (length string ) (error , string ) {
47
51
var iLength int
48
52
var err error
49
53
var passwd string
50
-
54
+
51
55
iLength , _ = strconv .Atoi (length )
52
- err , passwd = generateRandomPassword (iLength )
56
+ err , passwd = generatePassword (iLength )
53
57
54
58
if err != nil {
55
59
fmt .Printf ("Error generating password - \" %s\" \n " , err .Error ())
@@ -62,7 +66,7 @@ func generatePassword(length string) (error, string) {
62
66
copyPasswordToClipboard (passwd )
63
67
fmt .Println ("Password copied to clipboard" )
64
68
}
65
-
69
+
66
70
return nil , passwd
67
71
}
68
72
@@ -93,7 +97,7 @@ func performAction(optMap map[string]interface{}) {
93
97
94
98
stringActions2Map := map [string ]actionFunc2 {
95
99
"decrypt" : decryptDatabase ,
96
- "genpass" : generatePassword ,
100
+ "genpass" : genPass ,
97
101
}
98
102
99
103
flagsActionsMap := map [string ]voidFunc {
@@ -108,7 +112,6 @@ func performAction(optMap map[string]interface{}) {
108
112
}
109
113
}
110
114
111
-
112
115
// One of bool or string actions
113
116
for key , mappedFunc := range boolActionsMap {
114
117
if * optMap [key ].(* bool ) {
@@ -151,38 +154,38 @@ func initializeCmdLine(parser *argparse.Parser) map[string]interface{} {
151
154
152
155
optMap = make (map [string ]interface {})
153
156
157
+ stringOptions := []CmdOption {
158
+ {"I" , "init" , "Initialize a new database" , "<path>" , "" },
159
+ {"d" , "decrypt" , "Decrypt password database" , "<path>" , "" },
160
+ {"C" , "clone" , "Clone an entry with <id>" , "<id>" , "" },
161
+ {"R" , "remove" , "Remove an entry with <id>" , "<id>" , "" },
162
+ {"U" , "use-db" , "Set <path> as active database" , "<path>" , "" },
163
+ {"f" , "find" , "Search entries with <term>" , "<term>" , "" },
164
+ {"E" , "edit" , "Edit entry by <id>" , "<id>" , "" },
165
+ {"l" , "list-entry" , "List entry by <id>" , "<id>" , "" },
166
+ {"x" , "export" , "Export all entries to <filename>" , "<filename>" , "" },
167
+ {"g" , "genpass" , "Generate password of given <length>" , "<length>" , "" },
168
+ }
169
+
170
+ for _ , opt := range stringOptions {
171
+ optMap [opt .Long ] = parser .String (opt .Short , opt .Long , & argparse.Options {Help : opt .Help , Path : opt .Path })
172
+ }
173
+
154
174
boolOptions := []CmdOption {
155
- {"e" , "encrypt" , "Encrypt the current database" , "" },
156
- {"A" , "add" , "Add a new entry" , "" },
157
- {"p" , "path" , "Show current database path" , "" },
158
- {"a" , "list-all" , "List all entries in current database" , "" },
159
- {"s" , "show" , "Show passwords when listing entries" , "" },
160
- {"c" , "copy" , "Copy password to clipboard" , "" },
161
- {"v" , "version" , "Show version information and exit" , "" },
162
- {"h" , "help" , "Print this help message and exit" , "" },
175
+ {"e" , "encrypt" , "Encrypt the current database" , "" , "" },
176
+ {"A" , "add" , "Add a new entry" , "" , "" },
177
+ {"p" , "path" , "Show current database path" , "" , "" },
178
+ {"a" , "list-all" , "List all entries in current database" , "" , "" },
179
+ {"s" , "show" , "Show passwords when listing entries" , "" , "" },
180
+ {"c" , "copy" , "Copy password to clipboard" , "" , "" },
181
+ {"v" , "version" , "Show version information and exit" , "" , "" },
182
+ {"h" , "help" , "Print this help message and exit" , "" , "" },
163
183
}
164
184
165
185
for _ , opt := range boolOptions {
166
186
optMap [opt .Long ] = parser .Flag (string (opt .Short ), opt .Long , & argparse.Options {Help : opt .Help })
167
- }
168
-
169
- stringOptions := []CmdOption {
170
- {"I" , "init" , "Initialize a new database" , "" },
171
- {"d" , "decrypt" , "Decrypt password database" , "" },
172
- {"C" , "clone" , "Clone an entry" , "" },
173
- {"R" , "remove" , "Remove an entry" , "" },
174
- {"U" , "use-db" , "Set as active database" , "" },
175
- {"f" , "find" , "Search entries" , "" },
176
- {"E" , "edit" , "Edit entry by id" , "" },
177
- {"l" , "list-entry" , "List entry by id" , "" },
178
- {"x" , "export" , "Export all entries to <filename>" , "" },
179
- {"g" , "genpass" , "Generate password of given length" , "12" },
180
187
}
181
188
182
- for _ , opt := range stringOptions {
183
- optMap [opt .Long ] = parser .String (opt .Short , opt .Long , & argparse.Options {Help : opt .Help , Default : opt .Default })
184
- }
185
-
186
189
return optMap
187
190
}
188
191
@@ -192,19 +195,19 @@ func main() {
192
195
os .Args = append (os .Args , "-h" )
193
196
}
194
197
195
- parser := argparse .NewParser ("varuh" , "Password manager for the command line for Unix like operating systems" )
196
-
197
- // optMap, optionMap := initializeCommandLine(parser)
198
+ parser := argparse .NewParser ("varuh" ,
199
+ "Password manager for the command line for Unix like operating systems" ,
200
+ AUTHOR_INFO ,
201
+ )
198
202
199
- // versionFlag := parser.Flag("v", "version", &argparse.Options{Help: "Show version information and exit"})
200
203
optMap := initializeCmdLine (parser )
201
-
204
+
202
205
err := parser .Parse (os .Args )
203
206
204
207
if err != nil {
205
208
fmt .Println (parser .Usage (err ))
206
209
}
207
-
210
+
208
211
getOrCreateLocalConfig (APP )
209
212
210
213
performAction (optMap )
0 commit comments