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.
This commit is contained in:
shreyas-goenka 2023-11-27 11:42:39 +01:00 committed by GitHub
parent f5f57b6bf9
commit 96e9545cf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -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"
]
}
}

View File

@ -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
}