From 8571faee94fdb0b9ac2f21d06136a66c9e2a3879 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 16 May 2024 21:15:16 -0400 Subject: [PATCH] When a folder is created on the client, also create it on the server --- libmuttonserver.go | 3 +++ mutn.go | 2 +- src/backend/add.go | 23 ----------------------- src/sync/common.go | 25 ++++++++++++++++++++++++- 4 files changed, 28 insertions(+), 25 deletions(-) delete mode 100644 src/backend/add.go diff --git a/libmuttonserver.go b/libmuttonserver.go index 11c94aa..16c62b7 100644 --- a/libmuttonserver.go +++ b/libmuttonserver.go @@ -24,6 +24,9 @@ func main() { deviceIDTargetLocation := strings.Split(args[2], "\x1d") targetLocationIncomplete := strings.ReplaceAll(strings.ReplaceAll(deviceIDTargetLocation[1], "\x1f", " "), "\x1e", backend.PathSeparator) sync.Shear(targetLocationIncomplete, deviceIDTargetLocation[0]) + case "addfolder": + // add a new folder to the server + sync.AddFolder(strings.ReplaceAll(args[2], "\x1f", " "), true) case "register": // register a new device ID os.Create(backend.ConfigDir + backend.PathSeparator + "devices" + backend.PathSeparator + args[2]) diff --git a/mutn.go b/mutn.go index 3ded61e..43d9213 100644 --- a/mutn.go +++ b/mutn.go @@ -142,7 +142,7 @@ func main() { case "note", "-n": cli.AddEntry(targetLocation, true, 2) case "folder", "-f": - backend.AddFolder(targetLocation) + sync.AddFolder(args[1], false) default: cli.HelpAdd() } diff --git a/src/backend/add.go b/src/backend/add.go deleted file mode 100644 index 258c9fb..0000000 --- a/src/backend/add.go +++ /dev/null @@ -1,23 +0,0 @@ -package backend - -import ( - "fmt" - "os" -) - -// AddFolder creates a new directory at targetLocation -func AddFolder(targetLocation string) { - // create the directory specified by targetLocation - err := os.Mkdir(targetLocation, 0700) - if err != nil { - if os.IsExist(err) { - fmt.Println(AnsiError + "Directory already exists" + AnsiReset) - os.Exit(1) - } else { - fmt.Println(AnsiError + "Failed to create directory: " + err.Error() + AnsiReset) - os.Exit(1) - } - } - // TODO If in online mode, create the directory on the server - os.Exit(0) -} diff --git a/src/sync/common.go b/src/sync/common.go index 50c3414..a27221c 100644 --- a/src/sync/common.go +++ b/src/sync/common.go @@ -90,7 +90,7 @@ func Shear(targetLocationIncomplete string, deviceID string) { } } } - } else { // if running on the client... (online mode determined dynamically in GetSSHOutput, will simply exit if not in online mode) + } else { // if running on the client... (online mode determined dynamically in GetSSHOutput, will silently exit if not in online mode) // determine client device ID (to send to server, avoids creating a deletion file for the client device) deviceID = deviceIDList[0].Name() // below: deviceID and targetLocationIncomplete are separated by \x1d, path separators are replaced with \x1e, and spaces are replaced with \x1f @@ -99,3 +99,26 @@ func Shear(targetLocationIncomplete string, deviceID string) { os.Exit(0) } + +// AddFolder creates a new directory at targetLocation +// if running on the client, it will also call the server to create the directory +func AddFolder(targetLocationIncomplete string, onServer bool) { + // get the full targetLocation path and create the target + targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:]) + err := os.Mkdir(targetLocationComplete, 0700) + if err != nil { + if os.IsExist(err) { + fmt.Println(backend.AnsiError + "Directory already exists" + backend.AnsiReset) + os.Exit(1) + } else { + fmt.Println(backend.AnsiError + "Failed to create directory: " + err.Error() + backend.AnsiReset) + os.Exit(1) + } + } + + if !onServer { // if running on the client... (online mode determined dynamically in GetSSHOutput, will silently exit if not in online mode) + GetSSHOutput("libmuttonserver addfolder "+strings.ReplaceAll(targetLocationIncomplete, " ", "\x1f"), false) + } + + os.Exit(0) +}