From b208ea415a7aab6f95ca6bc775efba5e06b528b1 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 7 Mar 2024 18:34:12 -0500 Subject: [PATCH] Implement initial entry editing support (no notes editing or gen update) --- src/cli/add.go | 12 ++---------- src/cli/edit.go | 40 +++++++++++++++++++++++++++++++++++++++- src/cli/utilitiesMisc.go | 7 ++++--- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/cli/add.go b/src/cli/add.go index a3b2413..2503514 100644 --- a/src/cli/add.go +++ b/src/cli/add.go @@ -40,16 +40,8 @@ func AddEntry(targetLocation string, hidePassword bool, entryType uint8) { unencryptedEntry = append([]string{"", "", ""}, note...) } - // ensure entry data is not empty - var entryDataPresent bool - for _, line := range unencryptedEntry { - if line != "" { - entryDataPresent = true - break - } - } - - if entryDataPresent { + // write and preview the new entry + if offline.EntryIsNotEmpty(unencryptedEntry) { offline.WriteEntry(targetLocation, unencryptedEntry) fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset) EntryReader(unencryptedEntry, hidePassword) diff --git a/src/cli/edit.go b/src/cli/edit.go index 7787d42..f4bc5b1 100644 --- a/src/cli/edit.go +++ b/src/cli/edit.go @@ -1,6 +1,10 @@ package cli -import "github.com/rwinkhart/MUTN/src/offline" +import ( + "fmt" + "github.com/rwinkhart/MUTN/src/offline" + "os" +) // RenameCli renames an entry at oldLocation to a new location (user input) func RenameCli(oldLocation string) { @@ -13,3 +17,37 @@ func RenameCli(oldLocation string) { // exit is done from offline.Rename } + +// EditEntry edits a field of an entry at targetLocation (user input), does not allow for editing notes +func EditEntry(targetLocation string, hidePassword bool, field int) { + // ensure targetLocation exists + offline.TargetIsFile(targetLocation, true, 2) + + // read old entry data + unencryptedEntry := offline.DecryptGPG(targetLocation) + + // ensure slice is long enough for field + for len(unencryptedEntry) <= field { + unencryptedEntry = append(unencryptedEntry, "") + } + + // edit the field + switch field { + case 0: + unencryptedEntry[field] = inputHidden("Password:") + case 1: + unencryptedEntry[field] = input("Username:") + case 2: + unencryptedEntry[field] = input("URL:") + } + + // write and preview the modified entry + if offline.EntryIsNotEmpty(unencryptedEntry) { + offline.WriteEntry(targetLocation, unencryptedEntry) + fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset) + EntryReader(unencryptedEntry, hidePassword) + } else { + fmt.Println(offline.AnsiError + "No data supplied for entry" + offline.AnsiReset) + os.Exit(1) + } +} diff --git a/src/cli/utilitiesMisc.go b/src/cli/utilitiesMisc.go index 7669afe..28dbbfa 100644 --- a/src/cli/utilitiesMisc.go +++ b/src/cli/utilitiesMisc.go @@ -7,14 +7,15 @@ import ( "golang.org/x/crypto/ssh/terminal" "os" "os/exec" + "strings" ) // input prompts the user for input and returns the input as a string func input(prompt string) string { fmt.Print("\n" + prompt + " ") - var userInput string - fmt.Scanln(&userInput) - return userInput + reader := bufio.NewReader(os.Stdin) + userInput, _ := reader.ReadString('\n') + return strings.TrimRight(userInput, "\n ") // remove trailing newlines and spaces } // inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal