From 988724bac6043454dbd6a36c2079ead9849a159a Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sun, 3 Mar 2024 21:24:29 -0500 Subject: [PATCH] Handle errors when running clipboard-related commands with exec --- src/offline/copyUNIXGeneric.go | 23 ++++++++++++++++------- src/offline/copyWIN.go | 21 ++++++++++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/offline/copyUNIXGeneric.go b/src/offline/copyUNIXGeneric.go index 320488c..949ed63 100644 --- a/src/offline/copyUNIXGeneric.go +++ b/src/offline/copyUNIXGeneric.go @@ -10,8 +10,7 @@ import ( "time" ) -// TODO Handle cmd.Run errors, especially those due to missing clipboard utilities -// TODO Handle 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 // 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 @@ -32,13 +31,19 @@ func CopyField(targetLocation string, field uint8, executableName string) { } writeToStdin(cmd, copySubject) - cmd.Run() + err := cmd.Run() + if err != nil { + fmt.Println(AnsiError + "Failed to copy to clipboard: " + err.Error() + AnsiReset) + os.Exit(1) + } - // TODO Strongly encourage other implementations to support a clipclear argument - // TODO If an implementation opts out of doing this, this may error out cmd = exec.Command(executableName, "clipclear") writeToStdin(cmd, copySubject) - cmd.Start() + err = cmd.Start() + if err != nil { + fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset) + os.Exit(1) + } } else { fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset) @@ -70,7 +75,11 @@ func ClipClear(oldContents string) { case true: cmd = exec.Command("xclip", "-i", "/dev/null", "-sel", "c") } - cmd.Run() + err := cmd.Run() + if err != nil { + fmt.Println(AnsiError + "Failed to clear clipboard: " + err.Error() + AnsiReset) + os.Exit(1) + } } os.Exit(0) } diff --git a/src/offline/copyWIN.go b/src/offline/copyWIN.go index aebc1ef..bae8fe8 100644 --- a/src/offline/copyWIN.go +++ b/src/offline/copyWIN.go @@ -10,8 +10,7 @@ import ( "time" ) -// TODO Handle cmd.Run errors, especially those due to missing clipboard utilities -// TODO Handle 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 func CopyField(targetLocation string, field uint8, executableName string) { @@ -19,11 +18,19 @@ func CopyField(targetLocation string, field uint8, executableName string) { copySubject := DecryptGPG(targetLocation)[field] cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", copySubject)) - cmd.Run() + err := cmd.Run() + if err != nil { + fmt.Println(AnsiError + "Failed to copy to clipboard: " + err.Error() + AnsiReset) + os.Exit(1) + } cmd = exec.Command(executableName, "clipclear") writeToStdin(cmd, copySubject) - cmd.Start() + err = cmd.Start() + if err != nil { + fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset) + os.Exit(1) + } } else { fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset) @@ -41,7 +48,11 @@ func ClipClear(oldContents string) { if oldContents == strings.TrimRight(string(newContents), "\r\n") { cmd = exec.Command("powershell.exe", "-c", "Set-Clipboard") - cmd.Run() + err := cmd.Run() + if err != nil { + fmt.Println(AnsiError + "Failed to clear clipboard: " + err.Error() + AnsiReset) + os.Exit(1) + } } os.Exit(0) }