From c497431a5205cddb6e151f9b6c0e11280b2a7519 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Sun, 3 Jan 2021 11:06:32 +0800 Subject: [PATCH] Add .tmplkeep (#16) Resolves #15 Co-authored-by: jolheiser Reviewed-on: https://gitea.com/jolheiser/tmpl/pulls/16 Co-authored-by: John Olheiser Co-committed-by: John Olheiser --- registry/registry_test.go | 3 ++- registry/template.go | 5 +++++ registry/template_test.go | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/registry/registry_test.go b/registry/registry_test.go index 8adc7ca..628e29d 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -98,7 +98,8 @@ func setupTemplate() { if err := os.MkdirAll(pkgPath, os.ModePerm); err != nil { panic(err) } - fi, err = os.Create(filepath.Join(pkgPath, ".keep")) + // .tmplkeep file + fi, err = os.Create(filepath.Join(pkgPath, ".tmplkeep")) if err != nil { panic(err) } diff --git a/registry/template.go b/registry/template.go index 938eff0..76bf84c 100644 --- a/registry/template.go +++ b/registry/template.go @@ -85,6 +85,11 @@ func (t *Template) Execute(dest string, defaults, overwrite bool) error { return err } + // Skip .tmplkeep files, after creating the directory structure + if strings.EqualFold(walkInfo.Name(), ".tmplkeep") { + return nil + } + oldFi, err := os.Lstat(walkPath) if err != nil { return err diff --git a/registry/template_test.go b/registry/template_test.go index dd8c317..a51717b 100644 --- a/registry/template_test.go +++ b/registry/template_test.go @@ -72,6 +72,13 @@ func testExecute(t *testing.T) { t.FailNow() } + // Check for .tmplkeep + tmplKeep := filepath.Join(pkgPath, ".tmplkeep") + if _, err := os.Lstat(tmplKeep); err == nil { + t.Logf(".tmplkeep files should NOT be retained upon execution: %s\n", tmplKeep) + t.FailNow() + } + // Change file to test non-overwrite if err := ioutil.WriteFile(testPath, []byte(tmplNewGold), os.ModePerm); err != nil { t.Logf("could not write file: %v\n", err)