Move FSSpace, FSPath, and FSMisc constants to core package to avoid import cycles

This commit is contained in:
2024-08-13 20:28:33 -04:00
parent be46b0d6b2
commit a1c9827c04
9 changed files with 34 additions and 35 deletions
+7 -2
View File
@@ -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"
)
+2 -2
View File
@@ -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)
+5 -5
View File
@@ -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)
-6
View File
@@ -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
+5 -5
View File
@@ -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
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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)
+4 -4
View File
@@ -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()
+9 -9
View File
@@ -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