mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-30 22:06:32 -04:00
Assign error-specific exit codes to constants
This commit is contained in:
+12
-14
@@ -16,19 +16,17 @@ const (
|
||||
AnsiError = "\033[38;5;9m"
|
||||
AnsiReset = "\033[0m"
|
||||
|
||||
ErrorRead = 101
|
||||
ErrorWrite = 102
|
||||
ErrorSyncProcess = 103
|
||||
ErrorServerConnection = 104
|
||||
ErrorTargetNotFound = 105
|
||||
ErrorTargetExists = 106
|
||||
ErrorTargetWrongType = 107
|
||||
ErrorDecryption = 108
|
||||
ErrorEncryption = 109
|
||||
ErrorClipboard = 110
|
||||
ErrorOther = 111
|
||||
|
||||
LibmuttonVersion = "0.2.B" // untagged releases feature a letter suffix corresponding to the eventual release version, e.g "0.2.A" -> "0.2.0", "0.2.B" -> "0.2.1"
|
||||
)
|
||||
|
||||
// numeric error codes legend
|
||||
// 0: no error
|
||||
// 101: read error
|
||||
// 102: write error
|
||||
// 103: sync process error
|
||||
// 104: server connection error
|
||||
// 105: target not found
|
||||
// 106: target already exists
|
||||
// 107: target wrong type (file/directory)
|
||||
// 108: decryption error
|
||||
// 109: encryption error
|
||||
// 110: clipboard error
|
||||
// 111: other error
|
||||
|
||||
@@ -14,7 +14,7 @@ func loadConfig() *ini.File {
|
||||
cfg, err := ini.Load(ConfigPath)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to load libmutton.ini:", err.Error()+AnsiReset)
|
||||
os.Exit(101)
|
||||
os.Exit(ErrorRead)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func ParseConfig(valuesRequested [][2]string, missingValueError string) []string
|
||||
default:
|
||||
fmt.Println(AnsiError + missingValueError + AnsiReset)
|
||||
}
|
||||
os.Exit(101)
|
||||
os.Exit(ErrorRead)
|
||||
}
|
||||
|
||||
config = append(config, value)
|
||||
@@ -58,7 +58,7 @@ func GenDeviceIDList(errorOnFail bool) *[]fs.DirEntry {
|
||||
if err != nil {
|
||||
if errorOnFail {
|
||||
fmt.Println(AnsiError+"Failed to read the devices directory:", err.Error()+AnsiReset)
|
||||
os.Exit(101)
|
||||
os.Exit(ErrorRead)
|
||||
} else {
|
||||
return nil // a nil return value indicates that the devices directory could not be read/does not exist
|
||||
}
|
||||
@@ -98,6 +98,6 @@ func WriteConfig(valuesToWrite [][3]string, append bool) {
|
||||
err := cfg.SaveTo(ConfigPath)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to save libmutton.ini:", err.Error()+AnsiReset)
|
||||
os.Exit(102)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -24,7 +24,7 @@ func CopyArgument(executableName, targetLocation string, field int) {
|
||||
// ensure field is not empty
|
||||
if decryptedEntry[field] == "" {
|
||||
fmt.Println(AnsiError + "Field is empty" + AnsiReset)
|
||||
os.Exit(105)
|
||||
os.Exit(ErrorTargetNotFound)
|
||||
}
|
||||
|
||||
if field != 2 {
|
||||
@@ -51,7 +51,7 @@ func CopyArgument(executableName, targetLocation string, field int) {
|
||||
}
|
||||
} else {
|
||||
fmt.Println(AnsiError + "Field does not exist in entry" + AnsiReset)
|
||||
os.Exit(105)
|
||||
os.Exit(ErrorTargetNotFound)
|
||||
}
|
||||
|
||||
// copy field to clipboard, launch clipboard clearing process
|
||||
@@ -84,7 +84,7 @@ func GenTOTP(secret string, time time.Time, forSteam bool) string {
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Error generating TOTP code" + AnsiReset)
|
||||
os.Exit(111)
|
||||
os.Exit(ErrorOther)
|
||||
}
|
||||
|
||||
return totpToken
|
||||
|
||||
+4
-4
@@ -17,7 +17,7 @@ func copyField(executableName, copySubject string) {
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
@@ -27,7 +27,7 @@ func copyField(executableName, copySubject string) {
|
||||
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(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func clipClear(oldContents string) {
|
||||
newContents, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
@@ -50,7 +50,7 @@ func clipClear(oldContents string) {
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
|
||||
+4
-4
@@ -17,7 +17,7 @@ func copyField(executableName, copySubject string) {
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
@@ -27,7 +27,7 @@ func copyField(executableName, copySubject string) {
|
||||
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(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func clipClear(oldContents string) {
|
||||
newContents, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
@@ -50,7 +50,7 @@ func clipClear(oldContents string) {
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
|
||||
@@ -21,14 +21,14 @@ func copyField(executableName, copySubject string) {
|
||||
cmd = exec.Command("xclip", "-sel", "c")
|
||||
} else {
|
||||
fmt.Println(AnsiError + "Clipboard platform could not be determined - Note that the clipboard does not function in a raw TTY" + AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
writeToStdin(cmd, copySubject)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
@@ -38,7 +38,7 @@ func copyField(executableName, copySubject string) {
|
||||
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(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
@@ -59,14 +59,14 @@ func clipClear(oldContents string) {
|
||||
cmdPaste = exec.Command("xclip", "-o", "-sel", "c")
|
||||
} else {
|
||||
fmt.Println(AnsiError + "Clipboard platform could not be determined - Neither $WAYLAND_DISPLAY nor $DISPLAY are set" + AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// read current clipboard contents
|
||||
newContents, err := cmdPaste.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// clear clipboard if contents have not been modified
|
||||
@@ -74,7 +74,7 @@ func clipClear(oldContents string) {
|
||||
err = cmdClear.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
|
||||
+4
-4
@@ -16,7 +16,7 @@ func copyField(executableName, copySubject string) {
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to copy to clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
// launch clipboard clearing process if executableName is provided
|
||||
@@ -26,7 +26,7 @@ func copyField(executableName, copySubject string) {
|
||||
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(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func clipClear(oldContents string) {
|
||||
newContents, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
|
||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
@@ -48,7 +48,7 @@ func clipClear(oldContents string) {
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
|
||||
os.Exit(110)
|
||||
os.Exit(ErrorClipboard)
|
||||
}
|
||||
}
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
|
||||
+2
-2
@@ -19,7 +19,7 @@ func DecryptGPG(targetLocation string) []string {
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to decrypt \"" + targetLocation + "\" - Ensure it is a valid GPG-encrypted file and that you entered your passphrase correctly" + AnsiReset)
|
||||
os.Exit(108)
|
||||
os.Exit(ErrorDecryption)
|
||||
}
|
||||
outputSlice := strings.Split(string(output), "\n")
|
||||
|
||||
@@ -33,7 +33,7 @@ func EncryptGPG(input []string) []byte {
|
||||
encryptedBytes, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to encrypt data - Ensure that your GPG key is valid and that you have a valid GPG ID set in libmutton.ini" + AnsiReset)
|
||||
os.Exit(109)
|
||||
os.Exit(ErrorEncryption)
|
||||
}
|
||||
return encryptedBytes
|
||||
}
|
||||
|
||||
+4
-4
@@ -46,7 +46,7 @@ func GpgKeyGen() string {
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to generate GPG key:", err.Error()+AnsiReset)
|
||||
os.Exit(111)
|
||||
os.Exit(ErrorOther)
|
||||
}
|
||||
|
||||
return "libmutton-" + unixTime + " (gpg-libmutton) <github.com/rwinkhart/libmutton>"
|
||||
@@ -59,7 +59,7 @@ func DirInit(preserveOldConfigDir bool) string {
|
||||
err := os.MkdirAll(EntryRoot, 0700)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to create \""+EntryRoot+"\":", err.Error()+AnsiReset)
|
||||
os.Exit(102)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
|
||||
// get old device ID before its potential removal
|
||||
@@ -78,7 +78,7 @@ func DirInit(preserveOldConfigDir bool) string {
|
||||
err = os.RemoveAll(ConfigDir)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to remove existing config directory:", err.Error()+AnsiReset)
|
||||
os.Exit(102)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func DirInit(preserveOldConfigDir bool) string {
|
||||
err = os.MkdirAll(ConfigDir+PathSeparator+"devices", 0700)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to create \""+ConfigDir+"\":", err.Error()+AnsiReset)
|
||||
os.Exit(102)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
|
||||
return oldDeviceID
|
||||
|
||||
@@ -19,20 +19,20 @@ func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8)
|
||||
if err != nil {
|
||||
if errorOnFail {
|
||||
fmt.Println(AnsiError + "Failed to access \"" + targetLocation + "\" - Ensure it exists and has the correct permissions" + AnsiReset)
|
||||
os.Exit(105)
|
||||
os.Exit(ErrorTargetNotFound)
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
if targetInfo.IsDir() {
|
||||
if errorOnFail && failCondition == 2 {
|
||||
fmt.Println(AnsiError + "\"" + targetLocation + "\" is a directory" + AnsiReset)
|
||||
os.Exit(107)
|
||||
os.Exit(ErrorTargetWrongType)
|
||||
}
|
||||
return false, true
|
||||
} else {
|
||||
if errorOnFail && failCondition == 1 {
|
||||
fmt.Println(AnsiError + "\"" + targetLocation + "\" is a file" + AnsiReset)
|
||||
os.Exit(107)
|
||||
os.Exit(ErrorTargetWrongType)
|
||||
}
|
||||
return true, true
|
||||
}
|
||||
@@ -44,7 +44,7 @@ func WriteEntry(targetLocation string, entryData []string, verifyEntryDoesNotExi
|
||||
_, isAccessible := TargetIsFile(targetLocation, false, 0)
|
||||
if isAccessible {
|
||||
fmt.Println(AnsiError + "Target location already exists" + AnsiReset)
|
||||
os.Exit(106)
|
||||
os.Exit(ErrorTargetExists)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func WriteEntry(targetLocation string, entryData []string, verifyEntryDoesNotExi
|
||||
err := os.WriteFile(targetLocation, encryptedBytes, 0600)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to write to file:", err.Error()+AnsiReset)
|
||||
os.Exit(102)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ func writeToStdin(cmd *exec.Cmd, input string) {
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to access stdin for system command:", err.Error()+AnsiReset)
|
||||
os.Exit(111)
|
||||
os.Exit(ErrorOther)
|
||||
}
|
||||
|
||||
go func() {
|
||||
@@ -77,7 +77,7 @@ func CreateTempFile() *os.File {
|
||||
tempFile, err := os.CreateTemp("", "*.markdown")
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError+"Failed to create temporary file:", err.Error()+AnsiReset)
|
||||
os.Exit(102)
|
||||
os.Exit(ErrorWrite)
|
||||
}
|
||||
return tempFile
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user