From c83a3aa018826e18940b3d7ea1da6c2a5b10991f Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 8 Aug 2024 16:49:34 -0400 Subject: [PATCH] Rename "backend" package to "core" --- {backend => core}/1globals.go | 2 +- {backend => core}/2globalsUNIX.go | 2 +- {backend => core}/2globalsWIN.go | 2 +- {backend => core}/configParser.go | 2 +- {backend => core}/copy.go | 4 +- {backend => core}/copyDARWIN.go | 4 +- {backend => core}/copyTERMUX.go | 4 +- {backend => core}/copyUNIXGeneric.go | 4 +- {backend => core}/copyWIN.go | 4 +- {backend => core}/edit.go | 2 +- {backend => core}/exitHard.go | 2 +- {backend => core}/exitSoft.go | 2 +- {backend => core}/gpg.go | 2 +- {backend => core}/init.go | 2 +- {backend => core}/targetLocationFormatUNIX.go | 2 +- {backend => core}/targetLocationFormatWIN.go | 2 +- {backend => core}/utilitiesMisc.go | 2 +- sync/1globals.go | 6 +- sync/client.go | 90 +++++++++---------- sync/common.go | 34 +++---- sync/commonUNIX.go | 11 +-- sync/commonWIN.go | 9 +- sync/init.go | 10 +-- sync/server.go | 8 +- wiki/libmutton/developers.md | 4 +- 25 files changed, 109 insertions(+), 107 deletions(-) rename {backend => core}/1globals.go (96%) rename {backend => core}/2globalsUNIX.go (97%) rename {backend => core}/2globalsWIN.go (98%) rename {backend => core}/configParser.go (99%) rename {backend => core}/copy.go (92%) rename {backend => core}/copyDARWIN.go (89%) rename {backend => core}/copyTERMUX.go (88%) rename {backend => core}/copyUNIXGeneric.go (93%) rename {backend => core}/copyWIN.go (89%) rename {backend => core}/edit.go (97%) rename {backend => core}/exitHard.go (83%) rename {backend => core}/exitSoft.go (79%) rename {backend => core}/gpg.go (98%) rename {backend => core}/init.go (99%) rename {backend => core}/targetLocationFormatUNIX.go (94%) rename {backend => core}/targetLocationFormatWIN.go (95%) rename {backend => core}/utilitiesMisc.go (99%) diff --git a/backend/1globals.go b/core/1globals.go similarity index 96% rename from backend/1globals.go rename to core/1globals.go index dbe358a..1d9234b 100644 --- a/backend/1globals.go +++ b/core/1globals.go @@ -1,4 +1,4 @@ -package backend +package core import ( "os" diff --git a/backend/2globalsUNIX.go b/core/2globalsUNIX.go similarity index 97% rename from backend/2globalsUNIX.go rename to core/2globalsUNIX.go index 6e3bacc..c24a3a3 100644 --- a/backend/2globalsUNIX.go +++ b/core/2globalsUNIX.go @@ -1,6 +1,6 @@ //go:build !windows -package backend +package core // EntryRoot path to libmutton entry directory var EntryRoot = Home + "/.local/share/libmutton" diff --git a/backend/2globalsWIN.go b/core/2globalsWIN.go similarity index 98% rename from backend/2globalsWIN.go rename to core/2globalsWIN.go index 1da260f..5f4539b 100644 --- a/backend/2globalsWIN.go +++ b/core/2globalsWIN.go @@ -1,6 +1,6 @@ //go:build windows -package backend +package core import ( "os" diff --git a/backend/configParser.go b/core/configParser.go similarity index 99% rename from backend/configParser.go rename to core/configParser.go index dc27f8a..ea471e1 100644 --- a/backend/configParser.go +++ b/core/configParser.go @@ -1,4 +1,4 @@ -package backend +package core import ( "fmt" diff --git a/backend/copy.go b/core/copy.go similarity index 92% rename from backend/copy.go rename to core/copy.go index b099ca6..dc74ae8 100644 --- a/backend/copy.go +++ b/core/copy.go @@ -1,4 +1,4 @@ -package backend +package core import ( "bufio" @@ -67,7 +67,7 @@ func ClipClearArgument() { oldContents := clipScanner.Text() clipClear(oldContents) } else { - os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) + os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } } diff --git a/backend/copyDARWIN.go b/core/copyDARWIN.go similarity index 89% rename from backend/copyDARWIN.go rename to core/copyDARWIN.go index 2637f93..3acd452 100644 --- a/backend/copyDARWIN.go +++ b/core/copyDARWIN.go @@ -1,6 +1,6 @@ //go:build darwin -package backend +package core import ( "fmt" @@ -55,5 +55,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) + os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/backend/copyTERMUX.go b/core/copyTERMUX.go similarity index 88% rename from backend/copyTERMUX.go rename to core/copyTERMUX.go index da44354..dc5b70b 100644 --- a/backend/copyTERMUX.go +++ b/core/copyTERMUX.go @@ -1,6 +1,6 @@ //go:build linux && termux -package backend +package core import ( "fmt" @@ -53,5 +53,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) + os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/backend/copyUNIXGeneric.go b/core/copyUNIXGeneric.go similarity index 93% rename from backend/copyUNIXGeneric.go rename to core/copyUNIXGeneric.go index 58e75a9..11768e1 100644 --- a/backend/copyUNIXGeneric.go +++ b/core/copyUNIXGeneric.go @@ -1,6 +1,6 @@ //go:build !windows && !darwin && !termux && !wsl -package backend +package core import ( "fmt" @@ -77,5 +77,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) + os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/backend/copyWIN.go b/core/copyWIN.go similarity index 89% rename from backend/copyWIN.go rename to core/copyWIN.go index b31f524..b7f865c 100644 --- a/backend/copyWIN.go +++ b/core/copyWIN.go @@ -1,6 +1,6 @@ //go:build windows || (linux && wsl) -package backend +package core import ( "fmt" @@ -51,5 +51,5 @@ func clipClear(oldContents string) { os.Exit(1) } } - os.Exit(0) // use os.Exit instead of backend.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) + os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) } diff --git a/backend/edit.go b/core/edit.go similarity index 97% rename from backend/edit.go rename to core/edit.go index ac9a938..fb7cdb0 100644 --- a/backend/edit.go +++ b/core/edit.go @@ -1,4 +1,4 @@ -package backend +package core // GetOldEntryData decrypts and returns old entry data (with all required lines present) func GetOldEntryData(targetLocation string, field int) []string { diff --git a/backend/exitHard.go b/core/exitHard.go similarity index 83% rename from backend/exitHard.go rename to core/exitHard.go index 9759836..df16b53 100644 --- a/backend/exitHard.go +++ b/core/exitHard.go @@ -1,6 +1,6 @@ //go:build !returnOnExit -package backend +package core import "os" diff --git a/backend/exitSoft.go b/core/exitSoft.go similarity index 79% rename from backend/exitSoft.go rename to core/exitSoft.go index 9b198d1..0680291 100644 --- a/backend/exitSoft.go +++ b/core/exitSoft.go @@ -1,6 +1,6 @@ //go:build returnOnExit -package backend +package core func Exit(code int) { return code diff --git a/backend/gpg.go b/core/gpg.go similarity index 98% rename from backend/gpg.go rename to core/gpg.go index 11351ef..e1a6721 100644 --- a/backend/gpg.go +++ b/core/gpg.go @@ -1,4 +1,4 @@ -package backend +package core import ( "fmt" diff --git a/backend/init.go b/core/init.go similarity index 99% rename from backend/init.go rename to core/init.go index 810bcfe..68aeced 100644 --- a/backend/init.go +++ b/core/init.go @@ -1,4 +1,4 @@ -package backend +package core import ( "fmt" diff --git a/backend/targetLocationFormatUNIX.go b/core/targetLocationFormatUNIX.go similarity index 94% rename from backend/targetLocationFormatUNIX.go rename to core/targetLocationFormatUNIX.go index 7ea85d6..4162419 100644 --- a/backend/targetLocationFormatUNIX.go +++ b/core/targetLocationFormatUNIX.go @@ -1,6 +1,6 @@ //go:build !windows -package backend +package core // TargetLocationFormat returns the full location of an entry (given the name) formatted for the current platform func TargetLocationFormat(targetLocationIncomplete string) string { diff --git a/backend/targetLocationFormatWIN.go b/core/targetLocationFormatWIN.go similarity index 95% rename from backend/targetLocationFormatWIN.go rename to core/targetLocationFormatWIN.go index 0a82dfa..b1f8ce4 100644 --- a/backend/targetLocationFormatWIN.go +++ b/core/targetLocationFormatWIN.go @@ -1,6 +1,6 @@ //go:build windows -package backend +package core import "strings" diff --git a/backend/utilitiesMisc.go b/core/utilitiesMisc.go similarity index 99% rename from backend/utilitiesMisc.go rename to core/utilitiesMisc.go index 6d391b1..c5f871b 100644 --- a/backend/utilitiesMisc.go +++ b/core/utilitiesMisc.go @@ -1,4 +1,4 @@ -package backend +package core import ( "crypto/rand" diff --git a/sync/1globals.go b/sync/1globals.go index 03063fc..53367f1 100644 --- a/sync/1globals.go +++ b/sync/1globals.go @@ -1,6 +1,6 @@ package sync -import "github.com/rwinkhart/MUTN/src/backend" +import "github.com/rwinkhart/libmutton/core" // define field separator constants const ( @@ -9,5 +9,5 @@ const ( FSMisc = "\u259f" // ▟ misc. field separator (if \u259d is already used) ) -// rootLength stores length of backend.EntryRoot string -var rootLength = len(backend.EntryRoot) +// rootLength stores length of core.EntryRoot string +var rootLength = len(core.EntryRoot) diff --git a/sync/client.go b/sync/client.go index 2c2b2a0..8af012b 100644 --- a/sync/client.go +++ b/sync/client.go @@ -8,7 +8,7 @@ import ( "time" "github.com/pkg/sftp" - "github.com/rwinkhart/MUTN/src/backend" + "github.com/rwinkhart/libmutton/core" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/knownhosts" ) @@ -31,7 +31,7 @@ func getSSHClient(manualSync bool) (*ssh.Client, string, bool) { } else { missingValueError = "0" } - sshUserConfig = backend.ParseConfig([][2]string{{"LIBMUTTON", "sshUser"}, {"LIBMUTTON", "sshIP"}, {"LIBMUTTON", "sshPort"}, {"LIBMUTTON", "sshKey"}, {"LIBMUTTON", "sshKeyProtected"}, {"LIBMUTTON", "sshEntryRoot"}, {"LIBMUTTON", "sshIsWindows"}}, missingValueError) + sshUserConfig = core.ParseConfig([][2]string{{"LIBMUTTON", "sshUser"}, {"LIBMUTTON", "sshIP"}, {"LIBMUTTON", "sshPort"}, {"LIBMUTTON", "sshKey"}, {"LIBMUTTON", "sshKeyProtected"}, {"LIBMUTTON", "sshEntryRoot"}, {"LIBMUTTON", "sshIsWindows"}}, missingValueError) var user, ip, port, keyFile, keyFileProtected, entryRoot string var isWindows bool @@ -57,7 +57,7 @@ func getSSHClient(manualSync bool) (*ssh.Client, string, bool) { // read private key key, err := os.ReadFile(keyFile) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - unable to read private key file:", keyFile+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - unable to read private key file:", keyFile+core.AnsiReset) os.Exit(1) } @@ -69,14 +69,14 @@ func getSSHClient(manualSync bool) (*ssh.Client, string, bool) { parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, inputKeyFilePassphrase()) } if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to parse private key:", keyFile+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to parse private key:", keyFile+core.AnsiReset) os.Exit(1) } // read known hosts file - hostKeyCallback, err := knownhosts.New(backend.Home + backend.PathSeparator + ".ssh" + backend.PathSeparator + "known_hosts") + hostKeyCallback, err := knownhosts.New(core.Home + core.PathSeparator + ".ssh" + core.PathSeparator + "known_hosts") if err != nil { - fmt.Println(backend.AnsiError + "Sync failed - Unable to read known hosts file:" + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "Sync failed - Unable to read known hosts file:" + err.Error() + core.AnsiReset) os.Exit(1) } @@ -92,7 +92,7 @@ func getSSHClient(manualSync bool) (*ssh.Client, string, bool) { // connect to SSH server sshClient, err := ssh.Dial("tcp", ip+":"+port, sshConfig) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to connect to remote server:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to connect to remote server:", err.Error()+core.AnsiReset) os.Exit(1) } @@ -108,7 +108,7 @@ func GetSSHOutput(cmd, stdin string, manualSync bool) string { // create a session sshSession, err := sshClient.NewSession() if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to establish SSH session:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to establish SSH session:", err.Error()+core.AnsiReset) os.Exit(1) } @@ -119,7 +119,7 @@ func GetSSHOutput(cmd, stdin string, manualSync bool) string { var output []byte output, err = sshSession.CombinedOutput(cmd) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to run SSH command:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to run SSH command:", err.Error()+core.AnsiReset) os.Exit(1) } @@ -133,13 +133,13 @@ func GetSSHOutput(cmd, stdin string, manualSync bool) string { // getRemoteDataFromClient returns a map of remote entries to their modification times, a list of remote folders, a list of queued deletions, and the current server+client times as UNIX timestamps func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []string, int64, int64) { // get remote output over SSH - clientDeviceID, _ := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices") + clientDeviceID, _ := os.ReadDir(core.ConfigDir + core.PathSeparator + "devices") if len(clientDeviceID) == 0 { if manualSync { fmt.Println(joinErrorWithEXE("Sync failed - No device ID found; run \"", " init\" to generate a device ID")) os.Exit(1) } else { - backend.Exit(0) // exit silently if the sync job was called automatically, as the user may just be in offline mode + core.Exit(0) // exit silently if the sync job was called automatically, as the user may just be in offline mode } } clientTime := time.Now().Unix() // get client time now to avoid accuracy issues caused by unpredictable sync time @@ -150,7 +150,7 @@ func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []str // parse output/re-form lists if len(outputSlice) != 5 { // ensure information from server is complete - fmt.Println(backend.AnsiError + "Sync failed - Unable to fetch remote data; server returned an unexpected response" + backend.AnsiReset) + fmt.Println(core.AnsiError + "Sync failed - Unable to fetch remote data; server returned an unexpected response" + core.AnsiReset) os.Exit(1) } serverTime, _ := strconv.ParseInt(outputSlice[0], 10, 64) @@ -211,7 +211,7 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) { // create an SFTP client sftpClient, err := sftp.NewClient(sshClient) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to establish SFTP session:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to establish SFTP session:", err.Error()+core.AnsiReset) os.Exit(1) } defer sftpClient.Close() @@ -221,7 +221,7 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) { for _, entryName := range downloadList { filesTransferred = true // set a flag to indicate that files have been downloaded (used to determine whether to print a gap between download and upload messages) - fmt.Println("Downloading " + ansiDownload + entryName + backend.AnsiReset) + fmt.Println("Downloading " + ansiDownload + entryName + core.AnsiReset) // store path to remote entry remoteEntryFullPath := targetLocationFormatSFTP(entryName, sshEntryRoot, sshIsWindows) @@ -230,7 +230,7 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) { var fileInfo os.FileInfo fileInfo, err = sftpClient.Stat(remoteEntryFullPath) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to get remote file info (mod time):", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to get remote file info (mod time):", err.Error()+core.AnsiReset) os.Exit(1) } modTime := fileInfo.ModTime() @@ -239,25 +239,25 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) { var remoteFile *sftp.File remoteFile, err = sftpClient.Open(remoteEntryFullPath) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to open remote file:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to open remote file:", err.Error()+core.AnsiReset) os.Exit(1) } // store path to local entry - localEntryFullPath := backend.TargetLocationFormat(entryName) + localEntryFullPath := core.TargetLocationFormat(entryName) // create local file var localFile *os.File localFile, err = os.Create(localEntryFullPath) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to create local file:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to create local file:", err.Error()+core.AnsiReset) os.Exit(1) } // download the file _, err = remoteFile.WriteTo(localFile) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to download remote file:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to download remote file:", err.Error()+core.AnsiReset) os.Exit(1) } @@ -278,16 +278,16 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) { for _, entryName := range uploadList { filesTransferred = true // set a flag to indicate that files have been uploaded (used to determine whether to print a gap between upload and sync complete messages) - fmt.Println("Uploading " + ansiUpload + entryName + backend.AnsiReset) + fmt.Println("Uploading " + ansiUpload + entryName + core.AnsiReset) // store path to local entry - localEntryFullPath := backend.TargetLocationFormat(entryName) + localEntryFullPath := core.TargetLocationFormat(entryName) // save modification time of local file var fileInfo os.FileInfo fileInfo, err = os.Stat(localEntryFullPath) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to get local file info (mod time):", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to get local file info (mod time):", err.Error()+core.AnsiReset) os.Exit(1) } modTime := fileInfo.ModTime() @@ -296,7 +296,7 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) { var localFile *os.File localFile, err = os.Open(localEntryFullPath) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to open local file:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to open local file:", err.Error()+core.AnsiReset) os.Exit(1) } @@ -307,14 +307,14 @@ func sftpSync(downloadList, uploadList []string, manualSync bool) { var remoteFile *sftp.File remoteFile, err = sftpClient.Create(remoteEntryFullPath) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to create remote file ("+remoteEntryFullPath+"):", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to create remote file ("+remoteEntryFullPath+"):", err.Error()+core.AnsiReset) os.Exit(1) } // upload the file _, err = localFile.WriteTo(remoteFile) if err != nil { - fmt.Println(backend.AnsiError+"Sync failed - Unable to upload local file:", err.Error()+backend.AnsiReset) + fmt.Println(core.AnsiError+"Sync failed - Unable to upload local file:", err.Error()+core.AnsiReset) os.Exit(1) } @@ -342,7 +342,7 @@ func syncLists(localEntryModMap, remoteEntryModMap map[string]int64, manualSync timeDiff := serverTime - clientTime if timeDiff < -45 || timeDiff > 45 { timeSynced = false - fmt.Print(backend.AnsiError + "Client and server clocks are out of sync.\n\nPlease ensure both clocks are correct before attempting to sync again.\n\nA dry sync output will be printed below (if any operations would have been performed). It is strongly recommended to review it and manually update the modification times as applicable to ensure the correct version of each entry is kept.\n\nIf the client's clock is at fault, update the modification times of any entries pending upload, even if the correct (upload) operation is being performed on them. Failure to do so could result in entries being uploaded to the server with the incorrect modification times (could result in data loss).\n\n" + backend.AnsiReset) + fmt.Print(core.AnsiError + "Client and server clocks are out of sync.\n\nPlease ensure both clocks are correct before attempting to sync again.\n\nA dry sync output will be printed below (if any operations would have been performed). It is strongly recommended to review it and manually update the modification times as applicable to ensure the correct version of each entry is kept.\n\nIf the client's clock is at fault, update the modification times of any entries pending upload, even if the correct (upload) operation is being performed on them. Failure to do so could result in entries being uploaded to the server with the incorrect modification times (could result in data loss).\n\n" + core.AnsiReset) } // iterate over client entries @@ -351,23 +351,23 @@ func syncLists(localEntryModMap, remoteEntryModMap map[string]int64, manualSync if remoteModTime, present := remoteEntryModMap[entry]; present { // entry exists on both client and server, compare mod times if remoteModTime > localModTime { - fmt.Println(ansiDownload+entry+backend.AnsiReset, "is newer on server, adding to download list") + fmt.Println(ansiDownload+entry+core.AnsiReset, "is newer on server, adding to download list") downloadList = append(downloadList, entry) } else if remoteModTime < localModTime { - fmt.Println(ansiUpload+entry+backend.AnsiReset, "is newer on client, adding to upload list") + fmt.Println(ansiUpload+entry+core.AnsiReset, "is newer on client, adding to upload list") uploadList = append(uploadList, entry) } // remove entry from remoteEntryModMap (process of elimination) delete(remoteEntryModMap, entry) } else { - fmt.Println(ansiUpload+entry+backend.AnsiReset, "does not exist on server, adding to upload list") + fmt.Println(ansiUpload+entry+core.AnsiReset, "does not exist on server, adding to upload list") uploadList = append(uploadList, entry) } } // iterate over remaining entries in remoteEntryModMap for entry := range remoteEntryModMap { - fmt.Println(ansiDownload+entry+backend.AnsiReset, "does not exist on client, adding to download list") + fmt.Println(ansiDownload+entry+core.AnsiReset, "does not exist on client, adding to download list") downloadList = append(downloadList, entry) } @@ -377,7 +377,7 @@ func syncLists(localEntryModMap, remoteEntryModMap map[string]int64, manualSync sftpSync(downloadList, uploadList, manualSync) } else if !timeSynced { // do not call sftpSync if the client and server times are out of sync - backend.Exit(1) + core.Exit(1) } fmt.Println("Client is synchronized with server") @@ -388,8 +388,8 @@ func deletionSync(deletions []string) { var filesDeleted bool for _, deletion := range deletions { filesDeleted = true // set a flag to indicate that files have been deleted (used to determine whether to print a gap between deletion and other messages) - fmt.Println(ansiDelete+deletion+backend.AnsiReset, "has been sheared, removing locally (if it exists)") - os.RemoveAll(backend.TargetLocationFormat(deletion)) + fmt.Println(ansiDelete+deletion+core.AnsiReset, "has been sheared, removing locally (if it exists)") + os.RemoveAll(core.TargetLocationFormat(deletion)) } if filesDeleted { @@ -405,10 +405,10 @@ func ShearRemoteFromClient(targetLocationIncomplete string) { if deviceID != "" { // ensure a device ID exists (online mode) // call the server to remotely shear the target and add it to the deletions list GetSSHOutput("libmuttonserver shear", deviceID+"\n"+ - strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, FSPath), false) + strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, FSPath), false) } - backend.Exit(0) // sync is not required after shearing since the target has already been removed from the local system + core.Exit(0) // sync is not required after shearing since the target has already been removed from the local system } // RenameRemoteFromClient renames oldLocationIncomplete to newLocationIncomplete on the local system and calls the server to perform the rename remotely and add the old target to the deletions list @@ -421,35 +421,35 @@ 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("libmuttonserver rename", (*deviceIDList)[0].Name()+"\n"+ - strings.ReplaceAll(oldLocationIncomplete, backend.PathSeparator, FSPath)+"\n"+ - strings.ReplaceAll(newLocationIncomplete, backend.PathSeparator, FSPath), false) + strings.ReplaceAll(oldLocationIncomplete, core.PathSeparator, FSPath)+"\n"+ + strings.ReplaceAll(newLocationIncomplete, core.PathSeparator, FSPath), false) } - backend.Exit(0) + core.Exit(0) } // AddFolderRemoteFromClient creates a new entry-containing directory on the local system and calls the server to create the folder remotely // can safely be called in offline mode, as well, so this is the intended interface for adding folders (AddFolderLocal should only be used directly by the server binary) func AddFolderRemoteFromClient(targetLocationIncomplete string) { - AddFolderLocal(targetLocationIncomplete) // add the folder on the local system - GetSSHOutput("libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, FSPath), false) // call the server to create the folder remotely + AddFolderLocal(targetLocationIncomplete) // add the folder on the local system + GetSSHOutput("libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, FSPath), false) // call the server to create the folder remotely - backend.Exit(0) + core.Exit(0) } // folderSync creates folders on the client (from the given list of folder names) func folderSync(folders []string) { for _, folder := range folders { // store the full local path of the folder - folderFullPath := backend.TargetLocationFormat(folder) + folderFullPath := core.TargetLocationFormat(folder) // check if folder already exists - isFile, isAccessible := backend.TargetIsFile(folderFullPath, false, 1) + isFile, isAccessible := core.TargetIsFile(folderFullPath, false, 1) if !isFile && !isAccessible { os.MkdirAll(folderFullPath, 0700) } else if isFile { - fmt.Println(backend.AnsiError + "Sync failed - Failed to create folder \"" + folder + "\" - a file with the same name already exists" + backend.AnsiReset) + fmt.Println(core.AnsiError + "Sync failed - Failed to create folder \"" + folder + "\" - a file with the same name already exists" + core.AnsiReset) os.Exit(1) } } @@ -473,5 +473,5 @@ func RunJob(manualSync bool) { syncLists(localEntryModMap, remoteEntryModMap, manualSync, serverTime, clientTime) // exit program after successful sync - backend.Exit(0) + core.Exit(0) } diff --git a/sync/common.go b/sync/common.go index 04fb6c8..0141a08 100644 --- a/sync/common.go +++ b/sync/common.go @@ -6,14 +6,14 @@ import ( "os" "strings" - "github.com/rwinkhart/MUTN/src/backend" + "github.com/rwinkhart/libmutton/core" ) // getModTimes returns a list of all entry modification times func getModTimes(entryList []string) []int64 { var modList []int64 for _, file := range entryList { - modTime, _ := os.Stat(backend.TargetLocationFormat(file)) + modTime, _ := os.Stat(core.TargetLocationFormat(file)) modList = append(modList, modTime.ModTime().Unix()) } @@ -23,9 +23,9 @@ func getModTimes(entryList []string) []int64 { // genDeviceIDList returns a pointer to a slice of all registered device IDs func genDeviceIDList() *[]fs.DirEntry { // create a slice of all registered devices - deviceIDList, err := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices") + deviceIDList, err := os.ReadDir(core.ConfigDir + core.PathSeparator + "devices") if err != nil { - fmt.Println(backend.AnsiError + "Failed to read the devices directory: " + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "Failed to read the devices directory: " + err.Error() + core.AnsiReset) os.Exit(1) } return &deviceIDList @@ -48,7 +48,7 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { if onServer { for _, device := range *deviceIDList { if device.Name() != clientDeviceID { - _, err := os.Create(backend.ConfigDir + backend.PathSeparator + "deletions" + backend.PathSeparator + device.Name() + FSSpace + strings.ReplaceAll(targetLocationIncomplete, "/", FSPath)) + _, err := os.Create(core.ConfigDir + core.PathSeparator + "deletions" + core.PathSeparator + device.Name() + FSSpace + strings.ReplaceAll(targetLocationIncomplete, "/", FSPath)) 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) @@ -59,13 +59,13 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { } // get the full targetLocation path and remove the target - targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete) + targetLocationComplete := core.TargetLocationFormat(targetLocationIncomplete) if !onServer { // error if target does not exist on client, needed because os.RemoveAll does not return an error if target does not exist - backend.TargetIsFile(targetLocationComplete, true, 0) + core.TargetIsFile(targetLocationComplete, true, 0) } err := os.RemoveAll(targetLocationComplete) if err != nil { - fmt.Println(backend.AnsiError + "Failed to remove local target: " + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "Failed to remove local target: " + err.Error() + core.AnsiReset) os.Exit(1) } @@ -81,24 +81,24 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { // this function should only be used directly by the server binary func RenameLocal(oldLocationIncomplete, newLocationIncomplete string, verifyOldLocationExists bool) { // get full paths for both locations - oldLocation := backend.TargetLocationFormat(oldLocationIncomplete) - newLocation := backend.TargetLocationFormat(newLocationIncomplete) + oldLocation := core.TargetLocationFormat(oldLocationIncomplete) + newLocation := core.TargetLocationFormat(newLocationIncomplete) if verifyOldLocationExists { - backend.TargetIsFile(oldLocation, true, 0) + core.TargetIsFile(oldLocation, true, 0) } // ensure newLocation does not exist - _, isAccessible := backend.TargetIsFile(newLocation, false, 0) + _, isAccessible := core.TargetIsFile(newLocation, false, 0) if isAccessible { - fmt.Println(backend.AnsiError + "\"" + newLocation + "\" already exists" + backend.AnsiReset) + fmt.Println(core.AnsiError + "\"" + newLocation + "\" already exists" + core.AnsiReset) os.Exit(1) } // rename oldLocation to newLocation err := os.Rename(oldLocation, newLocation) if err != nil { - fmt.Println(backend.AnsiError + "Failed to rename - does the target containing directory exist?" + backend.AnsiReset) + fmt.Println(core.AnsiError + "Failed to rename - does the target containing directory exist?" + core.AnsiReset) } // do not exit program, as this function is used as part of RenameRemoteFromClient @@ -108,14 +108,14 @@ func RenameLocal(oldLocationIncomplete, newLocationIncomplete string, verifyOldL // this function should only be used directly by the server binary func AddFolderLocal(targetLocationIncomplete string) { // get the full targetLocation path and create the target - targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete) + targetLocationComplete := core.TargetLocationFormat(targetLocationIncomplete) err := os.Mkdir(targetLocationComplete, 0700) if err != nil { if os.IsExist(err) { - fmt.Println(backend.AnsiError + "Directory already exists" + backend.AnsiReset) + fmt.Println(core.AnsiError + "Directory already exists" + core.AnsiReset) os.Exit(1) } else { - fmt.Println(backend.AnsiError + "Failed to create directory: " + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "Failed to create directory: " + err.Error() + core.AnsiReset) os.Exit(1) } } diff --git a/sync/commonUNIX.go b/sync/commonUNIX.go index 2782a27..e0b77de 100644 --- a/sync/commonUNIX.go +++ b/sync/commonUNIX.go @@ -4,10 +4,11 @@ package sync import ( "fmt" - "github.com/rwinkhart/MUTN/src/backend" "io/fs" "os" "path/filepath" + + "github.com/rwinkhart/libmutton/core" ) // WalkEntryDir walks the entry directory and returns lists of all files and directories found (two separate lists) @@ -18,16 +19,16 @@ func WalkEntryDir() ([]string, []string) { var dirList []string // walk entry directory - _ = filepath.WalkDir(backend.EntryRoot, + _ = filepath.WalkDir(core.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+"The entry directory does not exist - run \""+os.Args[0], "init"+"\" to create it"+backend.AnsiReset) + fmt.Println(core.AnsiError+"The entry directory does not exist - run \""+os.Args[0], "init"+"\" to create it"+core.AnsiReset) } else { // otherwise, print the source of the error - fmt.Println(backend.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + core.AnsiReset) } os.Exit(1) } @@ -50,5 +51,5 @@ func WalkEntryDir() ([]string, []string) { // joinErrorWithEXE joins and returns the two strings it is provided (in error format) with the executable name inserted between them func joinErrorWithEXE(firstHalf, secondHalf string) string { - return backend.AnsiError + firstHalf + os.Args[0] + secondHalf + backend.AnsiReset + return core.AnsiError + firstHalf + os.Args[0] + secondHalf + core.AnsiReset } diff --git a/sync/commonWIN.go b/sync/commonWIN.go index f257ebe..ff0276b 100644 --- a/sync/commonWIN.go +++ b/sync/commonWIN.go @@ -4,11 +4,12 @@ package sync import ( "fmt" - "github.com/rwinkhart/MUTN/src/backend" "io/fs" "os" "path/filepath" "strings" + + "github.com/rwinkhart/libmutton/core" ) // WalkEntryDir walks the entry directory and returns lists of all files and directories found (two separate lists) @@ -19,7 +20,7 @@ func WalkEntryDir() ([]string, []string) { var dirList []string // walk entry directory - _ = filepath.WalkDir(backend.EntryRoot, + _ = filepath.WalkDir(core.EntryRoot, func(fullPath string, entry fs.DirEntry, err error) error { // check for errors encountered while walking directory @@ -28,7 +29,7 @@ func WalkEntryDir() ([]string, []string) { fmt.Println(joinErrorWithEXE("The entry directory does not exist - run \"", " init"+"\" to create it")) } else { // otherwise, print the source of the error - fmt.Println(backend.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + core.AnsiReset) } os.Exit(1) } @@ -51,5 +52,5 @@ func WalkEntryDir() ([]string, []string) { // joinErrorWithEXE joins and returns the two strings it is provided (in error format) with the executable name inserted between them func joinErrorWithEXE(firstHalf, secondHalf string) string { - return backend.AnsiError + firstHalf + os.Args[0][strings.LastIndex(os.Args[0], "\\")+1:] + secondHalf + backend.AnsiReset + return core.AnsiError + firstHalf + os.Args[0][strings.LastIndex(os.Args[0], "\\")+1:] + secondHalf + core.AnsiReset } diff --git a/sync/init.go b/sync/init.go index 88e2e78..6309e60 100644 --- a/sync/init.go +++ b/sync/init.go @@ -8,20 +8,20 @@ import ( "strings" "time" - "github.com/rwinkhart/MUTN/src/backend" + "github.com/rwinkhart/libmutton/core" ) // DeviceIDGen generates a new client device ID and registers it with the server // device IDs are only needed for online synchronization // device IDs are guaranteed unique as the current UNIX time is appended to them -// returns the remote EntryRoot and OS type (OS type is a bool: backend.IsWindows) +// returns the remote EntryRoot and OS type (OS type is a bool: core.IsWindows) func DeviceIDGen() (string, string) { deviceIDPrefix, _ := os.Hostname() - deviceIDSuffix := backend.StringGen(rand.Intn(32)+48, true, 0.2, true) + "-" + strconv.FormatInt(time.Now().Unix(), 10) + deviceIDSuffix := core.StringGen(rand.Intn(32)+48, true, 0.2, true) + "-" + strconv.FormatInt(time.Now().Unix(), 10) deviceID := deviceIDPrefix + "-" + deviceIDSuffix - _, err := os.Create(backend.ConfigDir + backend.PathSeparator + "devices" + backend.PathSeparator + deviceID) // TODO remove existing device ID file if it exists (from both client and server) + _, err := os.Create(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + deviceID) // TODO remove existing device ID file if it exists (from both client and server) if err != nil { - fmt.Println(backend.AnsiError + "Failed to create local device ID file: " + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "Failed to create local device ID file: " + err.Error() + core.AnsiReset) } // register device ID with server and fetch remote EntryRoot and OS type diff --git a/sync/server.go b/sync/server.go index bc7b329..2861b9e 100644 --- a/sync/server.go +++ b/sync/server.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "github.com/rwinkhart/MUTN/src/backend" + "github.com/rwinkhart/libmutton/core" ) // GetRemoteDataFromServer prints to stdout the remote entries, mod times, folders, and deletions @@ -15,9 +15,9 @@ import ( func GetRemoteDataFromServer(clientDeviceID string) { entryList, dirList := WalkEntryDir() modList := getModTimes(entryList) - deletionsList, err := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "deletions") + deletionsList, err := os.ReadDir(core.ConfigDir + core.PathSeparator + "deletions") if err != nil { - fmt.Println(backend.AnsiError + "Failed to read the deletions directory: " + err.Error() + backend.AnsiReset) + fmt.Println(core.AnsiError + "Failed to read the deletions directory: " + err.Error() + core.AnsiReset) os.Exit(1) } @@ -53,7 +53,7 @@ func GetRemoteDataFromServer(clientDeviceID string) { fmt.Print(FSMisc + strings.ReplaceAll(affectedIDTargetLocationIncomplete[1], 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(backend.ConfigDir + backend.PathSeparator + "deletions" + backend.PathSeparator + deletion.Name()) // error ignored; function not run from a user-facing argument and thus the error would not be visible + _ = 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 } } } diff --git a/wiki/libmutton/developers.md b/wiki/libmutton/developers.md index 29e2725..367af44 100644 --- a/wiki/libmutton/developers.md +++ b/wiki/libmutton/developers.md @@ -3,7 +3,7 @@ libmutton was designed to be usable as a library for building other compatible password managers off of. [MUTN](https://github.com/rwinkhart/MUTN) is the official reference CLI password manager, however libmutton can be implemented in many other unique ways. -All functionality in the `backend` and `sync` packages are designed to be used in other implementations. +All functionality in the `core` and `sync` packages are designed to be used in other implementations. If any functionality in these two packages proves to be difficult to implement in a third-party client, please open an issue so that it can be addressed. @@ -17,7 +17,7 @@ These are as follows: ## Required Arguments libmutton-based password manager clients should accept at least one specific required argument, as well as another recommended one: -- `clipclear`: This argument is required for correct functionality. In order to clear the clipboard on a timer, libmutton-based password managers call another instance of their executable with the `clipclear` argument (e.g. `mutn clipclear`) with the intended clipboard contents provided via STDIN. If after 30 seconds the clipboard contents have not changed, they are cleared. Please accept a `clipclear` argument that is processed before the launch of any interactive interface. All this argument needs to do is call `backend.ClipClearArgument()`. +- `clipclear`: This argument is required for correct functionality. In order to clear the clipboard on a timer, libmutton-based password managers call another instance of their executable with the `clipclear` argument (e.g. `mutn clipclear`) with the intended clipboard contents provided via STDIN. If after 30 seconds the clipboard contents have not changed, they are cleared. Please accept a `clipclear` argument that is processed before the launch of any interactive interface. All this argument needs to do is call `core.ClipClearArgument()`. - `init`: This argument is optional, but recommended for CLI interfaces. Some error messages request the user to use the `init` argument to fix configuration issues. If the argument does not exist, this may confuse the user. ## Configuration