From 0f65cd31cb07c087b1f8123dc1717dd468110072 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sun, 3 Mar 2024 22:02:51 -0500 Subject: [PATCH] Implement errors for copying entry fields that do not exist or are blank --- main.go | 2 +- src/offline/copyUNIXGeneric.go | 20 +++++++++++++++++--- src/offline/copyWIN.go | 19 +++++++++++++++++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 0c4e9d3..e348211 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { switch args[2] { case "copy": - var field uint8 + var field int switch args[3] { case "password", "-p": field = 0 diff --git a/src/offline/copyUNIXGeneric.go b/src/offline/copyUNIXGeneric.go index 949ed63..28c1ac5 100644 --- a/src/offline/copyUNIXGeneric.go +++ b/src/offline/copyUNIXGeneric.go @@ -10,13 +10,27 @@ import ( "time" ) -// TODO Avoid index out of range errors when copying fields that do not exist // TODO Implement support for MacOS via pbcopy, Termux via termux-clipboard-set (in separate files) // CopyField copies a field from an entry to the clipboard -func CopyField(targetLocation string, field uint8, executableName string) { +func CopyField(targetLocation string, field int, executableName string) { if isFile, _ := TargetIsFile(targetLocation, true); isFile { - copySubject := DecryptGPG(targetLocation)[field] + decryptedEntry := DecryptGPG(targetLocation) + var copySubject string // will store data to be copied + + // ensure field exists in entry + if len(decryptedEntry) > field { + copySubject = decryptedEntry[field] + } else { + fmt.Println(AnsiError + "Field does not exist in entry" + AnsiReset) + os.Exit(1) + } + + // ensure field is not blank + if copySubject == "" { + fmt.Println(AnsiError + "Field is empty" + AnsiReset) + os.Exit(1) + } var envSet bool // track whether environment variables are set var cmd *exec.Cmd diff --git a/src/offline/copyWIN.go b/src/offline/copyWIN.go index bae8fe8..f0404d7 100644 --- a/src/offline/copyWIN.go +++ b/src/offline/copyWIN.go @@ -13,9 +13,24 @@ import ( // TODO Avoid index out of range errors when copying fields that do not exist // CopyField copies a field from an entry to the clipboard -func CopyField(targetLocation string, field uint8, executableName string) { +func CopyField(targetLocation string, field int, executableName string) { if isFile, _ := TargetIsFile(targetLocation, true); isFile { - copySubject := DecryptGPG(targetLocation)[field] + decryptedEntry := DecryptGPG(targetLocation) + var copySubject string // will store data to be copied + + // ensure field exists in entry + if len(decryptedEntry) > field { + copySubject = decryptedEntry[field] + } else { + fmt.Println(AnsiError + "Field does not exist in entry" + AnsiReset) + os.Exit(1) + } + + // ensure field is not blank + if copySubject == "" { + fmt.Println(AnsiError + "Field is empty" + AnsiReset) + os.Exit(1) + } cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", copySubject)) err := cmd.Run()