From bafb792339b5898a0e6b6219ad54b5f501d727c2 Mon Sep 17 00:00:00 2001 From: Niklas Wik Date: Tue, 9 Jan 2018 15:12:50 +0200 Subject: [PATCH] feat(templater): Added support for file include Adds a 'fileContent' template function to insert the literal contents of a file specified in the template. Signed-off-by: Niklas Wik --- docs/templates.md | 5 +++-- example/some-api/some-api.yaml | 2 ++ templater/fromfile.go | 24 ++++++++++++++++++++++++ templater/templater.go | 1 + 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 templater/fromfile.go diff --git a/docs/templates.md b/docs/templates.md index 8bcc2c7f5..d9ff0fd12 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -72,10 +72,11 @@ right. Some template functions come from Go's standard library and are listed in the [Go documentation][]. In addition the functions declared by [sprig][] are -available in kontemplate, as well as two custom functions: +available in kontemplate, as well as three custom functions: `json`: Encodes any supplied data structure as JSON. -`passLookup`: Looks up the supplied key in [pass][] +`passLookup`: Looks up the supplied key in [pass][]. +`fromFile`: Insert the contents of the given file from the resource set folder. ## Examples: diff --git a/example/some-api/some-api.yaml b/example/some-api/some-api.yaml index 57ab7c652..44b615417 100644 --- a/example/some-api/some-api.yaml +++ b/example/some-api/some-api.yaml @@ -25,6 +25,8 @@ spec: value: {{ .importantFeature }} - name: SOME_GLOBAL_VAR value: {{ .globalVar }} + - name: FILE_VAR + value: {{ fileContent "some-api/filevar.txt" }} --- apiVersion: v1 kind: Service diff --git a/templater/fromfile.go b/templater/fromfile.go new file mode 100644 index 000000000..f4f1e79cb --- /dev/null +++ b/templater/fromfile.go @@ -0,0 +1,24 @@ +// Copyright (C) 2017 Niklas Wik +// +// This file is part of Kontemplate. +// +// Kontemplate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +package templater + +import ( + "io/ioutil" +) + +//GetFromFile returns file content as string +func GetFromFile(file string) (string, error) { + + data, err := ioutil.ReadFile(file) + if err != nil { + return "", err + } + return string(data), nil +} diff --git a/templater/templater.go b/templater/templater.go index 7fb85e8b2..5bdb2f2e3 100644 --- a/templater/templater.go +++ b/templater/templater.go @@ -193,6 +193,7 @@ func templateFuncs() template.FuncMap { } m["passLookup"] = GetFromPass m["lookupIPAddr"] = GetIPsFromDNS + m["fileContent"] = GetFromFile return m }