From e81bfa935a1f461edf2171f99f37ac2794a928e8 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Wed, 17 Jul 2024 15:49:45 -0400 Subject: [PATCH] Allow writing to different section headers in libmutton.ini --- src/backend/configParser.go | 53 ++++++++++++++----------------------- src/cli/init.go | 6 ++--- src/cli/initUNIX.go | 1 - 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/backend/configParser.go b/src/backend/configParser.go index a4ae6a0..dc27f8a 100644 --- a/src/backend/configParser.go +++ b/src/backend/configParser.go @@ -22,12 +22,12 @@ func loadConfig() *ini.File { // requires requestedValues: a slice of arrays (length 2) each containing a section and a key name // requires 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: a slice of values for the specified keys -func ParseConfig(requestedValues [][2]string, missingValueError string) []string { +func ParseConfig(valuesRequested [][2]string, missingValueError string) []string { cfg := loadConfig() var config []string - for _, pair := range requestedValues { + for _, pair := range valuesRequested { value := cfg.Section(pair[0]).Key(pair[1]).String() // ensure specified key has a value @@ -49,51 +49,38 @@ func ParseConfig(requestedValues [][2]string, missingValueError string) []string return config } -// WriteConfig writes the provided key-value pairs to the libmutton.ini file -func WriteConfig(configFileMap map[string]string, append bool) { +// WriteConfig writes the provided key-value pairs under the specified section headers in the libmutton.ini file +// requires valuesToWrite: a slice of arrays (length 3) each containing a section, a key name, and a value +func WriteConfig(valuesToWrite [][3]string, append bool) { var cfg *ini.File - var libmuttonSection *ini.Section if append { // load existing ini file cfg = loadConfig() - - // acquire LIBMUTTON section - libmuttonSection, _ = cfg.GetSection("LIBMUTTON") } else { - // create empty ini file + // create empty ini container cfg = ini.Empty() - - // create LIBMUTTON section - libmuttonSection, _ = cfg.NewSection("LIBMUTTON") } - // write provided configFileMap key-value pairs to the LIBMUTTON section - for key, value := range configFileMap { - libmuttonSection.Key(key).SetValue(value) + // set all specified key-value pairs in their respective sections + var section *ini.Section + for _, trio := range valuesToWrite { + if cfg.Section(trio[0]) == nil { + // create and aquire section if it doesn't exist + section, _ = cfg.NewSection(trio[0]) + } else { + // acquire existing section + section = cfg.Section(trio[0]) + } + + // set key-value pair + section.Key(trio[1]).SetValue(trio[2]) } - // save the new config file + // save to libmutton.ini err := cfg.SaveTo(ConfigPath) if err != nil { fmt.Println(AnsiError + "Failed to save libmutton.ini: " + err.Error() + AnsiReset) os.Exit(1) } } - -// libmutton.ini layout -// [LIBMUTTON] -// gpgID = -// textEditor = TODO move to MUTN section heading, as it only applies to the CLI implementation -// sshUser = -// sshIP = -// sshPort = -// sshKey = -// sshKeyProtected = -// netPinEnabled = TODO netPin functionality not yet implemented -// sshEntryRoot = -// sshIsWindows = - -// Developers of alternative clients: -// If you are adding additional settings to the config file, -// please create a new section heading for your app-specific settings. diff --git a/src/cli/init.go b/src/cli/init.go index 88e4ef2..36f01ae 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -47,18 +47,18 @@ func TempInitCli() { backend.DirInit(false) // write config file (temporarily assigns sshEntryRoot and sshIsWindows to null to pass initial device ID registration) - backend.WriteConfig(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshKey": sshKey, "sshKeyProtected": strconv.FormatBool(sshKeyProtected), "sshEntryRoot": "null", "sshIsWindows": "null"}, false) + backend.WriteConfig([][3]string{{"LIBMUTTON", "textEditor", textEditor}, {"LIBMUTTON", "gpgID", gpgID}, {"LIBMUTTON", "sshUser", sshUser}, {"LIBMUTTON", "sshIP", sshIP}, {"LIBMUTTON", "sshPort", sshPort}, {"LIBMUTTON", "sshKey", sshKey}, {"LIBMUTTON", "sshKeyProtected", strconv.FormatBool(sshKeyProtected)}, {"LIBMUTTON", "sshEntryRoot", "null"}, {"LIBMUTTON", "sshIsWindows", "null"}}, false) // generate and register device ID sshEntryRoot, sshIsWindows := sync.DeviceIDGen() // update config file with sshEntryRoot and sshIsWindows - backend.WriteConfig(map[string]string{"sshEntryRoot": sshEntryRoot, "sshIsWindows": sshIsWindows}, true) + backend.WriteConfig([][3]string{{"LIBMUTTON", "sshEntryRoot", sshEntryRoot}, {"LIBMUTTON", "sshIsWindows", sshIsWindows}}, true) } else { // initialize libmutton directories backend.DirInit(false) // write config file - backend.WriteConfig(map[string]string{"textEditor": textEditor, "gpgID": gpgID}, false) + backend.WriteConfig([][3]string{{"LIBMUTTON", "textEditor", textEditor}, {"LIBMUTTON", "gpgID", gpgID}}, false) } } diff --git a/src/cli/initUNIX.go b/src/cli/initUNIX.go index 1c63fb5..e79bdad 100644 --- a/src/cli/initUNIX.go +++ b/src/cli/initUNIX.go @@ -10,7 +10,6 @@ const fallbackEditor = "vi" // vi is pre-installed on most UNIX systems // textEditorFallback returns the value of the $EDITOR environment variable, or FallbackEditor if it is not set func textEditorFallback() string { - // ensure textEditor is set textEditor := os.Getenv("EDITOR") if textEditor == "" { textEditor = fallbackEditor