diff --git a/cmd/fs.go b/cmd/fs/fs.go similarity index 88% rename from cmd/fs.go rename to cmd/fs/fs.go index bb113cb0..600bfd9c 100644 --- a/cmd/fs.go +++ b/cmd/fs/fs.go @@ -1,6 +1,7 @@ -package cmd +package fs import ( + "github.com/databricks/bricks/cmd/root" "github.com/spf13/cobra" ) @@ -12,7 +13,7 @@ var fsCmd = &cobra.Command{ } func init() { - rootCmd.AddCommand(fsCmd) + root.RootCmd.AddCommand(fsCmd) // Here you will define your flags and configuration settings. diff --git a/cmd/ls.go b/cmd/fs/ls.go similarity index 90% rename from cmd/ls.go rename to cmd/fs/ls.go index 33113f53..d236024c 100644 --- a/cmd/ls.go +++ b/cmd/fs/ls.go @@ -1,4 +1,4 @@ -package cmd +package fs import ( "fmt" @@ -16,7 +16,7 @@ var lsCmd = &cobra.Command{ Long: `Lists files`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - api := storage.NewDbfsAPI(cmd.Context(), project.ClientFromContext(cmd.Context())) + api := storage.NewDbfsAPI(cmd.Context(), project.Current.Client()) files, err := api.List(args[0], false) if err != nil { panic(err) diff --git a/cmd/launch.go b/cmd/launch/launch.go similarity index 87% rename from cmd/launch.go rename to cmd/launch/launch.go index 12251d7e..6d45c9ca 100644 --- a/cmd/launch.go +++ b/cmd/launch/launch.go @@ -1,10 +1,11 @@ -package cmd +package launch import ( "fmt" "log" "os" + "github.com/databricks/bricks/cmd/root" "github.com/databricks/bricks/project" "github.com/spf13/cobra" ) @@ -29,5 +30,5 @@ var launchCmd = &cobra.Command{ } func init() { - rootCmd.AddCommand(launchCmd) + root.RootCmd.AddCommand(launchCmd) } diff --git a/cmd/root.go b/cmd/root/root.go similarity index 64% rename from cmd/root.go rename to cmd/root/root.go index da773b4b..3a455bd7 100644 --- a/cmd/root.go +++ b/cmd/root/root.go @@ -1,4 +1,4 @@ -package cmd +package root import ( "context" @@ -6,15 +6,20 @@ import ( "os" "strings" - "github.com/databricks/bricks/project" "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ +// RootCmd represents the base command when called without any subcommands +var RootCmd = &cobra.Command{ Use: "bricks", Short: "Databricks project lifecycle management", Long: `Where's "data"? Secured by the unity catalog. Projects build lifecycle is secured by bricks`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if Verbose { + logLevel = append(logLevel, "[DEBUG]") + } + log.SetOutput(&logLevel) + }, // Uncomment the following line if your bare application // has an action associated with it: } @@ -23,7 +28,9 @@ var rootCmd = &cobra.Command{ type levelWriter []string var logLevel = levelWriter{"[INFO]", "[ERROR]", "[WARN]"} -var verbose bool + +// Verbose means additional debug information, like API logs +var Verbose bool func (lw *levelWriter) Write(p []byte) (n int, err error) { a := string(p) @@ -38,17 +45,14 @@ func (lw *levelWriter) Write(p []byte) (n int, err error) { // 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() { - if verbose { - logLevel = append(logLevel, "[DEBUG]") - } - ctx := project.Authenticate(context.Background()) - err := rootCmd.ExecuteContext(ctx) + // TODO: deferred panic recovery + ctx := context.Background() + err := RootCmd.ExecuteContext(ctx) if err != nil { os.Exit(1) } } func init() { - rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "print debug logs") - log.SetOutput(&logLevel) + RootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "print debug logs") } diff --git a/cmd/test.go b/cmd/test/test.go similarity index 91% rename from cmd/test.go rename to cmd/test/test.go index ff58c4e4..33a0a6e1 100644 --- a/cmd/test.go +++ b/cmd/test/test.go @@ -1,8 +1,9 @@ -package cmd +package test import ( "log" + "github.com/databricks/bricks/cmd/root" "github.com/databricks/bricks/project" "github.com/spf13/cobra" ) @@ -22,7 +23,7 @@ var testCmd = &cobra.Command{ } func init() { - rootCmd.AddCommand(testCmd) + root.RootCmd.AddCommand(testCmd) // Here you will define your flags and configuration settings.