diff --git a/.codegen.json b/.codegen.json index da4f3dd6..7e688a8c 100644 --- a/.codegen.json +++ b/.codegen.json @@ -8,6 +8,9 @@ ".codegen/cmds-account.go.tmpl": "cmd/account/cmd.go" }, "toolchain": { - "required": ["go"] + "required": ["go"], + "post_generate": [ + "go run main.go bundle schema --only-docs --output-file ./bundle/schema/docs/bundle_descriptions.json" + ] } } diff --git a/cmd/bundle/schema.go b/cmd/bundle/schema.go index 8b2c0177..ec817037 100644 --- a/cmd/bundle/schema.go +++ b/cmd/bundle/schema.go @@ -2,6 +2,7 @@ package bundle import ( "encoding/json" + "os" "reflect" "github.com/databricks/cli/bundle/config" @@ -16,11 +17,18 @@ func newSchemaCommand() *cobra.Command { } var openapi string + var outputFile string var onlyDocs bool cmd.Flags().StringVar(&openapi, "openapi", "", "path to a databricks openapi spec") cmd.Flags().BoolVar(&onlyDocs, "only-docs", false, "only generate descriptions for the schema") + cmd.Flags().StringVar(&outputFile, "output-file", "", "File path to write the schema to. If not specified, the schema will be written to stdout.") cmd.RunE = func(cmd *cobra.Command, args []string) error { + // If no openapi spec is provided, try to use the environment variable. + // This environment variable is set during CLI code generation. + if openapi == "" { + openapi = os.Getenv("DATABRICKS_OPENAPI_SPEC") + } docs, err := schema.BundleDocs(openapi) if err != nil { return err @@ -39,6 +47,16 @@ func newSchemaCommand() *cobra.Command { return err } } + + // If outputFile is provided, write to that file. + if outputFile != "" { + f, err := os.OpenFile(outputFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + return err + } + defer f.Close() + cmd.SetOut(f) + } cmd.OutOrStdout().Write(result) return nil }