Implement utility function for formatting and printing errors

This commit is contained in:
2025-02-01 20:48:03 -05:00
parent 173574eb85
commit e9d0c37043
18 changed files with 84 additions and 159 deletions
+3 -7
View File
@@ -1,7 +1,6 @@
package sync
import (
"fmt"
"math/rand"
"os"
"strconv"
@@ -24,8 +23,7 @@ func DeviceIDGen(oldDeviceID string) (string, string) {
// create new device ID file (locally)
fileToClose, err := os.OpenFile(core.ConfigDir+core.PathSeparator+"devices"+core.PathSeparator+newDeviceID, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
fmt.Println(core.AnsiError+"Failed to create local device ID file:", err.Error()+core.AnsiReset)
os.Exit(core.ErrorWrite)
core.PrintError("Failed to create local device ID file: "+err.Error(), core.ErrorWrite, true)
}
_ = fileToClose.Close() // error ignored; if the file could be created, it can probably be closed
@@ -36,15 +34,13 @@ func DeviceIDGen(oldDeviceID string) (string, string) {
sshEntryRootSSHIsWindows := strings.Split(GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID), core.FSSpace)
err = sshClient.Close()
if err != nil {
fmt.Println(core.AnsiError+"Init failed - Unable to close SSH client:", err.Error()+core.AnsiReset)
os.Exit(core.ErrorServerConnection)
core.PrintError("Init failed - Unable to close SSH client: "+err.Error(), core.ErrorServerConnection, true)
}
// remove old device ID file (locally; may not exist)
err = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + oldDeviceID)
if err != nil {
fmt.Println(core.AnsiError+"Failed to remove old device ID file (locally):", err.Error()+core.AnsiReset)
os.Exit(core.ErrorWrite)
core.PrintError("Failed to remove old device ID file (locally): "+err.Error(), core.ErrorWrite, true)
}
return sshEntryRootSSHIsWindows[0], sshEntryRootSSHIsWindows[1]