diff --git a/cmd/template.go b/cmd/template.go index 881f791..007de73 100644 --- a/cmd/template.go +++ b/cmd/template.go @@ -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 @@ -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 == "" { diff --git a/pkg/templates/funcs.go b/pkg/templates/funcs.go index 78c9b29..dd76a94 100644 --- a/pkg/templates/funcs.go +++ b/pkg/templates/funcs.go @@ -2,6 +2,9 @@ package templates import ( "fmt" + "io/ioutil" + "log" + "os" "strings" "text/template" @@ -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 { @@ -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. @@ -145,7 +153,7 @@ 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 @@ -153,7 +161,7 @@ func ToYaml(v interface{}) string { // 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 { @@ -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) {