summaryrefslogtreecommitdiff
path: root/internal/cmd/root.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-13 17:37:16 +0300
committerPaul Buetow <paul@buetow.org>2025-07-13 17:37:16 +0300
commitfa5ef028ec9a7af801710eed190057d3b3c172f0 (patch)
tree41ef41dd1edace0438be20c4f35328c0fbfd8090 /internal/cmd/root.go
parent79225d4df3a181f08a2160ff8ec361001b9dea18 (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/root.go')
-rw-r--r--internal/cmd/root.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/internal/cmd/root.go b/internal/cmd/root.go
new file mode 100644
index 0000000..04ea6dd
--- /dev/null
+++ b/internal/cmd/root.go
@@ -0,0 +1,77 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "github.com/spf13/cobra"
+ "codeberg.org/snonux/gitsyncer/internal/config"
+ "codeberg.org/snonux/gitsyncer/internal/version"
+)
+
+var (
+ cfgFile string
+ workDir string
+ cfg *config.Config
+ rootCmd = &cobra.Command{
+ Use: "gitsyncer",
+ Short: "Synchronize git repositories across multiple platforms",
+ Long: `GitSyncer is a tool for synchronizing git repositories between
+multiple organizations (e.g., GitHub and Codeberg). It automatically
+keeps all branches in sync across different git hosting platforms.`,
+ PersistentPreRun: func(cmd *cobra.Command, args []string) {
+ // Skip config loading for version command
+ if cmd.Use == "version" {
+ return
+ }
+
+ // Load configuration
+ var err error
+ cfg, err = config.Load(cfgFile)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error loading configuration: %v\n", err)
+ fmt.Fprintf(os.Stderr, "\nPlease create a configuration file with your organizations and repositories.\n")
+ fmt.Fprintf(os.Stderr, "See 'gitsyncer help' for more information.\n")
+ os.Exit(1)
+ }
+
+ // Use config WorkDir if no flag was explicitly provided
+ if !cmd.Flags().Changed("work-dir") && cfg.WorkDir != "" {
+ workDir = cfg.WorkDir
+ }
+ },
+ }
+)
+
+// Execute runs the root command
+func Execute() {
+ if err := rootCmd.Execute(); err != nil {
+ os.Exit(1)
+ }
+}
+
+func init() {
+ // Global flags
+ rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "configuration file (default: ~/.gitsyncer.json)")
+
+ // Set default work directory
+ home, err := os.UserHomeDir()
+ defaultWorkDir := ".gitsyncer-work"
+ if err == nil {
+ defaultWorkDir = filepath.Join(home, "git", "gitsyncer-workdir")
+ }
+
+ rootCmd.PersistentFlags().StringVarP(&workDir, "work-dir", "w", defaultWorkDir, "working directory for operations")
+
+ // Version command
+ rootCmd.AddCommand(&cobra.Command{
+ Use: "version",
+ Short: "Show version information",
+ Run: func(cmd *cobra.Command, args []string) {
+ fmt.Println(version.GetVersion())
+ },
+ })
+
+}
+