Skip to content

Commit 15147e8

Browse files
author
Anand
committed
ref issue #6 - Allow custom fields
1 parent 0f06b64 commit 15147e8

File tree

1 file changed

+92
-2
lines changed

1 file changed

+92
-2
lines changed

actions.go

+92-2
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)