@@ -15,6 +15,11 @@ import (
15
15
"syscall"
16
16
)
17
17
18
+ type CustomEntry struct {
19
+ fieldName string
20
+ fieldValue string
21
+ }
22
+
18
23
// Wrappers (closures) for functions accepting strings as input for in/out encryption
19
24
func WrapperMaxKryptStringFunc (fn actionFunc ) actionFunc {
20
25
@@ -219,6 +224,7 @@ func addNewEntry() error {
219
224
var notes string
220
225
var passwd string
221
226
var err error
227
+ var customEntries []CustomEntry
222
228
223
229
if err = checkActiveDatabase (); err != nil {
224
230
return err
@@ -260,8 +266,10 @@ func addNewEntry() error {
260
266
return errors .New ("invalid input" )
261
267
}
262
268
269
+ customEntries = addCustomFields (reader )
270
+
263
271
// Trim spaces
264
- err = addNewDatabaseEntry (title , userName , url , passwd , notes )
272
+ err = addNewDatabaseEntry (title , userName , url , passwd , notes , customEntries )
265
273
266
274
if err != nil {
267
275
fmt .Printf ("Error adding entry - \" %s\" \n " , err .Error ())
@@ -270,6 +278,86 @@ func addNewEntry() error {
270
278
return err
271
279
}
272
280
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 , "\t New 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 , "\t New 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
+
273
361
// Edit a current entry by id
274
362
func editCurrentEntry (idString string ) error {
275
363
@@ -322,8 +410,10 @@ func editCurrentEntry(idString string) error {
322
410
fmt .Printf ("\n Current Notes: %s\n " , entry .Notes )
323
411
notes = readInput (reader , "New Notes" )
324
412
413
+ customEntries , flag := addOrUpdateCustomFields (reader , entry )
414
+
325
415
// Update
326
- err = updateDatabaseEntry (entry , title , userName , url , passwd , notes )
416
+ err = updateDatabaseEntry (entry , title , userName , url , passwd , notes , customEntries , flag )
327
417
if err != nil {
328
418
fmt .Printf ("Error updating entry - \" %s\" \n " , err .Error ())
329
419
}
0 commit comments