From 96e9545cf08b8feb46decc5ab3f6b4b0fd7220cd Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Mon, 27 Nov 2023 11:42:39 +0100 Subject: [PATCH] Automate the generation of bundle schema descriptions (#1007) ## Changes This PR makes changes required to automatically update the bundle docs during the CLI release process. We rely on `post_generate` scripts that are executed after code generation with CWD as the CLI repo root. The new `output-file` flag is introduced because stdout redirect does not work here and would otherwise require changes to our release automation CLI (deco CLI) ## Tests Manually. Regenerated the CLI and the descriptions were indeed generated for the CLI from the provided openapi spec. --- .codegen.json | 5 ++++- cmd/bundle/schema.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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 }