summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-09 12:38:32 +0300
committerPaul Buetow <paul@buetow.org>2025-07-09 12:38:32 +0300
commit13a0cd1055b34a93e06b429ca75492ceb8ca1434 (patch)
tree455cec87470278aca95f3f6f6a85940c4fae2b18 /cmd
parent60ad0b50390d455607c89801ded91f57422fcf8c (diff)
feat: add --batch-run flag for weekly automated sync
- Add --batch-run flag that enables --full and --showcase - Implement state management to track last batch run timestamp - Enforce one-week minimum interval between batch runs - Save state to .gitsyncer-state.json in work directory - Show state file location in output messages - Bump version to 0.5.0 This feature enables automated weekly full synchronization from cron or shell scripts while preventing excessive API usage. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/gitsyncer/main.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/cmd/gitsyncer/main.go b/cmd/gitsyncer/main.go
index 9828776..f27cff6 100644
--- a/cmd/gitsyncer/main.go
+++ b/cmd/gitsyncer/main.go
@@ -6,8 +6,23 @@ import (
"path/filepath"
"codeberg.org/snonux/gitsyncer/internal/cli"
+ "codeberg.org/snonux/gitsyncer/internal/state"
)
+// saveBatchRunState saves the batch run timestamp if this is a batch run
+func saveBatchRunState(flags *cli.Flags) {
+ if flags.BatchRun && flags.BatchRunStateManager != nil && flags.BatchRunState != nil {
+ flags.BatchRunState.UpdateBatchRunTime()
+ if err := flags.BatchRunStateManager.Save(flags.BatchRunState); err != nil {
+ fmt.Fprintf(os.Stderr, "Warning: Failed to save batch run state: %v\n", err)
+ } else {
+ stateFile := filepath.Join(flags.WorkDir, ".gitsyncer-state.json")
+ fmt.Printf("Batch run completed successfully. State saved to: %s\n", stateFile)
+ fmt.Println("Next batch run allowed after one week.")
+ }
+ }
+}
+
func main() {
// Parse command-line flags
flags := cli.ParseFlags()
@@ -43,6 +58,32 @@ func main() {
flags.WorkDir = cfg.WorkDir
}
+ // Handle --batch-run flag: check if it has run within the past week
+ if flags.BatchRun {
+ stateManager := state.NewManager(flags.WorkDir)
+ s, err := stateManager.Load()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Warning: Failed to load state: %v\n", err)
+ // Continue anyway on first run
+ }
+
+ if s.HasRunWithinWeek() {
+ fmt.Printf("Batch run was already executed within the past week (last run: %s).\n", s.LastBatchRun.Format("2006-01-02 15:04:05"))
+ stateFile := filepath.Join(flags.WorkDir, ".gitsyncer-state.json")
+ fmt.Printf("State file location: %s\n", stateFile)
+ fmt.Println("Skipping batch run. Use --full and --showcase directly to force execution.")
+ os.Exit(0)
+ }
+
+ // If we get here, we can proceed with the batch run
+ fmt.Println("Starting weekly batch run (--full --showcase)...")
+
+ // Update the state to record this batch run (we'll save it after successful completion)
+ // Store the state manager for later use
+ flags.BatchRunStateManager = stateManager
+ flags.BatchRunState = s
+ }
+
// Handle delete repository flag
if flags.DeleteRepo != "" {
os.Exit(cli.HandleDeleteRepo(cfg, flags.DeleteRepo))
@@ -108,6 +149,11 @@ func main() {
}
}
+ // Save batch run state if this was a successful batch run
+ if exitCode == 0 {
+ saveBatchRunState(flags)
+ }
+
os.Exit(exitCode)
}