diff --git a/cmd/aitools/install_test.go b/cmd/aitools/install_test.go index f98e0697e79..0b80056009f 100644 --- a/cmd/aitools/install_test.go +++ b/cmd/aitools/install_test.go @@ -1087,9 +1087,7 @@ func TestInstallOutputJSONThroughRoot(t *testing.T) { var out, errOut bytes.Buffer cli.SetOut(&out) cli.SetErr(&errOut) - cli.SetArgs([]string{"install", "--agents", "codex", "--scope", "global", "--output", "json"}) - - err := root.Execute(ctx, cli) + err := root.Execute(ctx, cli, []string{"install", "--agents", "codex", "--scope", "global", "--output", "json"}) require.ErrorIs(t, err, root.ErrAlreadyPrinted) assert.NotContains(t, errOut.String(), "Error:") diff --git a/cmd/configure/configure_test.go b/cmd/configure/configure_test.go index 3f3d7ea8633..44d75999522 100644 --- a/cmd/configure/configure_test.go +++ b/cmd/configure/configure_test.go @@ -56,9 +56,7 @@ func TestDefaultConfigureNoInteractive(t *testing.T) { os.Stdin = inp cmd := cmd.New(ctx) - cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"}) - - err := root.Execute(ctx, cmd) + err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host"}) assert.NoError(t, err) cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") @@ -90,9 +88,7 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) { os.Stdin = inp cmd := cmd.New(ctx) - cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"}) - - err := root.Execute(ctx, cmd) + err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host"}) assert.NoError(t, err) _, err = os.Stat(cfgPath) @@ -130,9 +126,7 @@ func TestEnvVarsConfigureNoInteractive(t *testing.T) { t.Setenv("DATABRICKS_METADATA_SERVICE_URL", "https://metadata") cmd := cmd.New(ctx) - cmd.SetArgs([]string{"configure", "--token"}) - - err := root.Execute(ctx, cmd) + err := root.Execute(ctx, cmd, []string{"configure", "--token"}) assert.NoError(t, err) _, err = os.Stat(cfgPath) @@ -163,9 +157,7 @@ func TestEnvVarsConfigureNoArgsNoInteractive(t *testing.T) { t.Setenv("DATABRICKS_TOKEN", "secret") cmd := cmd.New(ctx) - cmd.SetArgs([]string{"configure"}) - - err := root.Execute(ctx, cmd) + err := root.Execute(ctx, cmd, []string{"configure"}) assert.NoError(t, err) _, err = os.Stat(cfgPath) @@ -194,9 +186,7 @@ func TestEnvProfileConfigureNoInteractive(t *testing.T) { t.Setenv("DATABRICKS_CONFIG_PROFILE", "FROM-ENV") cmd := cmd.New(ctx) - cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"}) - - err := root.Execute(ctx, cmd) + err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host"}) assert.NoError(t, err) cfg, err := ini.Load(cfgPath) @@ -222,9 +212,7 @@ func TestProfileShorthandOverridesEnvConfigureNoInteractive(t *testing.T) { t.Setenv("DATABRICKS_CONFIG_PROFILE", "FROM-ENV") cmd := cmd.New(ctx) - cmd.SetArgs([]string{"configure", "--token", "--host", "https://host", "-p", "FROM-FLAG"}) - - err := root.Execute(ctx, cmd) + err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host", "-p", "FROM-FLAG"}) assert.NoError(t, err) cfg, err := ini.Load(cfgPath) @@ -251,9 +239,7 @@ func TestCustomProfileConfigureNoInteractive(t *testing.T) { os.Stdin = inp cmd := cmd.New(ctx) - cmd.SetArgs([]string{"configure", "--token", "--host", "https://host", "--profile", "CUSTOM"}) - - err := root.Execute(ctx, cmd) + err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host", "--profile", "CUSTOM"}) assert.NoError(t, err) _, err = os.Stat(cfgPath) diff --git a/cmd/root/bundle_test.go b/cmd/root/bundle_test.go index 6116003ea70..ae98df4b028 100644 --- a/cmd/root/bundle_test.go +++ b/cmd/root/bundle_test.go @@ -387,10 +387,9 @@ workspace: func TestTargetFlagFull(t *testing.T) { cmd := emptyCommand(t) initTargetFlag(cmd) - cmd.SetArgs([]string{"version", "--target", "development"}) ctx := t.Context() - err := Execute(ctx, cmd) + err := Execute(ctx, cmd, []string{"version", "--target", "development"}) assert.NoError(t, err) assert.Equal(t, "development", getTarget(cmd)) @@ -399,10 +398,9 @@ func TestTargetFlagFull(t *testing.T) { func TestTargetFlagShort(t *testing.T) { cmd := emptyCommand(t) initTargetFlag(cmd) - cmd.SetArgs([]string{"version", "-t", "production"}) ctx := t.Context() - err := Execute(ctx, cmd) + err := Execute(ctx, cmd, []string{"version", "-t", "production"}) assert.NoError(t, err) assert.Equal(t, "production", getTarget(cmd)) @@ -413,10 +411,9 @@ func TestTargetEnvironmentFlag(t *testing.T) { cmd := emptyCommand(t) initTargetFlag(cmd) initEnvironmentFlag(cmd) - cmd.SetArgs([]string{"version", "--environment", "development"}) ctx := t.Context() - err := Execute(ctx, cmd) + err := Execute(ctx, cmd, []string{"version", "--environment", "development"}) assert.NoError(t, err) assert.Equal(t, "development", getTarget(cmd)) diff --git a/cmd/root/root.go b/cmd/root/root.go index 59b9777ef69..52e14eae0e7 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -114,9 +114,11 @@ func flagErrorFunc(c *cobra.Command, err error) error { return fmt.Errorf("%w\n\n%s", err, c.UsageString()) } -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute(ctx context.Context, cmd *cobra.Command) (err error) { +// Execute runs cmd with args, which select the subcommand to run. It is called +// by main.main() with the arguments after the program name. Unlike +// [cobra.Command.SetArgs], nil args mean no arguments rather than the process +// arguments. +func Execute(ctx context.Context, cmd *cobra.Command, args []string) (err error) { defer func() { r := recover() @@ -152,6 +154,12 @@ Stack Trace: // Set a command execution ID value in the context ctx = cmdctx.GenerateExecId(ctx) + // Cobra runs with the process arguments if args are nil. + if args == nil { + args = []string{} + } + cmd.SetArgs(args) + startTime := time.Now() // Run the command diff --git a/cmd/root/root_test.go b/cmd/root/root_test.go index ce1584eacd4..c52e7b99dfa 100644 --- a/cmd/root/root_test.go +++ b/cmd/root/root_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "os" "testing" "github.com/databricks/cli/libs/cmdctx" @@ -45,7 +46,7 @@ func TestExecuteEnrichesAuthErrors(t *testing.T) { return nil } - err := Execute(ctx, cmd) + err := Execute(ctx, cmd, nil) require.Error(t, err) output := stderr.String() @@ -71,7 +72,7 @@ func TestExecuteNoEnrichmentWithoutConfigUsed(t *testing.T) { } cmd.SetErr(stderr) - err := Execute(ctx, cmd) + err := Execute(ctx, cmd, nil) require.Error(t, err) output := stderr.String() @@ -144,7 +145,7 @@ func TestExecuteAppendsAccountHostHint(t *testing.T) { return nil } - err := Execute(t.Context(), cmd) + err := Execute(t.Context(), cmd, nil) require.Error(t, err) output := stderr.String() @@ -173,7 +174,24 @@ func TestExecuteErrAlreadyPrintedNotEnriched(t *testing.T) { } cmd.SetErr(stderr) - err := Execute(ctx, cmd) + err := Execute(ctx, cmd, nil) require.Error(t, err) assert.Empty(t, stderr.String()) } + +func TestExecuteWithNilArgsRunsWithoutArgs(t *testing.T) { + processArgs := os.Args + t.Cleanup(func() { os.Args = processArgs }) + os.Args = []string{"databricks", "process-arg"} + + var got []string + cmd := &cobra.Command{ + Use: "test", + RunE: func(cmd *cobra.Command, args []string) error { + got = args + return nil + }, + } + require.NoError(t, Execute(t.Context(), cmd, nil)) + assert.Empty(t, got) +} diff --git a/internal/testcli/runner.go b/internal/testcli/runner.go index 5f2d1a23b75..efd4dfecaaf 100644 --- a/internal/testcli/runner.go +++ b/internal/testcli/runner.go @@ -104,7 +104,6 @@ func (r *Runner) RunBackground() { cli := cmd.New(r.ctx) cli.SetOut(stdoutW) cli.SetErr(stderrW) - cli.SetArgs(r.args) if r.stdinW != nil { cli.SetIn(r.stdinR) } @@ -123,7 +122,7 @@ func (r *Runner) RunBackground() { // Run command in background. go func() { - err := root.Execute(ctx, cli) + err := root.Execute(ctx, cli, r.args) if err != nil { if r.Verbose { r.Logf("Error running command: %s", err) @@ -181,13 +180,12 @@ func (r *Runner) Run() (bytes.Buffer, bytes.Buffer, error) { cli := cmd.New(r.ctx) cli.SetOut(&stdout) cli.SetErr(&stderr) - cli.SetArgs(r.args) if r.Verbose { r.Logf(" args: %s", strings.Join(r.args, ", ")) } - err := root.Execute(r.ctx, cli) + err := root.Execute(r.ctx, cli, r.args) if err != nil { if r.Verbose { r.Logf(" error: %s", err) diff --git a/main.go b/main.go index 6c4dddd40b6..0eb99617b78 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,7 @@ func main() { } ctx := context.Background() - err := root.Execute(ctx, cmd.New(ctx)) + err := root.Execute(ctx, cmd.New(ctx), os.Args[1:]) if err != nil { os.Exit(1) }