2022-11-21 14:39:53 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-04-03 15:33:43 +00:00
|
|
|
"fmt"
|
2022-11-21 14:39:53 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-04-18 15:13:16 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/validate"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/phases"
|
2024-07-01 09:01:10 +00:00
|
|
|
"github.com/databricks/cli/bundle/render"
|
2024-02-14 18:04:45 +00:00
|
|
|
"github.com/databricks/cli/cmd/bundle/utils"
|
2024-03-12 14:12:34 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
2024-04-03 15:33:43 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
|
|
|
"github.com/databricks/cli/libs/flags"
|
2022-11-21 14:39:53 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2024-04-03 15:33:43 +00:00
|
|
|
func renderJsonOutput(cmd *cobra.Command, b *bundle.Bundle, diags diag.Diagnostics) error {
|
2024-06-18 15:04:20 +00:00
|
|
|
buf, err := json.MarshalIndent(b.Config.Value().AsAny(), "", " ")
|
2024-04-03 15:33:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.OutOrStdout().Write(buf)
|
|
|
|
return diags.Error()
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func newValidateCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2024-03-28 10:32:34 +00:00
|
|
|
Use: "validate",
|
|
|
|
Short: "Validate configuration",
|
|
|
|
Args: root.NoArgs,
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2024-03-28 10:32:34 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
b, diags := utils.ConfigureBundleWithVariables(cmd)
|
2024-07-01 09:01:10 +00:00
|
|
|
|
|
|
|
if b == nil {
|
|
|
|
if err := diags.Error(); err != nil {
|
|
|
|
return diags.Error()
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invariant failed: returned bundle is nil")
|
|
|
|
}
|
2024-03-28 10:32:34 +00:00
|
|
|
}
|
2023-05-15 09:34:05 +00:00
|
|
|
|
2024-07-01 09:01:10 +00:00
|
|
|
if !diags.HasError() {
|
|
|
|
diags = diags.Extend(bundle.Apply(ctx, b, phases.Initialize()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !diags.HasError() {
|
|
|
|
diags = diags.Extend(bundle.Apply(ctx, b, validate.Validate()))
|
2022-12-16 14:00:48 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 15:33:43 +00:00
|
|
|
switch root.OutputType(cmd) {
|
|
|
|
case flags.OutputText:
|
2024-07-10 11:14:57 +00:00
|
|
|
renderOpts := render.RenderOptions{RenderSummaryTable: true}
|
|
|
|
err := render.RenderTextOutput(cmd.OutOrStdout(), b, diags, renderOpts)
|
2024-07-01 09:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to render output: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diags.HasError() {
|
|
|
|
return root.ErrAlreadyPrinted
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-04-03 15:33:43 +00:00
|
|
|
case flags.OutputJSON:
|
|
|
|
return renderJsonOutput(cmd, b, diags)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown output type %s", root.OutputType(cmd))
|
2022-11-21 14:39:53 +00:00
|
|
|
}
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2022-11-21 14:39:53 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
2022-11-21 14:39:53 +00:00
|
|
|
}
|