mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 04:46:42 -04:00
Slim down ConfigParser error system
This commit is contained in:
+4
-19
@@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/rwinkhart/go-boilerplate/back"
|
|
||||||
"github.com/rwinkhart/libmutton/global"
|
"github.com/rwinkhart/libmutton/global"
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
@@ -20,35 +19,21 @@ func loadConfig() (*ini.File, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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: valuesRequested (a slice of length 2 arrays each containing a section and a key name),
|
// Requires: valuesRequested (a slice of length 2 arrays each containing a section and a key name).
|
||||||
// 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 (slice of values for the specified keys).
|
||||||
// Returns: config (slice of values for the specified keys),
|
func ParseConfig(valuesRequested [][2]string) ([]string, error) {
|
||||||
// error (nil if no error occurred, otherwise an error using the generated or provided message).
|
|
||||||
func ParseConfig(valuesRequested [][2]string, missingValueError string) ([]string, error) {
|
|
||||||
var err error
|
|
||||||
cfg, err := loadConfig()
|
cfg, err := loadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var config []string
|
var config []string
|
||||||
|
|
||||||
for _, pair := range valuesRequested {
|
for _, pair := range valuesRequested {
|
||||||
value := cfg.Section(pair[0]).Key(pair[1]).String()
|
value := cfg.Section(pair[0]).Key(pair[1]).String()
|
||||||
|
|
||||||
// ensure specified key has a value
|
// ensure specified key has a value
|
||||||
if value == "" {
|
if value == "" {
|
||||||
switch missingValueError {
|
return nil, fmt.Errorf("unable to find value for key \"%s\" in section \"[%s]\" in libmutton.ini", pair[1], pair[0])
|
||||||
case "":
|
|
||||||
err = fmt.Errorf("unable to find value for key \"%s\" in section \"[%s]\" in libmutton.ini", pair[1], pair[0])
|
|
||||||
case "0":
|
|
||||||
back.Exit(0) // hard (expected) exit for CLI; GUI/TUI continue silently
|
|
||||||
default:
|
|
||||||
err = fmt.Errorf("%s", missingValueError)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config = append(config, value)
|
config = append(config, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-12
@@ -19,20 +19,15 @@ import (
|
|||||||
|
|
||||||
// GetSSHClient returns an SSH client connection to the server (also returns the remote EntryRoot and an indicator of the server's OS).
|
// GetSSHClient returns an SSH client connection to the server (also returns the remote EntryRoot and an indicator of the server's OS).
|
||||||
// Only supports key-based authentication (passphrases are supported for CLI-based implementations).
|
// Only supports key-based authentication (passphrases are supported for CLI-based implementations).
|
||||||
func GetSSHClient(manualSync bool) (*ssh.Client, string, bool, error) {
|
func GetSSHClient() (*ssh.Client, string, bool, error) {
|
||||||
// get SSH config info, exit if not configured (displaying an error if the sync job was called manually)
|
// get SSH config info, exit if not configured
|
||||||
var sshUserConfig []string
|
sshUserConfig, err := cfg.ParseConfig([][2]string{{"LIBMUTTON", "sshUser"}, {"LIBMUTTON", "sshIP"}, {"LIBMUTTON", "sshPort"}, {"LIBMUTTON", "sshKey"}, {"LIBMUTTON", "sshKeyProtected"}, {"LIBMUTTON", "sshEntryRoot"}, {"LIBMUTTON", "sshIsWindows"}})
|
||||||
var missingValueError string
|
if err != nil {
|
||||||
if manualSync {
|
return nil, "", false, errors.New("unable to parse SSH config: " + err.Error())
|
||||||
missingValueError = "SSH settings not fully configured"
|
|
||||||
} else {
|
|
||||||
missingValueError = "0" // allow silent exit at this point in offline mode
|
|
||||||
}
|
}
|
||||||
sshUserConfig, _ = cfg.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
|
||||||
var err error
|
|
||||||
for i, key := range sshUserConfig {
|
for i, key := range sshUserConfig {
|
||||||
switch i {
|
switch i {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -436,9 +431,13 @@ func folderSync(folders []string) error {
|
|||||||
// Setting returnLists to true will return the deletions, downloads, and uploads lists for use by the client.
|
// Setting returnLists to true will return the deletions, downloads, and uploads lists for use by the client.
|
||||||
func RunJob(manualSync, returnLists bool) ([3][]string, error) {
|
func RunJob(manualSync, returnLists bool) ([3][]string, error) {
|
||||||
// get SSH client to re-use throughout the sync process
|
// get SSH client to re-use throughout the sync process
|
||||||
sshClient, sshEntryRoot, sshIsWindows, err := GetSSHClient(manualSync)
|
sshClient, sshEntryRoot, sshIsWindows, err := GetSSHClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return [3][]string{nil, nil, nil}, errors.New("unable to connect to SSH client: " + err.Error())
|
if manualSync {
|
||||||
|
return [3][]string{nil, nil, nil}, errors.New("unable to connect to SSH client: " + err.Error())
|
||||||
|
} else {
|
||||||
|
return [3][]string{nil, nil, nil}, nil // return silently if the sync job was called automatically, as the user may just be in offline mode
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer func(sshClient *ssh.Client) {
|
defer func(sshClient *ssh.Client) {
|
||||||
_ = sshClient.Close()
|
_ = sshClient.Close()
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func ShearRemoteFromClient(targetLocationIncomplete string, forceOffline bool) e
|
|||||||
|
|
||||||
if !forceOffline && deviceID != "" { // ensure a device ID exists (online mode)
|
if !forceOffline && deviceID != "" { // ensure a device ID exists (online mode)
|
||||||
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
||||||
sshClient, _, _, err := GetSSHClient(false)
|
sshClient, _, _, err := GetSSHClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to connect to SSH client: " + err.Error())
|
return errors.New("unable to connect to SSH client: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,7 @@ func RenameRemoteFromClient(oldLocationIncomplete, newLocationIncomplete string,
|
|||||||
}
|
}
|
||||||
if !forceOffline && len(deviceIDList) > 0 { // ensure a device ID exists (online mode)
|
if !forceOffline && len(deviceIDList) > 0 { // ensure a device ID exists (online mode)
|
||||||
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
||||||
sshClient, _, _, err := GetSSHClient(false)
|
sshClient, _, _, err := GetSSHClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to connect to SSH client: " + err.Error())
|
return errors.New("unable to connect to SSH client: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ func AddFolderRemoteFromClient(targetLocationIncomplete string, forceOffline boo
|
|||||||
}
|
}
|
||||||
if !forceOffline && len(deviceIDList) > 0 { // ensure a device ID exists (online mode)
|
if !forceOffline && len(deviceIDList) > 0 { // ensure a device ID exists (online mode)
|
||||||
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
// create an SSH client; manualSync is false in case a device ID exists but SSH is not configured
|
||||||
sshClient, _, _, err := GetSSHClient(false)
|
sshClient, _, _, err := GetSSHClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to connect to SSH client: " + err.Error())
|
return errors.New("unable to connect to SSH client: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ func DeviceIDGen(oldDeviceID string) (string, string, error) {
|
|||||||
// register new device ID with server and fetch remote EntryRoot and OS type
|
// register new device ID with server and fetch remote EntryRoot and OS type
|
||||||
// also removes the old device ID file (remotely)
|
// also removes the old device ID file (remotely)
|
||||||
// manualSync is true so the user is alerted if device ID registration fails
|
// manualSync is true so the user is alerted if device ID registration fails
|
||||||
sshClient, _, _, err := syncclient.GetSSHClient(true)
|
sshClient, _, _, err := syncclient.GetSSHClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", errors.New("unable to connect to SSH client: " + err.Error())
|
return "", "", errors.New("unable to connect to SSH client: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user