From a1c9827c04bcf7da2baf59081885d0bf7d1c0ebc Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Tue, 13 Aug 2024 20:28:33 -0400 Subject: [PATCH] Move FSSpace, FSPath, and FSMisc constants to core package to avoid import cycles --- core/1globals.go | 9 +++++++-- core/init.go | 4 ++-- libmuttonserver.go | 10 +++++----- sync/1globals.go | 6 ------ sync/client.go | 10 +++++----- sync/common.go | 2 +- sync/init.go | 2 +- sync/oneOff.go | 8 ++++---- sync/server.go | 18 +++++++++--------- 9 files changed, 34 insertions(+), 35 deletions(-) diff --git a/core/1globals.go b/core/1globals.go index ddc8673..9734e0c 100644 --- a/core/1globals.go +++ b/core/1globals.go @@ -9,8 +9,13 @@ var ( ) const ( - AnsiError = "\033[38;5;9m" - AnsiReset = "\033[0m" + FSSpace = "\u259d" // ▝ space/list separator + FSPath = "\u259e" // ▞ path separator + FSMisc = "\u259f" // ▟ misc. field separator (if \u259d is already used) + + AnsiError = "\033[38;5;9m" + AnsiReset = "\033[0m" + LibmuttonVersion = "0.2.B" // untagged releases feature a letter suffix corresponding to the eventual release version, e.g "0.2.A" -> "0.2.0", "0.2.B" -> "0.2.1" ) diff --git a/core/init.go b/core/init.go index 3f3f662..f3ee4e0 100644 --- a/core/init.go +++ b/core/init.go @@ -53,7 +53,7 @@ func GpgKeyGen() string { } // DirInit creates the libmutton directories. -// Returns: oldDeviceID (from before the directory reset; will be sync.FSMisc if there is no pre-existing ID). +// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID). func DirInit(preserveOldConfigDir bool) string { // create EntryRoot err := os.MkdirAll(EntryRoot, 0700) @@ -68,7 +68,7 @@ func DirInit(preserveOldConfigDir bool) string { if oldDeviceIDList != nil && len(*oldDeviceIDList) > 0 { // ensure not derferencing nil, which occurs when the devices directory does not exist oldDeviceID = (*oldDeviceIDList)[0].Name() } else { - oldDeviceID = "\u259f" // sync.FSMisc cannot be used directly due to import cycle; indicates to server that no device ID is being replaced + oldDeviceID = FSMisc // indicates to server that no device ID is being replaced } // remove existing config directory (if it exists and not in append mode) diff --git a/libmuttonserver.go b/libmuttonserver.go index 0ca829d..a3e6754 100644 --- a/libmuttonserver.go +++ b/libmuttonserver.go @@ -42,28 +42,28 @@ func main() { // stdin[0] is evaluated after fallthrough // stdin[1] is expected to be the OLD incomplete target location with FSPath representing path separators - Always pass in UNIX format // stdin[2] is expected to be the NEW incomplete target location with FSPath representing path separators - Always pass in UNIX format - sync.RenameLocal(strings.ReplaceAll(stdin[1], sync.FSPath, "/"), strings.ReplaceAll(stdin[2], sync.FSPath, "/"), true) + sync.RenameLocal(strings.ReplaceAll(stdin[1], core.FSPath, "/"), strings.ReplaceAll(stdin[2], core.FSPath, "/"), true) fallthrough // fallthrough to add the old entry to the deletions directory case "shear": // shear an entry from the server and add it to the deletions directory // stdin[0] is expected to be the device ID // stdin[1] is expected to be the incomplete target location with FSPath representing path separators - Always pass in UNIX format - sync.ShearLocal(strings.ReplaceAll(stdin[1], sync.FSPath, "/"), stdin[0]) + sync.ShearLocal(strings.ReplaceAll(stdin[1], core.FSPath, "/"), stdin[0]) case "addfolder": // add a new folder to the server // stdin[0] is expected to be the incomplete target location with FSPath representing path separators - Always pass in UNIX format - sync.AddFolderLocal(strings.ReplaceAll(stdin[0], sync.FSPath, "/")) + sync.AddFolderLocal(strings.ReplaceAll(stdin[0], core.FSPath, "/")) case "register": // register a new device ID // stdin[0] is expected to be the device ID // stdin[1] is expected to be the old device ID (for removal) fileToClose, _ := os.OpenFile(core.ConfigDir+core.PathSeparator+"devices"+core.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600) // errors ignored; failure unlikely to occur if init was successful; "register" is not a user-facing argument and thus the error would not be visible _ = fileToClose.Close() - if stdin[1] != sync.FSMisc { // sync.FSMisc is used to indicate that no device ID is being replaced + if stdin[1] != core.FSMisc { // sync.FSMisc is used to indicate that no device ID is being replaced _ = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + stdin[1]) } // print EntryRoot and bool indicating OS type to stdout for client to store in config - fmt.Print(core.EntryRoot + sync.FSSpace + strconv.FormatBool(core.IsWindows)) + fmt.Print(core.EntryRoot + core.FSSpace + strconv.FormatBool(core.IsWindows)) case "init": // create the necessary directories for libmuttonserver to function core.DirInit(false) diff --git a/sync/1globals.go b/sync/1globals.go index 58972e4..e6180b0 100644 --- a/sync/1globals.go +++ b/sync/1globals.go @@ -2,10 +2,4 @@ package sync import "github.com/rwinkhart/libmutton/core" -const ( - FSSpace = "\u259d" // ▝ space/list separator - FSPath = "\u259e" // ▞ path separator - FSMisc = "\u259f" // ▟ misc. field separator (if \u259d is already used) -) - var rootLength = len(core.EntryRoot) // length of core.EntryRoot string diff --git a/sync/client.go b/sync/client.go index 05b16aa..20aeb87 100644 --- a/sync/client.go +++ b/sync/client.go @@ -148,7 +148,7 @@ func getRemoteDataFromClient(sshClient *ssh.Client, manualSync bool) (map[string output := GetSSHOutput(sshClient, "libmuttonserver fetch", (*deviceIDList)[0].Name()) // split output into slice based on occurrences of FSSpace - outputSlice := strings.Split(output, FSSpace) + outputSlice := strings.Split(output, core.FSSpace) // parse output/re-form lists if len(outputSlice) != 5 { // ensure information from server is complete @@ -160,10 +160,10 @@ func getRemoteDataFromClient(sshClient *ssh.Client, manualSync bool) (map[string fmt.Println(core.AnsiError+"Sync failed - Unable to parse server time:", err.Error()+core.AnsiReset) os.Exit(101) } - entries := strings.Split(outputSlice[1], FSMisc)[1:] - modsStrings := strings.Split(outputSlice[2], FSMisc)[1:] - folders := strings.Split(outputSlice[3], FSMisc)[1:] - deletions := strings.Split(outputSlice[4], FSMisc)[1:] + entries := strings.Split(outputSlice[1], core.FSMisc)[1:] + modsStrings := strings.Split(outputSlice[2], core.FSMisc)[1:] + folders := strings.Split(outputSlice[3], core.FSMisc)[1:] + deletions := strings.Split(outputSlice[4], core.FSMisc)[1:] // convert the mod times to int64 var mods []int64 diff --git a/sync/common.go b/sync/common.go index db67091..b74240b 100644 --- a/sync/common.go +++ b/sync/common.go @@ -36,7 +36,7 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { if onServer { for _, device := range *deviceIDList { if device.Name() != clientDeviceID { - fileToClose, err := os.OpenFile(core.ConfigDir+core.PathSeparator+"deletions"+core.PathSeparator+device.Name()+FSSpace+strings.ReplaceAll(targetLocationIncomplete, "/", FSPath), os.O_CREATE|os.O_WRONLY, 0600) + fileToClose, err := os.OpenFile(core.ConfigDir+core.PathSeparator+"deletions"+core.PathSeparator+device.Name()+core.FSSpace+strings.ReplaceAll(targetLocationIncomplete, "/", core.FSPath), os.O_CREATE|os.O_WRONLY, 0600) if err != nil { // do not print error as there is currently no way of seeing server-side errors // failure to add the target to the deletions list will exit the program and result in a client re-uploading the target (non-critical) diff --git a/sync/init.go b/sync/init.go index 2c7ad0f..fcdd3ec 100644 --- a/sync/init.go +++ b/sync/init.go @@ -33,7 +33,7 @@ func DeviceIDGen(oldDeviceID string) (string, string) { // also removes the old device ID file (remotely) // manualSync is true so the user is alerted if device ID registration fails sshClient, _, _ := GetSSHClient(true) - sshEntryRootSSHIsWindows := strings.Split(GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID), FSSpace) + sshEntryRootSSHIsWindows := strings.Split(GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID), core.FSSpace) err = sshClient.Close() if err != nil { fmt.Println(core.AnsiError+"Init failed - Unable to close SSH client:", err.Error()+core.AnsiReset) diff --git a/sync/oneOff.go b/sync/oneOff.go index 12e1b4f..c18d089 100644 --- a/sync/oneOff.go +++ b/sync/oneOff.go @@ -18,7 +18,7 @@ func ShearRemoteFromClient(targetLocationIncomplete string) { sshClient, _, _ := GetSSHClient(false) // call the server to remotely shear the target and add it to the deletions list - GetSSHOutput(sshClient, "libmuttonserver shear", deviceID+"\n"+strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, FSPath)) + GetSSHOutput(sshClient, "libmuttonserver shear", deviceID+"\n"+strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, core.FSPath)) // close the SSH client err := sshClient.Close() @@ -44,8 +44,8 @@ func RenameRemoteFromClient(oldLocationIncomplete, newLocationIncomplete string) // call the server to move the target on the remote system and add the old target to the deletions list GetSSHOutput(sshClient, "libmuttonserver rename", (*deviceIDList)[0].Name()+"\n"+ - strings.ReplaceAll(oldLocationIncomplete, core.PathSeparator, FSPath)+"\n"+ - strings.ReplaceAll(newLocationIncomplete, core.PathSeparator, FSPath)) + strings.ReplaceAll(oldLocationIncomplete, core.PathSeparator, core.FSPath)+"\n"+ + strings.ReplaceAll(newLocationIncomplete, core.PathSeparator, core.FSPath)) // close the SSH client err := sshClient.Close() @@ -69,7 +69,7 @@ func AddFolderRemoteFromClient(targetLocationIncomplete string) { sshClient, _, _ := GetSSHClient(false) // call the server to create the folder remotely - GetSSHOutput(sshClient, "libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, FSPath)) // call the server to create the folder remotely + GetSSHOutput(sshClient, "libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, core.FSPath)) // call the server to create the folder remotely // close the SSH client err := sshClient.Close() diff --git a/sync/server.go b/sync/server.go index 00f8ff9..07e311b 100644 --- a/sync/server.go +++ b/sync/server.go @@ -26,31 +26,31 @@ func GetRemoteDataFromServer(clientDeviceID string) { // print the lists to stdout // entry list - fmt.Print(FSSpace) + fmt.Print(core.FSSpace) for _, entry := range entryList { - fmt.Print(FSMisc + entry) + fmt.Print(core.FSMisc + entry) } // modification time list - fmt.Print(FSSpace) + fmt.Print(core.FSSpace) for _, mod := range modList { - fmt.Print(FSMisc) + fmt.Print(core.FSMisc) fmt.Print(mod) } // directory/folder list - fmt.Print(FSSpace) + fmt.Print(core.FSSpace) for _, dir := range dirList { - fmt.Print(FSMisc + dir) + fmt.Print(core.FSMisc + dir) } // deletions list - fmt.Print(FSSpace) + fmt.Print(core.FSSpace) for _, deletion := range deletionsList { // print deletion if it is relevant to the current client device - affectedIDTargetLocationIncomplete := strings.Split(deletion.Name(), FSSpace) + affectedIDTargetLocationIncomplete := strings.Split(deletion.Name(), core.FSSpace) if affectedIDTargetLocationIncomplete[0] == clientDeviceID { - fmt.Print(FSMisc + strings.ReplaceAll(affectedIDTargetLocationIncomplete[1], FSPath, "/")) + fmt.Print(core.FSMisc + strings.ReplaceAll(affectedIDTargetLocationIncomplete[1], core.FSPath, "/")) // assume successful client deletion and remove deletions file (if assumption is somehow false, worst case scenario is that the client will re-upload the deleted entry) _ = os.Remove(core.ConfigDir + core.PathSeparator + "deletions" + core.PathSeparator + deletion.Name()) // error ignored; function not run from a user-facing argument and thus the error would not be visible