Skip to content

Commit

Permalink
refactor: replace interface with function type
Browse files Browse the repository at this point in the history
Signed-off-by: Bird <aflybird0@gmail.com>
  • Loading branch information
aFlyBird0 committed Aug 16, 2022
1 parent 6e9380c commit bd0528a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 159 deletions.
File renamed without changes.
94 changes: 94 additions & 0 deletions pkg/util/file/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package file

import (
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("renderGitRepoDir func", func() {
var (
vars map[string]interface{}
srcPath, contentDir, rawContent, tplContent, renderdContent string
)

createFile := func(filePath, content string) {
f, err := os.Create(filePath)
Expect(err).Error().ShouldNot(HaveOccurred())
defer f.Close()
err = os.WriteFile(filePath, []byte(content), 0755)
Expect(err).Error().ShouldNot(HaveOccurred())
}
createDir := func(dirPath string) {
err := os.Mkdir(dirPath, 0755)
Expect(err).Error().ShouldNot(HaveOccurred())
}
BeforeEach(func() {
rawContent = "This is a file without template variable"
tplContent = `
metadata:
name: "[[ .App.Name ]]"
namespace: "[[ .App.NameSpace ]]"`
renderdContent = `
metadata:
name: "test"
namespace: "test_namespace"`

})

When("srcPath is not exist", func() {
BeforeEach(func() {
srcPath = "not_exist_path"
})
It("should return err", func() {
_, err := renderGitRepoDir("test", srcPath, vars)
Expect(err).Error().Should(HaveOccurred())
})
})
When("all config is right", func() {
BeforeEach(func() {
contentDir = "content"
vars = map[string]interface{}{
"App": map[string]interface{}{
"Name": "test",
"NameSpace": "test_namespace",
},
}
srcPath = GinkgoT().TempDir()
gitPath := filepath.Join(srcPath, ".git")
createDir(gitPath)
createFile(filepath.Join(gitPath, "gitFile"), tplContent)
createFile(filepath.Join(srcPath, "README.md"), "")
contentDirPath := filepath.Join(srcPath, contentDir)
createDir(contentDirPath)
createFile(filepath.Join(contentDirPath, "test.yaml.tpl"), tplContent)
createFile(filepath.Join(contentDirPath, "raw.txt"), rawContent)
})
It("should render all dir", func() {
dstPath, err := renderGitRepoDir("test", srcPath, vars)
Expect(err).Error().ShouldNot(HaveOccurred())
files, err := os.ReadDir(dstPath)
Expect(err).Error().ShouldNot(HaveOccurred())
// test README.md dir is not copied
Expect(len(files)).Should(Equal(2))
// test git dir files should not copied
gitDirFiles, err := os.ReadDir(filepath.Join(dstPath, ".git"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(gitDirFiles)).Should(Equal(0))
// test content dir files is copied
contentDirLoc := filepath.Join(dstPath, contentDir)
contentFiles, err := os.ReadDir(contentDirLoc)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(contentFiles)).Should(Equal(2))
// test file content
tplFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "test.yaml"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(tplFileContent)).Should(Equal(renderdContent))
rawFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "raw.txt"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(rawFileContent)).Should(Equal(rawContent))
})
})
})
87 changes: 0 additions & 87 deletions pkg/util/file/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package file

import (
"fmt"
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -40,88 +38,3 @@ var _ = Describe("replaceAppNameInPathStr func", func() {
})
})
})

var _ = Describe("renderGitRepoDir func", func() {
var (
vars map[string]interface{}
srcPath, contentDir, rawContent, tplContent, renderdContent string
)

createFile := func(filePath, content string) {
f, err := os.Create(filePath)
Expect(err).Error().ShouldNot(HaveOccurred())
defer f.Close()
err = os.WriteFile(filePath, []byte(content), 0755)
Expect(err).Error().ShouldNot(HaveOccurred())
}
createDir := func(dirPath string) {
err := os.Mkdir(dirPath, 0755)
Expect(err).Error().ShouldNot(HaveOccurred())
}
BeforeEach(func() {
rawContent = "This is a file without template variable"
tplContent = `
metadata:
name: "[[ .App.Name ]]"
namespace: "[[ .App.NameSpace ]]"`
renderdContent = `
metadata:
name: "test"
namespace: "test_namespace"`

})

When("srcPath is not exist", func() {
BeforeEach(func() {
srcPath = "not_exist_path"
})
It("should return err", func() {
_, err := renderGitRepoDir("test", srcPath, vars)
Expect(err).Error().Should(HaveOccurred())
})
})
When("all config is right", func() {
BeforeEach(func() {
contentDir = "content"
vars = map[string]interface{}{
"App": map[string]interface{}{
"Name": "test",
"NameSpace": "test_namespace",
},
}
srcPath = GinkgoT().TempDir()
gitPath := filepath.Join(srcPath, ".git")
createDir(gitPath)
createFile(filepath.Join(gitPath, "gitFile"), tplContent)
createFile(filepath.Join(srcPath, "README.md"), "")
contentDirPath := filepath.Join(srcPath, contentDir)
createDir(contentDirPath)
createFile(filepath.Join(contentDirPath, "test.yaml.tpl"), tplContent)
createFile(filepath.Join(contentDirPath, "raw.txt"), rawContent)
})
It("should render all dir", func() {
dstPath, err := renderGitRepoDir("test", srcPath, vars)
Expect(err).Error().ShouldNot(HaveOccurred())
files, err := os.ReadDir(dstPath)
Expect(err).Error().ShouldNot(HaveOccurred())
// test README.md dir is not copied
Expect(len(files)).Should(Equal(2))
// test git dir files should not copied
gitDirFiles, err := os.ReadDir(filepath.Join(dstPath, ".git"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(gitDirFiles)).Should(Equal(0))
// test content dir files is copied
contentDirLoc := filepath.Join(dstPath, contentDir)
contentFiles, err := os.ReadDir(contentDirLoc)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(contentFiles)).Should(Equal(2))
// test file content
tplFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "test.yaml"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(tplFileContent)).Should(Equal(renderdContent))
rawFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "raw.txt"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(rawFileContent)).Should(Equal(rawContent))
})
})
})
38 changes: 11 additions & 27 deletions pkg/util/template/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ import (
// Getters

func FromLocalFile(filepath string) ContentGetter {
return &localGetter{filepath: filepath}
return func() ([]byte, error) {
return os.ReadFile(filepath)
}
}

func FromContent(content string) ContentGetter {
return &contentGetter{content: content}
return func() ([]byte, error) {
return []byte(content), nil
}
}

func FromURL(url string) ContentGetter {
return &urlGetter{url: url}
return func() ([]byte, error) {
return getContentFromURL(url)
}
}

// Quick Calls
Expand All @@ -37,30 +43,8 @@ func (r *render) FromURL(url string) *rendererWithGetter {
return r.SetContentGetter(FromURL(url))
}

// Getters definition

type localGetter struct {
filepath string
}

func (g *localGetter) GetContent() ([]byte, error) {
return os.ReadFile(g.filepath)
}

type contentGetter struct {
content string
}

func (g *contentGetter) GetContent() ([]byte, error) {
return []byte(g.content), nil
}

type urlGetter struct {
url string
}

func (g *urlGetter) GetContent() ([]byte, error) {
resp, err := http.Get(g.url)
func getContentFromURL(url string) ([]byte, error) {
resp, err := http.Get(url)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
Expand Down
22 changes: 6 additions & 16 deletions pkg/util/template/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@ func AddDotForVariablesInConfig(s string) string {
return r.ReplaceAllString(s, "[[ .")
}

type dotProcessor struct{}

func (p *dotProcessor) Process(bytes []byte) ([]byte, error) {
return []byte(AddDotForVariablesInConfig(string(bytes))), nil
}

func AddDotForVariablesInConfigProcessor() Processor {
return &dotProcessor{}
return func(bytes []byte) ([]byte, error) {
return []byte(AddDotForVariablesInConfig(string(bytes))), nil
}
}

type processorFuncWrapper struct {
f func([]byte) ([]byte, error)
}

func (p *processorFuncWrapper) Process(bytes []byte) ([]byte, error) {
return p.f(bytes)
}
// Quick Calls

func NewProcessorFuncWrapper(f func([]byte) ([]byte, error)) Processor {
return &processorFuncWrapper{f: f}
func (r *rendererWithGetter) AddDotForVariablesInConfigProcessor() *rendererWithGetter {
return r.AddProcessor(AddDotForVariablesInConfigProcessor())
}
16 changes: 4 additions & 12 deletions pkg/util/template/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,10 @@ func RenderForFile(name, tplFileName, dstFileName string, variable any) error {
return os.WriteFile(dstFileName, []byte(renderedStr), 0644)
}

type defaultRender struct {
templateName string
vars any
funcMaps []template.FuncMap
}

func (r *defaultRender) Render(src []byte) (string, error) {
return Render(r.templateName, string(src), r.vars, r.funcMaps...)
}

func DefaultRender(templateName string, vars any, funcMaps ...template.FuncMap) RenderInf {
return &defaultRender{templateName: templateName, vars: vars, funcMaps: funcMaps}
func DefaultRender(templateName string, vars any, funcMaps ...template.FuncMap) RenderFunc {
return func(src []byte) (string, error) {
return Render(templateName, string(src), vars, funcMaps...)
}
}

// Quick Calls
Expand Down
28 changes: 11 additions & 17 deletions pkg/util/template/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package template

type (
// ContentGetter gets content from any source
ContentGetter interface {
GetContent() ([]byte, error)
}
ContentGetter func() ([]byte, error)

// Processor process content before render
Processor interface {
Process([]byte) ([]byte, error)
}
Processor func([]byte) ([]byte, error)

// RenderInf render content to string
RenderInf interface {
Render([]byte) (string, error)
}
// RenderFunc render content to string
RenderFunc func([]byte) (string, error)
)

type (
Expand All @@ -38,7 +32,7 @@ type (
rendererWithRender struct {
getter ContentGetter // mandatory
processors []Processor // optional
render RenderInf // mandatory
render RenderFunc // mandatory
}
)

Expand All @@ -59,7 +53,7 @@ func (r *rendererWithGetter) AddProcessor(processor Processor) *rendererWithGett
}
}

func (r *rendererWithGetter) SetRender(render RenderInf) *rendererWithRender {
func (r *rendererWithGetter) SetRender(render RenderFunc) *rendererWithRender {
return &rendererWithRender{
getter: r.getter,
processors: r.processors,
Expand All @@ -70,34 +64,34 @@ func (r *rendererWithGetter) SetRender(render RenderInf) *rendererWithRender {
// Render gets the content, process the content, render and returns the result string
func (c *rendererWithRender) Render() (string, error) {
// 1. get content
content, err := c.getter.GetContent()
content, err := c.getter()
if err != nil {
return "", err
}

// 2. process content
for _, processor := range c.processors {
content, err = processor.Process(content)
content, err = processor(content)
if err != nil {
return "", err
}
}

// 3. render content
return c.render.Render(content)
return c.render(content)
}

// String returns the string directly, without rendering
func (c *rendererWithGetter) String() (string, error) {
// 1. get content
content, err := c.getter.GetContent()
content, err := c.getter()
if err != nil {
return "", err
}

// 2. process content
for _, processor := range c.processors {
content, err = processor.Process(content)
content, err = processor(content)
if err != nil {
return "", err
}
Expand Down

0 comments on commit bd0528a

Please sign in to comment.