Skip to content

Commit 78ec676

Browse files
authored
Merge pull request #399 from aywan/multi-template
Nested templates and include function
2 parents d8354fa + 338283c commit 78ec676

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ e75a60548dc9 = 1 # a key can be either container name (nginx) or ID
214214

215215
The templates used by docker-gen are written using the Go [text/template](http://golang.org/pkg/text/template/) language. In addition to the [built-in functions](http://golang.org/pkg/text/template/#hdr-Functions) supplied by Go, docker-gen uses [sprig](https://masterminds.github.io/sprig/) and some additional functions to make it simpler (or possible) to generate your desired output. Some templates rely on environment variables within the container to make decisions on what to generate from the template.
216216

217+
For parsing several templates, split path with `;` (for example `template = "nginx.tmpl;header.tmpl"`). This makes possible to use go nested templates through standard `template` function.
218+
217219
#### Emit Structure
218220

219221
Within the templates, the object emitted by docker-gen will be a structure consisting of following Go structs:
@@ -378,6 +380,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
378380
* *`groupByKeys $containers $fieldPath`*: Returns the same as `groupBy` but only returns the keys of the map.
379381
* *`groupByMulti $containers $fieldPath $sep`*: Like `groupBy`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. A container whose `$fieldPath` value contains a list of strings will show up in the map output under each of those strings.
380382
* *`groupByLabel $containers $label`*: Returns the same as `groupBy` but grouping by the given label's value.
383+
* *`include $file`*: Returns content of `$file`, and empty string if file reading error.
381384
* *`intersect $slice1 $slice2`*: Returns the strings that exist in both string slices.
382385
* *`json $value`*: Returns the JSON representation of `$value` as a `string`.
383386
* *`keys $map`*: Returns the keys from `$map`. If `$map` is `nil`, a `nil` is returned. If `$map` is not a `map`, an error will be thrown.

internal/template/functions.go

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ func keys(input interface{}) (interface{}, error) {
3131
return k, nil
3232
}
3333

34+
func include(file string) string {
35+
data, err := os.ReadFile(file)
36+
if err != nil {
37+
return ""
38+
}
39+
return string(data)
40+
}
41+
3442
func intersect(l1, l2 []string) []string {
3543
m := make(map[string]bool)
3644
m2 := make(map[string]bool)

internal/template/functions_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ func TestKeysNil(t *testing.T) {
7777
}
7878
}
7979

80+
func TestInclude(t *testing.T) {
81+
data := include("some_random_file")
82+
assert.Equal(t, "", data)
83+
84+
_ = os.WriteFile("/tmp/docker-gen-test-temp-file", []byte("some string"), 0o777)
85+
data = include("/tmp/docker-gen-test-temp-file")
86+
assert.Equal(t, "some string", data)
87+
_ = os.Remove("/tmp/docker-gen-test-temp-file")
88+
}
89+
8090
func TestIntersect(t *testing.T) {
8191
i := intersect([]string{"foo.fo.com", "bar.com"}, []string{"foo.bar.com"})
8292
assert.Len(t, i, 0, "Expected no match")

internal/template/template.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func newTemplate(name string) *template.Template {
6969
"groupByMulti": groupByMulti,
7070
"groupByLabel": groupByLabel,
7171
"json": marshalJson,
72+
"include": include,
7273
"intersect": intersect,
7374
"keys": keys,
7475
"replace": strings.Replace,
@@ -223,13 +224,14 @@ func GenerateFile(config config.Config, containers context.Context) bool {
223224
}
224225

225226
func executeTemplate(templatePath string, containers context.Context) []byte {
226-
tmpl, err := newTemplate(filepath.Base(templatePath)).ParseFiles(templatePath)
227+
templatePathList := strings.Split(templatePath, ";")
228+
tmpl, err := newTemplate(filepath.Base(templatePath)).ParseFiles(templatePathList...)
227229
if err != nil {
228230
log.Fatalf("Unable to parse template: %s", err)
229231
}
230232

231233
buf := new(bytes.Buffer)
232-
err = tmpl.ExecuteTemplate(buf, filepath.Base(templatePath), &containers)
234+
err = tmpl.ExecuteTemplate(buf, filepath.Base(templatePathList[0]), &containers)
233235
if err != nil {
234236
log.Fatalf("Template error: %s\n", err)
235237
}

0 commit comments

Comments
 (0)