summaryrefslogtreecommitdiff
path: root/internal/cmd/sync.go
blob: abedb7e3e31e8cb7cf0a048e9a96161aa72dc2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
	noAIReleaseNotes 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 (AI release notes enabled by default)
  gitsyncer sync repo myproject
  
  # Sync with backup locations
  gitsyncer sync repo myproject --backup
  
  # Preview what would be synced
  gitsyncer sync repo myproject --dry-run
  
  # Sync without AI-generated release notes
  gitsyncer sync repo myproject --no-ai-release-notes`,
	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")
	syncCmd.PersistentFlags().BoolVar(&autoCreate, "auto-create-releases", false, "automatically create releases without confirmation")
	syncCmd.PersistentFlags().BoolVar(&noAIReleaseNotes, "no-ai-release-notes", false, "disable AI-generated release notes (AI notes are enabled by default)")
}

func buildFlags() *cli.Flags {
	return &cli.Flags{
		ConfigPath:   cfgFile,
		WorkDir:      workDir,
		DryRun:       dryRun,
		Backup:       backup,
		NoCheckReleases: noReleases,
		AutoCreateReleases: autoCreate,
		AIReleaseNotes: !noAIReleaseNotes,
		CreateGitHubRepos: createRepos,
		CreateCodebergRepos: createRepos,
	}
}