fix(template): fix go txt/template support

We can parse go text/template again.
This commit is contained in:
Philippe Caseiro 2022-03-11 10:39:07 +01:00
parent 66d290c298
commit 54ef86c46e
1 changed files with 35 additions and 19 deletions

46
main.go
View File

@ -1,15 +1,14 @@
package main package main
import ( import (
encjson "encoding/json"
"fmt" "fmt"
"os" "os"
"encoding/json"
"text/template" "text/template"
//"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/hcl/v2/hclsyntax"
//"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
//"github.com/zclconf/go-cty/cty/gocty"
ctyjson "github.com/zclconf/go-cty/cty/json" ctyjson "github.com/zclconf/go-cty/cty/json"
) )
@ -19,40 +18,58 @@ func checkErr(e error) {
} }
} }
func checkDiags(diag hcl.Diagnostics) {
if diag.HasErrors() {
panic(diag.Error())
}
}
func processGoTemplate(file string, config []byte) { func processGoTemplate(file string, config []byte) {
// The JSON configuration // The JSON configuration
var confData map[string]interface{} var confData map[string]interface{}
err := json.Unmarshal(config, &confData) err := encjson.Unmarshal(config, &confData)
checkErr(err) checkErr(err)
// Read the template // Read the template
data, err := os.ReadFile(file) data, err := os.ReadFile(file)
checkErr(err) checkErr(err)
tpl, err := template.New("conf").Parse(string(data)) tpl, err := template.New("conf").Parse(string(data))
checkErr(err) checkErr(err)
checkErr(tpl.Execute(os.Stdout,config)) checkErr(tpl.Execute(os.Stdout, confData))
} }
func processHCLTemplate(file string, config []byte) { func processHCLTemplate(file string, config []byte) {
fct, err:= os.ReadFile(file) fct, err := os.ReadFile(file)
checkErr(err) checkErr(err)
expr, diags := hclsyntax.ParseTemplate(fct, file, hcl.Pos{Line:1, Column: 1}) expr, diags := hclsyntax.ParseTemplate(fct, file, hcl.Pos{Line: 1, Column: 1})
checkDiags(diags)
// Retrieve values from JSON
var varsVal cty.Value
ctyType, err := ctyjson.ImpliedType(config)
if err != nil {
panic(err)
/* Maybe one day
cexpr, diags := hclsyntax.ParseExpression(config, "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() { if diags.HasErrors() {
panic(diags.Error()) panic(diags.Error())
} }
varsVal, diags = cexpr.Value(&hcl.EvalContext{})
ctyType, err := ctyjson.ImpliedType(config) fmt.Println(cexpr.Variables())
checkErr(err) checkDiags(diags)
varsVal, err := ctyjson.Unmarshal(config,ctyType) */
} else {
varsVal, err = ctyjson.Unmarshal(config, ctyType)
checkErr(err) checkErr(err)
}
fmt.Println(varsVal.AsValueMap())
ctx := &hcl.EvalContext{ ctx := &hcl.EvalContext{
Variables: varsVal.AsValueMap(), Variables: varsVal.AsValueMap(),
} }
@ -75,7 +92,7 @@ func processHCLTemplate(file string, config []byte) {
panic(diags.Error()) panic(diags.Error())
} }
fmt.Printf("%s",val.AsString()) fmt.Printf("%s", val.AsString())
} }
func main() { func main() {
@ -84,7 +101,6 @@ func main() {
templateFile := os.Args[2] templateFile := os.Args[2]
config := []byte(os.Args[3]) config := []byte(os.Args[3])
if templateType == "go" { if templateType == "go" {
processGoTemplate(templateFile, config) processGoTemplate(templateFile, config)
} else if templateType == "hcl" { } else if templateType == "hcl" {