address comments plus close file

This commit is contained in:
Shreyas Goenka 2023-05-17 15:33:04 +02:00
parent ecc07e893f
commit 9a8205b3df
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
4 changed files with 9 additions and 15 deletions

View File

@ -23,7 +23,6 @@ func TestTemplateInitializationForDevConfig(t *testing.T) {
tmp := t.TempDir()
f, err := os.Create(filepath.Join(tmp, "config.json"))
require.NoError(t, err)
defer f.Close()
_, err = f.WriteString(`
{
"project_name": "development_project",
@ -32,6 +31,7 @@ func TestTemplateInitializationForDevConfig(t *testing.T) {
"is_production": false
}
`)
f.Close()
require.NoError(t, err)
// materialize the template
@ -53,7 +53,6 @@ func TestTemplateInitializationForProdConfig(t *testing.T) {
tmp := t.TempDir()
f, err := os.Create(filepath.Join(tmp, "config.json"))
require.NoError(t, err)
defer f.Close()
_, err = f.WriteString(`
{
"project_name": "production_project",
@ -62,6 +61,7 @@ func TestTemplateInitializationForProdConfig(t *testing.T) {
"is_production": true
}
`)
f.Close()
require.NoError(t, err)
// materialize the template

View File

@ -42,7 +42,7 @@ func generateDirectory(config map[string]any, parentDir, nameTempate string) (st
func generateFile(config map[string]any, parentDir, nameTempate, contentTemplate string) error {
// compute file content
fileContent, err := executeTemplate(config, contentTemplate)
if errors.Is(err, ErrSkipThisFile) {
if errors.Is(err, errSkipThisFile) {
return nil
}
if err != nil {
@ -54,22 +54,16 @@ func generateFile(config map[string]any, parentDir, nameTempate, contentTemplate
if err != nil {
return err
}
f, err := os.Create(filepath.Join(parentDir, fileName))
if err != nil {
return err
}
// write to file the computed content
_, err = f.Write([]byte(fileContent))
return err
return os.WriteFile(filepath.Join(parentDir, fileName), []byte(fileContent), 0644)
}
func WalkFileTree(config map[string]any, templatePath, instancePath string) error {
enteries, err := os.ReadDir(templatePath)
entries, err := os.ReadDir(templatePath)
if err != nil {
return err
}
for _, entry := range enteries {
for _, entry := range entries {
if entry.IsDir() {
// case: materialize a template directory
dirName, err := generateDirectory(config, instancePath, entry.Name())

View File

@ -5,10 +5,10 @@ import (
"text/template"
)
var ErrSkipThisFile = errors.New("skip generating this file")
var errSkipThisFile = errors.New("skip generating this file")
var HelperFuncs = template.FuncMap{
"skipThisFile": func() error {
panic(ErrSkipThisFile)
panic(errSkipThisFile)
},
}

View File

@ -21,7 +21,7 @@ const (
type FieldInfo struct {
Type FieldType `json:"type"`
Description string `json:"description"`
Validation string `json:"validate"`
Validation string `json:"validation"`
}
// function to check whether a float value represents an integer