Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate the generation of bundle schema descriptions #1007

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
18 changes: 18 additions & 0 deletions cmd/bundle/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bundle

import (
"encoding/json"
"os"
"reflect"

"github.com/databricks/cli/bundle/config"
Expand All @@ -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.")
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved

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
Expand All @@ -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
}
Expand Down