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 /internal/cmd/sync.go | |
| 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 'internal/cmd/sync.go')
| -rw-r--r-- | internal/cmd/sync.go | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/internal/cmd/sync.go b/internal/cmd/sync.go new file mode 100644 index 0000000..ad67178 --- /dev/null +++ b/internal/cmd/sync.go @@ -0,0 +1,193 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + "codeberg.org/snonux/gitsyncer/internal/cli" +) + +var ( + dryRun bool + backup bool + createRepos bool + noReleases bool + autoCreate bool +) + +var syncCmd = &cobra.Command{ + Use: "sync", + Short: "Synchronize repositories between platforms", + Long: `Synchronize git repositories across multiple platforms. +This command provides various sync operations for keeping repositories +in sync between GitHub, Codeberg, and other configured platforms.`, +} + +var syncRepoCmd = &cobra.Command{ + Use: "repo [name]", + Short: "Sync a specific repository", + Long: `Synchronize a specific repository across all configured organizations.`, + Args: cobra.ExactArgs(1), + Example: ` # Sync a single repository + gitsyncer sync repo myproject + + # Sync with backup locations + gitsyncer sync repo myproject --backup + + # Preview what would be synced + gitsyncer sync repo myproject --dry-run`, + Run: func(cmd *cobra.Command, args []string) { + flags := buildFlags() + flags.SyncRepo = args[0] + + exitCode := cli.HandleSync(cfg, flags) + if exitCode == 0 && !noReleases { + cli.HandleCheckReleasesForRepo(cfg, flags, args[0]) + } + os.Exit(exitCode) + }, +} + +var syncAllCmd = &cobra.Command{ + Use: "all", + Short: "Sync all configured repositories", + Long: `Synchronize all repositories listed in the configuration file.`, + Example: ` # Sync all configured repositories + gitsyncer sync all + + # Include backup locations + gitsyncer sync all --backup + + # Preview changes + gitsyncer sync all --dry-run`, + Run: func(cmd *cobra.Command, args []string) { + flags := buildFlags() + flags.SyncAll = true + + exitCode := cli.HandleSyncAll(cfg, flags) + if exitCode == 0 && !noReleases { + cli.HandleCheckReleases(cfg, flags) + } + os.Exit(exitCode) + }, +} + +var syncCodebergToGitHubCmd = &cobra.Command{ + Use: "codeberg-to-github", + Short: "Sync public Codeberg repos to GitHub", + Long: `Synchronize all public repositories from Codeberg to GitHub.`, + Example: ` # Sync Codeberg public repos to GitHub + gitsyncer sync codeberg-to-github + + # Auto-create missing GitHub repos + gitsyncer sync codeberg-to-github --create-repos + + # Preview what would be synced + gitsyncer sync codeberg-to-github --dry-run`, + Run: func(cmd *cobra.Command, args []string) { + flags := buildFlags() + flags.SyncCodebergPublic = true + + if createRepos || autoCreate { + flags.CreateGitHubRepos = true + } + + exitCode := cli.HandleSyncCodebergPublic(cfg, flags) + if exitCode == 0 && !noReleases { + cli.HandleCheckReleases(cfg, flags) + } + os.Exit(exitCode) + }, +} + +var syncGitHubToCodebergCmd = &cobra.Command{ + Use: "github-to-codeberg", + Short: "Sync public GitHub repos to Codeberg", + Long: `Synchronize all public repositories from GitHub to Codeberg.`, + Example: ` # Sync GitHub public repos to Codeberg + gitsyncer sync github-to-codeberg + + # Auto-create missing Codeberg repos + gitsyncer sync github-to-codeberg --create-repos + + # Preview what would be synced + gitsyncer sync github-to-codeberg --dry-run`, + Run: func(cmd *cobra.Command, args []string) { + flags := buildFlags() + flags.SyncGitHubPublic = true + + if createRepos || autoCreate { + flags.CreateCodebergRepos = true + } + + exitCode := cli.HandleSyncGitHubPublic(cfg, flags) + if exitCode == 0 && !noReleases { + cli.HandleCheckReleases(cfg, flags) + } + os.Exit(exitCode) + }, +} + +var syncBidirectionalCmd = &cobra.Command{ + Use: "bidirectional", + Short: "Full bidirectional sync of all public repos", + Long: `Perform a complete bidirectional synchronization of all public +repositories between GitHub and Codeberg. This is equivalent to the old --full flag.`, + Example: ` # Full bidirectional sync + gitsyncer sync bidirectional + + # Preview what would be synced + gitsyncer sync bidirectional --dry-run + + # Include backup locations + gitsyncer sync bidirectional --backup`, + Run: func(cmd *cobra.Command, args []string) { + flags := buildFlags() + flags.FullSync = true + flags.SyncCodebergPublic = true + flags.SyncGitHubPublic = true + flags.CreateGitHubRepos = true + flags.CreateCodebergRepos = true + + // First sync Codeberg to GitHub + exitCode := cli.HandleSyncCodebergPublic(cfg, flags) + if exitCode != 0 { + os.Exit(exitCode) + } + + // Then sync GitHub to Codeberg + exitCode = cli.HandleSyncGitHubPublic(cfg, flags) + if exitCode == 0 && !noReleases { + cli.HandleCheckReleases(cfg, flags) + } + os.Exit(exitCode) + }, +} + +func init() { + rootCmd.AddCommand(syncCmd) + + // Add subcommands + syncCmd.AddCommand(syncRepoCmd) + syncCmd.AddCommand(syncAllCmd) + syncCmd.AddCommand(syncCodebergToGitHubCmd) + syncCmd.AddCommand(syncGitHubToCodebergCmd) + syncCmd.AddCommand(syncBidirectionalCmd) + + // Sync flags (available for all sync subcommands) + syncCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "preview what would be synced") + syncCmd.PersistentFlags().BoolVar(&backup, "backup", false, "include backup locations") + syncCmd.PersistentFlags().BoolVar(&createRepos, "create-repos", false, "auto-create missing repositories") + syncCmd.PersistentFlags().BoolVar(&noReleases, "no-releases", false, "skip release checking after sync") +} + +func buildFlags() *cli.Flags { + return &cli.Flags{ + ConfigPath: cfgFile, + WorkDir: workDir, + DryRun: dryRun, + Backup: backup, + NoCheckReleases: noReleases, + AutoCreateReleases: autoCreate, + } +}
\ No newline at end of file |
