2022-12-15 14:12:47 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2023-03-21 15:25:18 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/deploy/terraform"
|
|
|
|
"github.com/databricks/cli/bundle/phases"
|
|
|
|
"github.com/databricks/cli/bundle/run"
|
|
|
|
"github.com/databricks/cli/cmd/root"
|
2023-09-11 18:03:12 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/flags"
|
2022-12-15 14:12:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func newRunCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "run [flags] KEY",
|
2023-09-11 18:03:12 +00:00
|
|
|
Short: "Run a resource (e.g. a job or a pipeline)",
|
2022-12-23 14:17:16 +00:00
|
|
|
|
2023-09-11 18:03:12 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
2023-07-27 10:03:08 +00:00
|
|
|
PreRunE: ConfigureBundleWithVariables,
|
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
var runOptions run.Options
|
|
|
|
runOptions.Define(cmd.Flags())
|
|
|
|
|
|
|
|
var noWait bool
|
|
|
|
cmd.Flags().BoolVar(&noWait, "no-wait", false, "Don't wait for the run to complete.")
|
|
|
|
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-09-11 18:03:12 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
b := bundle.Get(ctx)
|
2023-07-12 06:51:54 +00:00
|
|
|
|
2023-09-11 18:03:12 +00:00
|
|
|
err := bundle.Apply(ctx, b, bundle.Seq(
|
2022-12-15 14:12:47 +00:00
|
|
|
phases.Initialize(),
|
2023-03-30 10:01:09 +00:00
|
|
|
terraform.Interpolate(),
|
|
|
|
terraform.Write(),
|
|
|
|
terraform.StatePull(),
|
2023-11-02 11:00:30 +00:00
|
|
|
terraform.Load(terraform.ErrorOnEmptyState),
|
2023-05-24 12:45:19 +00:00
|
|
|
))
|
2022-12-15 14:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:03:12 +00:00
|
|
|
// If no arguments are specified, prompt the user to select something to run.
|
|
|
|
if len(args) == 0 && cmdio.IsInteractive(ctx) {
|
|
|
|
// Invert completions from KEY -> NAME, to NAME -> KEY.
|
|
|
|
inv := make(map[string]string)
|
|
|
|
for k, v := range run.ResourceCompletionMap(b) {
|
|
|
|
inv[v] = k
|
|
|
|
}
|
|
|
|
id, err := cmdio.Select(ctx, inv, "Resource to run")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args = append(args, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("expected a KEY of the resource to run")
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:19:38 +00:00
|
|
|
runner, err := run.Find(b, args[0])
|
2022-12-15 14:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-12 06:51:54 +00:00
|
|
|
runOptions.NoWait = noWait
|
2023-09-11 18:03:12 +00:00
|
|
|
output, err := runner.Run(ctx, &runOptions)
|
2022-12-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
2023-03-21 15:25:18 +00:00
|
|
|
if output != nil {
|
2023-07-26 11:17:09 +00:00
|
|
|
switch root.OutputType(cmd) {
|
2023-03-21 15:25:18 +00:00
|
|
|
case flags.OutputText:
|
|
|
|
resultString, err := output.String()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.OutOrStdout().Write([]byte(resultString))
|
|
|
|
case flags.OutputJSON:
|
|
|
|
b, err := json.MarshalIndent(output, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.OutOrStdout().Write(b)
|
|
|
|
default:
|
2023-07-26 11:17:09 +00:00
|
|
|
return fmt.Errorf("unknown output type %s", root.OutputType(cmd))
|
2023-03-21 15:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
return nil
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2023-02-20 20:55:06 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-02-20 20:55:06 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
|
|
|
|
err := root.MustConfigureBundle(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
cobra.CompErrorln(err.Error())
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
|
|
|
|
// No completion in the context of a bundle.
|
|
|
|
// Source and destination paths are taken from bundle configuration.
|
|
|
|
b := bundle.GetOrNil(cmd.Context())
|
|
|
|
if b == nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
|
|
|
|
return run.ResourceCompletions(b), cobra.ShellCompDirectiveNoFileComp
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|