2022-03-24 00:49:23 +01:00
|
|
|
package templater
|
2022-03-10 17:31:29 +01:00
|
|
|
|
|
|
|
import (
|
2022-03-24 00:49:23 +01:00
|
|
|
"bytes"
|
2022-03-11 10:39:07 +01:00
|
|
|
encjson "encoding/json"
|
2022-03-10 17:31:29 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
2022-03-11 10:39:07 +01:00
|
|
|
|
2022-03-10 17:31:29 +01:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2022-03-11 10:39:07 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2022-03-10 17:31:29 +01:00
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
2022-03-24 00:49:23 +01:00
|
|
|
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
|
|
|
)
|
2022-03-11 10:39:07 +01:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
func ProcessGoTemplate(file string, config []byte) (string, error) {
|
2022-03-10 17:31:29 +01:00
|
|
|
|
|
|
|
// The JSON configuration
|
|
|
|
var confData map[string]interface{}
|
2022-03-24 00:49:23 +01:00
|
|
|
var res bytes.Buffer
|
|
|
|
|
2022-03-11 10:39:07 +01:00
|
|
|
err := encjson.Unmarshal(config, &confData)
|
2022-03-24 00:49:23 +01:00
|
|
|
utils.CheckErr(err)
|
2022-03-10 17:31:29 +01:00
|
|
|
|
|
|
|
// Read the template
|
|
|
|
data, err := os.ReadFile(file)
|
2022-03-24 00:49:23 +01:00
|
|
|
utils.CheckErr(err)
|
2022-03-10 17:31:29 +01:00
|
|
|
|
|
|
|
tpl, err := template.New("conf").Parse(string(data))
|
2022-03-24 00:49:23 +01:00
|
|
|
utils.CheckErr(err)
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
utils.CheckErr(tpl.Execute(&res, confData["Config"]))
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
return res.String(), nil
|
2022-03-10 17:31:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
func ProcessHCLTemplate(file string, config []byte) (string, error) {
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-03-11 10:39:07 +01:00
|
|
|
fct, err := os.ReadFile(file)
|
2022-03-24 00:49:23 +01:00
|
|
|
utils.CheckErr(err)
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-03-24 00:49:23 +01:00
|
|
|
expr, diags := hclsyntax.ParseTemplate(fct, file, hcl.Pos{Line: 0, Column: 1})
|
|
|
|
utils.CheckDiags(diags)
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-03-11 10:39:07 +01:00
|
|
|
// Retrieve values from JSON
|
|
|
|
var varsVal cty.Value
|
2022-03-10 17:31:29 +01:00
|
|
|
ctyType, err := ctyjson.ImpliedType(config)
|
2022-03-11 10:39:07 +01:00
|
|
|
if err != nil {
|
2022-06-15 17:29:45 +02:00
|
|
|
return "", err
|
2022-03-11 10:39:07 +01:00
|
|
|
/* Maybe one day
|
2022-03-24 00:49:23 +01:00
|
|
|
cexpr, diags := hclsyntax.ParseExpression(config, "", hcl.Pos{Line: 0, Column: 1})
|
2022-03-11 10:39:07 +01:00
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Error())
|
|
|
|
}
|
|
|
|
varsVal, diags = cexpr.Value(&hcl.EvalContext{})
|
|
|
|
fmt.Println(cexpr.Variables())
|
|
|
|
checkDiags(diags)
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
varsVal, err = ctyjson.Unmarshal(config, ctyType)
|
2022-03-24 00:49:23 +01:00
|
|
|
utils.CheckErr(err)
|
2022-03-11 10:39:07 +01:00
|
|
|
}
|
2022-03-10 17:31:29 +01:00
|
|
|
|
|
|
|
ctx := &hcl.EvalContext{
|
|
|
|
Variables: varsVal.AsValueMap(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := range ctx.Variables {
|
|
|
|
if !hclsyntax.ValidIdentifier(n) {
|
|
|
|
panic(fmt.Errorf("invalid template variable name %q: must start with a letter, followed by zero or more letters, digits, and underscores", n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, traversal := range expr.Variables() {
|
|
|
|
root := traversal.RootName()
|
|
|
|
if _, ok := ctx.Variables[root]; !ok {
|
|
|
|
panic(fmt.Errorf("vars map does not contain key %q, referenced at %s", root, traversal[0].SourceRange()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val, diags := expr.Value(ctx)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Error())
|
|
|
|
}
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
return val.AsString(), nil
|
2022-03-11 10:39:07 +01:00
|
|
|
}
|