Allow parsing different section headers in libmutton.ini

This commit is contained in:
2024-07-17 14:44:18 -04:00
parent 0fc0a2cfc0
commit 2ae4fe19e8
5 changed files with 23 additions and 18 deletions
+10 -9
View File
@@ -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
+1 -1
View File
@@ -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 {
+4 -3
View File
@@ -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 {
+6 -5
View File
@@ -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
+2
View File
@@ -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.