mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 04:46:42 -04:00
Return errors from ParseConfig (facilitates GUI development)
This commit is contained in:
+15
-8
@@ -22,8 +22,10 @@ func loadConfig() *ini.File {
|
||||
// ParseConfig reads the libmutton.ini file and returns a slice of values for the specified keys.
|
||||
// Requires: valuesRequested (a slice of length 2 arrays each containing a section and a key name),
|
||||
// missingValueError (an error message to display if a key is missing a value, set to "" for auto-generated or "0" to exit/return silently with code 0).
|
||||
// Returns: config (slice of values for the specified keys).
|
||||
func ParseConfig(valuesRequested [][2]string, missingValueError string) []string {
|
||||
// Returns: config (slice of values for the specified keys),
|
||||
// error (nil if no error occurred, otherwise an error using the generated or provided message).
|
||||
func ParseConfig(valuesRequested [][2]string, missingValueError string) ([]string, error) {
|
||||
var err error
|
||||
cfg := loadConfig()
|
||||
|
||||
var config []string
|
||||
@@ -35,19 +37,23 @@ func ParseConfig(valuesRequested [][2]string, missingValueError string) []string
|
||||
if value == "" {
|
||||
switch missingValueError {
|
||||
case "":
|
||||
fmt.Println(AnsiError + "Failed to find value for key \"" + pair[1] + "\" in section \"[" + pair[0] + "]\" in libmutton.ini" + AnsiReset)
|
||||
err = fmt.Errorf("Failed to find value for key \"%s\" in section \"[%s]\" in libmutton.ini", pair[1], pair[0])
|
||||
fmt.Println(err.Error())
|
||||
case "0":
|
||||
Exit(0)
|
||||
Exit(0) // hard (expected) exit for CLI; GUI/TUI continue silently
|
||||
default:
|
||||
fmt.Println(AnsiError + missingValueError + AnsiReset)
|
||||
err = fmt.Errorf("%s", missingValueError)
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
os.Exit(ErrorRead)
|
||||
Exit(ErrorRead) // hard exit for CLI; GUI/TUI continue silently
|
||||
// if interactive (soft exit), return nil and the error to be handled by the caller
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config = append(config, value)
|
||||
}
|
||||
|
||||
return config
|
||||
return config, err
|
||||
}
|
||||
|
||||
// GenDeviceIDList returns a pointer to a slice of all registered device IDs.
|
||||
@@ -67,7 +73,8 @@ func GenDeviceIDList(errorOnFail bool) *[]fs.DirEntry {
|
||||
}
|
||||
|
||||
// WriteConfig writes the provided key-value pairs under the specified section headers in the libmutton.ini file.
|
||||
// Requires: valuesToWrite (a slice of length 3 arrays each containing a section, a key name, and a value).
|
||||
// Requires: valuesToWrite (a slice of length 3 arrays each containing a section, a key name, and a value),
|
||||
// append (set to true to append to the existing libmutton.ini file, false to overwrite it).
|
||||
func WriteConfig(valuesToWrite [][3]string, append bool) {
|
||||
var cfg *ini.File
|
||||
|
||||
|
||||
+2
-1
@@ -27,7 +27,8 @@ func DecryptGPG(targetLocation string) []string {
|
||||
|
||||
// EncryptGPG encrypts a slice of strings using GPG and returns the encrypted data as a byte slice.
|
||||
func EncryptGPG(input []string) []byte {
|
||||
cmd := exec.Command("gpg", "-q", "-r", ParseConfig([][2]string{{"LIBMUTTON", "gpgID"}}, "")[0], "-e")
|
||||
gpgCfg, _ := ParseConfig([][2]string{{"LIBMUTTON", "gpgID"}}, "")
|
||||
cmd := exec.Command("gpg", "-q", "-r", gpgCfg[0], "-e")
|
||||
writeToStdin(cmd, strings.Join(input, "\n"))
|
||||
encryptedBytes, err := cmd.Output()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user