Skip to content

Commit ca3344e

Browse files
committed
/funcs. /environment. /readme.
1 parent abf5ac4 commit ca3344e

File tree

5 files changed

+139
-57
lines changed

5 files changed

+139
-57
lines changed

README.md

+33-24
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ Supports providing top-level name/value pairs on the command line:
1010

1111
renderizer --name=value --top=first template-file
1212

13-
**NOTE:** that _all_ parameter values are provided `name=value`.
13+
Sets:
14+
15+
Name: value
16+
Top: first
17+
18+
**NOTE:**
19+
20+
- Template values are provided `--name=value`.
21+
- Renderizer controls are provided using `-NAME[=value]`.
1422

1523
## Example
1624

@@ -20,21 +28,21 @@ First:
2028
cd renderizer
2129
go build
2230

23-
Generate the master pod.yaml:
31+
Render the `pod.yaml` using values from `test/.renderizer.yaml`:
2432

2533
renderizer -S=test/.renderizer.yaml test/pod.yaml
2634

27-
or, set `RENDERIZER` in the environment:
35+
Or set `RENDERIZER` in the environment:
2836

2937
RENDERIZER=test/.renderizer.yaml renderizer test/pod.yaml
3038

31-
alternatively, it'll try `.renderizer.yaml` in the current directory.
39+
Alternatively, it'll try `.renderizer.yaml` in the current directory.
3240

3341
(cd test; renderizer pod.yaml)
3442

35-
Generate the dev pod.yaml (after `cd test/`):
43+
Next, override the `deployment` value to render the "dev" `pod.yaml` (after `cd test/`):
3644

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

3947
## Configuration
4048

@@ -65,21 +73,22 @@ Control the missingkeys template-engine option:
6573

6674
### Environment `-E=`
6775

68-
Add the environment to the variables map as `_env`:
69-
70-
renderizer -E template-file
71-
72-
or name the map key:
73-
74-
renderizer -E=environ template-file
75-
76-
## Functions
77-
78-
### `add`
79-
### `inc`
80-
### `now`
81-
### `lower`
82-
### `upper`
83-
### `trim`
84-
### `trimLeft`
85-
### `trimRight`
76+
Provide a default value for missing environment variables:
77+
78+
renderizer -E=--missing-- template-file
79+
80+
## Template Functions
81+
82+
- `add` - `func(a, b int) int`
83+
- `cleanse` - `func(s string) string` - remove `[^[:alpha:]]`
84+
- `commandLine` - `func() string` the command line
85+
- `environment` - `map[string]string` - the runtime environment
86+
- `inc` - `func(a int) int`
87+
- `join` - `func(a []interface, sep string) string`
88+
- `lower` - `strings.ToLower`
89+
- `now` - `time.Now`
90+
- `replace` - `strings.Replace`
91+
- `trim` - `strings.Trim`
92+
- `trimLeft` - `strings.TrimLeft`
93+
- `trimRight` - `strings.TrimRight`
94+
- `upper` - `strings.ToUpper`

TODO.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
- [ ] URL support
1+
- URL support
22
- [ ] settings
33
- [ ] templates
4-
- [ ] templates on stdin
5-
- [ ] modes
4+
- input
5+
- [ ] templates on stdin
6+
- modes
67
- [ ] HTML
78
- [x] text
9+
- functions
10+
- [x] environment
11+
- [x] commandLine
12+
- [ ] osquery

func.go

+52-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,63 @@
11
package main
22

33
import (
4+
"fmt"
5+
"os"
6+
"regexp"
47
"strings"
58
"text/template"
69
"time"
710
)
811

912
//
1013
var funcs = template.FuncMap{
11-
"add": func(a, b int) int { return a + b },
12-
"inc": func(a int) int { return a + 1 },
13-
"now": func() time.Time { return time.Now().UTC() },
14-
"lower": func(s string) string { return strings.ToLower(s) },
15-
"upper": func(s string) string { return strings.ToUpper(s) },
16-
"trim": func(s string, cut string) string { return strings.Trim(s, cut) },
17-
"trimLeft": func(s string, cut string) string { return strings.TrimLeft(s, cut) },
18-
"trimRight": func(s string, cut string) string { return strings.TrimRight(s, cut) },
14+
"add": func(a, b int) int { return a + b },
15+
"cleanse": cleanse(`[^[:alpha:]]`),
16+
"commandLine": func() string { return settings.CommandLine },
17+
"environment": environment(),
18+
"inc": func(a int) int { return a + 1 },
19+
"join": joiner,
20+
"lower": strings.ToLower,
21+
"now": time.Now,
22+
"replace": strings.Replace,
23+
"trim": strings.Trim,
24+
"trimLeft": strings.TrimLeft,
25+
"trimRight": strings.TrimRight,
26+
"upper": strings.ToUpper,
27+
}
28+
29+
//
30+
func joiner(y []interface{}, sep string) (s string) {
31+
if y == nil {
32+
return
33+
}
34+
i := make([]string, len(y))
35+
for j, z := range y {
36+
i[j] = fmt.Sprintf("%v", z)
37+
}
38+
return strings.Join(i, sep)
39+
}
40+
41+
//
42+
func cleanse(r string) func(string) string {
43+
re := regexp.MustCompile(r)
44+
return func(s string) string {
45+
return re.ReplaceAllString(s, "")
46+
}
47+
}
48+
49+
//
50+
func environment() func(string) string {
51+
env := make(map[string]string)
52+
for _, item := range os.Environ() {
53+
splits := strings.Split(item, "=")
54+
env[splits[0]] = splits[1]
55+
}
56+
return func(n string) string {
57+
v, exists := env[n]
58+
if !exists {
59+
v = settings.Environment
60+
}
61+
return v
62+
}
1963
}

main.go

+42-21
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"io/ioutil"
88
"log"
99
"os"
10+
"path/filepath"
1011
"strings"
1112
"text/template"
1213
"time"
1314

1415
"gopkg.in/yaml.v2"
1516
)
1617

18+
//
1719
func usage(out io.Writer) {
1820
fmt.Fprintf(out, `usage: renderizer [options] [--name=value...] template...
1921
options:
@@ -47,6 +49,8 @@ type Settings struct {
4749
Debugging bool
4850
//
4951
Verbose bool
52+
//
53+
CommandLine string
5054
}
5155

5256
//
@@ -59,10 +63,17 @@ var settings = Settings{
5963
//
6064
func main() {
6165

66+
if len(os.Args) == 1 {
67+
usage(os.Stdout)
68+
return
69+
}
70+
6271
vars := map[string]interface{}{}
6372
load := map[string]interface{}{}
6473
args := []string{}
6574

75+
settings.CommandLine = commandLine()
76+
6677
// Initialize some settings from the environment.
6778

6879
if m, exists := os.LookupEnv("RENDERIZER_MISSINGKEY"); exists {
@@ -93,23 +104,21 @@ func main() {
93104
usage(os.Stdout)
94105
return
95106
case 's', 'S':
96-
nv := strings.Split(arg, "=")
107+
nv := strings.SplitN(arg, "=", 2)
97108
if len(nv) != 1 {
98109
settings.Config = nv[1]
99110
}
100111
case 'e', 'E':
101-
nv := strings.Split(arg, "=")
102-
if len(nv) == 1 {
103-
settings.Environment = "_env"
104-
} else {
112+
nv := strings.SplitN(arg, "=", 2)
113+
if len(nv) != 1 {
105114
settings.Environment = nv[1]
106115
}
107116
case 'v', 'V':
108117
settings.Verbose = true
109118
case 'd', 'D':
110119
settings.Debugging = true
111120
case 'm', 'M':
112-
nv := strings.Split(arg, "=")
121+
nv := strings.SplitN(arg, "=", 2)
113122
if len(nv) == 1 {
114123
settings.MissingKey = "error"
115124
} else {
@@ -125,20 +134,6 @@ func main() {
125134
settings.MissingKey = "error"
126135
}
127136

128-
// Add the environment to the variables.
129-
130-
if settings.Environment != "" {
131-
env := make(map[string]string)
132-
for _, item := range os.Environ() {
133-
splits := strings.Split(item, "=")
134-
env[splits[0]] = splits[1]
135-
}
136-
vars[settings.Environment] = env
137-
if settings.Verbose {
138-
log.Printf("environment: %+v", settings.Environment)
139-
}
140-
}
141-
142137
// Load the settings.
143138

144139
forced := false
@@ -183,7 +178,7 @@ func main() {
183178
}
184179

185180
arg = strings.TrimLeft(arg, "-")
186-
nv := strings.Split(arg, "=")
181+
nv := strings.SplitN(arg, "=", 2)
187182
n := nv[0]
188183
i += 1
189184

@@ -337,3 +332,29 @@ func main() {
337332
fmt.Println(string(sqlBuffer.Bytes()))
338333
}
339334
}
335+
336+
// Reproduce a command line string that reflects a usable command line.
337+
func commandLine() string {
338+
339+
quoter := func(e string) string {
340+
if !strings.Contains(e, " ") {
341+
return e
342+
}
343+
p := strings.SplitN(e, "=", 2)
344+
if strings.Contains(p[0], " ") {
345+
p[0] = `"` + strings.Replace(p[0], `"`, `\"`, -1) + `"`
346+
}
347+
if len(p) == 1 {
348+
return p[0]
349+
}
350+
return p[0] + `="` + strings.Replace(p[1], `"`, `\"`, -1) + `"`
351+
}
352+
each := func(s []string) (o []string) {
353+
o = make([]string, len(s))
354+
for i, t := range s {
355+
o[i] = quoter(t)
356+
}
357+
return
358+
}
359+
return filepath.Base(os.Args[0]) + " " + strings.Join(each(os.Args[1:]), " ")
360+
}

test/pod.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# Generated: {{now.UTC.Format "2006-01-02T15:04:05MST"}}
2+
# With: {{commandLine}}
3+
# From: {{environment "USER"}}@{{environment "HOST"}}:{{environment "PWD"}}
14
apiVersion: v1
25
kind: Pod
36
metadata:
4-
name: {{lower .Name}}
7+
name: {{replace .Name " " "_" -1 | cleanse | lower}}
58
namespace: {{.App}}-{{.Deployment}}
69
labels:
710
app: {{.App}}

0 commit comments

Comments
 (0)