Skip to content

Commit eccc545

Browse files
committed
- Update to current funcmap
- Fixes some bugs with parameters and defaults - Update README.md - Improved verbose output
1 parent f4e7dd4 commit eccc545

File tree

9 files changed

+166
-83
lines changed

9 files changed

+166
-83
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@ And read from the environment:
2020

2121
## Examples
2222

23-
Render the `pod.yaml` using values from `examples/pod/.renderizer.yaml`:
23+
Render the `pod.yaml.tmpl` using values from `examples/pod/.renderizer.yaml`:
2424

2525
renderizer --settings=examples/pod/.pod.yaml examples/pod/pod.yaml.tmpl
2626

2727
Or set `RENDERIZER` in the environment:
2828

2929
RENDERIZER=examples/.pod.yaml renderizer examples/pod/pod.yaml.tmpl
3030

31-
Alternatively, it'll try `.renderizer.yaml` in the current directory.
31+
Alternatively, it'll try `.pod.yaml` in the current directory.
3232

33-
(cd examples/pod; renderizer pod.yaml)
33+
(cd examples/pod; renderizer)
3434

35-
Next, override the `deployment` value to render the "dev" `pod.yaml` (after `cd examples/pod`):
35+
Next, override the `deployment` value to render the "dev" `pod.yaml.tmpl` (after `cd examples/pod`):
3636

37-
renderizer --deployment=dev --name='spaced out' pod.yaml
37+
renderizer --deployment=dev --name='spaced out'
3838

3939
For more examples, see the [`examples`](examples) folder.
4040

4141
# Configuration
4242

4343
### Settings
4444

45-
Settings can be loaded from a yaml:
45+
Settings can be loaded from any YAMLs:
4646

47-
renderizer --settings=.renderizer.yaml --name=value --top=first template-file
47+
renderizer --settings=.settings1.yaml --settings=.settings2.yaml --name=value template-file
4848

4949
### Capitalization `-C`
5050

examples/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
EXAMPLES = $(patsubst %/,%,$(sort $(dir $(wildcard */*.tmpl))))
2+
3+
.PHONY : $(EXAMPLES)
4+
.PHONY : run
5+
.PHONY : help
6+
.DEFAULT_GOAL := run
7+
8+
run: $(EXAMPLES) ## Run all the examples
9+
10+
$(EXAMPLES):
11+
cd $@; renderizer
12+
13+
help: ## This help.
14+
@echo EXAMPLES=$(EXAMPLES)
15+
@echo Targets:
16+
@awk 'BEGIN {FS = ":.*?## "} / [#][#] / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

examples/functions/functions.txt.tmpl

+9-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ line {{next}} mod = 12345 % 10 = {{mod 10 12345}}
5050
line {{next}} rand = {{rand}}
5151
line {{next}} lower = {{lower "LOWER"}}
5252
line {{next}} upper = {{upper "upper"}}
53-
line {{next}} title = {{title "this is a title"}}
54-
line {{next}} title = {{title "THIS IS A TITLE"}}
55-
line {{next}} initcap = {{initcap "this is a title"}}
53+
line {{next}} title = {{title "this is a title from lowercase"}}
54+
line {{next}} title = {{title "THIS IS A TITLE FROM UPPERCASE"}}
55+
line {{next}} initcap = {{initcap "this is initcap from lowercase"}}
56+
line {{next}} initcap = {{initcap "THIS IS INITCAP FROM UPPERCASE"}}
57+
line {{next}} cleanser = {{cleanser "[^works]" "abcdefghijklmnopqrstuvwxyz"}}
58+
line {{next}} cleanse = [{{cleanse " abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ "}}]
59+
line {{next}} cleanse = {{cleanse "WOR1234567890KS"}}
60+
line {{next}} cleanse = {{cleanse "WOR!@#$%^&*()KS"}}
61+
line {{next}} cleanse = {{cleanse "WOR`~-=_+[]{};':\",.<>/?KS"}}
5662
line {{next}} cleanse = {{cleanse "zaq!xsw@cde#vfr$bgt%nhy^mju&,ki*.lo(/;p)"}}
5763
line {{next}} now = {{now}}
5864
line {{next}} pausing 1s = {{pause 1000}}

examples/pod/pod.yaml.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
apiVersion: v1
55
kind: Pod
66
metadata:
7-
name: {{replace .Name " " "_" -1 | cleanse | lower}}
7+
name: {{replace .Name " " "_" -1 | identifier | lower}}
88
namespace: {{.App}}-{{.Deployment}}
99
labels:
1010
app: &app_name {{.App}}
1111
spec:
1212
containers: {{$p := .}}{{range $i, $c := .Containers}}
13-
- name: &name_{{index $i}} {{.Name}}
13+
- name: &name_{{$i}} {{.Name}}
1414
image: {{$p.Repo}}/{{$p.App}}-{{.Name}}
1515
ports:
16-
- name: *name_{{index $i}}
16+
- name: *name_{{$i}}
1717
containerPort: {{inc $p.BasePort $i}}
1818
protocol: TCP{{end}}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/gomatic/renderizer
22

33
require (
4-
github.com/gomatic/funcmap v0.0.0-20170407194050-9ab29796c307
4+
github.com/gomatic/funcmap v0.0.0-20180916232612-d0d9de88be48
55
github.com/imdario/mergo v0.3.6
66
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1
77
github.com/pkg/errors v0.8.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/gomatic/funcmap v0.0.0-20170407194050-9ab29796c307 h1:vdV1B6lc0iOnze2OvoHREif4bzaYTY5WHo1+HhBD2io=
2-
github.com/gomatic/funcmap v0.0.0-20170407194050-9ab29796c307/go.mod h1:tgYMANYRbfiPtGpM0in6m2dnRENPq5Oj0f+eJkTBT1E=
1+
github.com/gomatic/funcmap v0.0.0-20180916232612-d0d9de88be48 h1:7zBC8mBAIu2aDXiF47LfBYzm60L0l8smmaiFEKGVUVw=
2+
github.com/gomatic/funcmap v0.0.0-20180916232612-d0d9de88be48/go.mod h1:tgYMANYRbfiPtGpM0in6m2dnRENPq5Oj0f+eJkTBT1E=
33
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
44
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
55
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1 h1:PJPDf8OUfOK1bb/NeTKd4f1QXZItOX389VN3B6qC8ro=

main.go

+42-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010

11+
"github.com/imdario/mergo"
1112
"github.com/kardianos/osext"
1213
"github.com/urfave/cli"
1314
"gopkg.in/yaml.v2"
@@ -126,7 +127,22 @@ func main() {
126127
settings.Stdin = settings.Stdin || (fi.Mode()&os.ModeCharDevice) == 0
127128

128129
settings.Arguments = append(settings.Arguments, ctx.Args()...)
129-
if len(settings.Arguments) == 0 && !settings.Stdin {
130+
131+
mainName, mainTemplate, noTemplate := "", "", true
132+
if largs := len(settings.Arguments); largs > 0 {
133+
i, found := largs-1, -1
134+
for ; i > 0; i-- {
135+
if !strings.HasPrefix(settings.Arguments[i], "--") {
136+
found = i
137+
}
138+
}
139+
if found >= 0 {
140+
noTemplate = false
141+
mainTemplate = settings.Arguments[found]
142+
}
143+
}
144+
145+
if mainTemplate == "" && !settings.Stdin {
130146
// Try default the template name
131147
folderName, err := os.Getwd()
132148
if err != nil {
@@ -143,12 +159,16 @@ func main() {
143159
if settings.Verbose {
144160
log.Printf("using template: %+v", name)
145161
}
146-
settings.Arguments = []string{name}
162+
mainTemplate = name
147163
}
148164
}
149165
}
166+
}
150167

168+
if noTemplate {
169+
settings.Arguments = append(settings.Arguments, mainTemplate)
151170
}
171+
mainName = strings.Split(strings.TrimLeft(filepath.Base(mainTemplate), "."), ".")[0]
152172

153173
switch settings.MissingKey {
154174
case "zero", "error", "default", "invalid":
@@ -158,12 +178,8 @@ func main() {
158178
}
159179

160180
if len(configs) == 0 {
161-
name := "renderizer"
162-
if len(settings.Arguments) >= 1 {
163-
name = strings.Split(strings.TrimLeft(filepath.Base(settings.Arguments[0]), "."), ".")[0]
164-
}
165181
settings.Defaulted = true
166-
settings.ConfigFiles = []string{"." + name + ".yaml"}
182+
settings.ConfigFiles = []string{"." + mainName + ".yaml"}
167183
} else {
168184
settings.ConfigFiles = configs
169185
}
@@ -175,18 +191,28 @@ func main() {
175191
return err
176192
}
177193
} else {
178-
yaml.Unmarshal(in, &settings.Config)
194+
loaded := map[string]interface{}{}
195+
err := yaml.Unmarshal(in, &loaded)
196+
if err != nil {
197+
return err
198+
}
179199
if settings.Verbose || settings.Defaulted {
180200
log.Printf("using settings: %+v", settings.ConfigFiles)
181201
}
202+
loaded = retyper(loaded)
203+
if settings.Debugging {
204+
log.Printf("loaded: %s = %#v", config, loaded)
205+
} else if settings.Verbose {
206+
log.Printf("loaded: %s = %+v", config, loaded)
207+
}
208+
mergo.Merge(&settings.Config, loaded)
182209
}
183-
if settings.Debugging {
184-
log.Printf("--settings:%#v", settings)
185-
log.Printf("loaded: %#v", settings.Config)
186-
} else if settings.Verbose {
187-
log.Printf("--settings:%+v", settings)
188-
log.Printf("loaded: %+v", settings.Config)
189-
}
210+
}
211+
212+
if settings.Debugging {
213+
log.Printf("--settings:%#v", settings)
214+
} else if settings.Verbose {
215+
log.Printf("--settings:%+v", settings)
190216
}
191217

192218
return nil
@@ -203,6 +229,7 @@ func main() {
203229
next = false
204230
continue
205231
}
232+
// TODO convert all '--name value' parameters to --name=value
206233
if strings.HasPrefix(larg, "--") {
207234
flag := larg
208235
parts := strings.SplitN(larg, "=", 2)

renderizer.go

+18-33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/gomatic/funcmap"
1414
"github.com/imdario/mergo"
1515
"github.com/urfave/cli"
16+
"gopkg.in/yaml.v2"
1617
)
1718

1819
//
@@ -62,58 +63,38 @@ func renderizer(_ *cli.Context) error {
6263

6364
var currentValue interface{}
6465
if len(nameValuePair) == 1 { // i.e. a boolean
65-
leaf[last] = []interface{}{true}
66+
currentValue = []interface{}{true}
6667
} else {
67-
leaf[last] = []interface{}{typer(nameValuePair[1])}
68+
currentValue = []interface{}{typer(nameValuePair[1])}
6869
}
70+
leaf[last] = currentValue
6971

7072
if settings.Debugging {
7173
log.Printf("index:%d name:%s value:%+v", a, currentName, currentValue)
7274
}
7375

74-
// log.Printf("currentContext: %[1]T %#[1]v", currentContext)
76+
if settings.Debugging {
77+
log.Printf("currentContext: %[1]T %#[1]v", currentContext)
78+
}
7579
mergo.Merge(&globalContext, currentContext, mergo.WithAppendSlice)
76-
// log.Printf("globalContext: %[1]T %#[1]v", globalContext)
80+
if settings.Debugging {
81+
log.Printf("globalContext: %[1]T %#[1]v", globalContext)
82+
}
7783
}
7884

79-
retyper(globalContext)
80-
81-
// Dump the settings
82-
83-
if settings.Debugging {
84-
log.Printf("globalContext: %#v", globalContext)
85-
} else if settings.Verbose {
86-
log.Printf("globalContext: %+v", globalContext)
87-
}
85+
globalContext = retyper(globalContext, retypeSingleElementSlice)
8886

8987
// If there's no files, read from stdin.
9088
files := args
9189
if len(args) == 0 {
92-
stat, _ := os.Stdin.Stat()
93-
isTTY := (stat.Mode() & os.ModeCharDevice) != 0
94-
if isTTY {
90+
if settings.Stdin {
9591
log.Println("source: stdin")
9692
}
9793
files = []string{""}
9894
}
9995

10096
// Copy any loaded keys into the globalContext unless they already exist, i.e. they were provided on the command line.
101-
for n, v := range settings.Config {
102-
if _, exists := globalContext[n]; !exists {
103-
switch x := v.(type) {
104-
case int8:
105-
globalContext[n] = int64(x)
106-
case int16:
107-
globalContext[n] = int64(x)
108-
case int32:
109-
globalContext[n] = int64(x)
110-
case int:
111-
globalContext[n] = int64(x)
112-
default:
113-
globalContext[n] = v
114-
}
115-
}
116-
}
97+
mergo.Merge(&globalContext, settings.Config)
11798

11899
if settings.Environment != "" || len(args) == 0 {
119100
v := make(map[string]string)
@@ -129,7 +110,11 @@ func renderizer(_ *cli.Context) error {
129110
if settings.Debugging {
130111
log.Printf("globalContext: %#v", globalContext)
131112
} else if settings.Verbose {
132-
log.Printf("globalContext: %+v", globalContext)
113+
if o, err := yaml.Marshal(globalContext); err != nil {
114+
log.Printf("globalContext: %+v", globalContext)
115+
} else {
116+
log.Printf("globalContext:\n%s", o)
117+
}
133118
}
134119

135120
// Execute each template

0 commit comments

Comments
 (0)