refactor: Rewrote in golang.

This commit is contained in:
2025-11-03 16:19:17 -05:00
parent e38f8a4c8f
commit d6ef585a45
27 changed files with 1945 additions and 343 deletions

62
cmd/sync.go Normal file
View File

@@ -0,0 +1,62 @@
package cmd
import (
"fmt"
"github.com/sbrow/envr/app"
"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",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := app.Open()
if err != nil {
return err
} else {
defer db.Close(app.Write)
files, err := db.List()
if err != nil {
return err
} else {
for _, file := range files {
fmt.Printf("%s\n", file.Path)
// Syncronize the filesystem with the database.
changed, err := file.Sync()
switch changed {
case app.Updated:
fmt.Printf("File updated - changes saved\n")
if err := db.Insert(file); err != nil {
return err
}
case app.Restored:
fmt.Printf("File missing - restored backup\n")
case app.Error:
if err == nil {
panic("err cannot be nil when Sync returns Error")
} else {
fmt.Printf("%s\n", err)
}
case app.Noop:
fmt.Println("Nothing to do")
default:
panic("Unknown result")
}
fmt.Println("")
}
return nil
}
}
},
}
func init() {
rootCmd.AddCommand(syncCmd)
}