2022-05-14 17:54:35 +00:00
|
|
|
package root
|
2022-05-13 13:30:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-21 10:38:30 +00:00
|
|
|
"fmt"
|
2022-05-13 13:30:22 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
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`,
|
2022-12-21 10:38:30 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
Add structured logging infrastructure (#246)
New global flags:
* `--log-file FILE`: can be literal `stdout`, `stderr`, or a file name (default `stderr`)
* `--log-level LEVEL`: can be `error`, `warn`, `info`, `debug`, `trace`, or `disabled` (default `disabled`)
* `--log-format TYPE`: can be `text` or `json` (default `text`)
New functions in the `log` package take a `context.Context` and retrieve
the logger from said context.
Because we carry the logger in a context, adding
[attributes](https://pkg.go.dev/golang.org/x/exp/slog#hdr-Attrs_and_Values)
to the logger can be done as follows:
```go
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("foo", "bar"))
```
2023-03-16 13:46:53 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2023-02-03 16:05:58 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
|
Add structured logging infrastructure (#246)
New global flags:
* `--log-file FILE`: can be literal `stdout`, `stderr`, or a file name (default `stderr`)
* `--log-level LEVEL`: can be `error`, `warn`, `info`, `debug`, `trace`, or `disabled` (default `disabled`)
* `--log-format TYPE`: can be `text` or `json` (default `text`)
New functions in the `log` package take a `context.Context` and retrieve
the logger from said context.
Because we carry the logger in a context, adding
[attributes](https://pkg.go.dev/golang.org/x/exp/slog#hdr-Attrs_and_Values)
to the logger can be done as follows:
```go
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("foo", "bar"))
```
2023-03-16 13:46:53 +00:00
|
|
|
// Configure default logger.
|
2023-03-29 12:58:09 +00:00
|
|
|
ctx, err := initializeLogger(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure progress logger
|
|
|
|
ctx, err = initializeProgressLogger(ctx)
|
Add structured logging infrastructure (#246)
New global flags:
* `--log-file FILE`: can be literal `stdout`, `stderr`, or a file name (default `stderr`)
* `--log-level LEVEL`: can be `error`, `warn`, `info`, `debug`, `trace`, or `disabled` (default `disabled`)
* `--log-format TYPE`: can be `text` or `json` (default `text`)
New functions in the `log` package take a `context.Context` and retrieve
the logger from said context.
Because we carry the logger in a context, adding
[attributes](https://pkg.go.dev/golang.org/x/exp/slog#hdr-Attrs_and_Values)
to the logger can be done as follows:
```go
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("foo", "bar"))
```
2023-03-16 13:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-03 15:47:33 +00:00
|
|
|
// Configure our user agent with the command that's about to be executed.
|
2023-02-03 16:05:58 +00:00
|
|
|
ctx = withCommandInUserAgent(ctx, cmd)
|
|
|
|
ctx = withUpstreamInUserAgent(ctx)
|
2023-02-03 15:47:33 +00:00
|
|
|
cmd.SetContext(ctx)
|
Add structured logging infrastructure (#246)
New global flags:
* `--log-file FILE`: can be literal `stdout`, `stderr`, or a file name (default `stderr`)
* `--log-level LEVEL`: can be `error`, `warn`, `info`, `debug`, `trace`, or `disabled` (default `disabled`)
* `--log-format TYPE`: can be `text` or `json` (default `text`)
New functions in the `log` package take a `context.Context` and retrieve
the logger from said context.
Because we carry the logger in a context, adding
[attributes](https://pkg.go.dev/golang.org/x/exp/slog#hdr-Attrs_and_Values)
to the logger can be done as follows:
```go
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("foo", "bar"))
```
2023-03-16 13:46:53 +00:00
|
|
|
return nil
|
2022-05-14 17:54:35 +00:00
|
|
|
},
|
2022-05-13 13:30:22 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:38:30 +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()
|
|
|
|
err := RootCmd.ExecuteContext(ctx)
|
2022-05-13 13:30:22 +00:00
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-12-21 10:38:30 +00:00
|
|
|
RootCmd.SetFlagErrorFunc(flagErrorFunc)
|
Add structured logging infrastructure (#246)
New global flags:
* `--log-file FILE`: can be literal `stdout`, `stderr`, or a file name (default `stderr`)
* `--log-level LEVEL`: can be `error`, `warn`, `info`, `debug`, `trace`, or `disabled` (default `disabled`)
* `--log-format TYPE`: can be `text` or `json` (default `text`)
New functions in the `log` package take a `context.Context` and retrieve
the logger from said context.
Because we carry the logger in a context, adding
[attributes](https://pkg.go.dev/golang.org/x/exp/slog#hdr-Attrs_and_Values)
to the logger can be done as follows:
```go
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("foo", "bar"))
```
2023-03-16 13:46:53 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|