From 941a2dc8b80cb6e0327b33714c4a894962621bda Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 11 Dec 2025 19:31:37 -0500 Subject: [PATCH] Eliminate synccycles package [synccycles.DeviceIDGen() => syncclient.DeviceIDGenFromClient()] --- core/init.go | 4 +-- syncclient/oneOff.go | 75 ++++++++++++++++++++++++++++++++++++++ synccycles/init.go | 85 -------------------------------------------- 3 files changed, 77 insertions(+), 87 deletions(-) delete mode 100644 synccycles/init.go diff --git a/core/init.go b/core/init.go index ecb8019..2b6d5da 100644 --- a/core/init.go +++ b/core/init.go @@ -9,7 +9,7 @@ import ( "github.com/rwinkhart/go-boilerplate/back" "github.com/rwinkhart/libmutton/cfg" "github.com/rwinkhart/libmutton/global" - "github.com/rwinkhart/libmutton/synccycles" + "github.com/rwinkhart/libmutton/syncclient" "github.com/rwinkhart/rcw/wrappers" ) @@ -80,7 +80,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData [][ return errors.New("unable to write config file: " + err.Error()) } // generate and register device ID - sshEntryRoot, sshAgeDir, sshIsWindows, err := synccycles.DeviceIDGen(oldDeviceID, "") + sshEntryRoot, sshAgeDir, sshIsWindows, err := syncclient.DeviceIDGenFromClient(oldDeviceID, "") if err != nil { return errors.New("unable to generate device ID: " + err.Error()) } diff --git a/syncclient/oneOff.go b/syncclient/oneOff.go index be3b1dc..d2522ad 100644 --- a/syncclient/oneOff.go +++ b/syncclient/oneOff.go @@ -1,10 +1,16 @@ package syncclient import ( + "encoding/json" "errors" + "math/rand" + "os" + "strconv" "strings" + "time" "github.com/rwinkhart/go-boilerplate/back" + "github.com/rwinkhart/go-boilerplate/stringy" "github.com/rwinkhart/libmutton/global" "github.com/rwinkhart/libmutton/synccommon" ) @@ -154,3 +160,72 @@ end: back.Exit(0) return nil } + +// DeviceIDGenFromClient generates a new client device ID +// and registers it with the server (will replace existing one). +// Device IDs are only needed for online synchronization. +// Device IDs are guaranteed unique as the current UNIX time is appended to them. +// Leave prefix empty to use the current hostname as the prefix. +// Returns: the remote EntryRoot, the remote AgeDir, and OS type indicator. +func DeviceIDGenFromClient(oldDeviceID, prefix string) (string, string, string, error) { + // generate new device ID + if prefix == "" { + prefix, _ = os.Hostname() + } + newDeviceID := prefix + "-" + stringy.StringGen(rand.Intn(32)+48, 0.2, 1) + "-" + strconv.FormatInt(time.Now().Unix(), 10) + + // create new device ID file (locally) + newDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID + oldDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID + f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) + if err != nil { + return "", "", "", errors.New("unable to create local device ID file: " + err.Error()) + } + _ = f.Close() // error ignored; if the file could be created, it can probably be closed + + cleanupOnFail := func() { + // remove new device ID file + _ = os.RemoveAll(newDeviceIDPath) + if oldDeviceID != global.FSMisc { + // restore old device ID file (if it existed and has already been removed due to DirInit) + if isAccessible, _ := back.TargetIsFile(oldDeviceIDPath, true); !isAccessible { + f, _ := os.OpenFile(oldDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) + _ = f.Close() // error ignored; if the file could be created, it can probably be closed + } + } + } + + // register new device ID with server and fetch remote EntryRoot and OS type + // also removes the old device ID file (remotely) + // if registration fails, remove the new device ID file locally and return before removing the old one + sshClient, _, _, _, _, err := GetSSHClient() + if err != nil { + cleanupOnFail() + return "", "", "", errors.New("unable to connect to SSH client: " + err.Error()) + } + output, err := GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID) + if err != nil { + cleanupOnFail() + return "", "", "", errors.New("unable to register device ID with server: " + err.Error()) + } + var registerResp synccommon.RegisterResp + err = json.Unmarshal(output, ®isterResp) + if err != nil { + cleanupOnFail() + return "", "", "", errors.New("unable to unmarshal server register response: " + err.Error()) + } + if registerResp.ErrMsg != nil { + cleanupOnFail() + return "", "", "", errors.New("unable to complete register; server-side error occurred: " + strings.ReplaceAll(*registerResp.ErrMsg, global.FSSpace, "\n")) + } + _ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it + + // remove old device ID file (locally; may not exist) + err = os.RemoveAll(oldDeviceIDPath) + if err != nil { + cleanupOnFail() + return "", "", "", errors.New("unable to remove old device ID file (locally): " + err.Error()) + } + + return registerResp.EntryRoot, registerResp.AgeDir, strconv.FormatBool(registerResp.IsWindows), nil +} diff --git a/synccycles/init.go b/synccycles/init.go deleted file mode 100644 index 69392ec..0000000 --- a/synccycles/init.go +++ /dev/null @@ -1,85 +0,0 @@ -package synccycles - -import ( - "encoding/json" - "errors" - "math/rand" - "os" - "strconv" - "strings" - "time" - - "github.com/rwinkhart/go-boilerplate/back" - "github.com/rwinkhart/go-boilerplate/stringy" - "github.com/rwinkhart/libmutton/global" - "github.com/rwinkhart/libmutton/syncclient" - "github.com/rwinkhart/libmutton/synccommon" -) - -// DeviceIDGen generates a new client device ID and registers it with the server (will replace existing one). -// Device IDs are only needed for online synchronization. -// Device IDs are guaranteed unique as the current UNIX time is appended to them. -// Leave prefix empty to use the current hostname as the prefix. -// Returns: the remote EntryRoot, the remote AgeDir, and OS type indicator. -func DeviceIDGen(oldDeviceID, prefix string) (string, string, string, error) { - // generate new device ID - if prefix == "" { - prefix, _ = os.Hostname() - } - newDeviceID := prefix + "-" + stringy.StringGen(rand.Intn(32)+48, 0.2, 1) + "-" + strconv.FormatInt(time.Now().Unix(), 10) - - // create new device ID file (locally) - newDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID - oldDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID - f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) - if err != nil { - return "", "", "", errors.New("unable to create local device ID file: " + err.Error()) - } - _ = f.Close() // error ignored; if the file could be created, it can probably be closed - - cleanupOnFail := func() { - // remove new device ID file - _ = os.RemoveAll(newDeviceIDPath) - if oldDeviceID != global.FSMisc { - // restore old device ID file (if it existed and has already been removed due to DirInit) - if isAccessible, _ := back.TargetIsFile(oldDeviceIDPath, true); !isAccessible { - f, _ := os.OpenFile(oldDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) - _ = f.Close() // error ignored; if the file could be created, it can probably be closed - } - } - } - - // register new device ID with server and fetch remote EntryRoot and OS type - // also removes the old device ID file (remotely) - // if registration fails, remove the new device ID file locally and return before removing the old one - sshClient, _, _, _, _, err := syncclient.GetSSHClient() - if err != nil { - cleanupOnFail() - return "", "", "", errors.New("unable to connect to SSH client: " + err.Error()) - } - output, err := syncclient.GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID) - if err != nil { - cleanupOnFail() - return "", "", "", errors.New("unable to register device ID with server: " + err.Error()) - } - var registerResp synccommon.RegisterResp - err = json.Unmarshal(output, ®isterResp) - if err != nil { - cleanupOnFail() - return "", "", "", errors.New("unable to unmarshal server register response: " + err.Error()) - } - if registerResp.ErrMsg != nil { - cleanupOnFail() - return "", "", "", errors.New("unable to complete register; server-side error occurred: " + strings.ReplaceAll(*registerResp.ErrMsg, global.FSSpace, "\n")) - } - _ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it - - // remove old device ID file (locally; may not exist) - err = os.RemoveAll(oldDeviceIDPath) - if err != nil { - cleanupOnFail() - return "", "", "", errors.New("unable to remove old device ID file (locally): " + err.Error()) - } - - return registerResp.EntryRoot, registerResp.AgeDir, strconv.FormatBool(registerResp.IsWindows), nil -}