Step 6: Restore with timestamp logic and confirm callback.

Restore writes tracked files back to their original locations.
Supports selective path restoration, force mode, and a confirm
callback for files where the on-disk mtime >= manifest timestamp
(truncated to seconds for cross-platform reliability). Creates
parent directories, recreates symlinks, and sets file permissions.

CLI: sgard restore [path...] [--force].
6 new tests (file, permissions, symlink, parent dirs, selective, confirm skip).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-23 21:41:53 -07:00
parent 661c050d83
commit c552a3657f
5 changed files with 407 additions and 5 deletions

46
cmd/sgard/restore.go Normal file
View File

@@ -0,0 +1,46 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/kisom/sgard/garden"
"github.com/spf13/cobra"
)
var forceRestore bool
var restoreCmd = &cobra.Command{
Use: "restore [path...]",
Short: "Restore tracked files to their original locations",
RunE: func(cmd *cobra.Command, args []string) error {
g, err := garden.Open(repoFlag)
if err != nil {
return err
}
confirm := func(path string) bool {
fmt.Printf("Overwrite %s? [y/N] ", path)
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
answer := strings.TrimSpace(strings.ToLower(scanner.Text()))
return answer == "y" || answer == "yes"
}
return false
}
if err := g.Restore(args, forceRestore, confirm); err != nil {
return err
}
fmt.Println("Restore complete.")
return nil
},
}
func init() {
restoreCmd.Flags().BoolVarP(&forceRestore, "force", "f", false, "overwrite without prompting")
rootCmd.AddCommand(restoreCmd)
}