diff --git a/cmd/bundle/debug/whoami.go b/cmd/bundle/debug/whoami.go index 1fe238c8..b3401261 100644 --- a/cmd/bundle/debug/whoami.go +++ b/cmd/bundle/debug/whoami.go @@ -4,14 +4,14 @@ import ( "fmt" "github.com/databricks/bricks/bundle" - parent "github.com/databricks/bricks/cmd/bundle" + "github.com/databricks/bricks/cmd/root" "github.com/spf13/cobra" ) var whoamiCmd = &cobra.Command{ Use: "whoami", - PreRunE: parent.ConfigureBundle, + PreRunE: root.MustConfigureBundle, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() w := bundle.Get(ctx).WorkspaceClient() diff --git a/cmd/bundle/deploy.go b/cmd/bundle/deploy.go index 4bd726fc..5b786c0f 100644 --- a/cmd/bundle/deploy.go +++ b/cmd/bundle/deploy.go @@ -3,6 +3,7 @@ package bundle import ( "github.com/databricks/bricks/bundle" "github.com/databricks/bricks/bundle/phases" + "github.com/databricks/bricks/cmd/root" "github.com/spf13/cobra" ) @@ -10,7 +11,7 @@ var deployCmd = &cobra.Command{ Use: "deploy", Short: "Deploy bundle", - PreRunE: ConfigureBundle, + PreRunE: root.MustConfigureBundle, RunE: func(cmd *cobra.Command, args []string) error { b := bundle.Get(cmd.Context()) return bundle.Apply(cmd.Context(), b, []bundle.Mutator{ diff --git a/cmd/bundle/environment.go b/cmd/bundle/environment.go deleted file mode 100644 index 4eb33822..00000000 --- a/cmd/bundle/environment.go +++ /dev/null @@ -1,24 +0,0 @@ -package bundle - -import ( - "os" - - "github.com/spf13/cobra" -) - -const envName = "DATABRICKS_BUNDLE_ENV" - -// getEnvironment returns the name of the environment to operate in. -func getEnvironment(cmd *cobra.Command) (value string) { - // The command line flag takes precedence. - flag := cmd.Flag("environment") - if flag != nil { - value = flag.Value.String() - if value != "" { - return - } - } - - // If it's not set, use the environment variable. - return os.Getenv(envName) -} diff --git a/cmd/bundle/root.go b/cmd/bundle/root.go index ec2715fa..60f9914d 100644 --- a/cmd/bundle/root.go +++ b/cmd/bundle/root.go @@ -1,8 +1,6 @@ package bundle import ( - "github.com/databricks/bricks/bundle" - "github.com/databricks/bricks/bundle/config/mutator" "github.com/databricks/bricks/cmd/root" "github.com/spf13/cobra" ) @@ -13,39 +11,10 @@ var rootCmd = &cobra.Command{ Short: "Databricks Application Bundles", } -// ConfigureBundle loads the bundle configuration -// and configures it on the command's context. -func ConfigureBundle(cmd *cobra.Command, args []string) error { - ctx := cmd.Context() - b, err := bundle.MustLoad() - if err != nil { - return err - } - - ms := mutator.DefaultMutators() - env := getEnvironment(cmd) - if env == "" { - ms = append(ms, mutator.SelectDefaultEnvironment()) - } else { - ms = append(ms, mutator.SelectEnvironment(env)) - } - - err = bundle.Apply(ctx, b, ms) - if err != nil { - return err - } - - cmd.SetContext(bundle.Context(ctx, b)) - return nil -} - func AddCommand(cmd *cobra.Command) { rootCmd.AddCommand(cmd) } func init() { - // All bundle commands take an "environment" parameter. - rootCmd.PersistentFlags().StringP("environment", "e", "", "Environment to use") - // Add to top level root. root.RootCmd.AddCommand(rootCmd) } diff --git a/cmd/bundle/run.go b/cmd/bundle/run.go index b1cd616b..335c6954 100644 --- a/cmd/bundle/run.go +++ b/cmd/bundle/run.go @@ -5,6 +5,7 @@ import ( "github.com/databricks/bricks/bundle/deploy/terraform" "github.com/databricks/bricks/bundle/phases" "github.com/databricks/bricks/bundle/run" + "github.com/databricks/bricks/cmd/root" "github.com/spf13/cobra" ) @@ -15,7 +16,7 @@ var runCmd = &cobra.Command{ Short: "Run a workload (e.g. a job or a pipeline)", Args: cobra.ExactArgs(1), - PreRunE: ConfigureBundle, + PreRunE: root.MustConfigureBundle, RunE: func(cmd *cobra.Command, args []string) error { b := bundle.Get(cmd.Context()) err := bundle.Apply(cmd.Context(), b, []bundle.Mutator{ diff --git a/cmd/bundle/validate.go b/cmd/bundle/validate.go index d92913c7..58a7d58f 100644 --- a/cmd/bundle/validate.go +++ b/cmd/bundle/validate.go @@ -5,6 +5,7 @@ import ( "github.com/databricks/bricks/bundle" "github.com/databricks/bricks/bundle/phases" + "github.com/databricks/bricks/cmd/root" "github.com/spf13/cobra" ) @@ -12,7 +13,7 @@ var validateCmd = &cobra.Command{ Use: "validate", Short: "Validate configuration", - PreRunE: ConfigureBundle, + PreRunE: root.MustConfigureBundle, RunE: func(cmd *cobra.Command, args []string) error { b := bundle.Get(cmd.Context()) err := bundle.Apply(cmd.Context(), b, []bundle.Mutator{ diff --git a/cmd/root/bundle.go b/cmd/root/bundle.go new file mode 100644 index 00000000..40951f98 --- /dev/null +++ b/cmd/root/bundle.go @@ -0,0 +1,72 @@ +package root + +import ( + "os" + + "github.com/databricks/bricks/bundle" + "github.com/databricks/bricks/bundle/config/mutator" + "github.com/spf13/cobra" +) + +const envName = "DATABRICKS_BUNDLE_ENV" + +// getEnvironment returns the name of the environment to operate in. +func getEnvironment(cmd *cobra.Command) (value string) { + // The command line flag takes precedence. + flag := cmd.Flag("environment") + if flag != nil { + value = flag.Value.String() + if value != "" { + return + } + } + + // If it's not set, use the environment variable. + return os.Getenv(envName) +} + +// configureBundle loads the bundle configuration and configures it on the command's context. +func configureBundle(cmd *cobra.Command, args []string, load func() (*bundle.Bundle, error)) error { + b, err := load() + if err != nil { + return err + } + + // No bundle is fine in case of `TryConfigureBundle`. + if b == nil { + return nil + } + + ms := mutator.DefaultMutators() + env := getEnvironment(cmd) + if env == "" { + ms = append(ms, mutator.SelectDefaultEnvironment()) + } else { + ms = append(ms, mutator.SelectEnvironment(env)) + } + + ctx := cmd.Context() + err = bundle.Apply(ctx, b, ms) + if err != nil { + return err + } + + cmd.SetContext(bundle.Context(ctx, b)) + return nil +} + +// MustConfigureBundle configures a bundle on the command context. +func MustConfigureBundle(cmd *cobra.Command, args []string) error { + return configureBundle(cmd, args, bundle.MustLoad) +} + +// TryConfigureBundle configures a bundle on the command context +// if there is one, but doesn't fail if there isn't one. +func TryConfigureBundle(cmd *cobra.Command, args []string) error { + return configureBundle(cmd, args, bundle.TryLoad) +} + +func init() { + // To operate in the context of a bundle, all commands must take an "environment" parameter. + RootCmd.PersistentFlags().StringP("environment", "e", "", "bundle environment to use (if applicable)") +}