From ceecc230451c9afe82b495ec67908f8977254499 Mon Sep 17 00:00:00 2001 From: FogDong Date: Wed, 26 Apr 2023 17:00:30 +0800 Subject: [PATCH] add time provider Signed-off-by: FogDong --- go.mod | 4 - go.sum | 9 -- pkg/providers/legacy/legacy.go | 2 + pkg/providers/legacy/time/time.cue | 23 ++++ pkg/providers/legacy/time/time.go | 85 ++++++++++++ pkg/providers/legacy/time/time_test.go | 131 +++++++++++++++++++ pkg/providers/legacy/workspace/workspace.cue | 4 + 7 files changed, 245 insertions(+), 13 deletions(-) create mode 100644 pkg/providers/legacy/time/time.cue create mode 100644 pkg/providers/legacy/time/time.go create mode 100644 pkg/providers/legacy/time/time_test.go diff --git a/go.mod b/go.mod index 27f3f05..428df85 100644 --- a/go.mod +++ b/go.mod @@ -64,13 +64,11 @@ require ( github.com/go-openapi/jsonreference v0.20.0 // indirect github.com/go-openapi/swag v0.19.15 // indirect github.com/go-stack/stack v1.8.1 // indirect - github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/cel-go v0.12.6 // indirect github.com/google/gnostic v0.5.7-v3refs // indirect github.com/google/gofuzz v1.2.0 // indirect - github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect @@ -90,7 +88,6 @@ require ( github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/oam-dev/cluster-gateway v1.9.0-alpha.1 // indirect - github.com/onsi/ginkgo/v2 v2.9.2 // indirect github.com/openshift/library-go v0.0.0-20230327085348-8477ec72b725 // indirect github.com/pierrec/lz4 v2.6.0+incompatible // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -121,7 +118,6 @@ require ( golang.org/x/sys v0.6.0 // indirect golang.org/x/term v0.6.0 // indirect golang.org/x/text v0.8.0 // indirect - golang.org/x/tools v0.7.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20221202195650-67e5cbc046fd // indirect google.golang.org/grpc v1.50.1 // indirect diff --git a/go.sum b/go.sum index 29a1458..4a2a7e3 100644 --- a/go.sum +++ b/go.sum @@ -63,9 +63,6 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng= @@ -165,7 +162,6 @@ github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= -github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/go-zookeeper/zk v1.0.2/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -223,7 +219,6 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= -github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -271,7 +266,6 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hudl/fargo v1.4.0/go.mod h1:9Ai6uvFy5fQNq6VPKtg+Ceq1+eTY4nKUlR2JElEOcDo= -github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= @@ -382,7 +376,6 @@ github.com/onsi/ginkgo v1.16.2/go.mod h1:CObGmKUOKaSC0RjmoAK7tKyn4Azo5P2IWuoMnvw github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU= -github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= @@ -649,7 +642,6 @@ golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -717,7 +709,6 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= -golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/providers/legacy/legacy.go b/pkg/providers/legacy/legacy.go index 43e9001..3edb51e 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 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: _