Simplify error codes

This commit is contained in:
2025-06-01 14:31:46 -04:00
parent 1488c7d536
commit 499c9cdd40
9 changed files with 48 additions and 43 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ package cli
import (
"github.com/rwinkhart/go-boilerplate/back"
"github.com/rwinkhart/go-boilerplate/front"
"github.com/rwinkhart/go-boilerplate/other"
"github.com/rwinkhart/libmutton/core"
)
@@ -12,7 +13,7 @@ func AddEntry(targetLocation string, hideSecrets bool, entryType uint8) {
// ensure targetLocation is valid
_, err := core.EntryAddPrecheck(targetLocation)
if err != nil {
back.PrintError("Failed to add entry: "+err.Error(), back.ErrorWrite, true)
other.PrintError("Failed to add entry: "+err.Error(), back.ErrorWrite, true)
}
var decryptedEntry []string
+10 -6
View File
@@ -8,6 +8,7 @@ import (
"github.com/rwinkhart/go-boilerplate/back"
"github.com/rwinkhart/go-boilerplate/front"
"github.com/rwinkhart/go-boilerplate/other"
"github.com/rwinkhart/libmutton/cfg"
"github.com/rwinkhart/libmutton/core"
"github.com/rwinkhart/libmutton/syncclient"
@@ -19,7 +20,7 @@ func RenameCli(oldLocationIncomplete string) {
newLocationIncomplete := front.Input("New location:")
err := syncclient.RenameRemoteFromClient(oldLocationIncomplete, newLocationIncomplete)
if err != nil {
back.PrintError("Failed to rename entry: "+err.Error(), back.ErrorWrite, true)
other.PrintError("Failed to rename entry: "+err.Error(), back.ErrorWrite, true)
}
// exit is done from sync.RenameRemoteFromClient
@@ -30,7 +31,7 @@ func EditEntryField(targetLocation string, hideSecrets bool, field int) {
// fetch old entry data (with all required lines present)
decryptedEntry, err := core.GetOldEntryData(targetLocation, field)
if err != nil {
back.PrintError("Failed to fetch entry data: "+err.Error(), back.ErrorRead, true)
other.PrintError("Failed to fetch entry data: "+err.Error(), back.ErrorRead, true)
}
// edit the field
@@ -51,7 +52,7 @@ func EditEntryField(targetLocation string, hideSecrets bool, field int) {
// edit the note
editedNote, noteEdited := editNote(fieldsNote)
if !noteEdited { // exit early if the note was not edited
back.PrintError("Entry is unchanged", 0, true)
other.PrintError("Entry is unchanged", 0, true)
}
decryptedEntry = append(fieldsMain, editedNote...)
}
@@ -65,7 +66,7 @@ func GenUpdate(targetLocation string, hideSecrets bool) {
// fetch old entry data
decryptedEntry, err := core.GetOldEntryData(targetLocation, 0)
if err != nil {
back.PrintError("Failed to fetch entry data: "+err.Error(), back.ErrorRead, true)
other.PrintError("Failed to fetch entry data: "+err.Error(), back.ErrorRead, true)
}
// generate a new password
@@ -78,7 +79,10 @@ func GenUpdate(targetLocation string, hideSecrets bool) {
// editNote uses the user-specified text editor to edit an existing note (or create a new one if baseNote is empty).
// Returns the edited note and a boolean indicating whether the note was edited.
func editNote(baseNote []string) ([]string, bool) {
tempFile := back.CreateTempFile()
tempFile, err := back.CreateTempFile()
if err != nil {
other.PrintError("Failed to create temporary note file: "+err.Error(), back.ErrorWrite, true)
}
defer func(name string) {
_ = os.Remove(name) // error ignored; if the file could be created, it can probably be removed
}(tempFile.Name())
@@ -112,7 +116,7 @@ func editNote(baseNote []string) ([]string, bool) {
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
err = cmd.Run()
if err != nil {
panic(back.AnsiError + "Failed to write note with " + editor + back.AnsiReset) // panic is used to ensure the tempFile is removed, as per the defer statement
}
+2 -1
View File
@@ -6,6 +6,7 @@ import (
"strings"
"github.com/rwinkhart/go-boilerplate/back"
"github.com/rwinkhart/go-boilerplate/other"
"github.com/rwinkhart/libmutton/synccommon"
)
@@ -81,7 +82,7 @@ func printFileEntry(entry string, lastSlash, charCounter, indent int, colorAlter
func EntryListGen() {
fileList, dirList, err := synccommon.WalkEntryDir()
if err != nil {
back.PrintError("Failed to generate entry list: "+err.Error(), back.ErrorRead, true)
other.PrintError("Failed to generate entry list: "+err.Error(), back.ErrorRead, true)
}
// print header bar w/total entry count
+8 -3
View File
@@ -5,6 +5,7 @@ import (
"os"
"github.com/rwinkhart/go-boilerplate/back"
"github.com/rwinkhart/go-boilerplate/other"
"github.com/rwinkhart/libmutton/crypt"
"github.com/rwinkhart/libmutton/global"
"github.com/rwinkhart/libmutton/syncclient"
@@ -68,7 +69,7 @@ fieldLoop:
if syncEnabled {
_, err := syncclient.RunJob(false)
if err != nil {
back.PrintError("Failed to sync entries: "+err.Error(), global.ErrorSyncProcess, true)
other.PrintError("Failed to sync entries: "+err.Error(), global.ErrorSyncProcess, true)
}
}
@@ -77,10 +78,14 @@ fieldLoop:
// EntryReaderDecrypt is a wrapper for EntryReader that first decrypts an RCW-wrapped file before sending it to EntryReader.
func EntryReaderDecrypt(targetLocation string, hideSecrets bool) {
if isFile, _ := back.TargetIsFile(targetLocation, true, 2); isFile {
isFile, _, err := back.TargetIsFile(targetLocation, true, 2)
if err != nil {
other.PrintError("Failed to verify target location: "+err.Error(), back.ErrorTargetNotFound, true)
}
if isFile {
decBytes, err := crypt.DecryptFileToSlice(targetLocation)
if err != nil {
back.PrintError("Failed to decrypt entry: "+err.Error(), global.ErrorDecryption, true)
other.PrintError("Failed to decrypt entry: "+err.Error(), global.ErrorDecryption, true)
}
EntryReader(decBytes, hideSecrets, false) // never sync if decrypting straight to EntryReader, as this means the entry could not have been modified
}
+3 -2
View File
@@ -6,6 +6,7 @@ import (
"github.com/rwinkhart/go-boilerplate/back"
"github.com/rwinkhart/go-boilerplate/front"
"github.com/rwinkhart/go-boilerplate/other"
"github.com/rwinkhart/libmutton/core"
"github.com/rwinkhart/libmutton/synccycles"
)
@@ -33,12 +34,12 @@ func writeEntryCLI(targetLocation string, decryptedEntry []string, hideSecrets b
// write the entry to the target location
err := core.WriteEntry(targetLocation, []byte(strings.Join(decryptedEntry, "\n")))
if err != nil {
back.PrintError("Failed to write entry: "+err.Error(), back.ErrorWrite, true)
other.PrintError("Failed to write entry: "+err.Error(), back.ErrorWrite, true)
}
// preview the entry
fmt.Println(back.AnsiBold + "\nEntry Preview:" + back.AnsiReset)
EntryReader(decryptedEntry, hideSecrets, true)
} else {
back.PrintError("No data supplied for entry", back.ErrorTargetNotFound, true)
other.PrintError("No data supplied for entry", back.ErrorTargetNotFound, true)
}
}