Handle errors when running clipboard-related commands with exec

This commit is contained in:
2024-03-03 21:24:29 -05:00
parent 9802b7498b
commit 988724bac6
2 changed files with 32 additions and 12 deletions
+16 -7
View File
@@ -10,8 +10,7 @@ import (
"time" "time"
) )
// TODO Handle cmd.Run errors, especially those due to missing clipboard utilities // TODO Avoid index out of range errors when copying fields that do not exist
// TODO Handle 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
@@ -32,13 +31,19 @@ func CopyField(targetLocation string, field uint8, executableName string) {
} }
writeToStdin(cmd, copySubject) 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") cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject) 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 { } else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset) fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset)
@@ -70,7 +75,11 @@ func ClipClear(oldContents string) {
case true: case true:
cmd = exec.Command("xclip", "-i", "/dev/null", "-sel", "c") 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) os.Exit(0)
} }
+16 -5
View File
@@ -10,8 +10,7 @@ import (
"time" "time"
) )
// TODO Handle cmd.Run errors, especially those due to missing clipboard utilities // TODO Avoid index out of range errors when copying fields that do not exist
// TODO Handle 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 uint8, executableName string) {
@@ -19,11 +18,19 @@ func CopyField(targetLocation string, field uint8, executableName string) {
copySubject := DecryptGPG(targetLocation)[field] copySubject := DecryptGPG(targetLocation)[field]
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))
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") cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject) 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 { } else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset) 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") { if oldContents == strings.TrimRight(string(newContents), "\r\n") {
cmd = exec.Command("powershell.exe", "-c", "Set-Clipboard") 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) os.Exit(0)
} }