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
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config/mutator"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/bundle/env"
|
|
|
|
envlib "github.com/databricks/cli/libs/env"
|
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
|
|
|
)
|
|
|
|
|
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 {
|
2023-08-28 17:05:55 +00:00
|
|
|
value = oldFlag.Value.String()
|
2023-08-17 15:22:32 +00:00
|
|
|
if value != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:05:57 +00:00
|
|
|
// If it's not set, use the environment variable.
|
2023-09-11 08:18:43 +00:00
|
|
|
target, _ := env.Target(cmd.Context())
|
2023-08-17 15:22:32 +00:00
|
|
|
return target
|
2023-01-27 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:01:08 +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.
|
2023-09-11 08:18:43 +00:00
|
|
|
return envlib.Get(cmd.Context(), "DATABRICKS_CONFIG_PROFILE")
|
2023-07-12 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
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-30 14:01:08 +00:00
|
|
|
profile := getProfile(cmd)
|
2023-07-12 12:09:25 +00:00
|
|
|
if profile != "" {
|
Use dynamic configuration model in bundles (#1098)
## Changes
This is a fundamental change to how we load and process bundle
configuration. We now depend on the configuration being represented as a
`dyn.Value`. This representation is functionally equivalent to Go's
`any` (it is variadic) and allows us to capture metadata associated with
a value, such as where it was defined (e.g. file, line, and column). It
also allows us to represent Go's zero values properly (e.g. empty
string, integer equal to 0, or boolean false).
Using this representation allows us to let the configuration model
deviate from the typed structure we have been relying on so far
(`config.Root`). We need to deviate from these types when using
variables for fields that are not a string themselves. For example,
using `${var.num_workers}` for an integer `workers` field was impossible
until now (though not implemented in this change).
The loader for a `dyn.Value` includes functionality to capture any and
all type mismatches between the user-defined configuration and the
expected types. These mismatches can be surfaced as validation errors in
future PRs.
Given that many mutators expect the typed struct to be the source of
truth, this change converts between the dynamic representation and the
typed representation on mutator entry and exit. Existing mutators can
continue to modify the typed representation and these modifications are
reflected in the dynamic representation (see `MarkMutatorEntry` and
`MarkMutatorExit` in `bundle/config/root.go`).
Required changes included in this change:
* The existing interpolation package is removed in favor of
`libs/dyn/dynvar`.
* Functionality to merge job clusters, job tasks, and pipeline clusters
are now all broken out into their own mutators.
To be implemented later:
* Allow variable references for non-string types.
* Surface diagnostics about the configuration provided by the user in
the validation output.
* Some mutators use a resource's configuration file path to resolve
related relative paths. These depend on `bundle/config/paths.Path` being
set and populated through `ConfigureConfigFilePath`. Instead, they
should interact with the dynamically typed configuration directly. Doing
this also unlocks being able to differentiate different base paths used
within a job (e.g. a task override with a relative path defined in a
directory other than the base job).
## Tests
* Existing unit tests pass (some have been modified to accommodate)
* Integration tests pass
2024-02-16 19:41:58 +00:00
|
|
|
err = bundle.ApplyFunc(ctx, b, func(ctx context.Context, b *bundle.Bundle) error {
|
|
|
|
b.Config.Workspace.Profile = profile
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-12 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|