Pass session overrides directly to runCommand

Amolith created

Change summary

cmd/root.go        | 27 +++------------------------
cmd/root_test.go   |  4 ++--
cmd/subcommands.go |  2 +-
3 files changed, 6 insertions(+), 27 deletions(-)

Detailed changes

cmd/root.go 🔗

@@ -61,28 +61,7 @@ var rootCmd = &cobra.Command{
 		// so runCommand does not re-prompt even when preset is "".
 		presetResolved = true
 
-		// If the session collected command-specific overrides
-		// (e.g. restore flow), pass them as raw args so runCommand
-		// merges them and skips its own prompts.
-		var rawArgs []string
-		for k, vals := range overrides {
-			if k == overrideArgumentsKey {
-				// Positional args go after "--".
-				continue
-			}
-			for _, v := range vals {
-				rawArgs = append(rawArgs, "--"+k, v)
-			}
-			if vals == nil {
-				rawArgs = append(rawArgs, "--"+k)
-			}
-		}
-		if args := overrides[overrideArgumentsKey]; len(args) > 0 {
-			rawArgs = append(rawArgs, "--")
-			rawArgs = append(rawArgs, args...)
-		}
-
-		return runCommand(commandName, cmd, rawArgs)
+		return runCommand(commandName, cmd, nil, overrides)
 	},
 }
 
@@ -93,7 +72,7 @@ func registerRootFlags() {
 	flags.StringVarP(&flagConfigFile, "config", "C", "", "path to keld config file")
 }
 
-func runCommand(commandName string, cmd *cobra.Command, rawArgs []string) error {
+func runCommand(commandName string, cmd *cobra.Command, rawArgs []string, sessionOverrides map[string][]string) error {
 	if flagConfigFile != "" {
 		if err := os.Setenv("KELD_CONFIG_FILE", flagConfigFile); err != nil {
 			return fmt.Errorf("setting KELD_CONFIG_FILE: %w", err)
@@ -114,7 +93,7 @@ func runCommand(commandName string, cmd *cobra.Command, rawArgs []string) error
 		}
 	}
 
-	overrides := parsePassthrough(rawArgs)
+	overrides := mergeOverrides(parsePassthrough(rawArgs), sessionOverrides)
 
 	// In interactive mode, fill missing preset/command inputs via prompts.
 	// When the unified session has already resolved the preset (presetResolved),

cmd/root_test.go 🔗

@@ -108,7 +108,7 @@ func TestRunCommandAppliesRootFlagsAndPassthrough(t *testing.T) {
 
 	backup := lookupSubcommand(t, "backup")
 	out, err := captureStdout(t, func() error {
-		return runCommand("backup", backup, []string{"--tag", "daily", "/src"})
+		return runCommand("backup", backup, []string{"--tag", "daily", "/src"}, nil)
 	})
 	if err != nil {
 		t.Fatalf("runCommand returned error: %v", err)
@@ -133,7 +133,7 @@ func TestRunCommandRejectsUnknownPreset(t *testing.T) {
 	setRootFlagValuesForTest(t, "missing", true, configFile)
 
 	backup := lookupSubcommand(t, "backup")
-	err := runCommand("backup", backup, nil)
+	err := runCommand("backup", backup, nil, nil)
 	if err == nil {
 		t.Fatal("expected unknown preset error")
 	}

cmd/subcommands.go 🔗

@@ -44,7 +44,7 @@ func registerSubcommands() {
 				if wantsCommandHelp(args) {
 					return cmd.Help()
 				}
-				return runCommand(commandName, cmd, args)
+				return runCommand(commandName, cmd, args, nil)
 			},
 		}