From 691723f9f9fd61339cf6e2fadd7d47612335d9bc Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Mon, 18 Jul 2022 11:29:35 +0100 Subject: [PATCH] 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 --- bake/hclparser/stdlib.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bake/hclparser/stdlib.go b/bake/hclparser/stdlib.go index 8ee31748..f78b6dce 100644 --- a/bake/hclparser/stdlib.go +++ b/bake/hclparser/stdlib.go @@ -1,12 +1,15 @@ package hclparser import ( + "time" + "github.com/hashicorp/go-cty-funcs/cidr" "github.com/hashicorp/go-cty-funcs/crypto" "github.com/hashicorp/go-cty-funcs/encoding" "github.com/hashicorp/go-cty-funcs/uuid" "github.com/hashicorp/hcl/v2/ext/tryfunc" "github.com/hashicorp/hcl/v2/ext/typeexpr" + "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/function" "github.com/zclconf/go-cty/cty/function/stdlib" ) @@ -96,6 +99,7 @@ var stdlibFunctions = map[string]function.Function{ "substr": stdlib.SubstrFunc, "subtract": stdlib.SubtractFunc, "timeadd": stdlib.TimeAddFunc, + "timestamp": timestampFunc, "title": stdlib.TitleFunc, "trim": stdlib.TrimFunc, "trimprefix": stdlib.TrimPrefixFunc, @@ -109,3 +113,14 @@ var stdlibFunctions = map[string]function.Function{ "values": stdlib.ValuesFunc, "zipmap": stdlib.ZipmapFunc, } + +// timestampFunc constructs a function that returns a string representation of the current date and time. +// +// This function was imported from terraform's datetime utilities. +var timestampFunc = function.New(&function.Spec{ + Params: []function.Parameter{}, + Type: function.StaticReturnType(cty.String), + Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { + return cty.StringVal(time.Now().UTC().Format(time.RFC3339)), nil + }, +})