create util function in hcl2 template that will load config values into hcl values without panicing if it finds something it cannot handle
This commit is contained in:
parent
c8d5d56f61
commit
e3b14d888b
|
@ -2,7 +2,6 @@ package hcl2template
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
|
@ -33,18 +32,12 @@ func (p *HCL2PostProcessor) HCL2Prepare(buildVars map[string]interface{}) error
|
|||
ectx = p.evalContext.NewChild()
|
||||
buildValues := map[string]cty.Value{}
|
||||
for k, v := range buildVars {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
buildValues[k] = cty.StringVal(v)
|
||||
case int64:
|
||||
buildValues[k] = cty.NumberIntVal(v)
|
||||
case uint64:
|
||||
buildValues[k] = cty.NumberUIntVal(v)
|
||||
case bool:
|
||||
buildValues[k] = cty.BoolVal(v)
|
||||
default:
|
||||
return fmt.Errorf("unhandled buildvar type: %T", v)
|
||||
val, err := ConvertPluginConfigValueToHCLValue(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buildValues[k] = val
|
||||
}
|
||||
ectx.Variables = map[string]cty.Value{
|
||||
buildAccessor: cty.ObjectVal(buildValues),
|
||||
|
|
|
@ -2,7 +2,6 @@ package hcl2template
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
|
@ -37,20 +36,12 @@ func (p *HCL2Provisioner) HCL2Prepare(buildVars map[string]interface{}) error {
|
|||
buildValues = p.evalContext.Variables[buildAccessor].AsValueMap()
|
||||
}
|
||||
for k, v := range buildVars {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
buildValues[k] = cty.StringVal(v)
|
||||
case uint8:
|
||||
buildValues[k] = cty.NumberUIntVal(uint64(v))
|
||||
case int64:
|
||||
buildValues[k] = cty.NumberIntVal(v)
|
||||
case uint64:
|
||||
buildValues[k] = cty.NumberUIntVal(v)
|
||||
case bool:
|
||||
buildValues[k] = cty.BoolVal(v)
|
||||
default:
|
||||
return fmt.Errorf("unhandled buildvar type: %T", v)
|
||||
val, err := ConvertPluginConfigValueToHCLValue(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buildValues[k] = val
|
||||
}
|
||||
ectx.Variables = map[string]cty.Value{
|
||||
buildAccessor: cty.ObjectVal(buildValues),
|
||||
|
|
|
@ -124,3 +124,48 @@ func PrintableCtyValue(v cty.Value) string {
|
|||
str := repl.FormatResult(gval)
|
||||
return str
|
||||
}
|
||||
|
||||
func ConvertPluginConfigValueToHCLValue(v interface{}) (cty.Value, error) {
|
||||
var buildValue cty.Value
|
||||
switch v := v.(type) {
|
||||
case bool:
|
||||
buildValue = cty.BoolVal(v)
|
||||
case string:
|
||||
buildValue = cty.StringVal(v)
|
||||
case uint8:
|
||||
buildValue = cty.NumberUIntVal(uint64(v))
|
||||
case float64:
|
||||
buildValue = cty.NumberFloatVal(v)
|
||||
case int64:
|
||||
buildValue = cty.NumberIntVal(v)
|
||||
case uint64:
|
||||
buildValue = cty.NumberUIntVal(v)
|
||||
case []string:
|
||||
vals := make([]cty.Value, len(v))
|
||||
for i, ev := range v {
|
||||
vals[i] = cty.StringVal(ev)
|
||||
}
|
||||
buildValue = cty.ListVal(vals)
|
||||
case []uint8:
|
||||
vals := make([]cty.Value, len(v))
|
||||
for i, ev := range v {
|
||||
vals[i] = cty.NumberUIntVal(uint64(ev))
|
||||
}
|
||||
buildValue = cty.ListVal(vals)
|
||||
case []int64:
|
||||
vals := make([]cty.Value, len(v))
|
||||
for i, ev := range v {
|
||||
vals[i] = cty.NumberIntVal(ev)
|
||||
}
|
||||
buildValue = cty.ListVal(vals)
|
||||
case []uint64:
|
||||
vals := make([]cty.Value, len(v))
|
||||
for i, ev := range v {
|
||||
vals[i] = cty.NumberUIntVal(ev)
|
||||
}
|
||||
buildValue = cty.ListVal(vals)
|
||||
default:
|
||||
return cty.Value{}, fmt.Errorf("unhandled buildvar type: %T", v)
|
||||
}
|
||||
return buildValue, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue