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:
+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).
|
||||
// Only supports key-based authentication (passphrases are supported for CLI-based implementations).
|
||||
func GetSSHClient(manualSync bool) (*ssh.Client, string, bool, error) {
|
||||
// get SSH config info, exit if not configured (displaying an error if the sync job was called manually)
|
||||
var sshUserConfig []string
|
||||
var missingValueError string
|
||||
if manualSync {
|
||||
missingValueError = "SSH settings not fully configured"
|
||||
} else {
|
||||
missingValueError = "0" // allow silent exit at this point in offline mode
|
||||
func GetSSHClient() (*ssh.Client, string, bool, error) {
|
||||
// get SSH config info, exit if not configured
|
||||
sshUserConfig, err := cfg.ParseConfig([][2]string{{"LIBMUTTON", "sshUser"}, {"LIBMUTTON", "sshIP"}, {"LIBMUTTON", "sshPort"}, {"LIBMUTTON", "sshKey"}, {"LIBMUTTON", "sshKeyProtected"}, {"LIBMUTTON", "sshEntryRoot"}, {"LIBMUTTON", "sshIsWindows"}})
|
||||
if err != nil {
|
||||
return nil, "", false, errors.New("unable to parse SSH config: " + err.Error())
|
||||
}
|
||||
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 isWindows bool
|
||||
var err error
|
||||
for i, key := range sshUserConfig {
|
||||
switch i {
|
||||
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.
|
||||
func RunJob(manualSync, returnLists bool) ([3][]string, error) {
|
||||
// get SSH client to re-use throughout the sync process
|
||||
sshClient, sshEntryRoot, sshIsWindows, err := GetSSHClient(manualSync)
|
||||
sshClient, sshEntryRoot, sshIsWindows, err := GetSSHClient()
|
||||
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) {
|
||||
_ = sshClient.Close()
|
||||
|
||||
@@ -19,7 +19,7 @@ func ShearRemoteFromClient(targetLocationIncomplete string, forceOffline bool) e
|
||||
|
||||
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
|
||||
sshClient, _, _, err := GetSSHClient(false)
|
||||
sshClient, _, _, err := GetSSHClient()
|
||||
if err != nil {
|
||||
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)
|
||||
// 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 {
|
||||
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)
|
||||
// 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 {
|
||||
return errors.New("unable to connect to SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user