Skip to content

Commit bc4208a

Browse files
committed
fix: Use Cobra's Flag Inheritance
A former refactoring to avoid global variables was wrong and broke the parser
1 parent 7b933f8 commit bc4208a

4 files changed

Lines changed: 14 additions & 16 deletions

File tree

backup/cmd/backup.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ func listCommands(cfg internal.Config) {
5555
}
5656
}
5757

58-
func AddBackupCommands(rootCmd *cobra.Command, configPath string) {
58+
func AddBackupCommands(rootCmd *cobra.Command) {
5959
var runCmd = &cobra.Command{
6060
Use: "run",
6161
Short: "Execute the sync jobs",
6262
Run: func(cmd *cobra.Command, args []string) {
63+
configPath, _ := cmd.Flags().GetString("config")
6364
cfg := internal.LoadResolvedConfig(configPath)
6465
executeSyncJobs(cfg, false)
6566
},
@@ -69,6 +70,7 @@ func AddBackupCommands(rootCmd *cobra.Command, configPath string) {
6970
Use: "simulate",
7071
Short: "Simulate the sync jobs",
7172
Run: func(cmd *cobra.Command, args []string) {
73+
configPath, _ := cmd.Flags().GetString("config")
7274
cfg := internal.LoadResolvedConfig(configPath)
7375
executeSyncJobs(cfg, true)
7476
},
@@ -78,6 +80,7 @@ func AddBackupCommands(rootCmd *cobra.Command, configPath string) {
7880
Use: "list",
7981
Short: "List the commands that will be executed",
8082
Run: func(cmd *cobra.Command, args []string) {
83+
configPath, _ := cmd.Flags().GetString("config")
8184
cfg := internal.LoadResolvedConfig(configPath)
8285
listCommands(cfg)
8386
},

backup/cmd/check.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12-
func AddCheckCommands(rootCmd *cobra.Command, configPath string) {
12+
func AddCheckCommands(rootCmd *cobra.Command) {
1313
var fs = afero.NewOsFs()
1414

1515
var checkCmd = &cobra.Command{
1616
Use: "check-coverage",
1717
Short: "Check path coverage",
1818
Run: func(cmd *cobra.Command, args []string) {
19+
configPath, _ := cmd.Flags().GetString("config")
1920
cfg := internal.LoadResolvedConfig(configPath)
2021
uncoveredPaths := internal.ListUncoveredPaths(fs, cfg)
2122

backup/cmd/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// AddConfigCommands binds the config command and its subcommands to the root command.
13-
func AddConfigCommands(rootCmd *cobra.Command, configPath string) {
13+
func AddConfigCommands(rootCmd *cobra.Command) {
1414
// configCmd represents the config command.
1515
var configCmd = &cobra.Command{
1616
Use: "config",
@@ -26,6 +26,7 @@ func AddConfigCommands(rootCmd *cobra.Command, configPath string) {
2626
Use: "show",
2727
Short: "Show resolved configuration",
2828
Run: func(cmd *cobra.Command, args []string) {
29+
configPath, _ := cmd.Flags().GetString("config")
2930
cfg := internal.LoadResolvedConfig(configPath)
3031

3132
out, err := yaml.Marshal(cfg)
@@ -42,6 +43,7 @@ func AddConfigCommands(rootCmd *cobra.Command, configPath string) {
4243
Use: "validate",
4344
Short: "Validate configuration",
4445
Run: func(cmd *cobra.Command, args []string) {
46+
configPath, _ := cmd.Flags().GetString("config")
4547
internal.LoadResolvedConfig(configPath)
4648
fmt.Println("Configuration is valid.")
4749
},

backup/cmd/root.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,19 @@ import (
99

1010
// Execute adds all child commands to the root command and sets flags appropriately.
1111
func Execute() {
12-
var configPath string
13-
1412
rootCmd := &cobra.Command{
1513
Use: "backup-tool",
1614
Short: "A tool for managing backups",
1715
Long: `backup-tool is a CLI tool for managing backups and configurations.`,
1816
}
1917

20-
rootCmd.PersistentFlags().StringVar(&configPath, "config", "config.yaml", "Path to the configuration file")
21-
22-
// Parse flags before adding commands to ensure configPath is available.
23-
err := rootCmd.ParseFlags(os.Args[1:])
24-
if err != nil {
25-
os.Exit(1)
26-
}
18+
rootCmd.PersistentFlags().String("config", "config.yaml", "Path to the configuration file")
2719

28-
AddConfigCommands(rootCmd, configPath)
29-
AddBackupCommands(rootCmd, configPath)
30-
AddCheckCommands(rootCmd, configPath)
20+
AddConfigCommands(rootCmd)
21+
AddBackupCommands(rootCmd)
22+
AddCheckCommands(rootCmd)
3123

32-
err = rootCmd.Execute()
24+
err := rootCmd.Execute()
3325
if err != nil {
3426
os.Exit(1)
3527
}

0 commit comments

Comments
 (0)