From 0807413ab6b7626aabae5880de8a072434a48c8f Mon Sep 17 00:00:00 2001 From: Cameron Garrison Date: Tue, 12 Dec 2023 17:33:11 -0500 Subject: [PATCH] rename func, use more generic fs --- pkg/feature/feature.go | 4 ++-- pkg/feature/manifest.go | 18 +++++++++++++----- pkg/feature/raw_resources.go | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/feature/feature.go b/pkg/feature/feature.go index 6395c537d36..7c4a57470de 100644 --- a/pkg/feature/feature.go +++ b/pkg/feature/feature.go @@ -176,13 +176,13 @@ func (f *Feature) apply(m manifest) error { applier = func(data string) error { log.Info("patching using manifest", "feature", f.Name, "name", m.name, "path", targetPath) - return f.patchResourceFromData(data) + return f.patchResources(data) } } else { applier = func(data string) error { log.Info("applying manifest", "feature", f.Name, "name", m.name, "path", targetPath) - return f.createResourceFromData(data) + return f.createResources(data) } } diff --git a/pkg/feature/manifest.go b/pkg/feature/manifest.go index 09fca6a68a3..1511666393b 100644 --- a/pkg/feature/manifest.go +++ b/pkg/feature/manifest.go @@ -5,6 +5,7 @@ import ( "embed" "fmt" "html/template" + "io" "io/fs" "path/filepath" "strings" @@ -29,10 +30,10 @@ type manifest struct { processedContent string } -func loadManifestsFrom(embeddedFS embed.FS, rootPath string) ([]manifest, error) { +func loadManifestsFrom(fsys fs.FS, rootPath string) ([]manifest, error) { var manifests []manifest - err := fs.WalkDir(embeddedFS, rootPath, func(path string, dirEntry fs.DirEntry, err error) error { + err := fs.WalkDir(fsys, rootPath, func(path string, dirEntry fs.DirEntry, err error) error { if err != nil { return err } @@ -41,7 +42,7 @@ func loadManifestsFrom(embeddedFS embed.FS, rootPath string) ([]manifest, error) return nil } - _, err = fs.ReadFile(embeddedFS, path) + _, err = fs.ReadFile(fsys, path) if err != nil { log.Error(err, "Failed to load manifest from", "path", path) return err @@ -75,12 +76,19 @@ func (m *manifest) targetPath() string { return fmt.Sprintf("%s%s", m.path[:len(m.path)-len(filepath.Ext(m.path))], ".yaml") } -func (m *manifest) processTemplate(fs embed.FS, data interface{}) error { +func (m *manifest) processTemplate(fsys fs.FS, data interface{}) error { if !m.template { return nil } - templateContent, err := fs.ReadFile(m.path) + templateFile, err := fsys.Open(m.path) + if err != nil { + log.Error(err, "Failed to open template file", "path", m.path) + return err + } + defer templateFile.Close() + + templateContent, err := io.ReadAll(templateFile) if err != nil { log.Error(err, "Failed to read template file", "path", m.path) return err diff --git a/pkg/feature/raw_resources.go b/pkg/feature/raw_resources.go index ed74111605b..958cbeb0129 100644 --- a/pkg/feature/raw_resources.go +++ b/pkg/feature/raw_resources.go @@ -31,7 +31,7 @@ const ( YamlSeparator = "(?m)^---[ \t]*$" ) -func (f *Feature) createResourceFromData(resources string) error { +func (f *Feature) createResources(resources string) error { splitter := regexp.MustCompile(YamlSeparator) objectStrings := splitter.Split(resources, -1) for _, str := range objectStrings { @@ -73,7 +73,7 @@ func (f *Feature) createResourceFromData(resources string) error { return nil } -func (f *Feature) patchResourceFromData(resources string) error { +func (f *Feature) patchResources(resources string) error { splitter := regexp.MustCompile(YamlSeparator) objectStrings := splitter.Split(resources, -1) for _, str := range objectStrings {