mirror of https://github.com/databricks/cli.git
42 lines
960 B
Go
42 lines
960 B
Go
|
package root
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
|
||
|
"github.com/databricks/bricks/libs/cmdio"
|
||
|
"github.com/databricks/bricks/libs/flags"
|
||
|
"github.com/spf13/cobra"
|
||
|
)
|
||
|
|
||
|
const envBricksOutputFormat = "BRICKS_OUTPUT_FORMAT"
|
||
|
|
||
|
var outputType flags.Output = flags.OutputText
|
||
|
|
||
|
func init() {
|
||
|
// Configure defaults from environment, if applicable.
|
||
|
// If the provided value is invalid it is ignored.
|
||
|
if v, ok := os.LookupEnv(envBricksOutputFormat); ok {
|
||
|
outputType.Set(v)
|
||
|
}
|
||
|
|
||
|
RootCmd.PersistentFlags().VarP(&outputType, "output", "o", "output type: text or json")
|
||
|
}
|
||
|
|
||
|
func OutputType() flags.Output {
|
||
|
return outputType
|
||
|
}
|
||
|
|
||
|
func initializeIO(cmd *cobra.Command) error {
|
||
|
var template string
|
||
|
if cmd.Annotations != nil {
|
||
|
// rely on zeroval being an empty string
|
||
|
template = cmd.Annotations["template"]
|
||
|
}
|
||
|
|
||
|
cmdIO := cmdio.NewIO(outputType, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), template)
|
||
|
ctx := cmdio.InContext(cmd.Context(), cmdIO)
|
||
|
cmd.SetContext(ctx)
|
||
|
|
||
|
return nil
|
||
|
}
|