2023-01-27 16:05:57 +00:00
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-02-20 20:56:31 +00:00
|
|
|
// loadBundle loads the bundle configuration and applies default mutators.
|
|
|
|
func loadBundle(cmd *cobra.Command, args []string, load func() (*bundle.Bundle, error)) (*bundle.Bundle, error) {
|
|
|
|
b, err := load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No bundle is fine in case of `TryConfigureBundle`.
|
|
|
|
if b == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := cmd.Context()
|
|
|
|
err = bundle.Apply(ctx, b, mutator.DefaultMutators())
|
|
|
|
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.
|
|
|
|
func configureBundle(cmd *cobra.Command, args []string, load func() (*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-01-27 16:05:57 +00:00
|
|
|
env := getEnvironment(cmd)
|
|
|
|
if env == "" {
|
2023-02-20 20:56:31 +00:00
|
|
|
m = mutator.SelectDefaultEnvironment()
|
2023-01-27 16:05:57 +00:00
|
|
|
} else {
|
2023-02-20 20:56:31 +00:00
|
|
|
m = mutator.SelectEnvironment(env)
|
2023-01-27 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := cmd.Context()
|
2023-02-20 20:56:31 +00:00
|
|
|
err = bundle.Apply(ctx, b, []bundle.Mutator{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-02-20 20:56:31 +00:00
|
|
|
// environmentCompletion executes to autocomplete the argument to the environment flag.
|
|
|
|
func environmentCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
b, err := loadBundle(cmd, args, bundle.MustLoad)
|
|
|
|
if err != nil {
|
|
|
|
cobra.CompErrorln(err.Error())
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
|
|
|
|
return maps.Keys(b.Config.Environments), cobra.ShellCompDirectiveDefault
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:05:57 +00:00
|
|
|
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)")
|
2023-02-20 20:56:31 +00:00
|
|
|
RootCmd.RegisterFlagCompletionFunc("environment", environmentCompletion)
|
2023-01-27 16:05:57 +00:00
|
|
|
}
|