diff --git a/src/backend/utilitiesMisc.go b/src/backend/utilitiesMisc.go index 4fec3f7..6d391b1 100644 --- a/src/backend/utilitiesMisc.go +++ b/src/backend/utilitiesMisc.go @@ -40,7 +40,15 @@ func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8) } // WriteEntry writes entryData to an encrypted file at targetLocation -func WriteEntry(targetLocation string, entryData []string) { +func WriteEntry(targetLocation string, entryData []string, verifyEntryDoesNotExist bool) { + if verifyEntryDoesNotExist { + _, isAccessible := TargetIsFile(targetLocation, false, 0) + if isAccessible { + fmt.Println(AnsiError + "Target location already exists" + AnsiReset) + os.Exit(1) + } + } + encryptedBytes := EncryptGPG(entryData) err := os.WriteFile(targetLocation, encryptedBytes, 0600) if err != nil { diff --git a/src/cli/add.go b/src/cli/add.go index ef28adf..7e6370e 100644 --- a/src/cli/add.go +++ b/src/cli/add.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "os" + "strings" "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 // entryType: 0 = standard (password), 1 = auto-generated password, 2 = note func AddEntry(targetLocation string, hideSecrets bool, entryType uint8) { + // ensure target location does not already exist _, isAccessible := backend.TargetIsFile(targetLocation, false, 0) if isAccessible { fmt.Println(backend.AnsiError + "Target location already exists" + backend.AnsiReset) 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 if entryType < 2 { @@ -43,5 +48,5 @@ func AddEntry(targetLocation string, hideSecrets bool, entryType uint8) { } // write and preview the new entry - writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets) + writeEntryCLI(targetLocation, unencryptedEntry, hideSecrets, false) } diff --git a/src/cli/edit.go b/src/cli/edit.go index 32ecbee..35ec0fc 100644 --- a/src/cli/edit.go +++ b/src/cli/edit.go @@ -51,7 +51,7 @@ func EditEntryField(targetLocation string, hideSecrets bool, field int) { } // 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) @@ -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) // 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) diff --git a/src/cli/utilitiesMisc.go b/src/cli/utilitiesMisc.go index aa7d13b..369368a 100644 --- a/src/cli/utilitiesMisc.go +++ b/src/cli/utilitiesMisc.go @@ -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) -func writeEntryCLI(targetLocation string, unencryptedEntry []string, hideSecrets bool) { +func writeEntryCLI(targetLocation string, unencryptedEntry []string, hideSecrets, verifyEntryDoesNotExist bool) { if backend.EntryIsNotEmpty(unencryptedEntry) { // write the entry to the target location - backend.WriteEntry(targetLocation, unencryptedEntry) + backend.WriteEntry(targetLocation, unencryptedEntry, verifyEntryDoesNotExist) // preview the entry fmt.Println(AnsiBold + "\nEntry Preview:" + backend.AnsiReset) EntryReader(unencryptedEntry, hideSecrets, true)