mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-31 14:26:36 -04:00
Allow parsing different section headers in libmutton.ini
This commit is contained in:
@@ -2,8 +2,9 @@ package backend
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gopkg.in/ini.v1"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// loadConfig loads the libmutton.ini file and returns the configuration
|
// 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
|
// 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 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 silently with status 0
|
// 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
|
// 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()
|
cfg := loadConfig()
|
||||||
|
|
||||||
var config []string
|
var config []string
|
||||||
|
|
||||||
for _, key := range readKeys {
|
for _, pair := range requestedValues {
|
||||||
keyConfig := cfg.Section("LIBMUTTON").Key(key).String()
|
value := cfg.Section(pair[0]).Key(pair[1]).String()
|
||||||
|
|
||||||
// ensure specified key has a value
|
// ensure specified key has a value
|
||||||
if keyConfig == "" {
|
if value == "" {
|
||||||
switch missingValueError {
|
switch missingValueError {
|
||||||
case "":
|
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":
|
case "0":
|
||||||
Exit(0)
|
Exit(0)
|
||||||
default:
|
default:
|
||||||
@@ -42,7 +43,7 @@ func ParseConfig(readKeys []string, missingValueError string) []string {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
config = append(config, keyConfig)
|
config = append(config, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|||||||
+1
-1
@@ -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
|
// EncryptGPG encrypts a slice of strings using GPG and returns the encrypted data as a byte slice
|
||||||
func EncryptGPG(input []string) []byte {
|
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"))
|
writeToStdin(cmd, strings.Join(input, "\n"))
|
||||||
encryptedBytes, err := cmd.Output()
|
encryptedBytes, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+6
-5
@@ -2,14 +2,15 @@ package sync
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/sftp"
|
|
||||||
"github.com/rwinkhart/MUTN/src/backend"
|
|
||||||
"golang.org/x/crypto/ssh"
|
|
||||||
"golang.org/x/crypto/ssh/knownhosts"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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
|
// global constants used only in this file
|
||||||
@@ -30,7 +31,7 @@ func getSSHClient(manualSync bool) (*ssh.Client, string, bool) {
|
|||||||
} else {
|
} else {
|
||||||
missingValueError = "0"
|
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 user, ip, port, keyFile, keyFileProtected, entryRoot string
|
||||||
var isWindows bool
|
var isWindows bool
|
||||||
|
|||||||
@@ -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.
|
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
|
## Build Tags
|
||||||
Custom build tags can (and sometimes must) be used to achieve desired results.
|
Custom build tags can (and sometimes must) be used to achieve desired results.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user