2023-08-07 13:14:25 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
"github.com/databricks/cli/libs/jsonschema"
|
2023-09-07 14:36:06 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-08-07 13:14:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
ctx context.Context
|
|
|
|
values map[string]any
|
|
|
|
schema *jsonschema.Schema
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfig(ctx context.Context, schemaPath string) (*config, error) {
|
|
|
|
// Read config schema
|
2023-08-15 14:28:04 +00:00
|
|
|
schema, err := jsonschema.Load(schemaPath)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-15 14:28:04 +00:00
|
|
|
if err := validateSchema(schema); err != nil {
|
2023-08-07 13:14:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-07 14:36:06 +00:00
|
|
|
// Do not allow template input variables that are not defined in the schema.
|
|
|
|
schema.AdditionalProperties = false
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
// Return config
|
|
|
|
return &config{
|
|
|
|
ctx: ctx,
|
|
|
|
schema: schema,
|
|
|
|
values: make(map[string]any, 0),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-08-15 14:28:04 +00:00
|
|
|
func validateSchema(schema *jsonschema.Schema) error {
|
|
|
|
for _, v := range schema.Properties {
|
|
|
|
if v.Type == jsonschema.ArrayType || v.Type == jsonschema.ObjectType {
|
|
|
|
return fmt.Errorf("property type %s is not supported by bundle templates", v.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
// Reads json file at path and assigns values from the file
|
|
|
|
func (c *config) assignValuesFromFile(path string) error {
|
2023-09-07 14:36:06 +00:00
|
|
|
// Load the config file.
|
|
|
|
configFromFile, err := c.schema.LoadInstance(path)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
2023-09-07 14:36:06 +00:00
|
|
|
return fmt.Errorf("failed to load config from file %s: %w", path, err)
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write configs from the file to the input map, not overwriting any existing
|
|
|
|
// configurations.
|
|
|
|
for name, val := range configFromFile {
|
|
|
|
if _, ok := c.values[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.values[name] = val
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assigns default values from schema to input config map
|
2023-10-19 07:08:36 +00:00
|
|
|
func (c *config) assignDefaultValues(r *renderer) error {
|
2023-08-07 13:14:25 +00:00
|
|
|
for name, property := range c.schema.Properties {
|
|
|
|
// Config already has a value assigned
|
|
|
|
if _, ok := c.values[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// No default value defined for the property
|
|
|
|
if property.Default == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-06 15:05:17 +00:00
|
|
|
defaultVal, err := property.DefaultString()
|
2023-10-19 07:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defaultVal, err = r.executeTemplate(defaultVal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-06 15:05:17 +00:00
|
|
|
defaultValTyped, err := property.ParseString(defaultVal)
|
2023-10-19 07:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.values[name] = defaultValTyped
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prompts user for values for properties that do not have a value set yet
|
2023-10-19 07:08:36 +00:00
|
|
|
func (c *config) promptForValues(r *renderer) error {
|
2023-09-05 11:08:25 +00:00
|
|
|
for _, p := range c.schema.OrderedProperties() {
|
|
|
|
name := p.Name
|
|
|
|
property := p.Schema
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
// Config already has a value assigned
|
|
|
|
if _, ok := c.values[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute default value to display by converting it to a string
|
2023-08-15 14:50:20 +00:00
|
|
|
var defaultVal string
|
|
|
|
var err error
|
2023-08-07 13:14:25 +00:00
|
|
|
if property.Default != nil {
|
2023-11-06 15:05:17 +00:00
|
|
|
defaultValRaw, err := property.DefaultString()
|
2023-10-19 07:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defaultVal, err = r.executeTemplate(defaultValRaw)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 07:08:36 +00:00
|
|
|
description, err := r.executeTemplate(property.Description)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
// Get user input by running the prompt
|
2023-09-08 12:07:22 +00:00
|
|
|
var userInput string
|
|
|
|
if property.Enum != nil {
|
|
|
|
// convert list of enums to string slice
|
2023-11-06 15:05:17 +00:00
|
|
|
enums, err := property.EnumStringSlice()
|
2023-09-08 12:07:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 07:08:36 +00:00
|
|
|
userInput, err = cmdio.AskSelect(c.ctx, description, enums)
|
2023-09-08 12:07:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-19 07:08:36 +00:00
|
|
|
userInput, err = cmdio.Ask(c.ctx, description, defaultVal)
|
2023-09-08 12:07:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-25 09:53:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
// Convert user input string back to a value
|
2023-11-06 15:05:17 +00:00
|
|
|
c.values[name], err = property.ParseString(userInput)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 07:08:36 +00:00
|
|
|
|
|
|
|
// Validate the partial config based on this update
|
|
|
|
if err := c.schema.ValidateInstance(c.values); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prompt user for any missing config values. Assign default values if
|
|
|
|
// terminal is not TTY
|
2023-10-19 07:08:36 +00:00
|
|
|
func (c *config) promptOrAssignDefaultValues(r *renderer) error {
|
2023-08-07 13:14:25 +00:00
|
|
|
if cmdio.IsOutTTY(c.ctx) && cmdio.IsInTTY(c.ctx) {
|
2023-10-19 07:08:36 +00:00
|
|
|
return c.promptForValues(r)
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
2023-10-19 07:08:36 +00:00
|
|
|
return c.assignDefaultValues(r)
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validates the configuration. If passes, the configuration is ready to be used
|
|
|
|
// to initialize the template.
|
|
|
|
func (c *config) validate() error {
|
2023-10-19 07:08:36 +00:00
|
|
|
// For final validation, all properties in the JSON schema should have a value defined.
|
2023-09-07 14:36:06 +00:00
|
|
|
c.schema.Required = maps.Keys(c.schema.Properties)
|
|
|
|
if err := c.schema.ValidateInstance(c.values); err != nil {
|
|
|
|
return fmt.Errorf("validation for template input parameters failed. %w", err)
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|