databricks-cli/cmd/bundle/summary.go

86 lines
2.1 KiB
Go
Raw Normal View History

package bundle
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/databricks/cli/bundle"
2024-08-24 18:48:36 +00:00
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/deploy/terraform"
"github.com/databricks/cli/bundle/phases"
2024-08-24 18:48:36 +00:00
"github.com/databricks/cli/bundle/render"
"github.com/databricks/cli/cmd/bundle/utils"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/flags"
"github.com/spf13/cobra"
)
func newSummaryCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "summary",
2024-08-24 18:48:36 +00:00
Short: "Summarize resources deployed by this bundle",
Args: root.NoArgs,
}
var forcePull bool
cmd.Flags().BoolVar(&forcePull, "force-pull", false, "Skip local cache and load the state from the remote workspace")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
b, diags := utils.ConfigureBundleWithVariables(cmd)
if err := diags.Error(); err != nil {
return diags.Error()
}
diags = bundle.Apply(ctx, b, phases.Initialize())
if err := diags.Error(); err != nil {
return err
}
cacheDir, err := terraform.Dir(ctx, b)
if err != nil {
return err
}
_, stateFileErr := os.Stat(filepath.Join(cacheDir, terraform.TerraformStateFileName))
_, configFileErr := os.Stat(filepath.Join(cacheDir, terraform.TerraformConfigFileName))
noCache := errors.Is(stateFileErr, os.ErrNotExist) || errors.Is(configFileErr, os.ErrNotExist)
if forcePull || noCache {
diags = bundle.Apply(ctx, b, bundle.Seq(
terraform.StatePull(),
terraform.Interpolate(),
terraform.Write(),
))
if err := diags.Error(); err != nil {
return err
}
}
2024-08-24 18:48:36 +00:00
diags = bundle.Apply(ctx, b,
bundle.Seq(terraform.Load(), mutator.InitializeURLs()))
if err := diags.Error(); err != nil {
return err
}
switch root.OutputType(cmd) {
case flags.OutputText:
2024-08-24 18:48:36 +00:00
return render.RenderSummary(ctx, cmd.OutOrStdout(), b)
case flags.OutputJSON:
buf, err := json.MarshalIndent(b.Config, "", " ")
if err != nil {
return err
}
cmd.OutOrStdout().Write(buf)
default:
return fmt.Errorf("unknown output type %s", root.OutputType(cmd))
}
return nil
}
return cmd
}