2022-12-15 14:12:47 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2023-03-21 15:25:18 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2022-12-15 14:12:47 +00:00
|
|
|
"github.com/databricks/bricks/bundle"
|
|
|
|
"github.com/databricks/bricks/bundle/deploy/terraform"
|
|
|
|
"github.com/databricks/bricks/bundle/phases"
|
|
|
|
"github.com/databricks/bricks/bundle/run"
|
2023-01-27 16:05:57 +00:00
|
|
|
"github.com/databricks/bricks/cmd/root"
|
2023-03-21 15:25:18 +00:00
|
|
|
"github.com/databricks/bricks/libs/flags"
|
2022-12-15 14:12:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-12-23 14:17:16 +00:00
|
|
|
var runOptions run.Options
|
|
|
|
|
2022-12-15 14:12:47 +00:00
|
|
|
var runCmd = &cobra.Command{
|
2022-12-22 15:19:38 +00:00
|
|
|
Use: "run [flags] KEY",
|
2022-12-15 14:12:47 +00:00
|
|
|
Short: "Run a workload (e.g. a job or a pipeline)",
|
|
|
|
|
2022-12-22 15:19:38 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2023-01-27 16:05:57 +00:00
|
|
|
PreRunE: root.MustConfigureBundle,
|
2022-12-15 14:12:47 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
b := bundle.Get(cmd.Context())
|
|
|
|
err := bundle.Apply(cmd.Context(), b, []bundle.Mutator{
|
|
|
|
phases.Initialize(),
|
|
|
|
terraform.Load(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-03-21 15:25:18 +00:00
|
|
|
output, err := runner.Run(cmd.Context(), &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 {
|
|
|
|
switch outputType {
|
|
|
|
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:
|
|
|
|
return fmt.Errorf("unknown output type %s", outputType)
|
|
|
|
}
|
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
return nil
|
|
|
|
},
|
2023-02-20 20:55:06 +00:00
|
|
|
|
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
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
|
|
|
|
},
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:25:18 +00:00
|
|
|
var outputType flags.Output = flags.OutputText
|
|
|
|
|
2022-12-15 14:12:47 +00:00
|
|
|
func init() {
|
2022-12-23 14:17:16 +00:00
|
|
|
runOptions.Define(runCmd.Flags())
|
2022-12-15 14:12:47 +00:00
|
|
|
rootCmd.AddCommand(runCmd)
|
2023-03-21 15:25:18 +00:00
|
|
|
runCmd.Flags().Var(&outputType, "output", "type of output format")
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|