Skip to content

Commit 4fc13f6

Browse files
authored
Merge pull request #20 from pythonhacker/issue-6-custom-fields
Issue 6 custom fields
2 parents 0f06b64 + 227d8d0 commit 4fc13f6

File tree

5 files changed

+287
-10
lines changed

5 files changed

+287
-10
lines changed

README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ The password database is created and is active now. You can start adding entries
148148
Password (enter to generate new):
149149
Generating password ...done
150150
Notes: Website uses Nginx auth
151+
Do you want to add custom fields [y/N]:
151152
Created new entry with id: 1
152153

153154
You can now list the entry with one of the list options.
@@ -163,6 +164,36 @@ You can now list the entry with one of the list options.
163164
Modified: 2021-21-09 23:12:35
164165
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
165166

167+
## Add an entry with custom fields
168+
169+
From version 0.3 onwards, custom fields are supported.
170+
171+
$ varuh -A
172+
Title: Github token
173+
URL: https://github.com/mydev/myproject
174+
Username: mydev
175+
Password (enter to generate new): ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
176+
Notes: Never Expires
177+
Do you want to add custom fields [y/N]: y
178+
Field Name: Domain
179+
Value for Domain: github.com
180+
Field Name: Type
181+
Value for Type: Auth Token
182+
Field Name:
183+
Created new entry with id: 6
184+
185+
$ varuh -l 6
186+
ID: 6
187+
Title: Github token
188+
User: mydev
189+
URL: https://github.com/mydev/myproject
190+
Password: ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
191+
Notes: Never Expires
192+
Domain: github.com
193+
Type: Auth Token
194+
Modified: 2021-21-13 00:07:18
195+
196+
166197
For more on listing see the [Listing and Searching](#listing-and-searching) section below.
167198

168199
## Edit an entry
@@ -178,6 +209,7 @@ For more on listing see the [Listing and Searching](#listing-and-searching) sect
178209
New Password ([y/Y] to generate new, enter will keep old one):
179210
Current Notes: Website uses Nginx auth
180211
New Notes: Website uses Apache
212+
Do you want to add custom fields [y/N]:
181213
Updated entry.
182214

183215
$ varuh -l 1 -s
@@ -191,14 +223,50 @@ For more on listing see the [Listing and Searching](#listing-and-searching) sect
191223
Modified: 2021-21-09 23:15:29
192224
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
193225

226+
## Edit an entry with custom fields
227+
228+
When you edit an entry with custom fields, you get the option to change the name of the fields or delete the fields entirely.
229+
230+
$ varuh -E 6
231+
Current Title: Github token
232+
New Title:
233+
Current URL: https://github.com/mydev/myproject
234+
New URL:
235+
Current Username: mydev
236+
New Username:
237+
Current Password: ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
238+
New Password ([y/Y] to generate new, enter will keep old one):
239+
Current Notes: Never Expires
240+
New Notes:
241+
Editing/deleting custom fields
242+
Field Name: Domain
243+
New Field Name (Enter to keep, "x" to delete): x
244+
Field Name: Type
245+
New Field Name (Enter to keep, "x" to delete): Token Type
246+
Field Value: Auth Token
247+
New Field Value (Enter to keep):
248+
Do you want to add custom fields [y/N]:
249+
Created 1 custom entries for entry: 21.
250+
Updated entry.
251+
252+
$ varuh -l 6 -s
253+
ID: 6
254+
Title: Github token
255+
User: mydev
256+
URL: https://github.com/mydev/myproject
257+
Password: ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
258+
Notes: Never Expires
259+
Token Type: Auth Token
260+
Modified: 2021-21-13 00:16:41
261+
194262
(*-s* turns on visible passwords)
195263

196264
## Clone an entry
197265

198266
To clone (copy) an entry,
199267

200268
$ $ varuh -C 1
201-
Cloned to new entry, id: 2
269+
Cloned to new entry, id: 3
202270

203271
## Remove an entry
204272

@@ -207,8 +275,8 @@ To clone (copy) an entry,
207275

208276
It is an error if the id does not exist.
209277

210-
$ varuh -R 3
211-
No entry with id 3 was found
278+
$ varuh -R 4
279+
No entry with id 4 was found
212280

213281
## Switch to a new database
214282

@@ -260,7 +328,7 @@ Manually decrypt the database using `-d` option.
260328

261329
Now the database is active again and you can see the listings.
262330

263-
$ varuh -l 2
331+
$ varuh -l 3
264332
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
265333
ID: 2
266334
Title: My Blog Login

actions.go

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
"syscall"
1616
)
1717

18+
type CustomEntry struct {
19+
fieldName string
20+
fieldValue string
21+
}
22+
1823
// Wrappers (closures) for functions accepting strings as input for in/out encryption
1924
func WrapperMaxKryptStringFunc(fn actionFunc) actionFunc {
2025

@@ -219,6 +224,7 @@ func addNewEntry() error {
219224
var notes string
220225
var passwd string
221226
var err error
227+
var customEntries []CustomEntry
222228

223229
if err = checkActiveDatabase(); err != nil {
224230
return err
@@ -260,8 +266,10 @@ func addNewEntry() error {
260266
return errors.New("invalid input")
261267
}
262268

269+
customEntries = addCustomFields(reader)
270+
263271
// Trim spaces
264-
err = addNewDatabaseEntry(title, userName, url, passwd, notes)
272+
err = addNewDatabaseEntry(title, userName, url, passwd, notes, customEntries)
265273

266274
if err != nil {
267275
fmt.Printf("Error adding entry - \"%s\"\n", err.Error())
@@ -270,6 +278,86 @@ func addNewEntry() error {
270278
return err
271279
}
272280

281+
// Function to update existing custom entries and add new ones
282+
// The bool part of the return value indicates whether to take action
283+
func addOrUpdateCustomFields(reader *bufio.Reader, entry *Entry) ([]CustomEntry, bool) {
284+
285+
var customEntries []ExtendedEntry
286+
var editedCustomEntries []CustomEntry
287+
var newCustomEntries []CustomEntry
288+
var flag bool
289+
290+
customEntries = getExtendedEntries(entry)
291+
292+
if len(customEntries) > 0 {
293+
294+
fmt.Println("Editing/deleting custom fields")
295+
for _, customEntry := range customEntries {
296+
var fieldName string
297+
var fieldValue string
298+
299+
fmt.Println("Field Name: " + customEntry.FieldName)
300+
fieldName = readInput(reader, "\tNew Field Name (Enter to keep, \"x\" to delete)")
301+
if strings.ToLower(strings.TrimSpace(fieldName)) == "x" {
302+
fmt.Println("Deleting field " + fieldName)
303+
} else {
304+
if strings.TrimSpace(fieldName) == "" {
305+
fieldName = customEntry.FieldName
306+
}
307+
308+
fmt.Println("Field Value: " + customEntry.FieldValue)
309+
fieldValue = readInput(reader, "\tNew Field Value (Enter to keep)")
310+
if strings.TrimSpace(fieldValue) == "" {
311+
fieldValue = customEntry.FieldValue
312+
}
313+
314+
editedCustomEntries = append(editedCustomEntries, CustomEntry{fieldName, fieldValue})
315+
}
316+
}
317+
}
318+
319+
newCustomEntries = addCustomFields(reader)
320+
321+
editedCustomEntries = append(editedCustomEntries, newCustomEntries...)
322+
323+
// Cases where length == 0
324+
// 1. Existing entries - all deleted
325+
flag = len(customEntries) > 0 || len(editedCustomEntries) > 0
326+
327+
return editedCustomEntries, flag
328+
}
329+
330+
// Function to add custom fields to an entry
331+
func addCustomFields(reader *bufio.Reader) []CustomEntry {
332+
333+
// Custom fields
334+
var custom string
335+
var customEntries []CustomEntry
336+
337+
custom = readInput(reader, "Do you want to add custom fields [y/N]")
338+
if strings.ToLower(custom) == "y" {
339+
340+
fmt.Println("Keep entering custom field name followed by the value. Press return with no input once done.")
341+
for true {
342+
var customFieldName string
343+
var customFieldValue string
344+
345+
customFieldName = strings.TrimSpace(readInput(reader, "Field Name"))
346+
if customFieldName != "" {
347+
customFieldValue = strings.TrimSpace(readInput(reader, "Value for "+customFieldName))
348+
}
349+
350+
if customFieldName == "" && customFieldValue == "" {
351+
break
352+
}
353+
354+
customEntries = append(customEntries, CustomEntry{customFieldName, customFieldValue})
355+
}
356+
}
357+
358+
return customEntries
359+
}
360+
273361
// Edit a current entry by id
274362
func editCurrentEntry(idString string) error {
275363

@@ -322,8 +410,10 @@ func editCurrentEntry(idString string) error {
322410
fmt.Printf("\nCurrent Notes: %s\n", entry.Notes)
323411
notes = readInput(reader, "New Notes")
324412

413+
customEntries, flag := addOrUpdateCustomFields(reader, entry)
414+
325415
// Update
326-
err = updateDatabaseEntry(entry, title, userName, url, passwd, notes)
416+
err = updateDatabaseEntry(entry, title, userName, url, passwd, notes, customEntries, flag)
327417
if err != nil {
328418
fmt.Printf("Error updating entry - \"%s\"\n", err.Error())
329419
}

0 commit comments

Comments
 (0)