diff --git a/cfg/configParser.go b/cfg/configParser.go index 4890a0e..5df74c3 100644 --- a/cfg/configParser.go +++ b/cfg/configParser.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" - "github.com/rwinkhart/go-boilerplate/back" "github.com/rwinkhart/libmutton/global" "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. -// 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), -// 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 +// Requires: valuesRequested (a slice of length 2 arrays each containing a section and a key name). +// Returns: config (slice of values for the specified keys). +func ParseConfig(valuesRequested [][2]string) ([]string, error) { cfg, err := loadConfig() if err != nil { return nil, err } var config []string - for _, pair := range valuesRequested { value := cfg.Section(pair[0]).Key(pair[1]).String() - // ensure specified key has a value if value == "" { - switch missingValueError { - 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 + return nil, fmt.Errorf("unable to find value for key \"%s\" in section \"[%s]\" in libmutton.ini", pair[1], pair[0]) } - config = append(config, value) } diff --git a/syncclient/client.go b/syncclient/client.go index 7203244..e61ffca 100644 --- a/syncclient/client.go +++ b/syncclient/client.go @@ -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() diff --git a/syncclient/oneOff.go b/syncclient/oneOff.go index f08043c..a28d287 100644 --- a/syncclient/oneOff.go +++ b/syncclient/oneOff.go @@ -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()) } diff --git a/synccycles/init.go b/synccycles/init.go index 1958f0d..32a657c 100644 --- a/synccycles/init.go +++ b/synccycles/init.go @@ -38,7 +38,7 @@ func DeviceIDGen(oldDeviceID string) (string, string, error) { // register new device ID with server and fetch remote EntryRoot and OS type // also removes the old device ID file (remotely) // 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 { return "", "", errors.New("unable to connect to SSH client: " + err.Error()) }