mirror of
https://github.com/hashicorp/packer.git
synced 2026-05-28 04:35:38 -04:00
This commit adds 3 new HCL2 functions: * `sum`: computes the sum of a collection of numerical values * `startswith`: checks if a string has another as prefix * `endswith`: checks if a string has another as suffix
32 lines
734 B
Go
32 lines
734 B
Go
package function
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
"github.com/zclconf/go-cty/cty/function"
|
|
)
|
|
|
|
// StartsWithFunc constructs a function that checks if a string starts with
|
|
// a specific prefix using strings.HasPrefix
|
|
var StartsWithFunc = function.New(&function.Spec{
|
|
Params: []function.Parameter{
|
|
{
|
|
Name: "str",
|
|
Type: cty.String,
|
|
AllowUnknown: false,
|
|
},
|
|
{
|
|
Name: "prefix",
|
|
Type: cty.String,
|
|
},
|
|
},
|
|
Type: function.StaticReturnType(cty.Bool),
|
|
RefineResult: refineNotNull,
|
|
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
|
str := args[0].AsString()
|
|
prefix := args[1].AsString()
|
|
|
|
return cty.BoolVal(strings.HasPrefix(str, prefix)), nil
|
|
},
|
|
})
|