Merge bootstraper #3

Merged
pcaseiro merged 11 commits from dev into master 2023-01-12 11:46:21 +01:00
1 changed files with 35 additions and 19 deletions
Showing only changes of commit 54ef86c46e - Show all commits

54
main.go
View File

@ -1,15 +1,14 @@
package main
import (
encjson "encoding/json"
"fmt"
"os"
"encoding/json"
"text/template"
//"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
//"github.com/zclconf/go-cty/cty"
//"github.com/zclconf/go-cty/cty/gocty"
"github.com/zclconf/go-cty/cty"
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) {
// The JSON configuration
var confData map[string]interface{}
err := json.Unmarshal(config, &confData)
err := encjson.Unmarshal(config, &confData)
checkErr(err)
// Read the template
data, err := os.ReadFile(file)
checkErr(err)
tpl, err := template.New("conf").Parse(string(data))
checkErr(err)
checkErr(tpl.Execute(os.Stdout,config))
checkErr(tpl.Execute(os.Stdout, confData))
}
func processHCLTemplate(file string, config []byte) {
fct, err:= os.ReadFile(file)
fct, err := os.ReadFile(file)
checkErr(err)
expr, diags := hclsyntax.ParseTemplate(fct, file, hcl.Pos{Line:1, Column: 1})
if diags.HasErrors() {
panic(diags.Error())
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() {
panic(diags.Error())
}
varsVal, diags = cexpr.Value(&hcl.EvalContext{})
fmt.Println(cexpr.Variables())
checkDiags(diags)
*/
} else {
varsVal, err = ctyjson.Unmarshal(config, ctyType)
checkErr(err)
}
ctyType, err := ctyjson.ImpliedType(config)
checkErr(err)
varsVal, err := ctyjson.Unmarshal(config,ctyType)
checkErr(err)
fmt.Println(varsVal.AsValueMap())
ctx := &hcl.EvalContext{
Variables: varsVal.AsValueMap(),
}
@ -75,7 +92,7 @@ func processHCLTemplate(file string, config []byte) {
panic(diags.Error())
}
fmt.Printf("%s",val.AsString())
fmt.Printf("%s", val.AsString())
}
func main() {
@ -84,7 +101,6 @@ func main() {
templateFile := os.Args[2]
config := []byte(os.Args[3])
if templateType == "go" {
processGoTemplate(templateFile, config)
} else if templateType == "hcl" {
@ -92,4 +108,4 @@ func main() {
} else {
panic(fmt.Errorf("Unsupported template type"))
}
}
}