From 39dc586d3cf191f5670b7594091d25027a49aff7 Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Fri, 7 Nov 2025 12:09:37 -0500 Subject: [PATCH] refactor: Swapped position of `Sync` and `Restore` for better readability. --- app/env_file.go | 74 ++++++++++++++++++++++++++----------------------- cmd/sync.go | 1 - 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/app/env_file.go b/app/env_file.go index 780b478..0e88e5a 100644 --- a/app/env_file.go +++ b/app/env_file.go @@ -96,6 +96,45 @@ func getGitRemotes(dir string) []string { return remotes } +// Try to reconcile the EnvFile with the filesystem. +// +// If Updated is returned, [Db.Insert] should be called on file. +func (file *EnvFile) Sync() (result EnvFileSyncResult, err error) { + // TODO: If the directory doesn't exist, look for other directories with the same remote(s) + // TODO: If one is found, update file.Dir and File.Path + // TODO: If nothing if found, return an error + // TODO: If more than one is found, return a different error + + // Check if the path exists in the file system + _, err = os.Stat(file.Path) + if err == nil { + contents, err := os.ReadFile(file.Path) + if err != nil { + return Error, fmt.Errorf("failed to read file for SHA comparison: %w", err) + } + + // Check if sha matches by reading the current file and calculating its hash + hash := sha256.Sum256(contents) + currentSha := fmt.Sprintf("%x", hash) + if file.Sha256 == currentSha { + // Nothing to do + return Noop, nil + } else { + if err = file.Backup(); err != nil { + return Error, err + } else { + return Updated, nil + } + } + } else { + if err = file.Restore(); err != nil { + return Error, err + } else { + return Restored, nil + } + } +} + // Install the file into the file system. If the file already exists, // it will be overwritten. func (file EnvFile) Restore() error { @@ -136,41 +175,6 @@ func (file EnvFile) Restore() error { return nil } - -} - -// Try to reconcile the EnvFile with the filesystem. -// -// If Updated is returned, [Db.Insert] should be called on file. -func (file *EnvFile) Sync() (result EnvFileSyncResult, err error) { - // Check if the path exists in the file system - _, err = os.Stat(file.Path) - if err == nil { - contents, err := os.ReadFile(file.Path) - if err != nil { - return Error, fmt.Errorf("failed to read file for SHA comparison: %w", err) - } - - // Check if sha matches by reading the current file and calculating its hash - hash := sha256.Sum256(contents) - currentSha := fmt.Sprintf("%x", hash) - if file.Sha256 == currentSha { - // Nothing to do - return Noop, nil - } else { - if err = file.Backup(); err != nil { - return Error, err - } else { - return Updated, nil - } - } - } else { - if err = file.Restore(); err != nil { - return Error, err - } else { - return Restored, nil - } - } } // Update the EnvFile using the file system. diff --git a/cmd/sync.go b/cmd/sync.go index 8fed0e8..82a0ff7 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -10,7 +10,6 @@ import ( "github.com/spf13/cobra" ) -// TODO: Detect when file paths have moved and update accordingly. var syncCmd = &cobra.Command{ Use: "sync", Short: "Update or restore your env backups",