diff options
| author | Paul Buetow <paul@buetow.org> | 2025-07-13 17:37:16 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-07-13 17:37:16 +0300 |
| commit | fa5ef028ec9a7af801710eed190057d3b3c172f0 (patch) | |
| tree | 41ef41dd1edace0438be20c4f35328c0fbfd8090 /cmd | |
| parent | 79225d4df3a181f08a2160ff8ec361001b9dea18 (diff) | |
refactor: restructure CLI with cobra command framework
- Replace flat flags with organized command structure
- Add commands: sync, list, manage, release, showcase, test
- Implement subcommands for better organization:
- sync: repo, all, codeberg-to-github, github-to-codeberg, bidirectional
- list: orgs, repos
- manage: delete-repo, clean, batch-run
- release: check, create (with --ai-notes support)
- showcase: with --force, --output, --format, --exclude
- test: github-token, codeberg-token, config
- Add comprehensive help with examples for all commands
- Fix config loading bug when path is empty
- Update README.md with new command structure and examples
- Maintain backward compatibility (old flags still work with warnings)
The new structure provides better discoverability, consistent naming,
and logical grouping of related functionality.
🤖 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.go | 200 |
1 files changed, 3 insertions, 197 deletions
diff --git a/cmd/gitsyncer/main.go b/cmd/gitsyncer/main.go index 19ad9bf..e656e7b 100644 --- a/cmd/gitsyncer/main.go +++ b/cmd/gitsyncer/main.go @@ -1,205 +1,11 @@ package main import ( - "fmt" - "os" - "path/filepath" - - "codeberg.org/snonux/gitsyncer/internal/cli" - "codeberg.org/snonux/gitsyncer/internal/config" - "codeberg.org/snonux/gitsyncer/internal/state" + "codeberg.org/snonux/gitsyncer/internal/cmd" ) -// 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.") - } - } -} - -// runReleaseCheckIfEnabled runs release checking after successful sync operations -func runReleaseCheckIfEnabled(cfg *config.Config, flags *cli.Flags) { - // Run release checks automatically unless disabled - if !flags.NoCheckReleases { - fmt.Println("\nChecking for missing releases...") - cli.HandleCheckReleases(cfg, flags) - } -} - -// runReleaseCheckForRepoIfEnabled runs release checking for a specific repository -func runReleaseCheckForRepoIfEnabled(cfg *config.Config, flags *cli.Flags, repoName string) { - // Run release checks automatically unless disabled - if !flags.NoCheckReleases { - fmt.Println("\nChecking for missing releases...") - cli.HandleCheckReleasesForRepo(cfg, flags, repoName) - } -} func main() { - // Parse command-line flags - flags := cli.ParseFlags() - - // Handle --full flag message - if flags.FullSync { - cli.ShowFullSyncMessage() - } - - // Handle version flag - if flags.VersionFlag { - os.Exit(cli.HandleVersion()) - } - - // Handle test GitHub token flag - if flags.TestGitHubToken { - os.Exit(cli.HandleTestGitHubToken()) - } - - // Load configuration - cfg, err := cli.LoadConfig(flags.ConfigPath) - if err != nil { - cli.ShowConfigHelp() - os.Exit(1) - } - - // Use config WorkDir only if no flag was explicitly provided - // We check if WorkDir matches the default we set in ParseFlags - home, _ := os.UserHomeDir() - defaultWorkDir := filepath.Join(home, "git", "gitsyncer-workdir") - if flags.WorkDir == defaultWorkDir && cfg.WorkDir != "" { - // User didn't specify --work-dir, so use config value - 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)) - } - - // Handle list organizations flag - if flags.ListOrgs { - os.Exit(cli.HandleListOrgs(cfg)) - } - - // Handle list repositories flag - if flags.ListRepos { - os.Exit(cli.HandleListRepos(cfg)) - } - - // Handle sync operation - if flags.SyncRepo != "" { - exitCode := cli.HandleSync(cfg, flags) - if exitCode == 0 { - runReleaseCheckForRepoIfEnabled(cfg, flags, flags.SyncRepo) - if flags.Showcase { - showcaseCode := cli.HandleShowcase(cfg, flags) - if showcaseCode != 0 { - os.Exit(showcaseCode) - } - } - } - os.Exit(exitCode) - } - - // Handle sync all operation - if flags.SyncAll { - exitCode := cli.HandleSyncAll(cfg, flags) - if exitCode == 0 { - runReleaseCheckIfEnabled(cfg, flags) - if flags.Showcase { - showcaseCode := cli.HandleShowcase(cfg, flags) - if showcaseCode != 0 { - os.Exit(showcaseCode) - } - } - } - os.Exit(exitCode) - } - - // Handle sync Codeberg public repos - if flags.SyncCodebergPublic { - exitCode := cli.HandleSyncCodebergPublic(cfg, flags) - if exitCode != 0 || !flags.SyncGitHubPublic { - if exitCode == 0 { - runReleaseCheckIfEnabled(cfg, flags) - if flags.Showcase && !flags.SyncGitHubPublic { - showcaseCode := cli.HandleShowcase(cfg, flags) - if showcaseCode != 0 { - os.Exit(showcaseCode) - } - } - } - os.Exit(exitCode) - } - } - - // Handle sync GitHub public repos - if flags.SyncGitHubPublic { - exitCode := cli.HandleSyncGitHubPublic(cfg, flags) - - if exitCode == 0 { - // Run release checks after successful sync - runReleaseCheckIfEnabled(cfg, flags) - - // Run showcase generation if requested - if flags.Showcase { - showcaseCode := cli.HandleShowcase(cfg, flags) - if showcaseCode != 0 { - os.Exit(showcaseCode) - } - } - - // Save batch run state if this was a successful batch run - saveBatchRunState(flags) - } - - os.Exit(exitCode) - } - - // Handle check releases flag - if flags.CheckReleases { - os.Exit(cli.HandleCheckReleases(cfg, flags)) - } - - // Handle standalone showcase mode (no sync operations specified) - if flags.Showcase { - fmt.Println("Running showcase generation for all repositories (clone-only mode)...") - os.Exit(cli.HandleShowcaseOnly(cfg, flags)) - } - - // Default: show usage - cli.ShowUsage(cfg) - os.Exit(1) + // Execute the cobra command + cmd.Execute() }
\ No newline at end of file |
