Fix init deleting device ID immediately after generating it; fix sync failure for non-manual syncs

This commit is contained in:
2024-05-30 23:25:08 -04:00
parent b03d751618
commit b4f0a4e896
3 changed files with 21 additions and 19 deletions
+13 -13
View File
@@ -10,12 +10,10 @@ import (
) )
// TempInit ensures libmutton directories exist and writes the libmutton configuration file // TempInit ensures libmutton directories exist and writes the libmutton configuration file
func TempInit(configFileMap map[string]string) { // TODO if run in append mode, extend old config file with new values, rather than creating from scratch
func TempInit(configFileMap map[string]string, append bool) {
// create EntryRoot and ConfigDir // create EntryRoot and ConfigDir
DirInit() DirInit(append)
// remove existing config file
removeFile(ConfigPath)
if configFileMap["textEditor"] == "" { if configFileMap["textEditor"] == "" {
configFileMap["textEditor"] = textEditorFallback() configFileMap["textEditor"] = textEditorFallback()
@@ -68,7 +66,7 @@ func GpgKeyGen() string {
} }
// DirInit creates the libmutton directories // DirInit creates the libmutton directories
func DirInit() { func DirInit(preserveOldConfigDir bool) {
// create EntryRoot // create EntryRoot
err := os.MkdirAll(EntryRoot, 0700) err := os.MkdirAll(EntryRoot, 0700)
if err != nil { if err != nil {
@@ -76,14 +74,16 @@ func DirInit() {
os.Exit(1) os.Exit(1)
} }
// remove existing config directory (if it exists) // remove existing config directory (if it exists and not in append mode)
_, isAccessible := TargetIsFile(ConfigDir, false, 1) if !preserveOldConfigDir {
if isAccessible { _, isAccessible := TargetIsFile(ConfigDir, false, 1)
err = os.RemoveAll(ConfigDir) if isAccessible {
if err != nil { err = os.RemoveAll(ConfigDir)
fmt.Println(AnsiError + "Failed to remove existing config directory: " + err.Error() + AnsiReset) if err != nil {
os.Exit(1) fmt.Println(AnsiError + "Failed to remove existing config directory: " + err.Error() + AnsiReset)
os.Exit(1)
}
} }
} }
+3 -3
View File
@@ -40,16 +40,16 @@ func TempInitCli() {
sshKeyProtected := inputBinary("Is the identity file password-protected?") sshKeyProtected := inputBinary("Is the identity file password-protected?")
// write config file // write config file
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshKey": sshKey, "sshKeyProtected": strconv.FormatBool(sshKeyProtected)}) backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshKey": sshKey, "sshKeyProtected": strconv.FormatBool(sshKeyProtected)}, false)
// generate device ID // generate device ID
sshEntryRoot, sshIsWindows := sync.DeviceIDGen() sshEntryRoot, sshIsWindows := sync.DeviceIDGen()
// update config file with sshEntryRoot and sshIsWindows TODO append to existing config file // update config file with sshEntryRoot and sshIsWindows TODO append to existing config file
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshKey": sshKey, "sshKeyProtected": strconv.FormatBool(sshKeyProtected), "sshEntryRoot": sshEntryRoot, "sshIsWindows": sshIsWindows}) backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID, "sshUser": sshUser, "sshIP": sshIP, "sshPort": sshPort, "sshKey": sshKey, "sshKeyProtected": strconv.FormatBool(sshKeyProtected), "sshEntryRoot": sshEntryRoot, "sshIsWindows": sshIsWindows}, true)
} else { } else {
// write config file // write config file
backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID}) backend.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID}, false)
} }
} }
+5 -3
View File
@@ -24,11 +24,13 @@ const (
func getSSHClient(manualSync bool) (*ssh.Client, string, bool) { func getSSHClient(manualSync bool) (*ssh.Client, string, bool) {
// 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 (displaying an error if the sync job was called manually)
var sshUserConfig []string var sshUserConfig []string
var missingValueError string
if manualSync { if manualSync {
sshUserConfig = backend.ReadConfig([]string{"sshUser", "sshIP", "sshPort", "sshKey", "sshKeyProtected", "sshEntryRoot", "sshIsWindows"}, "SSH settings not configured - run \"mutn init\" to configure") missingValueError = "SSH settings not configured - run \"mutn init\" to configure"
} else { } else {
sshUserConfig = backend.ReadConfig([]string{"sshUser", "sshIP", "sshPort", "sshKey", "sshKeyProtected"}, "0") missingValueError = "0"
} }
sshUserConfig = backend.ReadConfig([]string{"sshUser", "sshIP", "sshPort", "sshKey", "sshKeyProtected", "sshEntryRoot", "sshIsWindows"}, missingValueError)
var user, ip, port, keyFile, keyFileProtected, entryRoot string var user, ip, port, keyFile, keyFileProtected, entryRoot string
var isWindows bool var isWindows bool
@@ -299,7 +301,7 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) {
var remoteFile *sftp.File var remoteFile *sftp.File
remoteFile, err = sftpClient.Create(remoteEntryFullPath) remoteFile, err = sftpClient.Create(remoteEntryFullPath)
if err != nil { if err != nil {
fmt.Println(backend.AnsiError+"Sync failed - Unable to create remote file:", err.Error()+backend.AnsiReset) fmt.Println(backend.AnsiError+"Sync failed - Unable to create remote file ("+remoteEntryFullPath+"):", err.Error()+backend.AnsiReset)
os.Exit(1) os.Exit(1)
} }