2023-01-27 16:05:57 +00:00
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
2023-08-11 12:28:05 +00:00
|
|
|
"context"
|
2023-01-27 16:05:57 +00:00
|
|
|
"os"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config/mutator"
|
2023-01-27 16:05:57 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-02-20 20:56:31 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-01-27 16:05:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const envName = "DATABRICKS_BUNDLE_ENV"
|
2023-08-17 15:22:32 +00:00
|
|
|
const targetName = "DATABRICKS_BUNDLE_TARGET"
|
2023-01-27 16:05:57 +00:00
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// getTarget returns the name of the target to operate in.
|
|
|
|
func getTarget(cmd *cobra.Command) (value string) {
|
2023-01-27 16:05:57 +00:00
|
|
|
// The command line flag takes precedence.
|
2023-08-17 15:22:32 +00:00
|
|
|
flag := cmd.Flag("target")
|
2023-01-27 16:05:57 +00:00
|
|
|
if flag != nil {
|
|
|
|
value = flag.Value.String()
|
|
|
|
if value != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
oldFlag := cmd.Flag("environment")
|
|
|
|
if oldFlag != nil {
|
|
|
|
value = flag.Value.String()
|
|
|
|
if value != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:05:57 +00:00
|
|
|
// If it's not set, use the environment variable.
|
2023-08-17 15:22:32 +00:00
|
|
|
target := os.Getenv(targetName)
|
|
|
|
// If target env is not set with a new variable, try to check for old variable name
|
|
|
|
// TODO: remove when environments section is not supported anymore
|
|
|
|
if target == "" {
|
|
|
|
target = os.Getenv(envName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return target
|
2023-01-27 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
func GetProfile(cmd *cobra.Command) (value string) {
|
2023-07-12 12:09:25 +00:00
|
|
|
// The command line flag takes precedence.
|
|
|
|
flag := cmd.Flag("profile")
|
|
|
|
if flag != nil {
|
|
|
|
value = flag.Value.String()
|
|
|
|
if value != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not set, use the environment variable.
|
|
|
|
return os.Getenv("DATABRICKS_CONFIG_PROFILE")
|
|
|
|
}
|
|
|
|
|
2023-02-20 20:56:31 +00:00
|
|
|
// loadBundle loads the bundle configuration and applies default mutators.
|
2023-08-11 12:28:05 +00:00
|
|
|
func loadBundle(cmd *cobra.Command, args []string, load func(ctx context.Context) (*bundle.Bundle, error)) (*bundle.Bundle, error) {
|
|
|
|
ctx := cmd.Context()
|
|
|
|
b, err := load(ctx)
|
2023-02-20 20:56:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No bundle is fine in case of `TryConfigureBundle`.
|
|
|
|
if b == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
profile := GetProfile(cmd)
|
2023-07-12 12:09:25 +00:00
|
|
|
if profile != "" {
|
|
|
|
b.Config.Workspace.Profile = profile
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err = bundle.Apply(ctx, b, bundle.Seq(mutator.DefaultMutators()...))
|
2023-02-20 20:56:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:05:57 +00:00
|
|
|
// configureBundle loads the bundle configuration and configures it on the command's context.
|
2023-08-11 12:28:05 +00:00
|
|
|
func configureBundle(cmd *cobra.Command, args []string, load func(ctx context.Context) (*bundle.Bundle, error)) error {
|
2023-02-20 20:56:31 +00:00
|
|
|
b, err := loadBundle(cmd, args, load)
|
2023-01-27 16:05:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No bundle is fine in case of `TryConfigureBundle`.
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-20 20:56:31 +00:00
|
|
|
var m bundle.Mutator
|
2023-08-17 15:22:32 +00:00
|
|
|
env := getTarget(cmd)
|
2023-01-27 16:05:57 +00:00
|
|
|
if env == "" {
|
2023-08-17 15:22:32 +00:00
|
|
|
m = mutator.SelectDefaultTarget()
|
2023-01-27 16:05:57 +00:00
|
|
|
} else {
|
2023-08-17 15:22:32 +00:00
|
|
|
m = mutator.SelectTarget(env)
|
2023-01-27 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := cmd.Context()
|
2023-05-24 12:45:19 +00:00
|
|
|
err = bundle.Apply(ctx, b, m)
|
2023-01-27 16:05:57 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// targetCompletion executes to autocomplete the argument to the target flag.
|
|
|
|
func targetCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-02-20 20:56:31 +00:00
|
|
|
b, err := loadBundle(cmd, args, bundle.MustLoad)
|
|
|
|
if err != nil {
|
|
|
|
cobra.CompErrorln(err.Error())
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
return maps.Keys(b.Config.Targets), cobra.ShellCompDirectiveDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTargetFlag(cmd *cobra.Command) {
|
|
|
|
// To operate in the context of a bundle, all commands must take an "target" parameter.
|
|
|
|
cmd.PersistentFlags().StringP("target", "t", "", "bundle target to use (if applicable)")
|
|
|
|
cmd.RegisterFlagCompletionFunc("target", targetCompletion)
|
2023-02-20 20:56:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// DEPRECATED flag
|
2023-07-26 11:17:09 +00:00
|
|
|
func initEnvironmentFlag(cmd *cobra.Command) {
|
2023-01-27 16:05:57 +00:00
|
|
|
// To operate in the context of a bundle, all commands must take an "environment" parameter.
|
2023-08-17 15:22:32 +00:00
|
|
|
cmd.PersistentFlags().StringP("environment", "e", "", "bundle target to use (if applicable)")
|
|
|
|
cmd.PersistentFlags().MarkDeprecated("environment", "use --target flag instead")
|
|
|
|
cmd.RegisterFlagCompletionFunc("environment", targetCompletion)
|
2023-01-27 16:05:57 +00:00
|
|
|
}
|