2023-01-23 14:00:11 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/schema"
|
2024-03-12 14:12:34 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
2024-04-25 11:23:50 +00:00
|
|
|
"github.com/databricks/cli/libs/jsonschema"
|
2023-01-23 14:00:11 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2024-04-25 11:23:50 +00:00
|
|
|
func overrideVariables(s *jsonschema.Schema) error {
|
|
|
|
// Override schema for default values to allow for multiple primitive types.
|
|
|
|
// These are normalized to strings when converted to the typed representation.
|
|
|
|
err := s.SetByPath("variables.*.default", jsonschema.Schema{
|
|
|
|
AnyOf: []*jsonschema.Schema{
|
|
|
|
{
|
|
|
|
Type: jsonschema.StringType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: jsonschema.BooleanType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: jsonschema.NumberType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: jsonschema.IntegerType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override schema for variables in targets to allow just specifying the value
|
|
|
|
// along side overriding the variable definition if needed.
|
|
|
|
ns, err := s.GetByPath("variables.*")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.SetByPath("targets.*.variables.*", jsonschema.Schema{
|
|
|
|
AnyOf: []*jsonschema.Schema{
|
|
|
|
{
|
|
|
|
Type: jsonschema.StringType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: jsonschema.BooleanType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: jsonschema.NumberType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: jsonschema.IntegerType,
|
|
|
|
},
|
|
|
|
&ns,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func newSchemaCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "schema",
|
|
|
|
Short: "Generate JSON Schema for bundle configuration",
|
2024-03-12 14:12:34 +00:00
|
|
|
Args: root.NoArgs,
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2023-01-23 14:00:11 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-12-06 10:45:18 +00:00
|
|
|
// Load embedded schema descriptions.
|
|
|
|
docs, err := schema.LoadBundleDescriptions()
|
2023-01-23 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-06 10:45:18 +00:00
|
|
|
|
|
|
|
// Generate the JSON schema from the bundle configuration struct in Go.
|
2023-01-23 14:00:11 +00:00
|
|
|
schema, err := schema.New(reflect.TypeOf(config.Root{}), docs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-06 10:45:18 +00:00
|
|
|
|
2024-04-25 11:23:50 +00:00
|
|
|
// Override schema for variables to take into account normalization of default
|
|
|
|
// variable values and variable overrides in a target.
|
|
|
|
err = overrideVariables(schema)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-06 10:45:18 +00:00
|
|
|
// Print the JSON schema to stdout.
|
2023-03-15 02:18:51 +00:00
|
|
|
result, err := json.MarshalIndent(schema, "", " ")
|
2023-01-23 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 02:18:51 +00:00
|
|
|
cmd.OutOrStdout().Write(result)
|
2023-01-23 14:00:11 +00:00
|
|
|
return nil
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2023-03-15 02:18:51 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
2023-01-23 14:00:11 +00:00
|
|
|
}
|