diff --git a/src/backend/configParser.go b/src/backend/configParser.go index 2cc03f7..9492ccd 100644 --- a/src/backend/configParser.go +++ b/src/backend/configParser.go @@ -2,8 +2,9 @@ package backend import ( "fmt" - "gopkg.in/ini.v1" "os" + + "gopkg.in/ini.v1" ) // loadConfig loads the libmutton.ini file and returns the configuration @@ -18,22 +19,22 @@ func loadConfig() *ini.File { } // ParseConfig reads the libmutton.ini file and returns a slice of values for the specified keys -// requires readKeys: a slice of key names (indicates requested values) -// requires missingValueError: an error message to display if a key is missing a value, set to "" for auto-generated or "0" to exit silently with status 0 +// 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(readKeys []string, missingValueError string) []string { +func ParseConfig(requestedValues [][2]string, missingValueError string) []string { cfg := loadConfig() var config []string - for _, key := range readKeys { - keyConfig := cfg.Section("LIBMUTTON").Key(key).String() + for _, pair := range requestedValues { + value := cfg.Section(pair[0]).Key(pair[1]).String() // ensure specified key has a value - if keyConfig == "" { + if value == "" { switch missingValueError { case "": - fmt.Println(AnsiError + "Failed to find value for key \"" + key + "\" in section \"[LIBMUTTON]\" in libmutton.ini" + AnsiReset) + fmt.Println(AnsiError + "Failed to find value for key \"" + pair[1] + "\" in section \"[" + pair[0] + "]\" in libmutton.ini" + AnsiReset) case "0": Exit(0) default: @@ -42,7 +43,7 @@ func ParseConfig(readKeys []string, missingValueError string) []string { os.Exit(1) } - config = append(config, keyConfig) + config = append(config, value) } return config diff --git a/src/backend/gpg.go b/src/backend/gpg.go index ef11d79..2d22aeb 100644 --- a/src/backend/gpg.go +++ b/src/backend/gpg.go @@ -23,7 +23,7 @@ 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([]string{"gpgID"}, "")[0], "-e") + cmd := exec.Command("gpg", "-q", "-r", ParseConfig([][2]string{{"LIBMUTTON", "gpgID"}}, "")[0], "-e") writeToStdin(cmd, strings.Join(input, "\n")) encryptedBytes, err := cmd.Output() if err != nil { diff --git a/src/cli/edit.go b/src/cli/edit.go index f149b6e..9fa3730 100644 --- a/src/cli/edit.go +++ b/src/cli/edit.go @@ -3,12 +3,13 @@ package cli import ( "bufio" "fmt" - "github.com/rwinkhart/MUTN/src/backend" - "github.com/rwinkhart/MUTN/src/sync" "os" "os/exec" "reflect" "strings" + + "github.com/rwinkhart/MUTN/src/backend" + "github.com/rwinkhart/MUTN/src/sync" ) // RenameCli renames an entry at oldLocationIncomplete to a new location (user input) on both the client and the server @@ -74,7 +75,7 @@ func editNote(baseNote []string) ([]string, bool) { }(tempFile.Name()) // fetch the user's text editor - editor := backend.ParseConfig([]string{"textEditor"}, "")[0] + editor := backend.ParseConfig([][2]string{{"LIBMUTTON", "textEditor"}}, "")[0] // write baseNote to tempFile (if it is not empty) if len(baseNote) > 0 { diff --git a/src/sync/client.go b/src/sync/client.go index 836ce7b..9e37c53 100644 --- a/src/sync/client.go +++ b/src/sync/client.go @@ -2,14 +2,15 @@ package sync import ( "fmt" - "github.com/pkg/sftp" - "github.com/rwinkhart/MUTN/src/backend" - "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/knownhosts" "os" "strconv" "strings" "time" + + "github.com/pkg/sftp" + "github.com/rwinkhart/MUTN/src/backend" + "golang.org/x/crypto/ssh" + "golang.org/x/crypto/ssh/knownhosts" ) // global constants used only in this file @@ -30,7 +31,7 @@ func getSSHClient(manualSync bool) (*ssh.Client, string, bool) { } else { missingValueError = "0" } - sshUserConfig = backend.ParseConfig([]string{"sshUser", "sshIP", "sshPort", "sshKey", "sshKeyProtected", "sshEntryRoot", "sshIsWindows"}, missingValueError) + sshUserConfig = backend.ParseConfig([][2]string{{"LIBMUTTON", "sshUser"}, {"LIBMUTTON", "sshIP"}, {"LIBMUTTON", "sshPort"}, {"LIBMUTTON", "sshKey"}, {"LIBMUTTON", "sshKeyProtected"}, {"LIBMUTTON", "sshEntryRoot"}, {"LIBMUTTON", "sshIsWindows"}}, missingValueError) var user, ip, port, keyFile, keyFileProtected, entryRoot string var isWindows bool diff --git a/wiki/libmutton/developers.md b/wiki/libmutton/developers.md index 40336e9..ebb0135 100644 --- a/wiki/libmutton/developers.md +++ b/wiki/libmutton/developers.md @@ -5,6 +5,8 @@ libmutton was designed to be usable as a library for building other compatible p All functionality in the `backend` and `sync` packages are designed to be used in other implementations. +If any functionality in these two packages proves to be difficult to implement in a third-party client, please open an issue so that it can be addressed. + ## Build Tags Custom build tags can (and sometimes must) be used to achieve desired results.