2023-08-07 13:14:25 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-03 12:39:36 +00:00
|
|
|
"errors"
|
2023-11-22 12:25:16 +00:00
|
|
|
"fmt"
|
2023-08-25 09:03:42 +00:00
|
|
|
"io/fs"
|
2023-08-30 14:01:08 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2024-11-18 21:28:28 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
2023-08-07 13:14:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-11-18 21:29:25 +00:00
|
|
|
// templateFS: root of the template definition
|
2023-08-17 20:32:30 +00:00
|
|
|
// outputDir: root of directory where to initialize the template
|
2024-11-18 19:43:12 +00:00
|
|
|
func Materialize(ctx context.Context, configFilePath string, templateFS fs.FS, outputDir string) error {
|
|
|
|
if _, err := fs.Stat(templateFS, schemaFileName); errors.Is(err, fs.ErrNotExist) {
|
2024-11-18 16:32:02 +00:00
|
|
|
return fmt.Errorf("not a bundle template: expected to find a template schema file at %s", schemaFileName)
|
2023-08-25 09:03:42 +00:00
|
|
|
}
|
2023-08-07 13:14:25 +00:00
|
|
|
|
2024-11-18 19:43:12 +00:00
|
|
|
config, err := newConfig(ctx, templateFS, schemaFileName)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and assign config values from file
|
|
|
|
if configFilePath != "" {
|
|
|
|
err = config.assignValuesFromFile(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-18 16:32:02 +00:00
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-18 21:28:28 +00:00
|
|
|
r, err := newRenderer(ctx, config.values, helpers, templateFS, templateDirName, libraryDirName)
|
2023-10-19 07:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:57:13 +00:00
|
|
|
// Print welcome message
|
|
|
|
welcome := config.schema.WelcomeMessage
|
|
|
|
if welcome != "" {
|
|
|
|
welcome, err = r.executeTemplate(welcome)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmdio.LogString(ctx, welcome)
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
// Prompt user for any missing config values. Assign default values if
|
|
|
|
// terminal is not TTY
|
2023-10-19 07:08:36 +00:00
|
|
|
err = config.promptOrAssignDefaultValues(r)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk and render the template, since input configuration is complete
|
|
|
|
err = r.walk()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-25 09:03:42 +00:00
|
|
|
|
2024-11-18 21:28:28 +00:00
|
|
|
out, err := filer.NewLocalClient(outputDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.persistToDisk(ctx, out)
|
2023-08-25 09:03:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 07:08:36 +00:00
|
|
|
|
|
|
|
success := config.schema.SuccessMessage
|
|
|
|
if success == "" {
|
|
|
|
cmdio.LogString(ctx, "✨ Successfully initialized template")
|
|
|
|
} else {
|
|
|
|
success, err = r.executeTemplate(success)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmdio.LogString(ctx, success)
|
|
|
|
}
|
2023-08-25 09:03:42 +00:00
|
|
|
return nil
|
|
|
|
}
|