From 9a8205b3dfab267b507f6c03db8b78e44b90d640 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Wed, 17 May 2023 15:33:04 +0200 Subject: [PATCH] address comments plus close file --- internal/init_test.go | 4 ++-- libs/template/execute.go | 14 ++++---------- libs/template/helpers.go | 4 ++-- libs/template/schema.go | 2 +- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/internal/init_test.go b/internal/init_test.go index b6f74252..793ee3a5 100644 --- a/internal/init_test.go +++ b/internal/init_test.go @@ -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 diff --git a/libs/template/execute.go b/libs/template/execute.go index 84da57b2..bec58884 100644 --- a/libs/template/execute.go +++ b/libs/template/execute.go @@ -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()) diff --git a/libs/template/helpers.go b/libs/template/helpers.go index ff8ae539..8e072bba 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -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) }, } diff --git a/libs/template/schema.go b/libs/template/schema.go index 2023eaf9..b536dc33 100644 --- a/libs/template/schema.go +++ b/libs/template/schema.go @@ -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