From 5586121239cddfe58c21466e3185cb613aa42670 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 11 Mar 2025 12:48:25 +0100 Subject: [PATCH] read target from flag --- cmd/bundle/exec.go | 9 +++------ cmd/root/bundle.go | 6 +++--- cmd/root/bundle_test.go | 6 +++--- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cmd/bundle/exec.go b/cmd/bundle/exec.go index 87cc9b929..d781ee493 100644 --- a/cmd/bundle/exec.go +++ b/cmd/bundle/exec.go @@ -7,7 +7,6 @@ import ( "strings" "sync" - "github.com/databricks/cli/bundle/config/mutator" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/auth" "github.com/spf13/cobra" @@ -42,13 +41,11 @@ Example usage: env := auth.ProcessEnv(root.ConfigUsed(cmd.Context())) - // If user has specified a target, pass it to the child command. DABs - // defines a "default" target which is a placeholder for when no target is defined. - // If that's the case, i.e. no targets are defined, then do not pass the target. + // If user has specified a target, pass it to the child command. // // This is only useful for when the Databricks CLI is the child command. - if b.Config.Bundle.Target != mutator.DefaultTargetPlaceholder { - env = append(env, "DATABRICKS_BUNDLE_TARGET="+b.Config.Bundle.Target) + if target := root.GetTarget(cmd); target != "" { + env = append(env, "DATABRICKS_CONFIG_TARGET="+target) } // If the bundle has a profile configured, explicitly pass it to the child command. diff --git a/cmd/root/bundle.go b/cmd/root/bundle.go index b40803707..85d47431d 100644 --- a/cmd/root/bundle.go +++ b/cmd/root/bundle.go @@ -12,8 +12,8 @@ import ( "golang.org/x/exp/maps" ) -// getTarget returns the name of the target to operate in. -func getTarget(cmd *cobra.Command) (value string) { +// GetTarget returns the name of the target to operate in. +func GetTarget(cmd *cobra.Command) (value string) { target, isFlagSet := targetFlagValue(cmd) if isFlagSet { return target @@ -77,7 +77,7 @@ func configureBundle(cmd *cobra.Command, b *bundle.Bundle) (*bundle.Bundle, diag // Load bundle and select target. ctx := cmd.Context() var diags diag.Diagnostics - if target := getTarget(cmd); target == "" { + if target := GetTarget(cmd); target == "" { diags = phases.LoadDefaultTarget(ctx, b) } else { diags = phases.LoadNamedTarget(ctx, b, target) diff --git a/cmd/root/bundle_test.go b/cmd/root/bundle_test.go index 3517b02e4..bf7c44188 100644 --- a/cmd/root/bundle_test.go +++ b/cmd/root/bundle_test.go @@ -215,7 +215,7 @@ func TestTargetFlagFull(t *testing.T) { err := cmd.ExecuteContext(ctx) assert.NoError(t, err) - assert.Equal(t, "development", getTarget(cmd)) + assert.Equal(t, "development", GetTarget(cmd)) } func TestTargetFlagShort(t *testing.T) { @@ -227,7 +227,7 @@ func TestTargetFlagShort(t *testing.T) { err := cmd.ExecuteContext(ctx) assert.NoError(t, err) - assert.Equal(t, "production", getTarget(cmd)) + assert.Equal(t, "production", GetTarget(cmd)) } // TODO: remove when environment flag is fully deprecated @@ -241,5 +241,5 @@ func TestTargetEnvironmentFlag(t *testing.T) { err := cmd.ExecuteContext(ctx) assert.NoError(t, err) - assert.Equal(t, "development", getTarget(cmd)) + assert.Equal(t, "development", GetTarget(cmd)) }