Allow parsing different section headers in libmutton.ini

This commit is contained in:
2024-07-17 14:44:18 -04:00
parent 93f8e1d867
commit 7ab0c91c0e
4 changed files with 19 additions and 15 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 {
+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.