Skip to content

Commit 1c84f27

Browse files
committed
feat(templater): file maker templating library
1 parent 59c3abb commit 1c84f27

File tree

7 files changed

+116
-2
lines changed

7 files changed

+116
-2
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
vendor/

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DOCKER_TARGET?=ci
2+
3+
test:
4+
cd ${module} && go test -v github.com/openopsdev/go-cli-commons/...
5+
6+
lint:
7+
go get -u golang.org/x/lint/golint
8+
test -z "$$(golint ./...)"
9+
10+
local-module:
11+
echo "replace github.com/openopsdev/go-cli-commons/$(module) => ../go-cli-commons/$(module)" >> go.mod
12+
13+
remote-module:
14+
sed -i -e "/replace\ github\.com\/openopsdev\/go-cli-commons\/$(module)\ =>/d" go.mod

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# go-cobra-commons
2-
Cobra CLI Tool Common Utility Methods
1+
# go-cli-commons
2+
3+
CLI Tool Common Utility Methods (currently we use cobra framework)

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/openopsdev/go-cobra-commons
2+
3+
go 1.15
4+
5+
require github.com/aymerick/raymond v2.0.2+incompatible

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/aymerick/raymond v1.1.0 h1:phuNN2s67eI/HtO8CrvqFcdR2JP+BtkGJZ9n692Hr2Y=
2+
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
3+
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=

templater/file.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package templater
2+
3+
import (
4+
"errors"
5+
"os"
6+
7+
"github.com/aymerick/raymond"
8+
)
9+
10+
// FileMaker - Renders a template with passed vars
11+
type FileMaker struct {
12+
Path string
13+
Result string
14+
}
15+
16+
// NewFile -
17+
func NewFile(path string) *FileMaker {
18+
return &FileMaker{
19+
Path: path,
20+
}
21+
}
22+
23+
// Render -
24+
func (f *FileMaker) Render(vars map[string]string) error {
25+
tpl, err := raymond.ParseFile(f.Path)
26+
if err != nil {
27+
return err
28+
}
29+
30+
result, err := tpl.Exec(vars)
31+
32+
if err != nil {
33+
return err
34+
}
35+
36+
f.Result = result
37+
return nil
38+
}
39+
40+
func (f *FileMaker) writeToFile(dest string) error {
41+
file, err := os.Create(dest)
42+
43+
if err != nil {
44+
return err
45+
}
46+
47+
defer file.Close()
48+
49+
_, err = file.WriteString(f.Result)
50+
51+
if err != nil {
52+
return err
53+
}
54+
55+
return nil
56+
}
57+
58+
// Save - saves results to a file
59+
func (f *FileMaker) Save(dest string) error {
60+
if len(f.Result) > 0 {
61+
err := f.writeToFile(dest)
62+
63+
if err != nil {
64+
return err
65+
}
66+
} else {
67+
return errors.New("no result")
68+
}
69+
return nil
70+
}

templater/templater.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package templater
2+
3+
// Templater -
4+
type Templater interface {
5+
Render() interface{}
6+
}

0 commit comments

Comments
 (0)