bake: support unix output in formattimestamp
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -286,7 +287,7 @@ func semvercmpFunc() function.Function {
|
||||
// formatdate.
|
||||
func formatTimestampFunc() function.Function {
|
||||
return function.New(&function.Spec{
|
||||
Description: `Formats a timestamp string in RFC 3339 syntax or a unix timestamp integer into another timestamp in some other machine-oriented time syntax, as described in the format string.`,
|
||||
Description: `Formats a timestamp string in RFC 3339 syntax or a unix timestamp integer into another timestamp in some other machine-oriented time syntax, as described in the format string. The special format string "X" returns the unix timestamp in seconds.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "format",
|
||||
@@ -299,14 +300,25 @@ func formatTimestampFunc() function.Function {
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
formatStr := args[0].AsString()
|
||||
switch args[1].Type() {
|
||||
case cty.String:
|
||||
if formatStr == "X" {
|
||||
t, err := time.Parse(time.RFC3339, args[1].AsString())
|
||||
if err != nil {
|
||||
return cty.DynamicVal, function.NewArgErrorf(1, "timestamp string must be RFC3339")
|
||||
}
|
||||
return cty.StringVal(strconv.FormatInt(t.Unix(), 10)), nil
|
||||
}
|
||||
return stdlib.FormatDateFunc.Call([]cty.Value{args[0], args[1]})
|
||||
case cty.Number:
|
||||
t, err := unixTimestampValue(args[1])
|
||||
if err != nil {
|
||||
return cty.DynamicVal, function.NewArgError(1, err)
|
||||
}
|
||||
if formatStr == "X" {
|
||||
return cty.StringVal(strconv.FormatInt(t.Unix(), 10)), nil
|
||||
}
|
||||
return stdlib.FormatDateFunc.Call([]cty.Value{args[0], cty.StringVal(t.Format(time.RFC3339))})
|
||||
default:
|
||||
return cty.DynamicVal, function.NewArgErrorf(1, "must be a string timestamp or a unix timestamp number")
|
||||
|
||||
@@ -353,6 +353,16 @@ func TestFormatTimestampFunc(t *testing.T) {
|
||||
wantErr bool
|
||||
}
|
||||
tests := map[string]testCase{
|
||||
"unix format from rfc3339 string": {
|
||||
format: cty.StringVal("X"),
|
||||
input: cty.StringVal("2015-10-21T00:00:00Z"),
|
||||
want: cty.StringVal("1445385600"),
|
||||
},
|
||||
"unix format from unix timestamp input": {
|
||||
format: cty.StringVal("X"),
|
||||
input: cty.NumberIntVal(1445385600),
|
||||
want: cty.StringVal("1445385600"),
|
||||
},
|
||||
"rfc3339 string input": {
|
||||
format: cty.StringVal("YYYY-MM-DD"),
|
||||
input: cty.StringVal("2025-09-16T12:00:00Z"),
|
||||
@@ -378,6 +388,11 @@ func TestFormatTimestampFunc(t *testing.T) {
|
||||
input: cty.StringVal("0"),
|
||||
wantErr: true,
|
||||
},
|
||||
"invalid string input for unix format": {
|
||||
format: cty.StringVal("X"),
|
||||
input: cty.StringVal("0"),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user