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 83aa4ee497
commit 35192fa568
4 changed files with 19 additions and 6 deletions
+9 -1
View File
@@ -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 {
+6 -1
View File
@@ -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)
}
+2 -2
View File
@@ -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)
+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)
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)