Initial groundwork for native sync support

This commit is contained in:
2024-05-11 20:52:38 -04:00
parent 3acaa607e4
commit f86c2d8153
11 changed files with 143 additions and 51 deletions
+6
View File
@@ -0,0 +1,6 @@
package sync
import "github.com/rwinkhart/MUTN/src/backend"
// RootLength store length of backend.EntryRoot string
var rootLength = len(backend.EntryRoot)
+42
View File
@@ -0,0 +1,42 @@
package sync
import (
"fmt"
"github.com/rwinkhart/MUTN/src/backend"
"os"
)
// returns lists of local entries and mod times (two separate lists)
func getLocalData() {
fileList, _ := WalkEntryDir()
fmt.Println(fileList) // TODO placeholder
}
// RunJob runs the SSH sync job
func RunJob(manualSync bool) {
// get SSH config info, exit if not configured (displaying an error if the sync job was called manually)
var sshUserIPPortIdentity []string
if manualSync {
sshUserIPPortIdentity = backend.ReadConfig([]string{"sshUser", "sshIP", "sshPort", "sshIdentity"}, "SSH settings not configured - run \"mutn init\" to configure")
} else {
sshUserIPPortIdentity = backend.ReadConfig([]string{"sshUser", "sshIP", "sshPort", "sshIdentity"}, "0")
}
var sshUser, sshIP, sshPort, sshIdentity string
for i, key := range sshUserIPPortIdentity {
switch i {
case 0:
sshUser = key
case 1:
sshIP = key
case 2:
sshPort = key
case 3:
sshIdentity = key
}
}
fmt.Println(sshUser, sshIP, sshPort, sshIdentity) // TODO placeholder
os.Exit(0)
}
+46
View File
@@ -0,0 +1,46 @@
package sync
import (
"fmt"
"github.com/rwinkhart/MUTN/src/backend"
"io/fs"
"os"
"path/filepath"
)
// WalkEntryDir walks the entry directory and returns lists of all files and directories found (two separate lists)
func WalkEntryDir() ([]string, []string) {
// define file/directory containing slices so that they may be accessed by the anonymous WalkDir function
var fileList []string
var dirList []string
// walk entry directory
_ = filepath.WalkDir(backend.EntryRoot,
func(fullPath string, entry fs.DirEntry, err error) error {
// 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)
} 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)
}
os.Exit(1)
}
// trim root path from each path before storing
trimmedPath := fullPath[rootLength:]
// create separate slices for entries and directories
if !entry.IsDir() {
fileList = append(fileList, trimmedPath)
} else {
dirList = append(dirList, trimmedPath)
}
return nil
})
return fileList, dirList
}