package main import ( "fmt" "io/ioutil" "os" "path/filepath" "strconv" "forge.cadoles.com/pcaseiro/templatefile/pkg/templater" "forge.cadoles.com/pcaseiro/templatefile/pkg/utils" "github.com/alexflint/go-arg" "github.com/zclconf/go-cty/cty" ctyjson "github.com/zclconf/go-cty/cty/json" ) func main() { var args struct { ConfigDirectory string `arg:"-c,--config-dir,env:CONFIG_DIR" help:"Configuration values directory path" default:"./data/config"` TemplateDirectory string `arg:"-t,--template-dir,env:TEMPLATE_DIR" help:"Template directory path" default:"./data/templates"` } arg.MustParse(&args) files, err := ioutil.ReadDir(args.ConfigDirectory) if err != nil { panic(err) } for _, file := range files { var varsVal cty.Value fname := fmt.Sprintf("%s/%s", args.ConfigDirectory, file.Name()) flContent, err := os.ReadFile(fname) if err != nil { panic(err) } ctyType, err := ctyjson.ImpliedType(flContent) if err != nil { panic(err) /* Maybe one day cexpr, diags := hclsyntax.ParseExpression(config, "", hcl.Pos{Line: 0, 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(flContent, ctyType) utils.CheckErr(err) } cnfVals := varsVal.AsValueMap() config := cnfVals["Config"].AsValueMap() fls := config["ConfigFiles"].AsValueSlice() for _, fl := range fls { data := fl.AsValueMap() dest := filepath.Join("/tmp/test", data["destination"].AsString()) source := filepath.Join(args.TemplateDirectory, data["source"].AsString()) mod := data["mod"].AsString() intMod, err := strconv.Atoi(mod) if err != nil { panic(err) } fmt.Printf("Processing %s\n", source) fileExtension := filepath.Ext(source) if fileExtension == ".hcl" { res := templater.ProcessHCLTemplate(source, flContent) dirname := filepath.Dir(dest) err = os.MkdirAll(dirname, os.FileMode(int(0700))) if err != nil { panic(err) } err = os.WriteFile(dest, []byte(res), os.FileMode(intMod)) if err != nil { panic(err) } } } } }