From 13a0cd1055b34a93e06b429ca75492ceb8ca1434 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 9 Jul 2025 12:38:32 +0300 Subject: feat: add --batch-run flag for weekly automated sync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- cmd/gitsyncer/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'cmd') 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) } -- cgit v1.2.3