Skip to content

Commit

Permalink
Support file functions in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
emilingerslev committed Oct 10, 2018
1 parent 878b86b commit bb684a2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
12 changes: 8 additions & 4 deletions cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
)

type context struct {
Vars interface{}
Args map[string]string
Vars interface{}
Args map[string]string
PlanPath string
ProjectPath string
}

var templateOutput string
Expand Down Expand Up @@ -51,8 +53,10 @@ var templateCmd = &cobra.Command{
}

context := context{
Args: namedArgs,
Vars: projectContext.Config.Variables,
Args: namedArgs,
Vars: projectContext.Config.Variables,
PlanPath: projectContext.LocalPlanPath,
ProjectPath: projectContext.ProjectPath,
}

if templateOutput == "" {
Expand Down
55 changes: 43 additions & 12 deletions pkg/templates/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package templates

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"text/template"

Expand All @@ -19,14 +22,19 @@ func GetFuncMap() template.FuncMap {
f := sprig.TxtFuncMap()

extra := template.FuncMap{
"get": TmplGet,
"string": TmplString,
"array": TmplArray,
"objectArray": TmplObjectArray,
"strConst": TmplStrConst,
"int": TmplInt,
"is": TmplIs,
"isnt": TmplIsnt,
"get": TmplGet,
"string": TmplString,
"array": TmplArray,
"objectArray": TmplObjectArray,
"strConst": TmplStrConst,
"int": TmplInt,
"is": TmplIs,
"isnt": TmplIsnt,
"toYaml": TmplToYaml,
"fromYaml": TmplFromYaml,
"getFiles": TmplGetFiles,
"getFileContent": TmplGetFileContent,
"fileExists": TmplFileExists,
}

for k, v := range extra {
Expand Down Expand Up @@ -130,13 +138,13 @@ func TmplIsnt(a interface{}, b interface{}) bool {
return a != b
}

// ToYaml takes an interface, marshals it to yaml, and returns a string. It will
// TmplToYaml takes an interface, marshals it to yaml, and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
//
// Borrowed from github.com/helm/helm/pkg/chartutil
func ToYaml(v interface{}) string {
func TmplToYaml(v interface{}) string {
data, err := yaml.Marshal(v)
if err != nil {
// Swallow errors inside of a template.
Expand All @@ -145,15 +153,15 @@ func ToYaml(v interface{}) string {
return string(data)
}

// FromYaml converts a YAML document into a map[string]interface{}.
// TmplFromYaml converts a YAML document into a map[string]interface{}.
//
// This is not a general-purpose YAML parser, and will not parse all valid
// YAML documents. Additionally, because its intended use is within templates
// it tolerates errors. It will insert the returned error message string into
// m["Error"] in the returned map.
//
// Borrowed from github.com/helm/helm/pkg/chartutil
func FromYaml(str string) map[string]interface{} {
func TmplFromYaml(str string) map[string]interface{} {
m := map[string]interface{}{}

if err := yaml.Unmarshal([]byte(str), &m); err != nil {
Expand All @@ -162,6 +170,29 @@ func FromYaml(str string) map[string]interface{} {
return m
}

func TmplGetFileContent(filePath string) string {
byteContent, err := ioutil.ReadFile(filePath)
if err != nil {
return "" // TODO: Print error to shuttle output
}
return string(byteContent)
}

func TmplFileExists(filePath string) bool {
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
return true
}
return false
}

func TmplGetFiles(directoryPath string) []os.FileInfo {
files, err := ioutil.ReadDir(directoryPath)
if err != nil {
log.Fatal(err)
}
return files
}

// TODO: Add description
func getInner(property string, input interface{}) interface{} {
switch t := input.(type) {
Expand Down

0 comments on commit bb684a2

Please sign in to comment.