From bc4208abacbe72519b4475c78f7618e89304acb5 Mon Sep 17 00:00:00 2001 From: Jaap de Haan <261428+jdehaan@users.noreply.github.com> Date: Sun, 16 Nov 2025 09:07:45 +0000 Subject: [PATCH] fix: Use Cobra's Flag Inheritance A former refactoring to avoid global variables was wrong and broke the parser --- backup/cmd/backup.go | 5 ++++- backup/cmd/check.go | 3 ++- backup/cmd/config.go | 4 +++- backup/cmd/root.go | 18 +++++------------- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/backup/cmd/backup.go b/backup/cmd/backup.go index c736565..445e925 100644 --- a/backup/cmd/backup.go +++ b/backup/cmd/backup.go @@ -55,11 +55,12 @@ func listCommands(cfg internal.Config) { } } -func AddBackupCommands(rootCmd *cobra.Command, configPath string) { +func AddBackupCommands(rootCmd *cobra.Command) { var runCmd = &cobra.Command{ Use: "run", Short: "Execute the sync jobs", Run: func(cmd *cobra.Command, args []string) { + configPath, _ := cmd.Flags().GetString("config") cfg := internal.LoadResolvedConfig(configPath) executeSyncJobs(cfg, false) }, @@ -69,6 +70,7 @@ func AddBackupCommands(rootCmd *cobra.Command, configPath string) { Use: "simulate", Short: "Simulate the sync jobs", Run: func(cmd *cobra.Command, args []string) { + configPath, _ := cmd.Flags().GetString("config") cfg := internal.LoadResolvedConfig(configPath) executeSyncJobs(cfg, true) }, @@ -78,6 +80,7 @@ func AddBackupCommands(rootCmd *cobra.Command, configPath string) { Use: "list", Short: "List the commands that will be executed", Run: func(cmd *cobra.Command, args []string) { + configPath, _ := cmd.Flags().GetString("config") cfg := internal.LoadResolvedConfig(configPath) listCommands(cfg) }, diff --git a/backup/cmd/check.go b/backup/cmd/check.go index b611c5e..e83b644 100644 --- a/backup/cmd/check.go +++ b/backup/cmd/check.go @@ -9,13 +9,14 @@ import ( "github.com/spf13/cobra" ) -func AddCheckCommands(rootCmd *cobra.Command, configPath string) { +func AddCheckCommands(rootCmd *cobra.Command) { var fs = afero.NewOsFs() var checkCmd = &cobra.Command{ Use: "check-coverage", Short: "Check path coverage", Run: func(cmd *cobra.Command, args []string) { + configPath, _ := cmd.Flags().GetString("config") cfg := internal.LoadResolvedConfig(configPath) uncoveredPaths := internal.ListUncoveredPaths(fs, cfg) diff --git a/backup/cmd/config.go b/backup/cmd/config.go index d3ba07c..05bff0e 100644 --- a/backup/cmd/config.go +++ b/backup/cmd/config.go @@ -10,7 +10,7 @@ import ( ) // AddConfigCommands binds the config command and its subcommands to the root command. -func AddConfigCommands(rootCmd *cobra.Command, configPath string) { +func AddConfigCommands(rootCmd *cobra.Command) { // configCmd represents the config command. var configCmd = &cobra.Command{ Use: "config", @@ -26,6 +26,7 @@ func AddConfigCommands(rootCmd *cobra.Command, configPath string) { Use: "show", Short: "Show resolved configuration", Run: func(cmd *cobra.Command, args []string) { + configPath, _ := cmd.Flags().GetString("config") cfg := internal.LoadResolvedConfig(configPath) out, err := yaml.Marshal(cfg) @@ -42,6 +43,7 @@ func AddConfigCommands(rootCmd *cobra.Command, configPath string) { Use: "validate", Short: "Validate configuration", Run: func(cmd *cobra.Command, args []string) { + configPath, _ := cmd.Flags().GetString("config") internal.LoadResolvedConfig(configPath) fmt.Println("Configuration is valid.") }, diff --git a/backup/cmd/root.go b/backup/cmd/root.go index 1505d84..8dfcfca 100644 --- a/backup/cmd/root.go +++ b/backup/cmd/root.go @@ -9,27 +9,19 @@ import ( // Execute adds all child commands to the root command and sets flags appropriately. func Execute() { - var configPath string - rootCmd := &cobra.Command{ Use: "backup-tool", Short: "A tool for managing backups", Long: `backup-tool is a CLI tool for managing backups and configurations.`, } - rootCmd.PersistentFlags().StringVar(&configPath, "config", "config.yaml", "Path to the configuration file") - - // Parse flags before adding commands to ensure configPath is available. - err := rootCmd.ParseFlags(os.Args[1:]) - if err != nil { - os.Exit(1) - } + rootCmd.PersistentFlags().String("config", "config.yaml", "Path to the configuration file") - AddConfigCommands(rootCmd, configPath) - AddBackupCommands(rootCmd, configPath) - AddCheckCommands(rootCmd, configPath) + AddConfigCommands(rootCmd) + AddBackupCommands(rootCmd) + AddCheckCommands(rootCmd) - err = rootCmd.Execute() + err := rootCmd.Execute() if err != nil { os.Exit(1) }