2023-08-07 13:14:25 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
const libraryDirName = "library"
|
|
|
|
const templateDirName = "template"
|
|
|
|
const schemaFileName = "databricks_template_schema.json"
|
|
|
|
|
|
|
|
// This function materializes the input templates as a project, using user defined
|
|
|
|
// configurations.
|
|
|
|
// Parameters:
|
|
|
|
//
|
|
|
|
// ctx: context containing a cmdio object. This is used to prompt the user
|
|
|
|
// configFilePath: file path containing user defined config values
|
|
|
|
// templateRoot: root of the template definition
|
2023-08-17 20:32:30 +00:00
|
|
|
// outputDir: root of directory where to initialize the template
|
|
|
|
func Materialize(ctx context.Context, configFilePath, templateRoot, outputDir string) error {
|
2023-08-07 13:14:25 +00:00
|
|
|
templatePath := filepath.Join(templateRoot, templateDirName)
|
|
|
|
libraryPath := filepath.Join(templateRoot, libraryDirName)
|
|
|
|
schemaPath := filepath.Join(templateRoot, schemaFileName)
|
|
|
|
|
|
|
|
config, err := newConfig(ctx, schemaPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and assign config values from file
|
|
|
|
if configFilePath != "" {
|
|
|
|
err = config.assignValuesFromFile(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prompt user for any missing config values. Assign default values if
|
|
|
|
// terminal is not TTY
|
|
|
|
err = config.promptOrAssignDefaultValues()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = config.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk and render the template, since input configuration is complete
|
2023-08-17 20:32:30 +00:00
|
|
|
r, err := newRenderer(ctx, config.values, templatePath, libraryPath, outputDir)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = r.walk()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return r.persistToDisk()
|
|
|
|
}
|