Implement initial server-side code

This commit is contained in:
2024-05-15 16:25:42 -04:00
parent 5e83178046
commit 3a03fae9ee
6 changed files with 67 additions and 13 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
#!/bin/sh
gofmt -l -w -s ./src/cli/*.go
gofmt -l -w -s ./src/backend/*.go
git add -f extra src .gitignore commit.sh go.mod go.sum LICENSE main.go README.md
git add -f extra src .gitignore commit.sh go.mod go.sum LICENSE main.go mutn.go libmuttonserver.go README.md
git commit -m "$1"
git push
+9
View File
@@ -0,0 +1,9 @@
package main
import (
"github.com/rwinkhart/MUTN/src/sync"
)
func main() {
sync.GetRemoteDataFromServer()
}
View File
+6 -10
View File
@@ -84,15 +84,15 @@ func getSSHOutput(cmd string, manualSync bool) string {
return outputString
}
// getRemoteData returns lists of remote entries, mod times, folders, and deletions (four separate lists)
func getRemoteData(manualSync bool) ([]string, []int64, []string, []string) {
// getRemoteDataFromClient returns lists of remote entries, mod times, folders, and deletions (four separate lists)
func getRemoteDataFromClient(manualSync bool) ([]string, []int64, []string, []string) {
// get remote output over SSH
output := getSSHOutput("ls && uname -r", manualSync) // TODO replace cmd with remote server command (e.g. "libmuttonserver fetch")
output := getSSHOutput("libmuttonserver", manualSync)
// split output into slice based on occurrences of "\x1f"
outputSlice := strings.Split(output, "\x1f")
// re-form the lists
// re-form the lists TODO handle error for index out of bounds (occurs if reading deletions directory on server fails)
entries := strings.Split(outputSlice[0], "\n")
modsStrings := strings.Split(outputSlice[1], "\n")
folders := strings.Split(outputSlice[2], "\n")
@@ -114,11 +114,7 @@ func getLocalData() ([]string, []int64) {
fileList, _ := WalkEntryDir()
// get a list of all entry modification times
var modList []int64
for _, file := range fileList {
modTime, _ := os.Stat(backend.EntryRoot + file)
modList = append(modList, modTime.ModTime().Unix())
}
modList := getModTimes(fileList)
// return the lists
return fileList, modList
@@ -127,7 +123,7 @@ func getLocalData() ([]string, []int64) {
// RunJob runs the SSH sync job
func RunJob(manualSync bool) {
// TODO fetch remote lists
remoteEntries, remoteMods, remoteFolders, remoteDeletions := getRemoteData(manualSync)
remoteEntries, remoteMods, remoteFolders, remoteDeletions := getRemoteDataFromClient(manualSync)
fmt.Println(remoteEntries, remoteMods, remoteFolders, remoteDeletions) // TODO placeholder
// TODO sync deletions and folders
+14 -2
View File
@@ -9,6 +9,7 @@ import (
)
// WalkEntryDir walks the entry directory and returns lists of all files and directories found (two separate lists)
// initCommand is used to specify to the end user how to generate the entry directory if it does not exists
func WalkEntryDir() ([]string, []string) {
// define file/directory containing slices so that they may be accessed by the anonymous WalkDir function
var fileList []string
@@ -21,10 +22,10 @@ func WalkEntryDir() ([]string, []string) {
// check for errors encountered while walking directory
if err != nil {
if os.IsNotExist(err) {
fmt.Println(backend.AnsiError + "\nThe entry directory does not exist - run \"mutn init\" to create it" + backend.AnsiReset)
fmt.Println(backend.AnsiError+"The entry directory does not exist - run \""+os.Args[0], "init"+"\" to create it"+backend.AnsiReset) // TODO implement init command for libmuttonserver
} else {
// otherwise, print the source of the error
fmt.Println(backend.AnsiError + "\nAn unexpected error occurred while generating the entry list: " + err.Error() + backend.AnsiReset)
fmt.Println(backend.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + backend.AnsiReset)
}
os.Exit(1)
}
@@ -44,3 +45,14 @@ func WalkEntryDir() ([]string, []string) {
return fileList, dirList
}
func getModTimes(entryList []string) []int64 {
// get a list of all entry modification times
var modList []int64
for _, file := range entryList {
modTime, _ := os.Stat(backend.EntryRoot + file)
modList = append(modList, modTime.ModTime().Unix())
}
return modList
}
+37
View File
@@ -0,0 +1,37 @@
package sync
import (
"fmt"
"github.com/rwinkhart/MUTN/src/backend"
"os"
)
// GetRemoteDataFromServer prints to stdout the remote entries, mod times, folders, and deletions
// lists in output are separated by "\x1f"
// output is meant to be captured over SSH for interpretation by the client
func GetRemoteDataFromServer() {
entryList, dirList := WalkEntryDir()
modList := getModTimes(entryList)
deletionsList, err := os.ReadDir(backend.ConfigDir + "/deletions")
if err != nil {
fmt.Println(backend.AnsiError + "Failed to read the deletions directory: " + err.Error() + backend.AnsiReset)
os.Exit(1)
}
// print the lists to stdout
for _, entry := range entryList {
fmt.Println(entry)
}
fmt.Println("\x1f")
for _, mod := range modList {
fmt.Println(mod)
}
fmt.Print("\x1f") // do not print new lines as the prior output is []int64
for _, dir := range dirList {
fmt.Println(dir)
}
fmt.Println("\x1f")
for _, deletion := range deletionsList {
fmt.Println(deletion.Name())
}
}