feat(sync): Now checks files for mismatched hashes before replacing.

This commit is contained in:
2025-11-07 11:25:31 -05:00
parent 9a729e6e2a
commit 8074f7ae6d

View File

@@ -96,25 +96,47 @@ func getGitRemotes(dir string) []string {
return remotes return remotes
} }
// Install the file into the file system // Install the file into the file system. If the file already exists,
// it will be overwritten.
func (file EnvFile) Restore() error { func (file EnvFile) Restore() error {
// TODO: Handle restores more cleanly // TODO: Duplicate work is being done when called from the Sync function.
if _, err := os.Stat(file.Path); err == nil {
// file already exists
// Read existing file and calculate its hash
existingContents, err := os.ReadFile(file.Path)
if err != nil {
return fmt.Errorf("failed to read existing file for hash comparison: %w", err)
}
hash := sha256.Sum256(existingContents)
existingSha := fmt.Sprintf("%x", hash)
if existingSha == file.Sha256 {
return fmt.Errorf("file already exists: %s", file.Path)
} else {
if err := os.WriteFile(file.Path, []byte(file.contents), 0644); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
} else {
// file doesn't exist
// Ensure the directory exists // Ensure the directory exists
if _, err := os.Stat(file.Dir); err != nil { if _, err := os.Stat(file.Dir); err != nil {
return fmt.Errorf("directory missing") return fmt.Errorf("directory missing")
} }
// Check if file already exists
if _, err := os.Stat(file.Path); err == nil {
return fmt.Errorf("file already exists: %s", file.Path)
}
// Write the contents to the file // Write the contents to the file
if err := os.WriteFile(file.Path, []byte(file.contents), 0644); err != nil { if err := os.WriteFile(file.Path, []byte(file.contents), 0644); err != nil {
return fmt.Errorf("failed to write file: %w", err) return fmt.Errorf("failed to write file: %w", err)
} }
return nil return nil
}
} }
// Try to reconcile the EnvFile with the filesystem. // Try to reconcile the EnvFile with the filesystem.
@@ -151,7 +173,7 @@ func (file *EnvFile) Sync() (result EnvFileSyncResult, err error) {
} }
} }
// Update the EnvFile using the file system // Update the EnvFile using the file system.
func (file *EnvFile) Backup() error { func (file *EnvFile) Backup() error {
// Read the contents of the file // Read the contents of the file
contents, err := os.ReadFile(file.Path) contents, err := os.ReadFile(file.Path)