Skip to content

Commit 691723f

Browse files
committed
bake: add timestamp function
Terraform includes a timestamp function to get the current time. go-cty has imported a number of the timestamp functions to it's standard library, however, this was one was not included. This patch simply pulls in the TimestampFunc from Terraform's internal/lang/funcs/datetime.go to allow easily fetching the current time in bake. Signed-off-by: Justin Chadwell <[email protected]>
1 parent 724cb29 commit 691723f

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

bake/hclparser/stdlib.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package hclparser
22

33
import (
4+
"time"
5+
46
"github.com/hashicorp/go-cty-funcs/cidr"
57
"github.com/hashicorp/go-cty-funcs/crypto"
68
"github.com/hashicorp/go-cty-funcs/encoding"
79
"github.com/hashicorp/go-cty-funcs/uuid"
810
"github.com/hashicorp/hcl/v2/ext/tryfunc"
911
"github.com/hashicorp/hcl/v2/ext/typeexpr"
12+
"github.com/zclconf/go-cty/cty"
1013
"github.com/zclconf/go-cty/cty/function"
1114
"github.com/zclconf/go-cty/cty/function/stdlib"
1215
)
@@ -96,6 +99,7 @@ var stdlibFunctions = map[string]function.Function{
9699
"substr": stdlib.SubstrFunc,
97100
"subtract": stdlib.SubtractFunc,
98101
"timeadd": stdlib.TimeAddFunc,
102+
"timestamp": timestampFunc,
99103
"title": stdlib.TitleFunc,
100104
"trim": stdlib.TrimFunc,
101105
"trimprefix": stdlib.TrimPrefixFunc,
@@ -109,3 +113,14 @@ var stdlibFunctions = map[string]function.Function{
109113
"values": stdlib.ValuesFunc,
110114
"zipmap": stdlib.ZipmapFunc,
111115
}
116+
117+
// timestampFunc constructs a function that returns a string representation of the current date and time.
118+
//
119+
// This function was imported from terraform's datetime utilities.
120+
var timestampFunc = function.New(&function.Spec{
121+
Params: []function.Parameter{},
122+
Type: function.StaticReturnType(cty.String),
123+
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
124+
return cty.StringVal(time.Now().UTC().Format(time.RFC3339)), nil
125+
},
126+
})

0 commit comments

Comments
 (0)