Fail early if adding an entry to a directory that does not exist (or if the directory is a file)

This commit is contained in:
2024-07-19 15:53:54 -04:00
parent 9077501d66
commit d659291a2f
3 changed files with 10 additions and 5 deletions
+6 -1
View File
@@ -3,6 +3,7 @@ package cli
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"github.com/rwinkhart/MUTN/src/backend" "github.com/rwinkhart/MUTN/src/backend"
) )
@@ -10,12 +11,16 @@ import (
// AddEntry creates a new entry at targetLocation by taking user input via CLI prompts // AddEntry creates a new entry at targetLocation by taking user input via CLI prompts
// entryType: 0 = standard (password), 1 = auto-generated password, 2 = note // entryType: 0 = standard (password), 1 = auto-generated password, 2 = note
func AddEntry(targetLocation string, hideSecrets bool, entryType uint8) { func AddEntry(targetLocation string, hideSecrets bool, entryType uint8) {
// ensure target location does not already exist
_, isAccessible := backend.TargetIsFile(targetLocation, false, 0) _, isAccessible := backend.TargetIsFile(targetLocation, false, 0)
if isAccessible { if isAccessible {
fmt.Println(backend.AnsiError + "Target location already exists" + backend.AnsiReset) fmt.Println(backend.AnsiError + "Target location already exists" + backend.AnsiReset)
os.Exit(1) os.Exit(1)
} }
// ensure target containing directory exists and is a directory (not a file)
backend.TargetIsFile(targetLocation[:strings.LastIndex(targetLocation, "/")], true, 1)
var unencryptedEntry []string var unencryptedEntry []string
if entryType < 2 { if entryType < 2 {
@@ -43,5 +48,5 @@ func AddEntry(targetLocation string, hideSecrets bool, entryType uint8) {
} }
// write and preview the new entry // write and preview the new entry
writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets) writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets, false)
} }
+2 -2
View File
@@ -51,7 +51,7 @@ func EditEntryField(targetLocation string, hideSecrets bool, field int) {
} }
// write and preview the modified entry // write and preview the modified entry
writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets) writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets, false)
} }
// GenUpdate generates a new password for an entry at targetLocation (user input) // GenUpdate generates a new password for an entry at targetLocation (user input)
@@ -63,7 +63,7 @@ func GenUpdate(targetLocation string, hideSecrets bool) {
unencryptedEntry[0] = backend.StringGen(inputInt("Password length:", -1), inputBinary("Generate a complex (special characters) password?"), 0.2, false) unencryptedEntry[0] = backend.StringGen(inputInt("Password length:", -1), inputBinary("Generate a complex (special characters) password?"), 0.2, false)
// write and preview the modified entry // write and preview the modified entry
writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets) writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets, false)
} }
// editNote uses the user-specified text editor to edit an existing note (or create a new one if baseNote is empty) // editNote uses the user-specified text editor to edit an existing note (or create a new one if baseNote is empty)
+2 -2
View File
@@ -65,10 +65,10 @@ func inputMenuGen(prompt string, options []string) int {
} }
// writeEntryCLI writes an entry to targetLocation and previews it (errors if no data is supplied) // writeEntryCLI writes an entry to targetLocation and previews it (errors if no data is supplied)
func writeEntryCLI(targetLocation string, unencryptedEntry []string, hideSecrets bool) { func writeEntryCLI(targetLocation string, unencryptedEntry []string, hideSecrets, verifyEntryDoesNotExist bool) {
if backend.EntryIsNotEmpty(unencryptedEntry) { if backend.EntryIsNotEmpty(unencryptedEntry) {
// write the entry to the target location // write the entry to the target location
backend.WriteEntry(targetLocation, unencryptedEntry) backend.WriteEntry(targetLocation, unencryptedEntry, verifyEntryDoesNotExist)
// preview the entry // preview the entry
fmt.Println(AnsiBold + "\nEntry Preview:" + backend.AnsiReset) fmt.Println(AnsiBold + "\nEntry Preview:" + backend.AnsiReset)
EntryReader(unencryptedEntry, hideSecrets, true) EntryReader(unencryptedEntry, hideSecrets, true)