databricks-cli/cmd/root/root.go

96 lines
2.6 KiB
Go
Raw Normal View History

2022-05-14 17:54:35 +00:00
package root
2022-05-13 13:30:22 +00:00
import (
"context"
"fmt"
2022-05-13 13:30:22 +00:00
"os"
2023-04-12 20:12:36 +00:00
"strings"
2022-05-13 13:30:22 +00:00
2023-04-12 20:12:36 +00:00
"github.com/databricks/bricks/internal/build"
"github.com/databricks/bricks/libs/log"
2022-05-13 13:30:22 +00:00
"github.com/spf13/cobra"
2023-04-12 20:12:36 +00:00
"golang.org/x/exp/slog"
2022-05-13 13:30:22 +00:00
)
2022-05-14 17:54:35 +00:00
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
2022-05-13 13:30:22 +00:00
Use: "bricks",
Short: "Databricks project lifecycle management",
Long: `Where's "data"? Secured by the unity catalog. Projects build lifecycle is secured by bricks`,
// Cobra prints the usage string to stderr if a command returns an error.
// This usage string should only be displayed if an invalid combination of flags
// is specified and not when runtime errors occur (e.g. resource not found).
// The usage string is include in [flagErrorFunc] for flag errors only.
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// Configure default logger.
2023-03-29 12:58:09 +00:00
ctx, err := initializeLogger(ctx)
if err != nil {
return err
}
2023-04-12 20:12:36 +00:00
logger := log.GetLogger(ctx)
logger.Info("start",
slog.String("version", build.GetInfo().Version),
slog.String("args", strings.Join(os.Args, ", ")))
2023-03-29 12:58:09 +00:00
// Configure progress logger
ctx, err = initializeProgressLogger(ctx)
if err != nil {
return err
}
// Configure our user agent with the command that's about to be executed.
ctx = withCommandInUserAgent(ctx, cmd)
ctx = withUpstreamInUserAgent(ctx)
cmd.SetContext(ctx)
return nil
2022-05-14 17:54:35 +00:00
},
2022-05-13 13:30:22 +00:00
}
// Wrap flag errors to include the usage string.
func flagErrorFunc(c *cobra.Command, err error) error {
return fmt.Errorf("%w\n\n%s", err, c.UsageString())
}
2022-05-13 13:30:22 +00:00
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
2022-05-14 17:54:35 +00:00
// TODO: deferred panic recovery
ctx := context.Background()
2023-04-12 20:12:36 +00:00
// Run the command
cmd, err := RootCmd.ExecuteContextC(ctx)
// Log exit status and error
// We only log if logger initialization succeeded and is stored in command
// context
if logger, ok := log.FromContext(cmd.Context()); ok {
if err == nil {
logger.Info("completed execution",
slog.String("exit_code", "0"))
} else {
logger.Error("failed execution",
slog.String("exit_code", "1"),
slog.String("error", err.Error()))
}
}
2022-05-13 13:30:22 +00:00
if err != nil {
os.Exit(1)
}
}
func init() {
RootCmd.SetFlagErrorFunc(flagErrorFunc)
// The VS Code extension passes `-v` in debug mode and must be changed
// to use the new flags in `./logger.go` prior to removing this flag.
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "")
RootCmd.PersistentFlags().MarkHidden("verbose")
2022-05-13 13:30:22 +00:00
}