From 356d1eab4e4cd0c68db9bd9aa0d3164c13cd254f Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 5 Jul 2024 22:18:17 -0400 Subject: [PATCH] Replace most occurrences of os.Exit(0) in the backend and sync packages with a custom backend.Exit function that can allow "soft exit" in GUI/TUI environments to avoid killing the entire process; currently all major errors still result in killing the entire process --- src/backend/configParser.go | 2 +- src/backend/copy.go | 2 +- src/backend/copyDARWIN.go | 4 ++-- src/backend/copyTERMUX.go | 4 ++-- src/backend/copyUNIXGeneric.go | 4 ++-- src/backend/copyWIN.go | 4 ++-- src/backend/edit.go | 2 +- src/backend/exitHard.go | 9 +++++++++ src/backend/exitSoft.go | 7 +++++++ src/sync/client.go | 8 ++++---- 10 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/backend/exitHard.go create mode 100644 src/backend/exitSoft.go diff --git a/src/backend/configParser.go b/src/backend/configParser.go index 5e98467..2cc03f7 100644 --- a/src/backend/configParser.go +++ b/src/backend/configParser.go @@ -35,7 +35,7 @@ func ParseConfig(readKeys []string, missingValueError string) []string { case "": fmt.Println(AnsiError + "Failed to find value for key \"" + key + "\" in section \"[LIBMUTTON]\" in libmutton.ini" + AnsiReset) case "0": - os.Exit(0) + Exit(0) default: fmt.Println(AnsiError + missingValueError + AnsiReset) } diff --git a/src/backend/copy.go b/src/backend/copy.go index 7c70913..f64d2d0 100644 --- a/src/backend/copy.go +++ b/src/backend/copy.go @@ -64,7 +64,7 @@ func ClipClearArgument() { oldContents := clipScanner.Text() clipClear(oldContents) } else { - os.Exit(0) + os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } } diff --git a/src/backend/copyDARWIN.go b/src/backend/copyDARWIN.go index 0edc316..2637f93 100644 --- a/src/backend/copyDARWIN.go +++ b/src/backend/copyDARWIN.go @@ -31,7 +31,7 @@ func copyField(executableName, copySubject string) { fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset) os.Exit(1) } - os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh + Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh } } @@ -55,5 +55,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) + os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/src/backend/copyTERMUX.go b/src/backend/copyTERMUX.go index a61b9e6..da44354 100644 --- a/src/backend/copyTERMUX.go +++ b/src/backend/copyTERMUX.go @@ -29,7 +29,7 @@ func copyField(executableName, copySubject string) { fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset) os.Exit(1) } - os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh + Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh } } @@ -53,5 +53,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) + os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/src/backend/copyUNIXGeneric.go b/src/backend/copyUNIXGeneric.go index f88c48f..58e75a9 100644 --- a/src/backend/copyUNIXGeneric.go +++ b/src/backend/copyUNIXGeneric.go @@ -40,7 +40,7 @@ func copyField(executableName, copySubject string) { fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset) os.Exit(1) } - os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh + Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh } } @@ -77,5 +77,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) + os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/src/backend/copyWIN.go b/src/backend/copyWIN.go index 987e0b5..b31f524 100644 --- a/src/backend/copyWIN.go +++ b/src/backend/copyWIN.go @@ -28,7 +28,7 @@ func copyField(executableName, copySubject string) { fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset) os.Exit(1) } - os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh + Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh } } @@ -51,5 +51,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) + os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/src/backend/edit.go b/src/backend/edit.go index 726a6b0..add05e7 100644 --- a/src/backend/edit.go +++ b/src/backend/edit.go @@ -37,7 +37,7 @@ func Rename(oldLocation, newLocation string) { } // TODO implement synced renaming - os.Exit(0) + Exit(0) } // EnsureSliceLength ensures slice is long enough to contain the specified index diff --git a/src/backend/exitHard.go b/src/backend/exitHard.go new file mode 100644 index 0000000..9759836 --- /dev/null +++ b/src/backend/exitHard.go @@ -0,0 +1,9 @@ +//go:build !returnOnExit + +package backend + +import "os" + +func Exit(code int) { + os.Exit(code) +} diff --git a/src/backend/exitSoft.go b/src/backend/exitSoft.go new file mode 100644 index 0000000..9b198d1 --- /dev/null +++ b/src/backend/exitSoft.go @@ -0,0 +1,7 @@ +//go:build returnOnExit + +package backend + +func Exit(code int) { + return code +} diff --git a/src/sync/client.go b/src/sync/client.go index effb1cc..c46a1fa 100644 --- a/src/sync/client.go +++ b/src/sync/client.go @@ -138,7 +138,7 @@ func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []str fmt.Println(backend.AnsiError + "Sync failed - No device ID found; run \"" + os.Args[0] + " init\" to generate a device ID" + backend.AnsiReset) os.Exit(1) } else { - os.Exit(0) // exit silently if the sync job was called automatically, as the user may just be in offline mode + backend.Exit(0) // exit silently if the sync job was called automatically, as the user may just be in offline mode } } output := GetSSHOutput("libmuttonserver fetch", clientDeviceID[0].Name(), manualSync) @@ -378,7 +378,7 @@ func ShearRemoteFromClient(targetLocationIncomplete string) { // call the server to remotely shear the target and add it to the deletions list GetSSHOutput("libmuttonserver shear", deviceID+"\n"+strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1d"), false) - os.Exit(0) // sync is not required after shearing since the target has already been removed from the local system + backend.Exit(0) // sync is not required after shearing since the target has already been removed from the local system } // deletionSync removes entries from the client that have been deleted on the server (multi-client deletion) @@ -400,7 +400,7 @@ func AddFolderRemoteFromClient(targetLocationIncomplete string) { AddFolderLocal(targetLocationIncomplete) // add the folder on the local system GetSSHOutput("libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1d"), false) // call the server to create the folder remotely - os.Exit(0) + backend.Exit(0) } // folderSync creates folders on the client (from the given list of folder names) @@ -439,5 +439,5 @@ func RunJob(manualSync bool) { syncLists(localEntryModMap, remoteEntryModMap, manualSync) // exit program after successful sync - os.Exit(0) + backend.Exit(0) }