Implement errors for copying entry fields that do not exist or are blank

This commit is contained in:
2024-03-03 22:02:51 -05:00
parent 988724bac6
commit 0f65cd31cb
3 changed files with 35 additions and 6 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ func main() {
switch args[2] { switch args[2] {
case "copy": case "copy":
var field uint8 var field int
switch args[3] { switch args[3] {
case "password", "-p": case "password", "-p":
field = 0 field = 0
+17 -3
View File
@@ -10,13 +10,27 @@ import (
"time" "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) // 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 // 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 { 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 envSet bool // track whether environment variables are set
var cmd *exec.Cmd var cmd *exec.Cmd
+17 -2
View File
@@ -13,9 +13,24 @@ import (
// TODO Avoid index out of range errors when copying fields that do not exist // TODO Avoid index out of range errors when copying fields that do not exist
// CopyField copies a field from an entry to the clipboard // 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 { 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)) cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", copySubject))
err := cmd.Run() err := cmd.Run()