diff --git a/pkg/providers/legacy/legacy.go b/pkg/providers/legacy/legacy.go index 43e9001..80b48d1 100644 --- a/pkg/providers/legacy/legacy.go +++ b/pkg/providers/legacy/legacy.go @@ -9,6 +9,7 @@ import ( "github.com/kubevela/workflow/pkg/providers/legacy/http" "github.com/kubevela/workflow/pkg/providers/legacy/kube" "github.com/kubevela/workflow/pkg/providers/legacy/metrics" + "github.com/kubevela/workflow/pkg/providers/legacy/time" "github.com/kubevela/workflow/pkg/providers/legacy/util" "github.com/kubevela/workflow/pkg/providers/legacy/workspace" ) @@ -27,6 +28,7 @@ func GetLegacyProviders() map[string]cuexruntime.ProviderFn { registerProviders(providers, http.GetProviders()) registerProviders(providers, kube.GetProviders()) registerProviders(providers, metrics.GetProviders()) + registerProviders(providers, time.GetProviders()) registerProviders(providers, util.GetProviders()) registerProviders(providers, workspace.GetProviders()) return providers @@ -34,5 +36,14 @@ func GetLegacyProviders() map[string]cuexruntime.ProviderFn { // GetLegacyTemplate get legacy template func GetLegacyTemplate() string { - return strings.Join([]string{email.GetTemplate(), http.GetTemplate(), kube.GetTemplate(), metrics.GetTemplate(), util.GetTemplate(), workspace.GetTemplate()}, "\n") + return strings.Join([]string{ + email.GetTemplate(), + http.GetTemplate(), + kube.GetTemplate(), + metrics.GetTemplate(), + time.GetTemplate(), + util.GetTemplate(), + workspace.GetTemplate(), + }, + "\n") } diff --git a/pkg/providers/legacy/time/time.cue b/pkg/providers/legacy/time/time.cue new file mode 100644 index 0000000..74498db --- /dev/null +++ b/pkg/providers/legacy/time/time.cue @@ -0,0 +1,23 @@ +// time.cue + +#DateToTimestamp: { + #do: "timestamp" + #provider: "op" + + date: string + layout: *"" | string + + timestamp?: int64 + ... +} + +#TimestampToDate: { + #do: "date" + #provider: "op" + + timestamp: int64 + layout: *"" | string + + date?: string + ... +} diff --git a/pkg/providers/legacy/time/time.go b/pkg/providers/legacy/time/time.go new file mode 100644 index 0000000..6db4543 --- /dev/null +++ b/pkg/providers/legacy/time/time.go @@ -0,0 +1,85 @@ +/* + Copyright 2021. The KubeVela Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package time + +import ( + "context" + _ "embed" + "fmt" + "time" + + cuexruntime "github.com/kubevela/pkg/cue/cuex/runtime" + providertypes "github.com/kubevela/workflow/pkg/providers/types" +) + +// Vars . +type Vars struct { + Date string `json:"date,omitempty"` + Timestamp int64 `json:"timestamp,omitempty"` + Layout string `json:"layout,omitempty"` +} + +// Params . +type Params = providertypes.LegacyParams[Vars] + +// Timestamp convert date to timestamp +func Timestamp(ctx context.Context, params *Params) (*Vars, error) { + date := params.Params.Date + layout := params.Params.Layout + if date == "" { + return nil, fmt.Errorf("empty date to convert") + } + if layout == "" { + layout = time.RFC3339 + } + t, err := time.Parse(layout, date) + if err != nil { + return nil, err + } + return &Vars{ + Timestamp: t.Unix(), + }, nil +} + +// Date convert timestamp to date +func Date(ctx context.Context, params *Params) (*Vars, error) { + timestamp := params.Params.Timestamp + layout := params.Params.Layout + if layout == "" { + layout = time.RFC3339 + } + t := time.Unix(timestamp, 0) + return &Vars{ + Date: t.UTC().Format(layout), + }, nil +} + +//go:embed time.cue +var template string + +// GetTemplate return the template +func GetTemplate() string { + return template +} + +// GetProviders return the provider +func GetProviders() map[string]cuexruntime.ProviderFn { + return map[string]cuexruntime.ProviderFn{ + "timestamp": providertypes.LegacyGenericProviderFn[Vars, Vars](Timestamp), + "date": providertypes.LegacyGenericProviderFn[Vars, Vars](Date), + } +} diff --git a/pkg/providers/legacy/time/time_test.go b/pkg/providers/legacy/time/time_test.go new file mode 100644 index 0000000..80214d8 --- /dev/null +++ b/pkg/providers/legacy/time/time_test.go @@ -0,0 +1,131 @@ +/* + Copyright 2021. The KubeVela Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package time + +import ( + "context" + "fmt" + "testing" + + "github.com/pkg/errors" + "github.com/stretchr/testify/require" +) + +func TestTimestamp(t *testing.T) { + ctx := context.Background() + testcases := map[string]struct { + from Vars + expected int64 + expectedErr error + }{ + "test convert date with default time layout": { + from: Vars{ + Date: "2021-11-07T01:47:51Z", + }, + expected: 1636249671, + expectedErr: nil, + }, + "test convert date with RFC3339 layout": { + from: Vars{ + Date: "2021-11-07T01:47:51Z", + Layout: "2006-01-02T15:04:05Z07:00", + }, + expected: 1636249671, + expectedErr: nil, + }, + "test convert date with RFC1123 layout": { + from: Vars{ + Date: "Fri, 01 Mar 2019 15:00:00 GMT", + Layout: "Mon, 02 Jan 2006 15:04:05 MST", + }, + expected: 1551452400, + expectedErr: nil, + }, + "test convert without date": { + from: Vars{}, + expected: 0, + expectedErr: fmt.Errorf("empty date to convert"), + }, + "test convert date with wrong time layout": { + from: Vars{ + Date: "2021-11-07T01:47:51Z", + Layout: "Mon, 02 Jan 2006 15:04:05 MST", + }, + expected: 0, + expectedErr: errors.New(`parsing time "2021-11-07T01:47:51Z" as "Mon, 02 Jan 2006 15:04:05 MST": cannot parse "2021-11-07T01:47:51Z" as "Mon"`), + }, + } + + for name, tc := range testcases { + t.Run(name, func(t *testing.T) { + r := require.New(t) + res, err := Timestamp(ctx, &Params{ + Params: tc.from, + }) + if tc.expectedErr != nil { + r.Equal(tc.expectedErr.Error(), err.Error()) + return + } + r.NoError(err) + r.Equal(tc.expected, res.Timestamp) + }) + } +} + +func TestDate(t *testing.T) { + ctx := context.Background() + testcases := map[string]struct { + from Vars + expected string + }{ + "test convert timestamp to default time layout": { + from: Vars{ + Timestamp: 1636249671, + }, + expected: "2021-11-07T01:47:51Z", + }, + "test convert date to RFC3339 layout": { + from: Vars{ + Timestamp: 1636249671, + Layout: "2006-01-02T15:04:05Z07:00", + }, + expected: "2021-11-07T01:47:51Z", + }, + "test convert date to RFC1123 layout": { + from: Vars{ + Timestamp: 1551452400, + Layout: "Mon, 02 Jan 2006 15:04:05 MST", + }, + expected: "Fri, 01 Mar 2019 15:00:00 UTC", + }, + "test convert without timestamp": { + from: Vars{}, + expected: "1970-01-01T00:00:00Z", + }, + } + + for name, tc := range testcases { + t.Run(name, func(t *testing.T) { + r := require.New(t) + res, err := Date(ctx, &Params{ + Params: tc.from, + }) + r.NoError(err) + r.Equal(tc.expected, res.Date) + }) + } +} diff --git a/pkg/providers/legacy/workspace/workspace.cue b/pkg/providers/legacy/workspace/workspace.cue index 67620a6..7a169d0 100644 --- a/pkg/providers/legacy/workspace/workspace.cue +++ b/pkg/providers/legacy/workspace/workspace.cue @@ -56,4 +56,8 @@ message?: string } +#Steps: { + ... +} +NoExist: _|_ context: _