mirror of https://github.com/databricks/cli.git
Complete argument for the environment flag (#221)
Command completion can be configured through `bricks completion`.
This commit is contained in:
parent
dd95668474
commit
ae9d6883ee
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/databricks/bricks/bundle"
|
"github.com/databricks/bricks/bundle"
|
||||||
"github.com/databricks/bricks/bundle/config/mutator"
|
"github.com/databricks/bricks/bundle/config/mutator"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
const envName = "DATABRICKS_BUNDLE_ENV"
|
const envName = "DATABRICKS_BUNDLE_ENV"
|
||||||
|
@ -25,9 +26,30 @@ func getEnvironment(cmd *cobra.Command) (value string) {
|
||||||
return os.Getenv(envName)
|
return os.Getenv(envName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// configureBundle loads the bundle configuration and configures it on the command's context.
|
// 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 {
|
func configureBundle(cmd *cobra.Command, args []string, load func() (*bundle.Bundle, error)) error {
|
||||||
b, err := load()
|
b, err := loadBundle(cmd, args, load)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -37,16 +59,16 @@ func configureBundle(cmd *cobra.Command, args []string, load func() (*bundle.Bun
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ms := mutator.DefaultMutators()
|
var m bundle.Mutator
|
||||||
env := getEnvironment(cmd)
|
env := getEnvironment(cmd)
|
||||||
if env == "" {
|
if env == "" {
|
||||||
ms = append(ms, mutator.SelectDefaultEnvironment())
|
m = mutator.SelectDefaultEnvironment()
|
||||||
} else {
|
} else {
|
||||||
ms = append(ms, mutator.SelectEnvironment(env))
|
m = mutator.SelectEnvironment(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := cmd.Context()
|
ctx := cmd.Context()
|
||||||
err = bundle.Apply(ctx, b, ms)
|
err = bundle.Apply(ctx, b, []bundle.Mutator{m})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -66,7 +88,19 @@ func TryConfigureBundle(cmd *cobra.Command, args []string) error {
|
||||||
return configureBundle(cmd, args, bundle.TryLoad)
|
return configureBundle(cmd, args, bundle.TryLoad)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// To operate in the context of a bundle, all commands must take an "environment" parameter.
|
// 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)")
|
RootCmd.PersistentFlags().StringP("environment", "e", "", "bundle environment to use (if applicable)")
|
||||||
|
RootCmd.RegisterFlagCompletionFunc("environment", environmentCompletion)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue