databricks-cli/libs/template/execute.go

91 lines
2.2 KiB
Go
Raw Normal View History

2023-05-17 09:53:13 +00:00
package template
2023-05-17 00:43:41 +00:00
import (
2023-05-17 13:27:03 +00:00
"errors"
2023-05-17 00:43:41 +00:00
"os"
"path/filepath"
"strings"
"text/template"
)
2023-05-17 13:00:27 +00:00
// Executes the template by applying config on it. Returns the materialized config
2023-05-17 00:43:41 +00:00
// as a string
func executeTemplate(config map[string]any, templateDefination string) (string, error) {
// configure template with helper functions
2023-05-17 13:00:27 +00:00
tmpl, err := template.New("").Funcs(HelperFuncs).Parse(templateDefination)
2023-05-17 00:43:41 +00:00
if err != nil {
return "", err
}
// execute template
result := strings.Builder{}
err = tmpl.Execute(&result, config)
if err != nil {
return "", err
}
return result.String(), nil
}
2023-05-22 07:46:26 +00:00
func generateFile(config map[string]any, pathTemplate, contentTemplate string) error {
2023-05-17 00:43:41 +00:00
// compute file content
fileContent, err := executeTemplate(config, contentTemplate)
2023-05-17 13:33:04 +00:00
if errors.Is(err, errSkipThisFile) {
2023-05-22 07:46:26 +00:00
// skip this file
2023-05-17 00:43:41 +00:00
return nil
}
if err != nil {
return err
}
2023-05-22 07:46:26 +00:00
// compute the path for this file
path, err := executeTemplate(config, pathTemplate)
if err != nil {
return err
}
// create any intermediate directories required. Directories are lazily generated
// only when they are required for a file.
err = os.MkdirAll(filepath.Dir(path), 0755)
2023-05-17 00:43:41 +00:00
if err != nil {
return err
}
2023-05-22 07:46:26 +00:00
// write content to file
return os.WriteFile(path, []byte(fileContent), 0644)
2023-05-17 00:43:41 +00:00
}
2023-05-22 07:46:26 +00:00
func walkFileTree(config map[string]any, templatePath, instancePath string) error {
2023-05-17 13:33:04 +00:00
entries, err := os.ReadDir(templatePath)
2023-05-17 00:43:41 +00:00
if err != nil {
return err
}
2023-05-17 13:33:04 +00:00
for _, entry := range entries {
2023-05-17 00:43:41 +00:00
if entry.IsDir() {
2023-05-22 07:46:26 +00:00
// compute directory name
dirName, err := executeTemplate(config, entry.Name())
2023-05-17 00:43:41 +00:00
if err != nil {
return err
}
2023-05-22 07:46:26 +00:00
// recusively generate files and directories inside inside our newly generated
2023-05-17 00:43:41 +00:00
// directory from the template defination
2023-05-22 07:46:26 +00:00
err = walkFileTree(config, filepath.Join(templatePath, entry.Name()), filepath.Join(instancePath, dirName))
2023-05-17 00:43:41 +00:00
if err != nil {
return err
}
} else {
// case: materialize a template file with it's contents
b, err := os.ReadFile(filepath.Join(templatePath, entry.Name()))
if err != nil {
return err
}
contentTemplate := string(b)
fileNameTemplate := entry.Name()
2023-05-22 07:46:26 +00:00
err = generateFile(config, filepath.Join(instancePath, fileNameTemplate), contentTemplate)
2023-05-17 00:43:41 +00:00
if err != nil {
return err
}
}
}
return nil
}