databricks-cli/cmd/root/root.go

79 lines
2.1 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"
"log"
2022-05-13 13:30:22 +00:00
"os"
"strings"
2022-05-13 13:30:22 +00:00
"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`,
// 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,
2022-05-14 17:54:35 +00:00
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Configure our user agent with the command that's about to be executed.
ctx := withCommandInUserAgent(cmd.Context(), cmd)
cmd.SetContext(ctx)
2022-05-14 17:54:35 +00:00
if Verbose {
logLevel = append(logLevel, "[DEBUG]")
}
log.SetOutput(&logLevel)
},
2022-05-13 13:30:22 +00:00
// Uncomment the following line if your bare application
// has an action associated with it:
}
// TODO: replace with zerolog
type levelWriter []string
var logLevel = levelWriter{"[INFO]", "[ERROR]", "[WARN]"}
2022-05-14 17:54:35 +00:00
// Verbose means additional debug information, like API logs
var Verbose bool
func (lw *levelWriter) Write(p []byte) (n int, err error) {
a := string(p)
for _, l := range *lw {
if strings.Contains(a, l) {
return os.Stderr.Write(p)
}
}
return
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()
err := RootCmd.ExecuteContext(ctx)
2022-05-13 13:30:22 +00:00
if err != nil {
os.Exit(1)
}
}
func init() {
RootCmd.SetFlagErrorFunc(flagErrorFunc)
2022-05-20 18:43:29 +00:00
// flags available for every child command
2022-05-14 17:54:35 +00:00
RootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "print debug logs")
2022-05-13 13:30:22 +00:00
}