@@ -4,22 +4,35 @@ package main
4
4
5
5
import (
6
6
"fmt"
7
- "strconv"
8
- getopt "github.com/pborman/getopt/v2"
7
+ "github.com/pythonhacker/argparse"
9
8
"os"
9
+ "strconv"
10
10
)
11
11
12
- const VERSION = 0.2
12
+ const VERSION = 0.3
13
13
const APP = "varuh"
14
- 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
+ `
15
19
16
20
type actionFunc func (string ) error
17
21
type actionFunc2 func (string ) (error , string )
18
22
type voidFunc func () error
19
23
24
+ // Structure to keep the options data
25
+ type CmdOption struct {
26
+ Short string
27
+ Long string
28
+ Help string
29
+ Path string
30
+ Default string
31
+ }
32
+
20
33
// Print the program's usage string and exit
21
34
func printUsage () error {
22
- getopt .Usage ()
35
+ // getopt.Usage()
23
36
os .Exit (0 )
24
37
25
38
return nil
@@ -34,13 +47,13 @@ func printVersionInfo() error {
34
47
}
35
48
36
49
// Command-line wrapper to generateRandomPassword
37
- func generatePassword (length string ) (error , string ) {
50
+ func genPass (length string ) (error , string ) {
38
51
var iLength int
39
52
var err error
40
53
var passwd string
41
-
54
+
42
55
iLength , _ = strconv .Atoi (length )
43
- err , passwd = generateRandomPassword (iLength )
56
+ err , passwd = generatePassword (iLength )
44
57
45
58
if err != nil {
46
59
fmt .Printf ("Error generating password - \" %s\" \n " , err .Error ())
@@ -53,12 +66,12 @@ func generatePassword(length string) (error, string) {
53
66
copyPasswordToClipboard (passwd )
54
67
fmt .Println ("Password copied to clipboard" )
55
68
}
56
-
69
+
57
70
return nil , passwd
58
71
}
59
72
60
- // Perform an action by using the command line options map
61
- func performAction (optMap map [string ]interface {}, optionMap map [ string ] interface {} ) {
73
+ // // Perform an action by using the command line options map
74
+ func performAction (optMap map [string ]interface {}) {
62
75
63
76
var flag bool
64
77
@@ -84,7 +97,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
84
97
85
98
stringActions2Map := map [string ]actionFunc2 {
86
99
"decrypt" : decryptDatabase ,
87
- "genpass" : generatePassword ,
100
+ "genpass" : genPass ,
88
101
}
89
102
90
103
flagsActionsMap := map [string ]voidFunc {
@@ -113,9 +126,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
113
126
}
114
127
115
128
for key , mappedFunc := range stringActionsMap {
116
- option := optionMap [key ].(Option )
117
-
118
- if * optMap [key ].(* string ) != option .Path {
129
+ if * optMap [key ].(* string ) != "" {
119
130
120
131
var val = * (optMap [key ].(* string ))
121
132
mappedFunc (val )
@@ -129,10 +140,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
129
140
}
130
141
131
142
for key , mappedFunc := range stringActions2Map {
132
- option := optionMap [key ].(Option )
133
-
134
- if * optMap [key ].(* string ) != option .Path {
135
-
143
+ if * optMap [key ].(* string ) != "" {
136
144
var val = * (optMap [key ].(* string ))
137
145
mappedFunc (val )
138
146
break
@@ -141,19 +149,66 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
141
149
142
150
}
143
151
152
+ func initializeCmdLine (parser * argparse.Parser ) map [string ]interface {} {
153
+ var optMap map [string ]interface {}
154
+
155
+ optMap = make (map [string ]interface {})
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
+
174
+ boolOptions := []CmdOption {
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" , "" , "" },
183
+ }
184
+
185
+ for _ , opt := range boolOptions {
186
+ optMap [opt .Long ] = parser .Flag (string (opt .Short ), opt .Long , & argparse.Options {Help : opt .Help })
187
+ }
188
+
189
+ return optMap
190
+ }
191
+
144
192
// Main routine
145
193
func main () {
146
194
if len (os .Args ) == 1 {
147
195
os .Args = append (os .Args , "-h" )
148
196
}
149
197
150
- optMap , optionMap := initializeCommandLine ()
151
- getopt .SetUsage (func () {
152
- usageString (optionMap )
153
- })
198
+ parser := argparse .NewParser ("varuh" ,
199
+ "Password manager for the command line for Unix like operating systems" ,
200
+ AUTHOR_INFO ,
201
+ )
202
+
203
+ optMap := initializeCmdLine (parser )
204
+
205
+ err := parser .Parse (os .Args )
206
+
207
+ if err != nil {
208
+ fmt .Println (parser .Usage (err ))
209
+ }
154
210
155
- getopt .Parse ()
156
211
getOrCreateLocalConfig (APP )
157
212
158
- performAction (optMap , optionMap )
213
+ performAction (optMap )
159
214
}
0 commit comments