diff --git a/docs/cmd/tkn.md b/docs/cmd/tkn.md index e0bbd27236..baa43b9694 100644 --- a/docs/cmd/tkn.md +++ b/docs/cmd/tkn.md @@ -25,7 +25,7 @@ CLI for tekton pipelines * [tkn completion](tkn_completion.md) - Prints shell completion scripts * [tkn customrun](tkn_customrun.md) - Manage CustomRuns * [tkn eventlistener](tkn_eventlistener.md) - Manage EventListeners -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn pipeline](tkn_pipeline.md) - Manage pipelines * [tkn pipelinerun](tkn_pipelinerun.md) - Manage PipelineRuns * [tkn task](tkn_task.md) - Manage Tasks diff --git a/docs/cmd/tkn_hub.md b/docs/cmd/tkn_hub.md index bf918041d0..edebc7a1b4 100644 --- a/docs/cmd/tkn_hub.md +++ b/docs/cmd/tkn_hub.md @@ -1,6 +1,6 @@ ## tkn hub -Interact with tekton hub +Interact with artifacthub ### Usage @@ -10,26 +10,38 @@ tkn hub ### Synopsis -Interact with tekton hub +Interact with artifacthub + +Deprecation Notice: Tekton Hub support in CLI is being deprecated in favor of Artifact Hub. +The following commands currently only work with Tekton Hub and may support Artifact Hub in a future release: + - check-upgrade + - downgrade + - get + - info + - reinstall + - search + - upgrade + +Action Required: Users should migrate to Artifact Hub by using the '--type artifact' flag +with the install command. For example: + tkn hub install task foo --type artifact --from tekton-catalog-tasks + +When using '--type tekton', a deprecation warning will now be displayed. +Artifact Hub (https://artifacthub.io) will become the only supported hub in future releases. ### Options ``` - --api-server string Hub API Server URL (default 'https://api.hub.tekton.dev' for 'tekton' type; default 'https://artifacthub.io' for 'artifact' type). - URL can also be defined in a file '$HOME/.tekton/hub-config' with a variable 'TEKTON_HUB_API_SERVER'/'ARTIFACT_HUB_API_SERVER'. + --api-server string Hub API Server URL. + For artifact type: default 'https://artifacthub.io' (env: ARTIFACT_HUB_API_SERVER) + For tekton type (DEPRECATED): default 'https://api.hub.tekton.dev' (env: TEKTON_HUB_API_SERVER) + Can also be set in '$HOME/.tekton/hub-config'. -h, --help help for hub - --type string The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (default "tekton") + --type string The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (DEPRECATED: tekton type will be removed in a future release) (default "artifact") ``` ### SEE ALSO * [tkn](tkn.md) - CLI for tekton pipelines -* [tkn hub check-upgrade](tkn_hub_check-upgrade.md) - Check for upgrades of resources if present -* [tkn hub downgrade](tkn_hub_downgrade.md) - Downgrade an installed resource -* [tkn hub get](tkn_hub_get.md) - Get resource manifest by its name, kind, catalog, and version -* [tkn hub info](tkn_hub_info.md) - Display info of resource by its name, kind, catalog, and version * [tkn hub install](tkn_hub_install.md) - Install a resource from a catalog by its kind, name and version -* [tkn hub reinstall](tkn_hub_reinstall.md) - Reinstall a resource by its kind and name -* [tkn hub search](tkn_hub_search.md) - Search resource by a combination of name, kind, categories, platforms, and tags -* [tkn hub upgrade](tkn_hub_upgrade.md) - Upgrade an installed resource diff --git a/docs/cmd/tkn_hub_check-upgrade.md b/docs/cmd/tkn_hub_check-upgrade.md index 0b352dc4c2..278fd3bc6e 100644 --- a/docs/cmd/tkn_hub_check-upgrade.md +++ b/docs/cmd/tkn_hub_check-upgrade.md @@ -31,6 +31,6 @@ Check for upgrades of resources if present ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn hub check-upgrade task](tkn_hub_check-upgrade_task.md) - Check updates for Task installed via Hub CLI diff --git a/docs/cmd/tkn_hub_downgrade.md b/docs/cmd/tkn_hub_downgrade.md index 338810db9d..d419c2c9dc 100644 --- a/docs/cmd/tkn_hub_downgrade.md +++ b/docs/cmd/tkn_hub_downgrade.md @@ -32,6 +32,6 @@ Downgrade an installed resource ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn hub downgrade task](tkn_hub_downgrade_task.md) - Downgrade an installed Task by its name to a lower version diff --git a/docs/cmd/tkn_hub_get.md b/docs/cmd/tkn_hub_get.md index b1c8d5a488..6506e24b3f 100644 --- a/docs/cmd/tkn_hub_get.md +++ b/docs/cmd/tkn_hub_get.md @@ -30,7 +30,7 @@ Get resource manifest by its name, kind, catalog, and version ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn hub get pipeline](tkn_hub_get_pipeline.md) - Get Pipeline by name, catalog and version * [tkn hub get task](tkn_hub_get_task.md) - Get Task by name, catalog and version diff --git a/docs/cmd/tkn_hub_info.md b/docs/cmd/tkn_hub_info.md index c0d5c0b1da..6d4e6bc426 100644 --- a/docs/cmd/tkn_hub_info.md +++ b/docs/cmd/tkn_hub_info.md @@ -30,6 +30,6 @@ Display info of resource by its name, kind, catalog, and version ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn hub info task](tkn_hub_info_task.md) - Display info of Task by its name, catalog and version diff --git a/docs/cmd/tkn_hub_install.md b/docs/cmd/tkn_hub_install.md index b96668014c..7636a520a6 100644 --- a/docs/cmd/tkn_hub_install.md +++ b/docs/cmd/tkn_hub_install.md @@ -26,13 +26,15 @@ Install a resource from a catalog by its kind, name and version ### Options inherited from parent commands ``` - --api-server string Hub API Server URL (default 'https://api.hub.tekton.dev' for 'tekton' type; default 'https://artifacthub.io' for 'artifact' type). - URL can also be defined in a file '$HOME/.tekton/hub-config' with a variable 'TEKTON_HUB_API_SERVER'/'ARTIFACT_HUB_API_SERVER'. - --type string The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (default "tekton") + --api-server string Hub API Server URL. + For artifact type: default 'https://artifacthub.io' (env: ARTIFACT_HUB_API_SERVER) + For tekton type (DEPRECATED): default 'https://api.hub.tekton.dev' (env: TEKTON_HUB_API_SERVER) + Can also be set in '$HOME/.tekton/hub-config'. + --type string The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (DEPRECATED: tekton type will be removed in a future release) (default "artifact") ``` ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn hub install task](tkn_hub_install_task.md) - Install Task from a catalog by its name and version diff --git a/docs/cmd/tkn_hub_install_task.md b/docs/cmd/tkn_hub_install_task.md index 135dcf9340..f3304fd968 100644 --- a/docs/cmd/tkn_hub_install_task.md +++ b/docs/cmd/tkn_hub_install_task.md @@ -15,18 +15,18 @@ Install Task from a catalog by its name and version ### Examples -Install a Task of name 'foo': +Install a Task of name 'foo' from Artifact Hub: - tkn hub install task foo + tkn hub install task foo --type artifact --from tekton-catalog-tasks --version 0.3.0 or -Install a Task of name 'foo' of version '0.3' from Catalog 'Tekton': +Install a Task of name 'foo' from Tekton Hub (DEPRECATED): tkn hub install task foo --version 0.3 --from tekton -Note that the resources in Artifact Hub follow full SemVer - .. (e.g. 0.3.0), -please double check the version used +Note: Tekton Hub is deprecated. Use '--type artifact' with Artifact Hub instead. +Resources in Artifact Hub follow full SemVer - .. (e.g. 0.3.0). ### Options @@ -38,13 +38,15 @@ please double check the version used ### Options inherited from parent commands ``` - --api-server string Hub API Server URL (default 'https://api.hub.tekton.dev' for 'tekton' type; default 'https://artifacthub.io' for 'artifact' type). - URL can also be defined in a file '$HOME/.tekton/hub-config' with a variable 'TEKTON_HUB_API_SERVER'/'ARTIFACT_HUB_API_SERVER'. + --api-server string Hub API Server URL. + For artifact type: default 'https://artifacthub.io' (env: ARTIFACT_HUB_API_SERVER) + For tekton type (DEPRECATED): default 'https://api.hub.tekton.dev' (env: TEKTON_HUB_API_SERVER) + Can also be set in '$HOME/.tekton/hub-config'. -c, --context string Name of the kubeconfig context to use (default: kubectl config current-context) --from string Name of Catalog to which resource belongs. -k, --kubeconfig string Kubectl config file (default: $HOME/.kube/config) -n, --namespace string Namespace to use (default: from $KUBECONFIG) - --type string The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (default "tekton") + --type string The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (DEPRECATED: tekton type will be removed in a future release) (default "artifact") --version string Version of Resource ``` diff --git a/docs/cmd/tkn_hub_reinstall.md b/docs/cmd/tkn_hub_reinstall.md index 59d0b2ac2b..cbbb681212 100644 --- a/docs/cmd/tkn_hub_reinstall.md +++ b/docs/cmd/tkn_hub_reinstall.md @@ -33,6 +33,6 @@ Reinstall a resource by its kind and name ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn hub reinstall task](tkn_hub_reinstall_task.md) - Reinstall a Task by its name diff --git a/docs/cmd/tkn_hub_search.md b/docs/cmd/tkn_hub_search.md index ae7c2ea658..c477a469ae 100644 --- a/docs/cmd/tkn_hub_search.md +++ b/docs/cmd/tkn_hub_search.md @@ -49,5 +49,5 @@ Search resources using tag 'cli': ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub diff --git a/docs/cmd/tkn_hub_upgrade.md b/docs/cmd/tkn_hub_upgrade.md index a464ec5032..6f9dea6fbe 100644 --- a/docs/cmd/tkn_hub_upgrade.md +++ b/docs/cmd/tkn_hub_upgrade.md @@ -32,6 +32,6 @@ Upgrade an installed resource ### SEE ALSO -* [tkn hub](tkn_hub.md) - Interact with tekton hub +* [tkn hub](tkn_hub.md) - Interact with artifacthub * [tkn hub upgrade task](tkn_hub_upgrade_task.md) - Upgrade a Task by its name diff --git a/docs/man/man1/tkn-hub-install-task.1 b/docs/man/man1/tkn-hub-install-task.1 index 81d0f7f845..64f1e5905f 100644 --- a/docs/man/man1/tkn-hub-install-task.1 +++ b/docs/man/man1/tkn-hub-install-task.1 @@ -27,10 +27,12 @@ Install Task from a catalog by its name and version .SH OPTIONS INHERITED FROM PARENT COMMANDS .PP \fB\-\-api\-server\fP="" - Hub API Server URL (default ' -\[la]https://api.hub.tekton.dev'\[ra] for 'tekton' type; default ' -\[la]https://artifacthub.io'\[ra] for 'artifact' type). -URL can also be defined in a file '$HOME/.tekton/hub\-config' with a variable 'TEKTON\_HUB\_API\_SERVER'/'ARTIFACT\_HUB\_API\_SERVER'. + Hub API Server URL. +For artifact type: default ' +\[la]https://artifacthub.io'\[ra] (env: ARTIFACT\_HUB\_API\_SERVER) +For tekton type (DEPRECATED): default ' +\[la]https://api.hub.tekton.dev'\[ra] (env: TEKTON\_HUB\_API\_SERVER) +Can also be set in '$HOME/.tekton/hub\-config'. .PP \fB\-c\fP, \fB\-\-context\fP="" @@ -49,8 +51,8 @@ URL can also be defined in a file '$HOME/.tekton/hub\-config' with a variable 'T Namespace to use (default: from $KUBECONFIG) .PP -\fB\-\-type\fP="tekton" - The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' +\fB\-\-type\fP="artifact" + The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (DEPRECATED: tekton type will be removed in a future release) .PP \fB\-\-version\fP="" @@ -59,13 +61,13 @@ URL can also be defined in a file '$HOME/.tekton/hub\-config' with a variable 'T .SH EXAMPLE .PP -Install a Task of name 'foo': +Install a Task of name 'foo' from Artifact Hub: .PP .RS .nf -tkn hub install task foo +tkn hub install task foo \-\-type artifact \-\-from tekton\-catalog\-tasks \-\-version 0.3.0 .fi .RE @@ -74,7 +76,7 @@ tkn hub install task foo or .PP -Install a Task of name 'foo' of version '0.3' from Catalog 'Tekton': +Install a Task of name 'foo' from Tekton Hub (DEPRECATED): .PP .RS @@ -86,8 +88,8 @@ tkn hub install task foo \-\-version 0.3 \-\-from tekton .RE .PP -Note that the resources in Artifact Hub follow full SemVer \- \&.\&. (e.g. 0.3.0), -please double check the version used +Note: Tekton Hub is deprecated. Use '\-\-type artifact' with Artifact Hub instead. +Resources in Artifact Hub follow full SemVer \- \&.\&. (e.g. 0.3.0). .SH SEE ALSO diff --git a/docs/man/man1/tkn-hub-install.1 b/docs/man/man1/tkn-hub-install.1 index dbb6924794..8914974f85 100644 --- a/docs/man/man1/tkn-hub-install.1 +++ b/docs/man/man1/tkn-hub-install.1 @@ -47,14 +47,16 @@ Install a resource from a catalog by its kind, name and version .SH OPTIONS INHERITED FROM PARENT COMMANDS .PP \fB\-\-api\-server\fP="" - Hub API Server URL (default ' -\[la]https://api.hub.tekton.dev'\[ra] for 'tekton' type; default ' -\[la]https://artifacthub.io'\[ra] for 'artifact' type). -URL can also be defined in a file '$HOME/.tekton/hub\-config' with a variable 'TEKTON\_HUB\_API\_SERVER'/'ARTIFACT\_HUB\_API\_SERVER'. + Hub API Server URL. +For artifact type: default ' +\[la]https://artifacthub.io'\[ra] (env: ARTIFACT\_HUB\_API\_SERVER) +For tekton type (DEPRECATED): default ' +\[la]https://api.hub.tekton.dev'\[ra] (env: TEKTON\_HUB\_API\_SERVER) +Can also be set in '$HOME/.tekton/hub\-config'. .PP -\fB\-\-type\fP="tekton" - The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' +\fB\-\-type\fP="artifact" + The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (DEPRECATED: tekton type will be removed in a future release) .SH SEE ALSO diff --git a/docs/man/man1/tkn-hub.1 b/docs/man/man1/tkn-hub.1 index 5c24c9f8f3..49e57adeba 100644 --- a/docs/man/man1/tkn-hub.1 +++ b/docs/man/man1/tkn-hub.1 @@ -5,7 +5,7 @@ .SH NAME .PP -tkn\-hub \- Interact with tekton hub +tkn\-hub \- Interact with artifacthub .SH SYNOPSIS @@ -15,26 +15,49 @@ tkn\-hub \- Interact with tekton hub .SH DESCRIPTION .PP -Interact with tekton hub +Interact with artifacthub + +.PP +Deprecation Notice: Tekton Hub support in CLI is being deprecated in favor of Artifact Hub. +The following commands currently only work with Tekton Hub and may support Artifact Hub in a future release: + \- check\-upgrade + \- downgrade + \- get + \- info + \- reinstall + \- search + \- upgrade + +.PP +Action Required: Users should migrate to Artifact Hub by using the '\-\-type artifact' flag +with the install command. For example: + tkn hub install task foo \-\-type artifact \-\-from tekton\-catalog\-tasks + +.PP +When using '\-\-type tekton', a deprecation warning will now be displayed. +Artifact Hub ( +\[la]https://artifacthub.io\[ra]) will become the only supported hub in future releases. .SH OPTIONS .PP \fB\-\-api\-server\fP="" - Hub API Server URL (default ' -\[la]https://api.hub.tekton.dev'\[ra] for 'tekton' type; default ' -\[la]https://artifacthub.io'\[ra] for 'artifact' type). -URL can also be defined in a file '$HOME/.tekton/hub\-config' with a variable 'TEKTON\_HUB\_API\_SERVER'/'ARTIFACT\_HUB\_API\_SERVER'. + Hub API Server URL. +For artifact type: default ' +\[la]https://artifacthub.io'\[ra] (env: ARTIFACT\_HUB\_API\_SERVER) +For tekton type (DEPRECATED): default ' +\[la]https://api.hub.tekton.dev'\[ra] (env: TEKTON\_HUB\_API\_SERVER) +Can also be set in '$HOME/.tekton/hub\-config'. .PP \fB\-h\fP, \fB\-\-help\fP[=false] help for hub .PP -\fB\-\-type\fP="tekton" - The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' +\fB\-\-type\fP="artifact" + The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' (DEPRECATED: tekton type will be removed in a future release) .SH SEE ALSO .PP -\fBtkn(1)\fP, \fBtkn\-hub\-check\-upgrade(1)\fP, \fBtkn\-hub\-downgrade(1)\fP, \fBtkn\-hub\-get(1)\fP, \fBtkn\-hub\-info(1)\fP, \fBtkn\-hub\-install(1)\fP, \fBtkn\-hub\-reinstall(1)\fP, \fBtkn\-hub\-search(1)\fP, \fBtkn\-hub\-upgrade(1)\fP +\fBtkn(1)\fP, \fBtkn\-hub\-install(1)\fP diff --git a/go.mod b/go.mod index 785a2299ae..13c1ec64f6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module github.com/tektoncd/cli go 1.25.8 require ( + github.com/ActiveState/vt10x v1.3.1 github.com/AlecAivazis/survey/v2 v2.3.7 + github.com/Masterminds/semver/v3 v3.4.0 github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 github.com/blang/semver v3.5.1+incompatible github.com/cpuguy83/go-md2man v1.0.10 @@ -19,6 +21,7 @@ require ( github.com/hashicorp/go-cleanhttp v0.5.2 github.com/hashicorp/golang-lru v1.0.2 github.com/hinshun/vt10x v0.0.0-20220228203356-1ab2cad5fd82 + github.com/joho/godotenv v1.5.1 github.com/jonboulle/clockwork v0.5.0 github.com/ktr0731/go-fuzzyfinder v0.9.0 github.com/letsencrypt/boulder v0.20260420.0 @@ -28,8 +31,9 @@ require ( github.com/sigstore/sigstore v1.10.6 github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.10 + github.com/spf13/viper v1.21.0 + github.com/stretchr/testify v1.11.1 github.com/tektoncd/chains v0.26.3 - github.com/tektoncd/hub v1.24.0 github.com/tektoncd/pipeline v1.12.0 github.com/tektoncd/plumbing v0.0.0-20250430145243-3b7cd59879c1 github.com/tektoncd/triggers v0.35.1-0.20260401091813-1aad8a1898ec @@ -38,10 +42,13 @@ require ( go.opentelemetry.io/otel v1.43.0 go.uber.org/multierr v1.11.0 go.uber.org/zap v1.28.0 + goa.design/goa/v3 v3.26.0 golang.org/x/crypto v0.51.0 golang.org/x/term v0.43.0 + golang.org/x/text v0.37.0 google.golang.org/grpc v1.81.1 google.golang.org/protobuf v1.36.11 + gopkg.in/h2non/gock.v1 v1.1.2 gotest.tools v2.2.0+incompatible gotest.tools/v3 v3.5.2 k8s.io/api v0.35.4 @@ -54,8 +61,6 @@ require ( replace github.com/alibabacloud-go/cr-20160607 => github.com/vdemeester/cr-20160607 v1.0.1 -replace github.com/tektoncd/hub => github.com/openshift-pipelines/hub v1.24.0 - require ( cel.dev/expr v0.25.1 // indirect cloud.google.com/go v0.123.0 // indirect @@ -78,7 +83,6 @@ require ( github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.55.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.55.0 // indirect github.com/IBM/sarama v1.45.2 // indirect - github.com/Masterminds/semver/v3 v3.4.0 // indirect github.com/ThalesIgnite/crypto11 v1.2.5 // indirect github.com/allegro/bigcache/v3 v3.1.0 // indirect github.com/antlr4-go/antlr/v4 v4.13.1 // indirect @@ -178,6 +182,7 @@ require ( github.com/grafeas/grafeas v0.2.3 // indirect github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect + github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-retryablehttp v0.7.8 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect @@ -194,11 +199,11 @@ require ( github.com/jcmturner/rpc/v2 v2.0.3 // indirect github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267 // indirect github.com/jmespath/go-jmespath v0.4.1-0.20220621161143-b0104c826a24 // indirect - github.com/joho/godotenv v1.5.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/kelseyhightower/envconfig v1.4.0 // indirect github.com/klauspost/compress v1.18.6 // indirect + github.com/kr/pty v1.1.1 // indirect github.com/ktr0731/go-ansisgr v0.1.0 // indirect github.com/kylelemons/godebug v1.1.0 // indirect github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect @@ -245,7 +250,6 @@ require ( github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect github.com/spf13/afero v1.15.0 // indirect github.com/spf13/cast v1.10.0 // indirect - github.com/spf13/viper v1.21.0 // indirect github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect @@ -285,7 +289,6 @@ require ( go.step.sm/crypto v0.77.2 // indirect go.yaml.in/yaml/v2 v2.4.3 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect - goa.design/goa/v3 v3.23.4 // indirect gocloud.dev v0.43.0 // indirect gocloud.dev/docstore/mongodocstore v0.43.0 // indirect gocloud.dev/pubsub/kafkapubsub v0.43.0 // indirect @@ -295,7 +298,6 @@ require ( golang.org/x/oauth2 v0.36.0 // indirect golang.org/x/sync v0.20.0 // indirect golang.org/x/sys v0.44.0 // indirect - golang.org/x/text v0.37.0 // indirect golang.org/x/time v0.15.0 // indirect golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect @@ -306,6 +308,7 @@ require ( gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiextensions-apiserver v0.35.4 // indirect k8s.io/klog/v2 v2.130.1 // indirect k8s.io/kube-openapi v0.0.0-20250910181357-589584f1c912 // indirect diff --git a/go.sum b/go.sum index 848675c6b5..5f43203440 100644 --- a/go.sum +++ b/go.sum @@ -81,6 +81,7 @@ github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1 github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= github.com/ThalesIgnite/crypto11 v1.2.5 h1:1IiIIEqYmBvUYFeMnHqRft4bwf/O36jryEUpY+9ef8E= @@ -91,6 +92,8 @@ github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYW github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= +github.com/autarch/testify v1.2.2 h1:9Q9V6zqhP7R6dv+zRUddv6kXKLo6ecQhnFRFWM71i1c= +github.com/autarch/testify v1.2.2/go.mod h1:oDbHKfFv2/D5UtVrxkk90OKcb6P4/AqF1Pcf6ZbvDQo= github.com/aws/aws-sdk-go v1.55.8 h1:JRmEUbU52aJQZ2AjX4q4Wu7t4uZjOu71uyNmaWlUkJQ= github.com/aws/aws-sdk-go v1.55.8/go.mod h1:ZkViS9AqA6otK+JBBNH2++sx1sgxrPKcSzPPvQkUtXk= github.com/aws/aws-sdk-go-v2 v1.41.5 h1:dj5kopbwUsVUVFgO4Fi5BIT3t4WyqIDjGKCangnV/yY= @@ -230,8 +233,10 @@ github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sa github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ= github.com/gaganhr94/docker-credential-acr v1.0.2 h1:0eMFjVqRUmwINbhFxb5xLTWLJpdNta34TGuInVGtEGk= github.com/gaganhr94/docker-credential-acr v1.0.2/go.mod h1:8yd2V0GhCyd17MpMxfAJzcZqldu1ghFmrUV0GS7qcGc= +github.com/gdamore/encoding v0.0.0-20151215212835-b23993cbb635/go.mod h1:yrQYJKKDTrHmbYxI7CYi+/hbdiDT2m4Hj+t0ikCjsrQ= github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw= github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo= +github.com/gdamore/tcell v1.0.1-0.20180608172421-b3cebc399d6f/go.mod h1:tqyG50u7+Ctv1w5VX67kLzKcj9YXR/JSBZQq/+mLl1A= github.com/gdamore/tcell/v2 v2.9.0 h1:N6t+eqK7/xwtRPwxzs1PXeRWnm0H9l02CrgJ7DLn1ys= github.com/gdamore/tcell/v2 v2.9.0/go.mod h1:8/ZoqM9rxzYphT9tH/9LnunhV9oPBqwS8WHGYm5nrmo= github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug= @@ -386,6 +391,7 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.14 h1:yh8ncqsbUY4shRD5dA github.com/googleapis/enterprise-certificate-proxy v0.3.14/go.mod h1:vqVt9yG9480NtzREnTlmGSBmFrA+bzb0yl0TxoBQXOg= github.com/googleapis/gax-go/v2 v2.19.0 h1:fYQaUOiGwll0cGj7jmHT/0nPlcrZDFPrZRhTsoCr8hE= github.com/googleapis/gax-go/v2 v2.19.0/go.mod h1:w2ROXVdfGEVFXzmlciUU4EdjHgWvB5h2n6x/8XSTTJA= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 h1:JeSE6pjso5THxAzdVpqr6/geYxZytqFMBCOtn/ujyeo= @@ -434,6 +440,7 @@ github.com/hashicorp/hcl v1.0.1-vault-7 h1:ag5OxFVy3QYTFTJODRzTKVZ6xvdfLLCA1cy/Y github.com/hashicorp/hcl v1.0.1-vault-7/go.mod h1:XYhtn6ijBSAj6n4YqAaf7RBPS4I06AItNorpy+MoQNM= github.com/hashicorp/vault/api v1.22.0 h1:+HYFquE35/B74fHoIeXlZIP2YADVboaPjaSicHEZiH0= github.com/hashicorp/vault/api v1.22.0/go.mod h1:IUZA2cDvr4Ok3+NtK2Oq/r+lJeXkeCrHRmqdyWfpmGM= +github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/hinshun/vt10x v0.0.0-20220228203356-1ab2cad5fd82 h1:uf1FmugJNeFovjWtxD7FSPWQXdi0KuKnZfvN4CFUAtA= github.com/hinshun/vt10x v0.0.0-20220228203356-1ab2cad5fd82/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= @@ -491,6 +498,7 @@ github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbd github.com/jonboulle/clockwork v0.5.0/go.mod h1:3mZlmanh0g2NDKO5TWZVJAfofYk64M7XN3SzBPjZF60= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= @@ -501,8 +509,8 @@ github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXD github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI= -github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/ktr0731/go-ansisgr v0.1.0 h1:fbuupput8739hQbEmZn1cEKjqQFwtCCZNznnF6ANo5w= @@ -515,6 +523,7 @@ github.com/letsencrypt/boulder v0.20260420.0 h1:PMFy37+tQAfNe2Qks7NhTrKbULzhVmj1 github.com/letsencrypt/boulder v0.20260420.0/go.mod h1:ZisB912eU757QUU0PTH+zq2JScaegVjPVKtPn2K1U3w= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= +github.com/lucasb-eyer/go-colorful v0.0.0-20180526135729-345fbb3dbcdb/go.mod h1:NXg0ArsFk0Y01623LgUqoqcouGDB+PwCCQlrwrG6xJ4= github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag= github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -523,6 +532,7 @@ github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stg github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw= github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= @@ -560,6 +570,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A= github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/nozzle/throttler v0.0.0-20180817012639-2ea982251481 h1:Up6+btDp321ZG5/zdSLo48H9Iaq0UQGthrhWC6pCxzE= github.com/nozzle/throttler v0.0.0-20180817012639-2ea982251481/go.mod h1:yKZQO8QE2bHlgozqWDiRVqTFlLQSj30K/6SAK8EeYFw= github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY= @@ -588,8 +600,6 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040= github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M= -github.com/openshift-pipelines/hub v1.24.0 h1:MHqB+SIrxh3CVA04qkkYFLUywxMDWrA0NELqcDdFj5I= -github.com/openshift-pipelines/hub v1.24.0/go.mod h1:yjfR+KhIpZBaQBxlauVA4dHE/YNcfG3PhgT5oqwQC4A= github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= @@ -668,6 +678,8 @@ github.com/sigstore/timestamp-authority/v2 v2.0.6 h1:1Vh7/SdmLsVLG6Br6/bisd1Snli github.com/sigstore/timestamp-authority/v2 v2.0.6/go.mod h1:Nk5ucGBDyH0tXAIMZ0prf6xn8qfTnbJhSq+CDabYcfc= github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U= github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= @@ -690,6 +702,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= @@ -826,8 +839,8 @@ go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0= go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= -goa.design/goa/v3 v3.23.4 h1:7d9IAtyC8aP9bAvTdY+YPQaScpoZRd/paDH3PSXaxbM= -goa.design/goa/v3 v3.23.4/go.mod h1:da3W585WfJe9gT+hJCbP8YFB9yc4gmuCwB0MvkbwhXk= +goa.design/goa/v3 v3.26.0 h1:lDHpqvhYpRGWcyAznXmU5m3LOZ1VFuP3r35XuL+hlbc= +goa.design/goa/v3 v3.26.0/go.mod h1:afBmJ7gfwPSXociyFfVzcKGVCqS2DlGn7F6Olf+9yog= gocloud.dev v0.43.0 h1:aW3eq4RMyehbJ54PMsh4hsp7iX8cO/98ZRzJJOzN/5M= gocloud.dev v0.43.0/go.mod h1:eD8rkg7LhKUHrzkEdLTZ+Ty/vgPHPCd+yMQdfelQVu4= gocloud.dev/docstore/mongodocstore v0.43.0 h1:Ay6NbJcqZOQYS3JULkv3QeaOhmEcVhU5OQEZCBJdCnM= @@ -947,6 +960,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= @@ -998,6 +1012,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/hack/update-deps.sh b/hack/update-deps.sh index 4ea69c596a..f787094c90 100755 --- a/hack/update-deps.sh +++ b/hack/update-deps.sh @@ -32,7 +32,6 @@ FLOATING_DEPS=( "github.com/tektoncd/pipeline@master" "github.com/tektoncd/triggers@master" "github.com/tektoncd/plumbing@master" - "github.com/tektoncd/hub@master" ) # Parse flags to determine any we should pass to dep. diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/app/app.go b/pkg/cmd/hub/app/app.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/app/app.go rename to pkg/cmd/hub/app/app.go index a84831a8c5..21e10229df 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/app/app.go +++ b/pkg/cmd/hub/app/app.go @@ -18,7 +18,7 @@ import ( "fmt" "io" - "github.com/tektoncd/hub/api/pkg/cli/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" ) type Stream struct { diff --git a/pkg/cmd/hub/cmd/check_upgrade/check_upgrade_test.go b/pkg/cmd/hub/cmd/check_upgrade/check_upgrade_test.go new file mode 100644 index 0000000000..a066616483 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/check_upgrade_test.go @@ -0,0 +1,669 @@ +// Copyright © 2021 The Tekton 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 checkupgrade + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + cb "github.com/tektoncd/cli/pkg/cmd/hub/test/builder" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + pipelinetest "github.com/tektoncd/pipeline/test" + "gopkg.in/h2non/gock.v1" + "gotest.tools/v3/golden" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var resVersion = &res.ResourceData{ + ID: 1, + Name: "foo", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4.8, + LatestVersion: &res.ResourceVersionData{ + ID: 12, + Version: "0.2", + Description: "v0.1 Task to run foo", + DisplayName: "foo-bar", + MinPipelinesVersion: "0.11", + RawURL: "http://raw.github.url/foo/0.1/foo.yaml", + WebURL: "http://web.github.com/foo/0.1/foo.yaml", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + }, + Tags: []*res.Tag{ + { + ID: 3, + Name: "cli", + }, + }, + Versions: []*res.ResourceVersionData{ + { + ID: 12, + Version: "0.2", + }, + }, +} + +func TestUpdateAvailable(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1UpdateAvailable(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestUpdateAvailable_WithSkippedTasks(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTasks := []*v1beta1.Task{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo-bar", + Namespace: "hub", + Labels: map[string]string{ + "app.kubernetes.io/version": "0.1", + }}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTasks[0], version), cb.UnstructuredV1beta1T(existingTasks[1], version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: existingTasks}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1UpdateAvailable_WithSkippedTasks(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTasks := []*v1.Task{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo-bar", + Namespace: "hub", + Labels: map[string]string{ + "app.kubernetes.io/version": "0.1", + }}, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTasks[0], version), cb.UnstructuredV1(existingTasks[1], version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: existingTasks}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestNoUpdateAvailable(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.2", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1NoUpdateAvailable(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.2", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestNoUpdateAvailable_TaskNotInstalledViaHubCLI(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), false) +} + +func TestV1NoUpdateAvailable_TaskNotInstalledViaHubCLI(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + MatchParam("pipelinesversion", "0.14"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), false) +} + +func TestUpdateAvailable_PipelinesUnknown(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1UpdateAvailable_PipelinesUnknown(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestUpdateAvailable_WithSkippedTasks_PipelinesUnknown(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTasks := []*v1beta1.Task{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo-bar", + Namespace: "hub", + Labels: map[string]string{ + "app.kubernetes.io/version": "0.1", + }}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTasks[0], version), cb.UnstructuredV1beta1T(existingTasks[1], version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: existingTasks}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1UpdateAvailable_WithSkippedTasks_PipelinesUnknown(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resource := &res.Resource{Data: resVersion} + res := res.NewViewedResource(resource, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTasks := []*v1.Task{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo-bar", + Namespace: "hub", + Labels: map[string]string{ + "app.kubernetes.io/version": "0.1", + }}, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTasks[0], version), cb.UnstructuredV1(existingTasks[1], version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: existingTasks}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/check_upgrade/check_uprade.go b/pkg/cmd/hub/cmd/check_upgrade/check_uprade.go similarity index 95% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/check_upgrade/check_uprade.go rename to pkg/cmd/hub/cmd/check_upgrade/check_uprade.go index 419b1daa65..0f8adea268 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/check_upgrade/check_uprade.go +++ b/pkg/cmd/hub/cmd/check_upgrade/check_uprade.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package check_upgrade +package checkupgrade import ( "fmt" @@ -20,12 +20,12 @@ import ( "text/template" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/formatter" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/pkg/cli/installer" - "github.com/tektoncd/hub/api/pkg/cli/kube" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/formatter" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/installer" + "github.com/tektoncd/cli/pkg/cmd/hub/kube" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" "golang.org/x/text/cases" "golang.org/x/text/language" ) diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestNoUpdateAvailable.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestNoUpdateAvailable.golden new file mode 100644 index 0000000000..48f5b14652 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestNoUpdateAvailable.golden @@ -0,0 +1,3 @@ +Upgrades Available + +No task for upgrade diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestNoUpdateAvailable_TaskNotInstalledViaHubCLI.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestNoUpdateAvailable_TaskNotInstalledViaHubCLI.golden new file mode 100644 index 0000000000..190f247b54 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestNoUpdateAvailable_TaskNotInstalledViaHubCLI.golden @@ -0,0 +1,8 @@ +Upgrades Available + +No task for upgrade + +Skipped Resources + +NAME +∙ foo diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable.golden new file mode 100644 index 0000000000..55d6afeef0 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable.golden @@ -0,0 +1,4 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_COMPATIBLE_VERSION +foo Tekton 0.1 0.2 diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_PipelinesUnknown.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_PipelinesUnknown.golden new file mode 100644 index 0000000000..b390423ff6 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_PipelinesUnknown.golden @@ -0,0 +1,6 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_VERSION +foo Tekton 0.1 0.2 + +WARN: Pipelines version unknown. Check your pipelines version before upgrading. diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_WithSkippedTasks.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_WithSkippedTasks.golden new file mode 100644 index 0000000000..1c423e163e --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_WithSkippedTasks.golden @@ -0,0 +1,9 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_COMPATIBLE_VERSION +foo Tekton 0.1 0.2 + +Skipped Resources + +NAME +∙ foo-bar diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_WithSkippedTasks_PipelinesUnknown.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_WithSkippedTasks_PipelinesUnknown.golden new file mode 100644 index 0000000000..861969bcaa --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestUpdateAvailable_WithSkippedTasks_PipelinesUnknown.golden @@ -0,0 +1,11 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_VERSION +foo Tekton 0.1 0.2 + +Skipped Resources + +NAME +∙ foo-bar + +WARN: Pipelines version unknown. Check your pipelines version before upgrading. diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1NoUpdateAvailable.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1NoUpdateAvailable.golden new file mode 100644 index 0000000000..48f5b14652 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1NoUpdateAvailable.golden @@ -0,0 +1,3 @@ +Upgrades Available + +No task for upgrade diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1NoUpdateAvailable_TaskNotInstalledViaHubCLI.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1NoUpdateAvailable_TaskNotInstalledViaHubCLI.golden new file mode 100644 index 0000000000..190f247b54 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1NoUpdateAvailable_TaskNotInstalledViaHubCLI.golden @@ -0,0 +1,8 @@ +Upgrades Available + +No task for upgrade + +Skipped Resources + +NAME +∙ foo diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable.golden new file mode 100644 index 0000000000..55d6afeef0 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable.golden @@ -0,0 +1,4 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_COMPATIBLE_VERSION +foo Tekton 0.1 0.2 diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_PipelinesUnknown.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_PipelinesUnknown.golden new file mode 100644 index 0000000000..b390423ff6 --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_PipelinesUnknown.golden @@ -0,0 +1,6 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_VERSION +foo Tekton 0.1 0.2 + +WARN: Pipelines version unknown. Check your pipelines version before upgrading. diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_WithSkippedTasks.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_WithSkippedTasks.golden new file mode 100644 index 0000000000..1c423e163e --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_WithSkippedTasks.golden @@ -0,0 +1,9 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_COMPATIBLE_VERSION +foo Tekton 0.1 0.2 + +Skipped Resources + +NAME +∙ foo-bar diff --git a/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_WithSkippedTasks_PipelinesUnknown.golden b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_WithSkippedTasks_PipelinesUnknown.golden new file mode 100644 index 0000000000..861969bcaa --- /dev/null +++ b/pkg/cmd/hub/cmd/check_upgrade/testdata/TestV1UpdateAvailable_WithSkippedTasks_PipelinesUnknown.golden @@ -0,0 +1,11 @@ +Upgrades Available + +NAME CATALOG CURRENT_VERSION LATEST_VERSION +foo Tekton 0.1 0.2 + +Skipped Resources + +NAME +∙ foo-bar + +WARN: Pipelines version unknown. Check your pipelines version before upgrading. diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/downgrade/downgrade.go b/pkg/cmd/hub/cmd/downgrade/downgrade.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/downgrade/downgrade.go rename to pkg/cmd/hub/cmd/downgrade/downgrade.go index d3a0487562..b84aeece97 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/downgrade/downgrade.go +++ b/pkg/cmd/hub/cmd/downgrade/downgrade.go @@ -19,12 +19,12 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/flag" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/pkg/cli/installer" - "github.com/tektoncd/hub/api/pkg/cli/kube" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/flag" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/installer" + "github.com/tektoncd/cli/pkg/cmd/hub/kube" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" "golang.org/x/text/cases" "golang.org/x/text/language" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/pkg/cmd/hub/cmd/downgrade/downgrade_test.go b/pkg/cmd/hub/cmd/downgrade/downgrade_test.go new file mode 100644 index 0000000000..b5906c157a --- /dev/null +++ b/pkg/cmd/hub/cmd/downgrade/downgrade_test.go @@ -0,0 +1,1097 @@ +// Copyright © 2020 The Tekton 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 downgrade + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + cb "github.com/tektoncd/cli/pkg/cmd/hub/test/builder" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + pipelinetest "github.com/tektoncd/pipeline/test" + "gopkg.in/h2non/gock.v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var resVersion = &res.ResourceVersionData{ + ID: 11, + Version: "0.3", + DisplayName: "foo-bar", + Description: "v0.3 Task to run foo", + MinPipelinesVersion: "0.12", + RawURL: "http://raw.github.url/foo/0.3/foo.yaml", + WebURL: "http://web.github.com/foo/0.3/foo.yaml", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + Resource: &res.ResourceData{ + ID: 1, + Name: "foo", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4.8, + Tags: []*res.Tag{ + { + ID: 3, + Name: "cli", + }, + }, + }, +} + +var ver03 = &res.ResourceVersionData{ + ID: 111, + Version: "0.3", + RawURL: "http://raw.github.url/foo/0.3/foo.yaml", + WebURL: "http://web.github.com/foo/0.3/foo.yaml", +} +var ver02 = &res.ResourceVersionData{ + ID: 113, + Version: "0.2", + RawURL: "http://raw.github.url/foo/0.2/foo.yaml", + WebURL: "http://web.github.com/foo/0.2/foo.yaml", +} + +var task1 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskWithNewVersionYaml = &res.ResourceContent{ + Yaml: &task1, +} + +var task2 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.2' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.2 Task to run foo +` + +var taskWitholdVersionYaml = &res.ResourceContent{ + Yaml: &task2, +} + +var task3 = `--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskV1WithNewVersionYaml = &res.ResourceContent{ + Yaml: &task3, +} + +var task4 = `--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.2' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.2 Task to run foo +` + +var taskV1WitholdVersionYaml = &res.ResourceContent{ + Yaml: &task4, +} + +func TestDowngrade_ResourceNotExist(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + version := "v1beta1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedV1beta1TestData(t, test.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo doesn't exist in hub namespace. Use install command to install the task") +} + +func TestV1Downgrade_ResourceNotExist(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + version := "v1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedTestData(t, pipelinetest.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo doesn't exist in hub namespace. Use install command to install the task") +} + +func TestDowngrade_VersionCatalogMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version and catalog label. Use reinstall command to overwrite existing task") +} + +func TestV1Downgrade_VersionCatalogMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version and catalog label. Use reinstall command to overwrite existing task") +} + +func TestDowngrade_VersionMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"hub.tekton.dev/catalog": "abc"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version label. Use reinstall command to overwrite existing task") +} + +func TestV1Downgrade_VersionMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"hub.tekton.dev/catalog": "abc"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version label. Use reinstall command to overwrite existing task") +} + +func TestDowngrade(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskWitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.13.1\nTask foo downgraded to v0.2 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Downgrade(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskWitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.13.1\nTask foo downgraded to v0.2 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestDowngrade_ToSpecificVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskWitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.2", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.13.1\nTask foo downgraded to v0.2 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Downgrade_ToSpecificVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskV1WitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.2", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.13.1\nTask foo downgraded to v0.2 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestDowngrade_SameVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot downgrade task foo to v0.3. existing resource seems to be of same version. Use reinstall command to overwrite existing task") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Downgrade_SameVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot downgrade task foo to v0.3. existing resource seems to be of same version. Use reinstall command to overwrite existing task") + assert.Equal(t, gock.IsDone(), true) +} + +func TestDowngrade_HigherVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.4", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot downgrade task foo to v0.4. existing resource seems to be of lower version(v0.3). Use upgrade command") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Downgrade_HigherVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.4", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot downgrade task foo to v0.4. existing resource seems to be of lower version(v0.3). Use upgrade command") + assert.Equal(t, gock.IsDone(), true) +} + +func TestDowngrade_ToSpecificVersionRespectingPipelinesVersionSuccess(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskWitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.2", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo downgraded to v0.2 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Downgrade_ToSpecificVersionRespectingPipelinesVersionSuccess(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + + rVer := &res.ResourceVersionYaml{Data: taskWitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.2", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo downgraded to v0.2 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestDowngrade_ToSpecificVersionRespectingPipelinesVersionFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + rVer := &res.ResourceVersionYaml{Data: taskWitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.2", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.12.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot downgrade Task foo to v0.2 as it requires Pipelines min version v0.13.1 but found v0.12.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Downgrade_ToSpecificVersionRespectingPipelinesVersionFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + versions := &res.ResourceVersions{Data: &res.Versions{Latest: ver03, Versions: []*res.ResourceVersionData{ver02, ver03}}} + ver := res.NewViewedResourceVersions(versions, "default") + + gock.New(test.API). + Get("/resource/1/versions"). + Reply(200). + JSON(&ver.Projected) + rVer := &res.ResourceVersionYaml{Data: taskV1WitholdVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.2", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.12.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot downgrade Task foo to v0.2 as it requires Pipelines min version v0.13.1 but found v0.12.0") + assert.Equal(t, gock.IsDone(), true) +} diff --git a/pkg/cmd/hub/cmd/downgrade/testdata/foo-v0.2.yaml b/pkg/cmd/hub/cmd/downgrade/testdata/foo-v0.2.yaml new file mode 100644 index 0000000000..43baba5a10 --- /dev/null +++ b/pkg/cmd/hub/cmd/downgrade/testdata/foo-v0.2.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.2' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.2 Task to run foo diff --git a/pkg/cmd/hub/cmd/downgrade/testdata/foo-v0.3.yaml b/pkg/cmd/hub/cmd/downgrade/testdata/foo-v0.3.yaml new file mode 100644 index 0000000000..c43a2e5705 --- /dev/null +++ b/pkg/cmd/hub/cmd/downgrade/testdata/foo-v0.3.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/get/get.go b/pkg/cmd/hub/cmd/get/get.go similarity index 94% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/get/get.go rename to pkg/cmd/hub/cmd/get/get.go index c2b99914af..8eb51d7762 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/get/get.go +++ b/pkg/cmd/hub/cmd/get/get.go @@ -19,11 +19,11 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/flag" - "github.com/tektoncd/hub/api/pkg/cli/hub" - so "github.com/tektoncd/hub/api/pkg/cli/options" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/flag" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + so "github.com/tektoncd/cli/pkg/cmd/hub/options" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" "golang.org/x/text/cases" "golang.org/x/text/language" ) diff --git a/pkg/cmd/hub/cmd/get/get_test.go b/pkg/cmd/hub/cmd/get/get_test.go new file mode 100644 index 0000000000..420f858271 --- /dev/null +++ b/pkg/cmd/hub/cmd/get/get_test.go @@ -0,0 +1,211 @@ +// Copyright © 2020 The Tekton 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 get + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + goa "goa.design/goa/v3/pkg" + "gopkg.in/h2non/gock.v1" + "gotest.tools/v3/golden" +) + +var pipeline1 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: mango + annotations: + tekton.dev/pipelines.minVersion: '0.18' + tekton.dev/tags: fruit + tekton.dev/displayName: 'Alphanso' +spec: + description: >- + v0.3 of Pipeline mango +` + +var pipelineWithLatestVersionYaml = &res.ResourceContent{ + Yaml: &pipeline1, +} + +var pipeline2 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: mango + annotations: + tekton.dev/pipelines.minVersion: '0.18' + tekton.dev/tags: fruit + tekton.dev/displayName: 'Alphanso' +spec: + description: >- + v0.2 of Pipeline mango +` + +var pipelineWithOldVersionYaml = &res.ResourceContent{ + Yaml: &pipeline2, +} + +var want = ` +Get a Abc of name 'foo': + + tkn hub get abc foo + +or + +Get a Abc of name 'foo' of version '0.3': + + tkn hub get abc foo --version 0.3 +` + +func TestValidate(t *testing.T) { + cli := app.New() + if err := cli.SetHub(hub.TektonHubType); err != nil { + assert.Error(t, err) + } + + opt := options{ + version: "0.1", + cli: cli, + } + err := opt.validate() + assert.NoError(t, err) + + opt = options{ + version: "0.3.1", + cli: cli, + } + err = opt.validate() + assert.NoError(t, err) +} + +func TestValidate_ErrorCase(t *testing.T) { + cli := app.New() + if err := cli.SetHub(hub.TektonHubType); err != nil { + assert.Error(t, err) + } + + opt := options{ + version: "abc", + cli: cli, + } + err := opt.validate() + assert.EqualError(t, err, "invalid value \"abc\" set for option version. valid eg. 0.1, 1.2.1") +} + +func TestGetResource_WithNewVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: pipelineWithLatestVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "fruit", "pipeline", "mango", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opt := &options{ + cli: cli, + kind: "pipeline", + args: []string{"mango"}, + from: "fruit", + version: "0.3", + } + + err := opt.run() + + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestGetResource_WithOldVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: pipelineWithOldVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "fruit", "pipeline", "mango", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opt := &options{ + cli: cli, + kind: "pipeline", + args: []string{"mango"}, + from: "fruit", + version: "0.2", + } + + err := opt.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestGet_ResourceNotFound(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + gock.New(test.API). + Get("/resource/tekton/pipeline/xyz"). + Reply(404). + JSON(&goa.ServiceError{ + ID: "123456", + Name: "not-found", + Message: "resource not found", + }) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opt := &options{ + cli: cli, + kind: "pipeline", + args: []string{"xyz"}, + from: "tekton", + } + + err := opt.run() + assert.Error(t, err) + assert.EqualError(t, err, "No Resource Found") + assert.Equal(t, gock.IsDone(), true) +} + +func Test_examples(t *testing.T) { + got := examples("abc") + assert.Equal(t, want, got) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/get/task.go b/pkg/cmd/hub/cmd/get/task.go similarity index 95% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/get/task.go rename to pkg/cmd/hub/cmd/get/task.go index 337eeaef26..c5cae13810 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/get/task.go +++ b/pkg/cmd/hub/cmd/get/task.go @@ -16,8 +16,8 @@ package get import ( "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" ) type taskOptions struct { diff --git a/pkg/cmd/hub/cmd/get/task_test.go b/pkg/cmd/hub/cmd/get/task_test.go new file mode 100644 index 0000000000..775806b683 --- /dev/null +++ b/pkg/cmd/hub/cmd/get/task_test.go @@ -0,0 +1,79 @@ +// Copyright © 2020 The Tekton 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 get + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + "gopkg.in/h2non/gock.v1" + "gotest.tools/v3/golden" +) + +var task1 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskWithNewVersionYaml = &res.ResourceContent{ + Yaml: &task1, +} + +func TestGetTask_WithNewVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opts := taskOptions{ + options: &options{ + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + }, + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} diff --git a/pkg/cmd/hub/cmd/get/testdata/TestGetResource_WithNewVersion.golden b/pkg/cmd/hub/cmd/get/testdata/TestGetResource_WithNewVersion.golden new file mode 100644 index 0000000000..83e2959609 --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/TestGetResource_WithNewVersion.golden @@ -0,0 +1,13 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: mango + annotations: + tekton.dev/pipelines.minVersion: '0.18' + tekton.dev/tags: fruit + tekton.dev/displayName: 'Alphanso' +spec: + description: >- + v0.3 of Pipeline mango + diff --git a/pkg/cmd/hub/cmd/get/testdata/TestGetResource_WithOldVersion.golden b/pkg/cmd/hub/cmd/get/testdata/TestGetResource_WithOldVersion.golden new file mode 100644 index 0000000000..a46b8e43ac --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/TestGetResource_WithOldVersion.golden @@ -0,0 +1,13 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: mango + annotations: + tekton.dev/pipelines.minVersion: '0.18' + tekton.dev/tags: fruit + tekton.dev/displayName: 'Alphanso' +spec: + description: >- + v0.2 of Pipeline mango + diff --git a/pkg/cmd/hub/cmd/get/testdata/TestGetTask_WithNewVersion.golden b/pkg/cmd/hub/cmd/get/testdata/TestGetTask_WithNewVersion.golden new file mode 100644 index 0000000000..9cd7424970 --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/TestGetTask_WithNewVersion.golden @@ -0,0 +1,13 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo + diff --git a/pkg/cmd/hub/cmd/get/testdata/foo-v0.1.yaml b/pkg/cmd/hub/cmd/get/testdata/foo-v0.1.yaml new file mode 100644 index 0000000000..1a7d09527c --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/foo-v0.1.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + annotations: + tekton.dev/pipelines.minVersion: '0.12.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.1 Task to run foo diff --git a/pkg/cmd/hub/cmd/get/testdata/foo-v0.3.yaml b/pkg/cmd/hub/cmd/get/testdata/foo-v0.3.yaml new file mode 100644 index 0000000000..7fa7d3e2fc --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/foo-v0.3.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo diff --git a/pkg/cmd/hub/cmd/get/testdata/pipeline-apple-v0.1.yaml b/pkg/cmd/hub/cmd/get/testdata/pipeline-apple-v0.1.yaml new file mode 100644 index 0000000000..2b8decc50e --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/pipeline-apple-v0.1.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: apple + annotations: + tekton.dev/pipelines.minVersion: '0.17.1' + tekton.dev/tags: fruit + tekton.dev/displayName: 'Red apple' +spec: + description: >- + v0.1 of Pipeline apple diff --git a/pkg/cmd/hub/cmd/get/testdata/pipeline-mango-v0.2.yaml b/pkg/cmd/hub/cmd/get/testdata/pipeline-mango-v0.2.yaml new file mode 100644 index 0000000000..30f1f84f5a --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/pipeline-mango-v0.2.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: mango + annotations: + tekton.dev/pipelines.minVersion: '0.18' + tekton.dev/tags: fruit + tekton.dev/displayName: 'Alphanso' +spec: + description: >- + v0.2 of Pipeline mango diff --git a/pkg/cmd/hub/cmd/get/testdata/pipeline-mango-v0.3.yaml b/pkg/cmd/hub/cmd/get/testdata/pipeline-mango-v0.3.yaml new file mode 100644 index 0000000000..6a1c844acf --- /dev/null +++ b/pkg/cmd/hub/cmd/get/testdata/pipeline-mango-v0.3.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: mango + annotations: + tekton.dev/pipelines.minVersion: '0.18' + tekton.dev/tags: fruit + tekton.dev/displayName: 'Alphanso' +spec: + description: >- + v0.3 of Pipeline mango diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/info/info.go b/pkg/cmd/hub/cmd/info/info.go similarity index 95% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/info/info.go rename to pkg/cmd/hub/cmd/info/info.go index 777b79b78b..804fbf18d5 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/info/info.go +++ b/pkg/cmd/hub/cmd/info/info.go @@ -20,12 +20,12 @@ import ( "text/template" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/flag" - "github.com/tektoncd/hub/api/pkg/cli/formatter" - "github.com/tektoncd/hub/api/pkg/cli/hub" - so "github.com/tektoncd/hub/api/pkg/cli/options" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/flag" + "github.com/tektoncd/cli/pkg/cmd/hub/formatter" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + so "github.com/tektoncd/cli/pkg/cmd/hub/options" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" "golang.org/x/text/cases" "golang.org/x/text/language" ) diff --git a/pkg/cmd/hub/cmd/info/info_test.go b/pkg/cmd/hub/cmd/info/info_test.go new file mode 100644 index 0000000000..618386de54 --- /dev/null +++ b/pkg/cmd/hub/cmd/info/info_test.go @@ -0,0 +1,233 @@ +// Copyright © 2020 The Tekton 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 info + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + "gopkg.in/h2non/gock.v1" + "gotest.tools/v3/golden" +) + +type InfoOptions struct { + ResID int + Name string + Kind string + Catalog string + Version string +} + +var taskResWithLatestVersion = &res.ResourceVersionData{ + ID: 12, + Version: "0.2", + Description: "Description for task foo-bar version 0.2", + MinPipelinesVersion: "0.12", + RawURL: "http://raw.github.url/foo-bar/", + WebURL: "http://web.github.com/foo-bar/", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + Platforms: []*res.Platform{ + { + ID: 3, + Name: "linux/amd64", + }, + }, + Resource: &res.ResourceData{ + ID: 2, + Name: "foo-bar", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4, + Categories: []*res.Category{ + { + ID: 1, + Name: "foo-bar", + }, + }, + Tags: []*res.Tag{ + { + ID: 3, + Name: "foo", + }, + }, + Platforms: []*res.Platform{ + { + ID: 3, + Name: "linux/amd64", + }, + }, + }, +} + +var deprecated = true +var taskResWithOldVersion = &res.ResourceVersionData{ + ID: 12, + Version: "0.1", + Description: "Description for task foo-bar version 0.1", + Deprecated: &deprecated, + MinPipelinesVersion: "0.12", + RawURL: "http://raw.github.url/foo-bar/", + WebURL: "http://web.github.com/foo-bar/", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + Platforms: []*res.Platform{ + { + ID: 2, + Name: "linux/s390x", + }, + }, + Resource: &res.ResourceData{ + ID: 2, + Name: "foo-bar", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4, + Categories: []*res.Category{ + { + ID: 1, + Name: "foo-bar", + }, + }, + Tags: []*res.Tag{ + { + ID: 3, + Name: "foo", + }, + }, + Platforms: []*res.Platform{ + { + ID: 2, + Name: "linux/s390x", + }, + }, + }, +} + +func mockAPI(io InfoOptions, taskWithVersion *res.ResourceVersionData) { + + // Get ResourceId in order to get all versions of resource + rVer := &res.ResourceVersion{Data: taskWithVersion} + resWithVersion := res.NewViewedResourceVersion(rVer, "default") + resInfo := fmt.Sprintf("%s/%s/%s", io.Catalog, io.Kind, io.Name) + + gock.New(test.API). + Get("/resource/" + resInfo + "/" + io.Version). + Reply(200). + JSON(&resWithVersion.Projected) +} + +func TestInfoTask_WithLatestVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + mockAPI(InfoOptions{ + ResID: 12, + Name: "foo-bar", + Kind: "task", + Catalog: "tekton", + Version: "0.2", + }, taskResWithLatestVersion) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opts := options{ + cli: cli, + kind: "task", + args: []string{"foo-bar"}, + from: "tekton", + version: "0.2", + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestInfoTask_WithOldVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + mockAPI(InfoOptions{ + ResID: 12, + Name: "foo-bar", + Kind: "task", + Catalog: "tekton", + Version: "0.1", + }, taskResWithOldVersion) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opts := options{ + cli: cli, + kind: "task", + args: []string{"foo-bar"}, + from: "tekton", + version: "0.1", + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestPipelineTask_MultiLineDescription(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + taskResWithLatestVersion.Description = "A Task is a collection of Steps that you define and arrange in a specific order of execution as part of your continuous integration flow. A Task executes as a Pod on your Kubernetes cluster. A Task is available within a specific namespace." + + mockAPI(InfoOptions{ + ResID: 12, + Name: "foo-bar", + Kind: "task", + Catalog: "tekton", + Version: "0.2", + }, taskResWithLatestVersion) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opts := options{ + cli: cli, + kind: "task", + args: []string{"foo-bar"}, + from: "tekton", + version: "0.2", + } + + err := opts.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} diff --git a/pkg/cmd/hub/cmd/info/testdata/TestInfoTask_WithLatestVersion.golden b/pkg/cmd/hub/cmd/info/testdata/TestInfoTask_WithLatestVersion.golden new file mode 100644 index 0000000000..5e5607f3e3 --- /dev/null +++ b/pkg/cmd/hub/cmd/info/testdata/TestInfoTask_WithLatestVersion.golden @@ -0,0 +1,21 @@ +📦 Name: foo-bar + +📌 Version: 0.2 + +📖 Description: Description for task foo-bar version 0.2 + +🗒 Minimum Pipeline Version: 0.12 + +⭐ ️Rating: 4 + +🏷️ ️Categories + ∙ foo-bar + +🏷 Tags + ∙ foo + +💻 Platforms + ∙ linux/amd64 + +⚒ Install Command: + tkn hub install task foo-bar --version 0.2 diff --git a/pkg/cmd/hub/cmd/info/testdata/TestInfoTask_WithOldVersion.golden b/pkg/cmd/hub/cmd/info/testdata/TestInfoTask_WithOldVersion.golden new file mode 100644 index 0000000000..8ad1f9e7a6 --- /dev/null +++ b/pkg/cmd/hub/cmd/info/testdata/TestInfoTask_WithOldVersion.golden @@ -0,0 +1,21 @@ +📦 Name: foo-bar + +📌 Version: 0.1 (Deprecated) + +📖 Description: Description for task foo-bar version 0.1 + +🗒 Minimum Pipeline Version: 0.12 + +⭐ ️Rating: 4 + +🏷️ ️Categories + ∙ foo-bar + +🏷 Tags + ∙ foo + +💻 Platforms + ∙ linux/s390x + +⚒ Install Command: + tkn hub install task foo-bar --version 0.1 diff --git a/pkg/cmd/hub/cmd/info/testdata/TestPipelineTask_MultiLineDescription.golden b/pkg/cmd/hub/cmd/info/testdata/TestPipelineTask_MultiLineDescription.golden new file mode 100644 index 0000000000..ce6de1a1e1 --- /dev/null +++ b/pkg/cmd/hub/cmd/info/testdata/TestPipelineTask_MultiLineDescription.golden @@ -0,0 +1,24 @@ +📦 Name: foo-bar + +📌 Version: 0.2 + +📖 Description: A Task is a collection of Steps that you define and arrange in + a specific order of execution as part of your continuous integration flow. A + Task executes as a Pod on your Kubernetes cluster. A Task is available within + a specific namespace. + +🗒 Minimum Pipeline Version: 0.12 + +⭐ ️Rating: 4 + +🏷️ ️Categories + ∙ foo-bar + +🏷 Tags + ∙ foo + +💻 Platforms + ∙ linux/amd64 + +⚒ Install Command: + tkn hub install task foo-bar --version 0.2 diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/install/install.go b/pkg/cmd/hub/cmd/install/install.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/install/install.go rename to pkg/cmd/hub/cmd/install/install.go index 85ecceb80c..8021379de0 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/install/install.go +++ b/pkg/cmd/hub/cmd/install/install.go @@ -19,12 +19,12 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/flag" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/pkg/cli/installer" - "github.com/tektoncd/hub/api/pkg/cli/kube" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/flag" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/installer" + "github.com/tektoncd/cli/pkg/cmd/hub/kube" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" "golang.org/x/text/cases" "golang.org/x/text/language" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/pkg/cmd/hub/cmd/install/install_test.go b/pkg/cmd/hub/cmd/install/install_test.go new file mode 100644 index 0000000000..a3f15a6f96 --- /dev/null +++ b/pkg/cmd/hub/cmd/install/install_test.go @@ -0,0 +1,1086 @@ +// Copyright © 2020 The Tekton 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 install + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + cb "github.com/tektoncd/cli/pkg/cmd/hub/test/builder" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + pipelinetest "github.com/tektoncd/pipeline/test" + goa "goa.design/goa/v3/pkg" + "gopkg.in/h2non/gock.v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var resVersion = &res.ResourceVersionData{ + ID: 11, + Version: "0.3", + DisplayName: "foo-bar", + Description: "v0.3 Task to run foo", + MinPipelinesVersion: "0.12", + RawURL: "http://raw.github.url/foo/0.3/foo.yaml", + WebURL: "http://web.github.com/foo/0.3/foo.yaml", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + Resource: &res.ResourceData{ + ID: 1, + Name: "foo", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4.8, + Tags: []*res.Tag{ + { + ID: 3, + Name: "cli", + }, + }, + }, +} + +var task1 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskWithNewVersionYaml = &res.ResourceContent{ + Yaml: &task1, +} + +var task2 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.2' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' + tekton.dev/deprecated: 'true' +spec: + description: >- + v0.2 Task to run foo +` + +var taskWithOldVersionYaml = &res.ResourceContent{ + Yaml: &task2, +} + +var task3 = `--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskV1WithNewVersionYaml = &res.ResourceContent{ + Yaml: &task3, +} + +var task4 = `--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.2' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' + tekton.dev/deprecated: 'true' +spec: + description: >- + v0.2 Task to run foo +` + +var taskV1WithOldVersionYaml = &res.ResourceContent{ + Yaml: &task4, +} + +func TestInstall_NewResource(t *testing.T) { + t.Run("TestInstall_NewResource_TektonHub", func(t *testing.T) { + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + from := "tekton" + version := "0.3" + opts := createOpts(t, buf, hub.TektonHubType, from, version) + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) + }) + + t.Run("TestInstall_NewResource_ArtifactHub", func(t *testing.T) { + defer gock.Off() + + from := "tekton-catalog-tasks" + task := "foo" + version := "0.3.0" + ahPkgData := hub.ArtifactHubPkgData{ + ManifestRaw: task1, + PipelineMinVer: "0.12", + } + ahPkg := hub.ArtifactHubPkgResponse{Data: ahPkgData} + resInfo := fmt.Sprintf("%s-%s/%s/%s/%s", "/api/v1/packages/tekton", "task", from, task, version) + + gock.New(test.API). + Get(resInfo). + Reply(200). + JSON(&ahPkg) + + buf := new(bytes.Buffer) + opts := createOpts(t, buf, hub.ArtifactHubType, from, version) + + err := opts.run() + + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) + }) +} + +func TestV1Install_NewResource(t *testing.T) { + t.Run("TestInstall_NewResource_TektonHub", func(t *testing.T) { + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + from := "tekton" + version := "0.3" + opts := createV1Opts(t, buf, hub.TektonHubType, from, version) + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) + }) + + t.Run("TestInstall_NewResource_ArtifactHub", func(t *testing.T) { + defer gock.Off() + + from := "tekton-catalog-tasks" + task := "foo" + version := "0.3.0" + ahPkgData := hub.ArtifactHubPkgData{ + ManifestRaw: task3, + PipelineMinVer: "0.12", + } + ahPkg := hub.ArtifactHubPkgResponse{Data: ahPkgData} + resInfo := fmt.Sprintf("%s-%s/%s/%s/%s", "/api/v1/packages/tekton", "task", from, task, version) + + gock.New(test.API). + Get(resInfo). + Reply(200). + JSON(&ahPkg) + + buf := new(bytes.Buffer) + opts := createV1Opts(t, buf, hub.ArtifactHubType, from, version) + + err := opts.run() + + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) + }) +} + +func TestInstall_ResourceNotFound(t *testing.T) { + serviceErr := &goa.ServiceError{ + ID: "123456", + Name: "not-found", + Message: "resource not found", + } + + t.Run("TestInstall_ResourceNotFound_TektonHub", func(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3/yaml"). + Reply(404). + JSON(serviceErr) + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(404). + JSON(serviceErr) + + opts := &options{ + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3) from tekton catalog not found in Hub") + }) + + t.Run("TestInstall_ResourceNotFound_ArtifactHub", func(t *testing.T) { + cli := test.NewCLI(hub.ArtifactHubType) + + defer gock.Off() + + from := "tekton-catalog-tasks-not-exist" + task := "foo" + version := "0.3.0" + resInfo := fmt.Sprintf("%s-%s/%s/%s/%s", "/api/v1/packages/tekton", "task", from, task, version) + + gock.New(test.API). + Get(resInfo). + Reply(404). + JSON(serviceErr) + + opts := &options{ + cli: cli, + kind: "task", + args: []string{"foo"}, + from: from, + version: version, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3.0) from tekton-catalog-tasks-not-exist catalog not found in Hub") + }) +} + +func TestInstall_ResourceAlreadyExistError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo already exists in hub namespace but seems to be missing version label. Use reinstall command to overwrite existing") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Install_ResourceAlreadyExistError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo already exists in hub namespace but seems to be missing version label. Use reinstall command to overwrite existing") + assert.Equal(t, gock.IsDone(), true) +} + +func TestInstall_UpgradeError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.1"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.1.0) already exists in hub namespace. Use upgrade command to install v0.3.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Install_UpgradeError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.1"}, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.1.0) already exists in hub namespace. Use upgrade command to install v0.3.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestInstall_SameVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.3"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3.0) already exists in hub namespace. Use reinstall command to overwrite existing") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Install_SameVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.3"}, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3.0) already exists in hub namespace. Use reinstall command to overwrite existing") + assert.Equal(t, gock.IsDone(), true) +} + +func TestInstall_LowerVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.7"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.7.0) already exists in hub namespace. Use downgrade command to install v0.3.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Install_LowerVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.7"}, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.7.0) already exists in hub namespace. Use downgrade command to install v0.3.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestInstall_RespectingPipelinesVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + version := "v1beta1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedV1beta1TestData(t, test.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo(0.3) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Install_RespectingPipelinesVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + version := "v1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedTestData(t, pipelinetest.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo(0.3) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestInstall_RespectingPipelinesVersionFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + version := "v1beta1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedV1beta1TestData(t, test.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.11.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3) requires Tekton Pipelines min version v0.12 but found v0.11.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Install_RespectingPipelinesVersionFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + version := "v1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedTestData(t, pipelinetest.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.11.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3) requires Tekton Pipelines min version v0.12 but found v0.11.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestInstall_DeprecatedVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithOldVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "0.2") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + version := "v1beta1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedV1beta1TestData(t, test.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.2", + } + + err := opts.run() + + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nWARN: This version has been deprecated\nTask foo(0.2) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Install_DeprecatedVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithOldVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.2") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "0.2") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.2"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + version := "v1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedTestData(t, pipelinetest.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.2", + } + + err := opts.run() + + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nWARN: This version has been deprecated\nTask foo(0.2) installed in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func createOpts(t *testing.T, buf *bytes.Buffer, hubType, from, version string) *options { + cli := test.NewCLI(hubType) + cli.SetStream(buf, buf) + + dynamic := test.DynamicClient() + cs, _ := test.SeedV1beta1TestData(t, test.Data{}) + cs.Pipeline.Resources = cb.APIResourceList("v1beta1", []string{"task"}) + + return &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: from, + version: version, + } +} + +func createV1Opts(t *testing.T, buf *bytes.Buffer, hubType, from, version string) *options { + cli := test.NewCLI(hubType) + cli.SetStream(buf, buf) + + dynamic := test.DynamicClient() + cs, _ := test.SeedTestData(t, pipelinetest.Data{}) + cs.Pipeline.Resources = cb.APIResourceList("v1", []string{"task"}) + + return &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: from, + version: version, + } +} diff --git a/pkg/cmd/hub/cmd/install/testdata/foo-v0.2.yaml b/pkg/cmd/hub/cmd/install/testdata/foo-v0.2.yaml new file mode 100644 index 0000000000..ae05793571 --- /dev/null +++ b/pkg/cmd/hub/cmd/install/testdata/foo-v0.2.yaml @@ -0,0 +1,15 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.2' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' + tekton.dev/deprecated: 'true' +spec: + description: >- + v0.2 Task to run foo diff --git a/pkg/cmd/hub/cmd/install/testdata/foo-v0.3.yaml b/pkg/cmd/hub/cmd/install/testdata/foo-v0.3.yaml new file mode 100644 index 0000000000..c43a2e5705 --- /dev/null +++ b/pkg/cmd/hub/cmd/install/testdata/foo-v0.3.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/reinstall/reinstall.go b/pkg/cmd/hub/cmd/reinstall/reinstall.go similarity index 96% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/reinstall/reinstall.go rename to pkg/cmd/hub/cmd/reinstall/reinstall.go index d24dd66493..f9a5a7986b 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/reinstall/reinstall.go +++ b/pkg/cmd/hub/cmd/reinstall/reinstall.go @@ -19,12 +19,12 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/flag" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/pkg/cli/installer" - "github.com/tektoncd/hub/api/pkg/cli/kube" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/flag" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/installer" + "github.com/tektoncd/cli/pkg/cmd/hub/kube" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" "golang.org/x/text/cases" "golang.org/x/text/language" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/pkg/cmd/hub/cmd/reinstall/reinstall_test.go b/pkg/cmd/hub/cmd/reinstall/reinstall_test.go new file mode 100644 index 0000000000..79444bc33e --- /dev/null +++ b/pkg/cmd/hub/cmd/reinstall/reinstall_test.go @@ -0,0 +1,820 @@ +// Copyright © 2020 The Tekton 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 reinstall + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + cb "github.com/tektoncd/cli/pkg/cmd/hub/test/builder" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + pipelinetest "github.com/tektoncd/pipeline/test" + "gopkg.in/h2non/gock.v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var resVersion = &res.ResourceVersionData{ + ID: 11, + Version: "0.3", + DisplayName: "foo-bar", + Description: "v0.3 Task to run foo", + MinPipelinesVersion: "0.12", + RawURL: "http://raw.github.url/foo/0.3/foo.yaml", + WebURL: "http://web.github.com/foo/0.3/foo.yaml", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + Resource: &res.ResourceData{ + ID: 1, + Name: "foo", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4.8, + Tags: []*res.Tag{ + { + ID: 3, + Name: "cli", + }, + }, + }, +} + +var task1 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskWithNewVersionYaml = &res.ResourceContent{ + Yaml: &task1, +} + +var task2 = `--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskV1WithNewVersionYaml = &res.ResourceContent{ + Yaml: &task2, +} + +func TestReinstall_ResourceNotExist(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + version := "v1beta1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedV1beta1TestData(t, test.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo doesn't exists in hub namespace. Use install command to install the task") +} + +func TestV1Reinstall_ResourceNotExist(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + version := "v1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedTestData(t, pipelinetest.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo doesn't exists in hub namespace. Use install command to install the task") +} + +func TestReinstall_VersionCatalogMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "existing task seems to be missing version and catalog label. Use --version & --catalog (Default: tekton) flag to reinstall the task") +} + +func TestV1Reinstall_VersionCatalogMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "existing task seems to be missing version and catalog label. Use --version & --catalog (Default: tekton) flag to reinstall the task") +} + +func TestReinstall_VersionMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"hub.tekton.dev/catalog": "abc"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "existing task seems to be missing version label. Use --version flag to reinstall the task") +} + +func TestV1Reinstall_VersionMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"hub.tekton.dev/catalog": "abc"}, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "existing task seems to be missing version label. Use --version flag to reinstall the task") +} + +func TestReinstall_DifferentVersionPassedByFlag(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.1"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Reinstall_DifferentVersionPassedByFlag(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"app.kubernetes.io/version": "0.1"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestReinstall_DifferentCatalogPassedByFlag(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "abc", + "app.kubernetes.io/version": "0.1", + }, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Reinstall_DifferentCatalogPassedByFlag(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "abc", + "app.kubernetes.io/version": "0.1", + }, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + from: "tekton", + version: "0.3", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestReinstall(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Reinstall(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestReinstall_RespectPipelinesVersionSuccess(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Reinstall_RespectPipelinesVersionSuccess(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo(0.3) reinstalled in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestReinstall_RespectPipelinesVersionFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := test.CreateTektonPipelineController(dynamic, "v0.11.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3) can't be reinstalled as it requires Pipelines min version v0.12 but found v0.11.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Reinstall_RespectPipelinesVersionFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := test.CreateTektonPipelineController(dynamic, "v0.11.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo(0.3) can't be reinstalled as it requires Pipelines min version v0.12 but found v0.11.0") + assert.Equal(t, gock.IsDone(), true) +} diff --git a/pkg/cmd/hub/cmd/reinstall/testdata/foo-v0.3.yaml b/pkg/cmd/hub/cmd/reinstall/testdata/foo-v0.3.yaml new file mode 100644 index 0000000000..c43a2e5705 --- /dev/null +++ b/pkg/cmd/hub/cmd/reinstall/testdata/foo-v0.3.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/root.go b/pkg/cmd/hub/cmd/root.go similarity index 79% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/root.go rename to pkg/cmd/hub/cmd/root.go index 8d684857ea..608db19922 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/root.go +++ b/pkg/cmd/hub/cmd/root.go @@ -18,16 +18,16 @@ import ( "fmt" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/cmd/check_upgrade" - "github.com/tektoncd/hub/api/pkg/cli/cmd/downgrade" - "github.com/tektoncd/hub/api/pkg/cli/cmd/get" - "github.com/tektoncd/hub/api/pkg/cli/cmd/info" - "github.com/tektoncd/hub/api/pkg/cli/cmd/install" - "github.com/tektoncd/hub/api/pkg/cli/cmd/reinstall" - "github.com/tektoncd/hub/api/pkg/cli/cmd/search" - "github.com/tektoncd/hub/api/pkg/cli/cmd/upgrade" - "github.com/tektoncd/hub/api/pkg/cli/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + checkupgrade "github.com/tektoncd/cli/pkg/cmd/hub/cmd/check_upgrade" + "github.com/tektoncd/cli/pkg/cmd/hub/cmd/downgrade" + "github.com/tektoncd/cli/pkg/cmd/hub/cmd/get" + "github.com/tektoncd/cli/pkg/cmd/hub/cmd/info" + "github.com/tektoncd/cli/pkg/cmd/hub/cmd/install" + "github.com/tektoncd/cli/pkg/cmd/hub/cmd/reinstall" + "github.com/tektoncd/cli/pkg/cmd/hub/cmd/search" + "github.com/tektoncd/cli/pkg/cmd/hub/cmd/upgrade" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" ) // Root represents the base command when called without any subcommands @@ -40,8 +40,8 @@ func Root(cli app.CLI) *cobra.Command { Annotations: map[string]string{ "commandType": "main", }, - Short: "Interact with tekton hub", - Long: `Interact with tekton hub + Short: "Interact with artifacthub", + Long: `Interact with artifacthub Deprecation Notice: Tekton Hub support in CLI is being deprecated in favor of Artifact Hub. The following commands currently only work with Tekton Hub and may support Artifact Hub in a future release: @@ -87,14 +87,14 @@ Artifact Hub (https://artifacthub.io) will become the only supported hub in futu reinstall.Command(cli), search.Command(cli), upgrade.Command(cli), - check_upgrade.Command(cli), + checkupgrade.Command(cli), ) cmd.PersistentFlags().StringVar(&apiURL, "api-server", "", "Hub API Server URL.\n"+ "For artifact type: default 'https://artifacthub.io' (env: ARTIFACT_HUB_API_SERVER)\n"+ "For tekton type (DEPRECATED): default 'https://api.hub.tekton.dev' (env: TEKTON_HUB_API_SERVER)\n"+ "Can also be set in '$HOME/.tekton/hub-config'.") - cmd.PersistentFlags().StringVar(&hubType, "type", "tekton", "The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' "+ + cmd.PersistentFlags().StringVar(&hubType, "type", "artifact", "The type of Hub from where to pull the resource. Either 'artifact' or 'tekton' "+ "(DEPRECATED: tekton type will be removed in a future release)") return cmd diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/search/search.go b/pkg/cmd/hub/cmd/search/search.go similarity index 93% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/search/search.go rename to pkg/cmd/hub/cmd/search/search.go index 1b8fd44fcf..bd3bd65bae 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/search/search.go +++ b/pkg/cmd/hub/cmd/search/search.go @@ -20,12 +20,12 @@ import ( "text/template" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/flag" - "github.com/tektoncd/hub/api/pkg/cli/formatter" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/pkg/cli/printer" - "github.com/tektoncd/hub/api/pkg/parser" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/flag" + "github.com/tektoncd/cli/pkg/cmd/hub/formatter" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/parser" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" ) const resTemplate = `{{- $rl := len .Resources }}{{ if eq $rl 0 -}} @@ -90,9 +90,9 @@ func Command(cli app.CLI) *cobra.Command { opts := &options{cli: cli} cmd := &cobra.Command{ - Use: "search", - Short: "Search resource by a combination of name, kind, categories, platforms, and tags", - Long: ``, + Use: "search", + Short: "Search resource by a combination of name, kind, categories, platforms, and tags", + Long: ``, Example: examples, Deprecated: "this command currently only works with Tekton Hub which is deprecated. It may support Artifact Hub in a future release.", Annotations: map[string]string{ diff --git a/pkg/cmd/hub/cmd/search/search_test.go b/pkg/cmd/hub/cmd/search/search_test.go new file mode 100644 index 0000000000..37a66cf4bc --- /dev/null +++ b/pkg/cmd/hub/cmd/search/search_test.go @@ -0,0 +1,288 @@ +// Copyright © 2020 The Tekton 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 search + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + goa "goa.design/goa/v3/pkg" + "gopkg.in/h2non/gock.v1" + "gotest.tools/v3/golden" +) + +var deprecated bool +var res1 = &res.ResourceData{ + ID: 1, + Name: "foo", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4.8, + LatestVersion: &res.ResourceVersionData{ + ID: 11, + Version: "0.1", + Description: "Description for task abc version 0.1", + DisplayName: "foo-0.1", + Deprecated: &deprecated, + MinPipelinesVersion: "0.11", + RawURL: "http://raw.github.url/foo/", + WebURL: "http://web.github.com/foo/", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + Platforms: []*res.Platform{ + {ID: 3, Name: "linux/amd64"}, + {ID: 1, Name: "linux/s390x"}, + }, + }, + Tags: []*res.Tag{ + {ID: 3, Name: "tag3"}, + {ID: 1, Name: "tag1"}, + }, + Platforms: []*res.Platform{ + {ID: 3, Name: "linux/amd64"}, + {ID: 1, Name: "linux/s390x"}, + }, +} + +var res2 = &res.ResourceData{ + ID: 2, + Name: "foo-bar", + Kind: "Pipeline", + Catalog: &res.Catalog{ + ID: 1, + Name: "foo", + Type: "community", + }, + Categories: []*res.Category{}, + Rating: 4, + HubURLPath: "foo/Pipeline/foo-bar", + LatestVersion: &res.ResourceVersionData{ + ID: 12, + Version: "0.2", + Description: "Description for pipeline foo-bar version 0.2", + DisplayName: "foo-bar-0.1", + Deprecated: &deprecated, + MinPipelinesVersion: "0.12", + RawURL: "http://raw.github.url/foo-bar/", + WebURL: "http://web.github.com/foo-bar/", + HubURLPath: "foo/Pipeline/foo-bar/0.2", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + }, + Tags: []*res.Tag{}, +} + +func TestValidate(t *testing.T) { + cli := app.New() + if err := cli.SetHub(hub.TektonHubType); err != nil { + assert.Error(t, err) + } + + opt := options{ + kinds: []string{"pipeline"}, + tags: []string{"abc,def", "mno"}, + match: "exact", + output: "table", + cli: cli, + } + + err := opt.validate() + assert.NoError(t, err) + + opt = options{ + args: []string{"abc"}, + match: "contains", + output: "json", + cli: cli, + } + + err = opt.validate() + assert.NoError(t, err) +} + +func TestValidate_ErrorCases(t *testing.T) { + + cli := app.New() + if err := cli.SetHub(hub.TektonHubType); err != nil { + assert.Error(t, err) + } + + opt := options{cli: cli} + err := opt.validate() + assert.Error(t, err) + assert.EqualError(t, err, "please specify a resource name, --tags, --platforms, --categories or --kinds flag to search") + + opt = options{ + kinds: []string{"abc"}, + match: "exact", + output: "table", + cli: cli, + } + err = opt.validate() + assert.Error(t, err) + assert.EqualError(t, err, "invalid value \"abc\" set for option kinds. supported kinds: [task, pipeline]") + + opt = options{ + kinds: []string{"task"}, + match: "abc", + output: "table", + cli: cli, + } + err = opt.validate() + assert.Error(t, err) + assert.EqualError(t, err, "invalid value \"abc\" set for option match. Valid options: [contains, exact]") + + opt = options{ + kinds: []string{"task"}, + match: "exact", + output: "abc", + cli: cli, + } + err = opt.validate() + assert.Error(t, err) + assert.EqualError(t, err, "invalid value \"abc\" set for option output. Valid options: [table, json, wide]") +} + +func TestSearch_TableFormat(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + rArr := &res.Resources{Data: res.ResourceDataCollection{res1, res2}} + res := res.NewViewedResources(rArr, "withoutVersion") + + gock.New(test.API). + Get("/query"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opt := &options{ + cli: cli, + args: []string{"foo"}, + match: "contains", + output: "wide", + } + + err := opt.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +// Updates golden file as GOA is unable to pick the min view of catalog +func TestSearch_JSONFormat(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + rArr := &res.Resources{Data: res.ResourceDataCollection{res2}} + res := res.NewViewedResources(rArr, "withoutVersion") + + gock.New(test.API). + Get("/query"). + Reply(200). + JSON(&res.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opt := &options{ + cli: cli, + args: []string{"foo-bar"}, + match: "exact", + output: "json", + } + + err := opt.run() + assert.NoError(t, err) + golden.Assert(t, buf.String(), fmt.Sprintf("%s.golden", t.Name())) + assert.Equal(t, gock.IsDone(), true) +} + +func TestSearch_ResourceNotFound(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + gock.New(test.API). + Get("/query"). + Reply(404). + JSON(&goa.ServiceError{ + ID: "123456", + Name: "not-found", + Message: "resource not found", + }) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opt := &options{ + cli: cli, + args: []string{"xyz"}, + match: "exact", + output: "json", + } + + err := opt.run() + assert.NoError(t, err) + assert.Equal(t, gock.IsDone(), true) +} + +func TestSearch_InternalServerError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + gock.New(test.API). + Get("/query"). + Reply(500). + JSON(&goa.ServiceError{ + ID: "123456", + Name: "internal-error", + Message: "failed to fetch resources", + }) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + opt := &options{ + cli: cli, + args: []string{"xyz"}, + match: "exact", + output: "json", + } + + err := opt.run() + assert.Error(t, err) + assert.EqualError(t, err, "Internal server Error: consider filing a bug report") + assert.Equal(t, gock.IsDone(), true) +} + +func TestSearch_InvalidAPIServerURL(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + err := cli.Hub().SetURL("api") + assert.Error(t, err) + assert.EqualError(t, err, "parse \"api\": invalid URI for request") +} diff --git a/pkg/cmd/hub/cmd/search/testdata/TestSearch_JSONFormat.golden b/pkg/cmd/hub/cmd/search/testdata/TestSearch_JSONFormat.golden new file mode 100644 index 0000000000..46b1f8112d --- /dev/null +++ b/pkg/cmd/hub/cmd/search/testdata/TestSearch_JSONFormat.golden @@ -0,0 +1,38 @@ +{ + "Data": [ + { + "ID": 2, + "Name": "foo-bar", + "Catalog": { + "ID": 1, + "Name": "foo", + "Type": "community", + "URL": null, + "Provider": null + }, + "Categories": [], + "Kind": "Pipeline", + "HubURLPath": "foo/Pipeline/foo-bar", + "HubRawURLPath": "", + "LatestVersion": { + "ID": 12, + "Version": "0.2", + "DisplayName": "foo-bar-0.1", + "Deprecated": false, + "Description": "Description for pipeline foo-bar version 0.2", + "MinPipelinesVersion": "0.12", + "RawURL": "http://raw.github.url/foo-bar/", + "WebURL": "http://web.github.com/foo-bar/", + "HubRawURLPath": "", + "UpdatedAt": "2020-01-01 12:00:00 +0000 UTC", + "Platforms": [], + "HubURLPath": "foo/Pipeline/foo-bar/0.2", + "Resource": null + }, + "Tags": [], + "Platforms": [], + "Rating": 4, + "Versions": null + } + ] +} diff --git a/pkg/cmd/hub/cmd/search/testdata/TestSearch_TableFormat.golden b/pkg/cmd/hub/cmd/search/testdata/TestSearch_TableFormat.golden new file mode 100644 index 0000000000..745a415af9 --- /dev/null +++ b/pkg/cmd/hub/cmd/search/testdata/TestSearch_TableFormat.golden @@ -0,0 +1,3 @@ +NAME KIND CATALOG DESCRIPTION PLATFORMS TAGS CATEGORIES +foo (0.1) Task Tekton Description for task abc version 0.1 linux/amd64, linux/s390x tag3, tag1 --- +foo-bar (0.2) Pipeline Foo Description for pipeline foo-bar versio... --- --- --- diff --git a/pkg/cmd/hub/cmd/upgrade/testdata/foo-v0.3.yaml b/pkg/cmd/hub/cmd/upgrade/testdata/foo-v0.3.yaml new file mode 100644 index 0000000000..c43a2e5705 --- /dev/null +++ b/pkg/cmd/hub/cmd/upgrade/testdata/foo-v0.3.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/upgrade/upgrade.go b/pkg/cmd/hub/cmd/upgrade/upgrade.go similarity index 96% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/upgrade/upgrade.go rename to pkg/cmd/hub/cmd/upgrade/upgrade.go index f5e780ba22..4af2611b02 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/cmd/upgrade/upgrade.go +++ b/pkg/cmd/hub/cmd/upgrade/upgrade.go @@ -19,12 +19,12 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/flag" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/pkg/cli/installer" - "github.com/tektoncd/hub/api/pkg/cli/kube" - "github.com/tektoncd/hub/api/pkg/cli/printer" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/flag" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/installer" + "github.com/tektoncd/cli/pkg/cmd/hub/kube" + "github.com/tektoncd/cli/pkg/cmd/hub/printer" "golang.org/x/text/cases" "golang.org/x/text/language" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/pkg/cmd/hub/cmd/upgrade/upgrade_test.go b/pkg/cmd/hub/cmd/upgrade/upgrade_test.go new file mode 100644 index 0000000000..2282d58317 --- /dev/null +++ b/pkg/cmd/hub/cmd/upgrade/upgrade_test.go @@ -0,0 +1,814 @@ +// Copyright © 2020 The Tekton 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 upgrade + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + res "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + cb "github.com/tektoncd/cli/pkg/cmd/hub/test/builder" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + pipelinetest "github.com/tektoncd/pipeline/test" + "gopkg.in/h2non/gock.v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var resVersion = &res.ResourceVersionData{ + ID: 11, + Version: "0.3", + DisplayName: "foo-bar", + Description: "v0.3 Task to run foo", + MinPipelinesVersion: "0.12", + RawURL: "http://raw.github.url/foo/0.3/foo.yaml", + WebURL: "http://web.github.com/foo/0.3/foo.yaml", + UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", + Resource: &res.ResourceData{ + ID: 1, + Name: "foo", + Kind: "Task", + Catalog: &res.Catalog{ + ID: 1, + Name: "tekton", + Type: "community", + }, + Rating: 4.8, + Tags: []*res.Tag{ + { + ID: 3, + Name: "cli", + }, + }, + }, +} + +var task1 = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskWithNewVersionYaml = &res.ResourceContent{ + Yaml: &task1, +} + +var task2 = `--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: foo + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'foo-bar' +spec: + description: >- + v0.3 Task to run foo +` + +var taskV1WithNewVersionYaml = &res.ResourceContent{ + Yaml: &task2, +} + +func TestUpgrade_ResourceNotExist(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + version := "v1beta1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedV1beta1TestData(t, test.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo doesn't exist in hub namespace. Use install command to install the task") +} + +func TestV1Upgrade_ResourceNotExist(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + version := "v1" + dynamic := test.DynamicClient() + + cs, _ := test.SeedTestData(t, pipelinetest.Data{}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo doesn't exist in hub namespace. Use install command to install the task") +} + +func TestUpgrade_VersionCatalogMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version and catalog label. Use reinstall command to overwrite existing task") +} + +func TestV1Upgrade_VersionCatalogMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version and catalog label. Use reinstall command to overwrite existing task") +} + +func TestUpgrade_VersionMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"hub.tekton.dev/catalog": "abc"}, + }, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version label. Use reinstall command to overwrite existing task") +} + +func TestV1Upgrade_VersionMissing(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{"hub.tekton.dev/catalog": "abc"}, + }, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "Task foo seems to be missing version label. Use reinstall command to overwrite existing task") +} + +func TestUpgrade_ToSpecificVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo upgraded to v0.3 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Upgrade_ToSpecificVersion(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.NoError(t, err) + assert.Equal(t, "WARN: tekton pipelines version unknown, this resource is compatible with pipelines min version v0.12\nTask foo upgraded to v0.3 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestUpgrade_SameVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot upgrade task foo to v0.3. existing resource seems to be of same version. Use reinstall command to overwrite existing task") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Upgrade_SameVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.3", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot upgrade task foo to v0.3. existing resource seems to be of same version. Use reinstall command to overwrite existing task") + assert.Equal(t, gock.IsDone(), true) +} + +func TestUpgrade_LowerVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.7", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot upgrade task foo to v0.3. existing resource seems to be of higher version(v0.7). Use downgrade command") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Upgrade_LowerVersionError(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.7", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot upgrade task foo to v0.3. existing resource seems to be of higher version(v0.7). Use downgrade command") + assert.Equal(t, gock.IsDone(), true) +} + +func TestUpgrade_ToSpecificVersion_RespectingPipelineSuccess(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo upgraded to v0.3 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Upgrade_ToSpecificVersion_RespectingPipelineSuccess(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.14.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.NoError(t, err) + assert.Equal(t, "Task foo upgraded to v0.3 in hub namespace\n", buf.String()) + assert.Equal(t, gock.IsDone(), true) +} + +func TestUpgrade_ToSpecificVersion_RespectingPipelineFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskWithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.11.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot upgrade Task foo(0.3) as it requires Tekton Pipelines min version v0.12 but found v0.11.0") + assert.Equal(t, gock.IsDone(), true) +} + +func TestV1Upgrade_ToSpecificVersion_RespectingPipelineFailure(t *testing.T) { + cli := test.NewCLI(hub.TektonHubType) + + defer gock.Off() + + rVer := &res.ResourceVersionYaml{Data: taskV1WithNewVersionYaml} + resWithVersion := res.NewViewedResourceVersionYaml(rVer, "default") + + resInfo := fmt.Sprintf("%s/%s/%s/%s", "tekton", "task", "foo", "0.3") + + gock.New(test.API). + Get("/resource/" + resInfo + "/yaml"). + Reply(200). + JSON(&resWithVersion.Projected) + + resVersion := &res.ResourceVersion{Data: resVersion} + resource := res.NewViewedResourceVersion(resVersion, "default") + gock.New(test.API). + Get("/resource/tekton/task/foo/0.3"). + Reply(200). + JSON(&resource.Projected) + + buf := new(bytes.Buffer) + cli.SetStream(buf, buf) + + existingTask := &v1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1" + dynamic := test.DynamicClient(cb.UnstructuredV1(existingTask, version)) + + cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: []*v1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + opts := &options{ + cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), + cli: cli, + kind: "task", + args: []string{"foo"}, + version: "0.3", + } + + err := test.CreateTektonPipelineController(dynamic, "v0.11.0") + if err != nil { + t.Errorf("%s", err.Error()) + } + + err = opts.run() + assert.Error(t, err) + assert.EqualError(t, err, "cannot upgrade Task foo(0.3) as it requires Tekton Pipelines min version v0.12 but found v0.11.0") + assert.Equal(t, gock.IsDone(), true) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/flag/validate.go b/pkg/cmd/hub/flag/validate.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/flag/validate.go rename to pkg/cmd/hub/flag/validate.go diff --git a/pkg/cmd/hub/flag/validate_test.go b/pkg/cmd/hub/flag/validate_test.go new file mode 100644 index 0000000000..0a6bb26959 --- /dev/null +++ b/pkg/cmd/hub/flag/validate_test.go @@ -0,0 +1,54 @@ +// Copyright © 2020 The Tekton 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 flag + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestInList(t *testing.T) { + + // Valid Case + err := InList("match", "abc", []string{"abc", "def"}) + assert.NoError(t, err) + + // Invalid Case + err = InList("match", "xyz", []string{"abc", "def"}) + assert.EqualError(t, err, "invalid value \"xyz\" set for option match. Valid options: [abc, def]") +} + +func TestTrimArray(t *testing.T) { + + input := []string{"a,b", "abc", "xyz,mno"} + res := TrimArray(input) + want := []string{"a", "b", "abc", "xyz", "mno"} + assert.Equal(t, want, res) +} + +func TestValidateVersion(t *testing.T) { + + // Valid Case + err := ValidateVersion("0.1") + assert.NoError(t, err) + + err = ValidateVersion("0.1.1") + assert.NoError(t, err) + + // Invalid Case + err = ValidateVersion("abc") + assert.EqualError(t, err, "invalid value \"abc\" set for option version. valid eg. 0.1, 1.2.1") +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/formatter/field.go b/pkg/cmd/hub/formatter/field.go similarity index 98% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/formatter/field.go rename to pkg/cmd/hub/formatter/field.go index 337e80175c..c0ba0f251c 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/formatter/field.go +++ b/pkg/cmd/hub/formatter/field.go @@ -19,8 +19,8 @@ import ( "strings" "github.com/fatih/color" - "github.com/tektoncd/hub/api/pkg/cli/hub" - "github.com/tektoncd/hub/api/v1/gen/http/resource/client" + "github.com/tektoncd/cli/pkg/cmd/hub/gen/http/resource/client" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" "golang.org/x/term" "golang.org/x/text/cases" "golang.org/x/text/language" diff --git a/pkg/cmd/hub/formatter/field_test.go b/pkg/cmd/hub/formatter/field_test.go new file mode 100644 index 0000000000..0dbc227b95 --- /dev/null +++ b/pkg/cmd/hub/formatter/field_test.go @@ -0,0 +1,145 @@ +// Copyright © 2020 The Tekton 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 formatter + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tektoncd/cli/pkg/cmd/hub/gen/http/resource/client" +) + +func TestFormatName(t *testing.T) { + name := FormatName("abc", "0.1") + assert.Equal(t, name, "abc (0.1)") +} + +func TestFormatCatalogName(t *testing.T) { + catalog := FormatCatalogName("foo") + assert.Equal(t, catalog, "Foo") +} + +func TestFormatDesc(t *testing.T) { + + // Description greater than 40 char + desc := FormatDesc("Buildah task builds source into a container image and then pushes it to a container registry.", 40) + assert.Equal(t, "Buildah task builds source into a conta...", desc) + + // Description less than 40 char + desc = FormatDesc("Buildah task builds images.", 40) + assert.Equal(t, "Buildah task builds images.", desc) + + // No Description + desc = FormatDesc("", 40) + assert.Equal(t, "---", desc) +} + +func TestFormatTags(t *testing.T) { + + tagName1 := "tag1" + tagName2 := "tag2" + + res := []*client.TagResponseBody{ + { + Name: &tagName1, + }, + { + Name: &tagName2, + }, + } + + tags := FormatTags(res) + assert.Equal(t, "tag1, tag2", tags) + + // No Tags + tags = FormatTags(nil) + assert.Equal(t, "---", tags) +} + +func TestFormatCategories(t *testing.T) { + + categoryName1 := "category1" + categoryName2 := "category2" + + res := []*client.CategoryResponseBody{ + { + Name: &categoryName1, + }, + { + Name: &categoryName2, + }, + } + + categories := FormatCategories(res) + assert.Equal(t, "category1, category2", categories) + + // No Categories + categories = FormatTags(nil) + assert.Equal(t, "---", categories) +} + +func TestFormatPlatforms(t *testing.T) { + + pName1 := "linux/amd64" + pName2 := "linux/s390x" + + pRes := []*client.PlatformResponseBody{ + { + Name: &pName1, + }, + { + Name: &pName2, + }, + } + + platforms := FormatPlatforms(pRes) + assert.Equal(t, "linux/amd64, linux/s390x", platforms) + + // No Tags + platforms = FormatTags(nil) + assert.Equal(t, "---", platforms) +} + +func TestWrapText(t *testing.T) { + + // Description of resource with just summa + desc := WrapText("The Buildpacks task builds source into a container image and pushes it to a registry, using Cloud Native Buildpacks.", 80, 16) + assert.Equal(t, "The Buildpacks task builds source into a container image and"+"\n"+" pushes it to a registry, using Cloud Native Buildpacks.", desc) + + // Description of resource with summary and description + desc = WrapText("Buildah task builds source into a container image and then pushes it to a container registry."+"\n"+"Buildah Task builds source into a container image using Project Atomic's Buildah build tool.It uses Buildah's support for building from Dockerfiles, using its buildah bud command.This command executes the directives in the Dockerfile to assemble a container image, then pushes that image to a container registry.", 80, 16) + assert.Equal(t, "Buildah task builds source into a container image and then"+"\n"+" pushes it to a container registry. Buildah Task builds source into a container"+"\n"+" image using Project Atomic's Buildah build tool.It uses Buildah's support for"+"\n"+" building from Dockerfiles, using its buildah bud command.This command executes"+"\n"+" the directives in the Dockerfile to assemble a container image, then pushes"+"\n"+" that image to a container registry.", desc) +} + +func TestFormatVersion(t *testing.T) { + got := FormatVersion("0.1", false, false) + assert.Equal(t, "0.1", got) + + got = FormatVersion("0.1", false, true) + assert.Equal(t, "0.1 (Deprecated)", got) + + got = FormatVersion("0.1", true, false) + assert.Equal(t, "0.1 (Latest)", got) +} + +func TestIcon(t *testing.T) { + got := Icon("bullet") + assert.Equal(t, "∙ ", got) +} + +func TestDecorate(t *testing.T) { + got := DecorateAttr("bold", "world") + assert.Equal(t, "world", got) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/formatter/json.go b/pkg/cmd/hub/formatter/json.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/formatter/json.go rename to pkg/cmd/hub/formatter/json.go diff --git a/pkg/cmd/hub/formatter/json_test.go b/pkg/cmd/hub/formatter/json_test.go new file mode 100644 index 0000000000..746a6b705d --- /dev/null +++ b/pkg/cmd/hub/formatter/json_test.go @@ -0,0 +1,34 @@ +// Copyright © 2020 The Tekton 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 formatter + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +var formatted = `{ + "id": 1, + "name": "abc" +}` + +func TestFormatJSON(t *testing.T) { + + data := []byte(`{"id": 1,"name": "abc"}`) + + res, _ := FormatJSON(data) + assert.Equal(t, formatted, string(res)) +} diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/client.go b/pkg/cmd/hub/gen/catalog/client.go similarity index 93% rename from vendor/github.com/tektoncd/hub/api/v1/gen/catalog/client.go rename to pkg/cmd/hub/gen/catalog/client.go index 553425ee0b..103487229a 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/client.go +++ b/pkg/cmd/hub/gen/catalog/client.go @@ -3,7 +3,7 @@ // catalog client // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package catalog diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/endpoints.go b/pkg/cmd/hub/gen/catalog/endpoints.go similarity index 93% rename from vendor/github.com/tektoncd/hub/api/v1/gen/catalog/endpoints.go rename to pkg/cmd/hub/gen/catalog/endpoints.go index e19ae3be2f..ff72a57dba 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/endpoints.go +++ b/pkg/cmd/hub/gen/catalog/endpoints.go @@ -3,7 +3,7 @@ // catalog endpoints // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package catalog diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/service.go b/pkg/cmd/hub/gen/catalog/service.go similarity index 95% rename from vendor/github.com/tektoncd/hub/api/v1/gen/catalog/service.go rename to pkg/cmd/hub/gen/catalog/service.go index 3516f24fa7..7cffb5a063 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/service.go +++ b/pkg/cmd/hub/gen/catalog/service.go @@ -3,14 +3,14 @@ // catalog service // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package catalog import ( "context" - catalogviews "github.com/tektoncd/hub/api/v1/gen/catalog/views" + catalogviews "github.com/tektoncd/cli/pkg/cmd/hub/gen/catalog/views" goa "goa.design/goa/v3/pkg" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/views/view.go b/pkg/cmd/hub/gen/catalog/views/view.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/v1/gen/catalog/views/view.go rename to pkg/cmd/hub/gen/catalog/views/view.go index 6a2bc9a0c3..7b9dba43bb 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/catalog/views/view.go +++ b/pkg/cmd/hub/gen/catalog/views/view.go @@ -3,7 +3,7 @@ // catalog views // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package views diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/cli.go b/pkg/cmd/hub/gen/http/catalog/client/cli.go similarity index 67% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/cli.go rename to pkg/cmd/hub/gen/http/catalog/client/cli.go index 21ea31e40b..c21ffeee0f 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/cli.go +++ b/pkg/cmd/hub/gen/http/catalog/client/cli.go @@ -3,6 +3,6 @@ // catalog HTTP client CLI support package // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/client.go b/pkg/cmd/hub/gen/http/catalog/client/client.go similarity index 96% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/client.go rename to pkg/cmd/hub/gen/http/catalog/client/client.go index aa7e5b5470..c65004c6fe 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/client.go +++ b/pkg/cmd/hub/gen/http/catalog/client/client.go @@ -3,7 +3,7 @@ // catalog client HTTP transport // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/encode_decode.go b/pkg/cmd/hub/gen/http/catalog/client/encode_decode.go similarity index 96% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/encode_decode.go rename to pkg/cmd/hub/gen/http/catalog/client/encode_decode.go index 49510ee390..424c2e36cf 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/encode_decode.go +++ b/pkg/cmd/hub/gen/http/catalog/client/encode_decode.go @@ -3,7 +3,7 @@ // catalog HTTP client encoders and decoders // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client @@ -14,7 +14,7 @@ import ( "net/http" "net/url" - catalog "github.com/tektoncd/hub/api/v1/gen/catalog" + catalog "github.com/tektoncd/cli/pkg/cmd/hub/gen/catalog" goahttp "goa.design/goa/v3/http" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/paths.go b/pkg/cmd/hub/gen/http/catalog/client/paths.go similarity index 82% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/paths.go rename to pkg/cmd/hub/gen/http/catalog/client/paths.go index 5462d1eba4..05fe1bcc3b 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/paths.go +++ b/pkg/cmd/hub/gen/http/catalog/client/paths.go @@ -3,7 +3,7 @@ // HTTP request path constructors for the catalog service. // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/types.go b/pkg/cmd/hub/gen/http/catalog/client/types.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/types.go rename to pkg/cmd/hub/gen/http/catalog/client/types.go index 0a776f8f4c..892f353f88 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/catalog/client/types.go +++ b/pkg/cmd/hub/gen/http/catalog/client/types.go @@ -3,12 +3,12 @@ // catalog HTTP client types // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client import ( - catalog "github.com/tektoncd/hub/api/v1/gen/catalog" + catalog "github.com/tektoncd/cli/pkg/cmd/hub/gen/catalog" goa "goa.design/goa/v3/pkg" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/cli.go b/pkg/cmd/hub/gen/http/resource/client/cli.go similarity index 98% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/cli.go rename to pkg/cmd/hub/gen/http/resource/client/cli.go index 233eb3b170..381842f768 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/cli.go +++ b/pkg/cmd/hub/gen/http/resource/client/cli.go @@ -3,7 +3,7 @@ // resource HTTP client CLI support package // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client @@ -12,7 +12,7 @@ import ( "fmt" "strconv" - resource "github.com/tektoncd/hub/api/v1/gen/resource" + resource "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" goa "goa.design/goa/v3/pkg" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/client.go b/pkg/cmd/hub/gen/http/resource/client/client.go similarity index 98% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/client.go rename to pkg/cmd/hub/gen/http/resource/client/client.go index 4a4c3b2432..d7f9d74798 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/client.go +++ b/pkg/cmd/hub/gen/http/resource/client/client.go @@ -3,7 +3,7 @@ // resource client HTTP transport // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client @@ -11,7 +11,7 @@ import ( "context" "net/http" - resource "github.com/tektoncd/hub/api/v1/gen/resource" + resource "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" goahttp "goa.design/goa/v3/http" goa "goa.design/goa/v3/pkg" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/encode_decode.go b/pkg/cmd/hub/gen/http/resource/client/encode_decode.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/encode_decode.go rename to pkg/cmd/hub/gen/http/resource/client/encode_decode.go index 8051815027..b1193207b1 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/encode_decode.go +++ b/pkg/cmd/hub/gen/http/resource/client/encode_decode.go @@ -3,7 +3,7 @@ // resource HTTP client encoders and decoders // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client @@ -15,8 +15,8 @@ import ( "net/http" "net/url" - resource "github.com/tektoncd/hub/api/v1/gen/resource" - resourceviews "github.com/tektoncd/hub/api/v1/gen/resource/views" + resource "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource" + resourceviews "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource/views" goahttp "goa.design/goa/v3/http" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/paths.go b/pkg/cmd/hub/gen/http/resource/client/paths.go similarity index 98% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/paths.go rename to pkg/cmd/hub/gen/http/resource/client/paths.go index 00062def33..1832556cc0 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/paths.go +++ b/pkg/cmd/hub/gen/http/resource/client/paths.go @@ -3,7 +3,7 @@ // HTTP request path constructors for the resource service. // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/types.go b/pkg/cmd/hub/gen/http/resource/client/types.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/types.go rename to pkg/cmd/hub/gen/http/resource/client/types.go index f76d3de821..82b2591807 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/http/resource/client/types.go +++ b/pkg/cmd/hub/gen/http/resource/client/types.go @@ -3,12 +3,12 @@ // resource HTTP client types // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package client import ( - resourceviews "github.com/tektoncd/hub/api/v1/gen/resource/views" + resourceviews "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource/views" goa "goa.design/goa/v3/pkg" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/client.go b/pkg/cmd/hub/gen/resource/client.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/v1/gen/resource/client.go rename to pkg/cmd/hub/gen/resource/client.go index e3e9b3f9e0..9cf07f9c53 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/client.go +++ b/pkg/cmd/hub/gen/resource/client.go @@ -3,7 +3,7 @@ // resource client // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package resource diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/endpoints.go b/pkg/cmd/hub/gen/resource/endpoints.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/v1/gen/resource/endpoints.go rename to pkg/cmd/hub/gen/resource/endpoints.go index 8ae1e4ffa7..7c9e175934 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/endpoints.go +++ b/pkg/cmd/hub/gen/resource/endpoints.go @@ -3,7 +3,7 @@ // resource endpoints // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package resource diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/service.go b/pkg/cmd/hub/gen/resource/service.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/v1/gen/resource/service.go rename to pkg/cmd/hub/gen/resource/service.go index 36816cf464..85881f8bc9 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/service.go +++ b/pkg/cmd/hub/gen/resource/service.go @@ -3,7 +3,7 @@ // resource service // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package resource @@ -11,7 +11,7 @@ import ( "context" "io" - resourceviews "github.com/tektoncd/hub/api/v1/gen/resource/views" + resourceviews "github.com/tektoncd/cli/pkg/cmd/hub/gen/resource/views" goa "goa.design/goa/v3/pkg" ) diff --git a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/views/view.go b/pkg/cmd/hub/gen/resource/views/view.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/v1/gen/resource/views/view.go rename to pkg/cmd/hub/gen/resource/views/view.go index ad15438846..432461e94b 100644 --- a/vendor/github.com/tektoncd/hub/api/v1/gen/resource/views/view.go +++ b/pkg/cmd/hub/gen/resource/views/view.go @@ -3,7 +3,7 @@ // resource views // // Command: -// $ goa gen github.com/tektoncd/hub/api/v1/design +// $ goa gen github.com/tektoncd/cli/pkg/cmd/hub/gen/design package views diff --git a/vendor/github.com/tektoncd/hub/api/pkg/git/fetch_spec.go b/pkg/cmd/hub/git/fetch_spec.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/git/fetch_spec.go rename to pkg/cmd/hub/git/fetch_spec.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/git/git.go b/pkg/cmd/hub/git/git.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/git/git.go rename to pkg/cmd/hub/git/git.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/git/repo.go b/pkg/cmd/hub/git/repo.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/git/repo.go rename to pkg/cmd/hub/git/repo.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/gvr/gvr.go b/pkg/cmd/hub/gvr/gvr.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/gvr/gvr.go rename to pkg/cmd/hub/gvr/gvr.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_catalog.go b/pkg/cmd/hub/hub/get_catalog.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_catalog.go rename to pkg/cmd/hub/hub/get_catalog.go index 1b6f000e51..1f3f1c9e91 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_catalog.go +++ b/pkg/cmd/hub/hub/get_catalog.go @@ -19,7 +19,7 @@ import ( "fmt" "net/http" - cclient "github.com/tektoncd/hub/api/v1/gen/http/catalog/client" + cclient "github.com/tektoncd/cli/pkg/cmd/hub/gen/http/catalog/client" ) type CatalogResult struct { diff --git a/pkg/cmd/hub/hub/get_catalog_test.go b/pkg/cmd/hub/hub/get_catalog_test.go new file mode 100644 index 0000000000..af22e7ba93 --- /dev/null +++ b/pkg/cmd/hub/hub/get_catalog_test.go @@ -0,0 +1,25 @@ +// Copyright © 2021 The Tekton 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 hub + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetCatalogEndpoint(t *testing.T) { + assert.Equal(t, "/v1/catalogs", tektonHubCatEndpoint) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_resource.go b/pkg/cmd/hub/hub/get_resource.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_resource.go rename to pkg/cmd/hub/hub/get_resource.go index d6dca50554..bc7e747fd4 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_resource.go +++ b/pkg/cmd/hub/hub/get_resource.go @@ -22,7 +22,7 @@ import ( "strings" "github.com/Masterminds/semver/v3" - rclient "github.com/tektoncd/hub/api/v1/gen/http/resource/client" + rclient "github.com/tektoncd/cli/pkg/cmd/hub/gen/http/resource/client" ) // ResourceOption defines option associated with API to fetch a diff --git a/pkg/cmd/hub/hub/get_resource_test.go b/pkg/cmd/hub/hub/get_resource_test.go new file mode 100644 index 0000000000..50b2a59586 --- /dev/null +++ b/pkg/cmd/hub/hub/get_resource_test.go @@ -0,0 +1,50 @@ +// Copyright © 2020 The Tekton 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 hub + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetResourceEndpoint(t *testing.T) { + + opt := ResourceOption{ + Version: "", + Catalog: "tekton", + Name: "abc", + Kind: "task", + } + url := opt.Endpoint() + assert.Equal(t, "/v1/resource/tekton/task/abc", url) + + opt.PipelineVersion = "0.17" + url = opt.Endpoint() + assert.Equal(t, "/v1/resource/tekton/task/abc?pipelinesversion=0.17", url) + + opt.Version = "0.1.1" + url = opt.Endpoint() + assert.Equal(t, "/v1/resource/tekton/task/abc/0.1.1", url) +} + +func TestSortVersionsSemanticaly(t *testing.T) { + // includes valid semver and some invalid strings (alpha/beta) + in := []string{"0.9.0", "1.0.0", "v0.10.0", "alpha", "2.0.0", "1.2.3", "beta"} + out := sortVersionsSemanticaly(in) + + // valid semvers should be in descending order first + assert.Equal(t, []string{"2.0.0", "1.2.3", "1.0.0", "v0.10.0", "0.9.0", "beta", "alpha"}, out) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_resource_version.go b/pkg/cmd/hub/hub/get_resource_version.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_resource_version.go rename to pkg/cmd/hub/hub/get_resource_version.go index 720ea3b85c..929f5ac812 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/get_resource_version.go +++ b/pkg/cmd/hub/hub/get_resource_version.go @@ -19,7 +19,7 @@ import ( "fmt" "strconv" - rclient "github.com/tektoncd/hub/api/v1/gen/http/resource/client" + rclient "github.com/tektoncd/cli/pkg/cmd/hub/gen/http/resource/client" ) // resVersionsResponse is the response of API when finding resource versions diff --git a/pkg/cmd/hub/hub/get_resource_version_test.go b/pkg/cmd/hub/hub/get_resource_version_test.go new file mode 100644 index 0000000000..77610cbe92 --- /dev/null +++ b/pkg/cmd/hub/hub/get_resource_version_test.go @@ -0,0 +1,26 @@ +// Copyright © 2020 The Tekton 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 hub + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetResourceVersionEndpoint(t *testing.T) { + url := resVersionsEndpoint(43) + assert.Equal(t, "/v1/resource/43/versions", url) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/hub.go b/pkg/cmd/hub/hub/hub.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/hub/hub.go rename to pkg/cmd/hub/hub/hub.go diff --git a/pkg/cmd/hub/hub/hub_test.go b/pkg/cmd/hub/hub/hub_test.go new file mode 100644 index 0000000000..22137a5f0e --- /dev/null +++ b/pkg/cmd/hub/hub/hub_test.go @@ -0,0 +1,49 @@ +package hub + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSetURL_TektonHub(t *testing.T) { + tHub := &tektonHubClient{} + err := tHub.SetURL("http://localhost:80000") + assert.NoError(t, err) + + err = tHub.SetURL("localhost:8000") + assert.NoError(t, err) + + err = tHub.SetURL("http://80.80.79.9:80") + assert.NoError(t, err) + + // default url + err = tHub.SetURL("") + assert.NoError(t, err) + assert.Equal(t, tHub.apiURL, tektonHubURL) +} + +func TestSetURL_ArtifactHub(t *testing.T) { + aHub := &artifactHubClient{} + err := aHub.SetURL("http://localhost:80000") + assert.NoError(t, err) + + err = aHub.SetURL("localhost:8000") + assert.NoError(t, err) + + err = aHub.SetURL("http://80.80.79.9:80") + assert.NoError(t, err) + + // default url + err = aHub.SetURL("") + assert.NoError(t, err) + assert.Equal(t, aHub.apiURL, artifactHubURL) +} + +func TestSetURL_InvalidCase(t *testing.T) { + + hub := &tektonHubClient{} + err := hub.SetURL("abc") + assert.Error(t, err) + assert.EqualError(t, err, "parse \"abc\": invalid URI for request") +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/search.go b/pkg/cmd/hub/hub/search.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/hub/search.go rename to pkg/cmd/hub/hub/search.go index 0824e6a154..97518c09cf 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/hub/search.go +++ b/pkg/cmd/hub/hub/search.go @@ -20,7 +20,7 @@ import ( "net/url" "strconv" - rclient "github.com/tektoncd/hub/api/v1/gen/http/resource/client" + rclient "github.com/tektoncd/cli/pkg/cmd/hub/gen/http/resource/client" ) // SearchOption defines option associated with query API diff --git a/pkg/cmd/hub/hub/search_test.go b/pkg/cmd/hub/hub/search_test.go new file mode 100644 index 0000000000..d43be6e188 --- /dev/null +++ b/pkg/cmd/hub/hub/search_test.go @@ -0,0 +1,41 @@ +// Copyright © 2020 The Tekton 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 hub + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEndpoint(t *testing.T) { + + opt := SearchOption{ + Match: "contains", + Limit: 100, + } + + url := opt.Endpoint() + assert.Equal(t, "/v1/query?limit=100&match=contains", url) + + opt = SearchOption{ + Name: "res", + Kinds: []string{"k1", "k2", "k3"}, + Tags: []string{"t1", "t2"}, + Match: "contains", + } + url = opt.Endpoint() + assert.Equal(t, "/v1/query?kinds=k1&kinds=k2&kinds=k3&match=contains&name=res&tags=t1&tags=t2", url) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/installer/action.go b/pkg/cmd/hub/installer/action.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/installer/action.go rename to pkg/cmd/hub/installer/action.go index 5829cc1d60..f780d9ce66 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/installer/action.go +++ b/pkg/cmd/hub/installer/action.go @@ -19,8 +19,8 @@ import ( "errors" "fmt" - "github.com/tektoncd/hub/api/pkg/cli/hub" - tknVer "github.com/tektoncd/hub/api/pkg/cli/version" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + tknVer "github.com/tektoncd/cli/pkg/cmd/hub/version" kErr "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/pkg/cmd/hub/installer/action_test.go b/pkg/cmd/hub/installer/action_test.go new file mode 100644 index 0000000000..f9f74b3b87 --- /dev/null +++ b/pkg/cmd/hub/installer/action_test.go @@ -0,0 +1,129 @@ +// Copyright © 2020 The Tekton 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 installer + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + cb "github.com/tektoncd/cli/pkg/cmd/hub/test/builder" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const res = `--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: gvr + labels: + app.kubernetes.io/version: '0.3' + annotations: + tekton.dev/pipelines.minVersion: '0.13.1' + tekton.dev/tags: cli + tekton.dev/displayName: 'gvr-bar' +spec: + description: >- + v0.3 Task to run gvr + +` + +func TestToUnstructuredAndAddLabel(t *testing.T) { + testCases := []struct { + name string + data string + hubType string + org string + catalog string + wantSupportTier string + wantCatalog string + wantOrg string + }{ + { + name: "Install From Tekton Hub", + data: res, + hubType: hub.TektonHubType, + org: "", + catalog: "tekton", + wantCatalog: "tekton", + }, + { + name: "Install Verified Catalog From Artifact Hub", + data: res, + hubType: hub.ArtifactHubType, + org: verifiedCatOrg, + catalog: "golang-build", + wantSupportTier: verifiedSupportTier, + wantCatalog: "golang-build", + wantOrg: "tektoncd", + }, + { + name: "Install Community Catalog From Artifact Hub", + data: res, + hubType: hub.ArtifactHubType, + org: "tekton-legacy", + catalog: "tekton-catalog-tasks", + wantSupportTier: communitySupportTier, + wantCatalog: "tekton-catalog-tasks", + wantOrg: "tekton-legacy", + }, + } + + for _, tc := range testCases { + obj, err := toUnstructured([]byte(res)) + assert.NoError(t, err) + assert.Equal(t, "gvr", obj.GetName()) + + if err := addCatalogLabel(obj, tc.hubType, tc.org, tc.catalog); err != nil { + t.Errorf("%s", err.Error()) + } + + if tc.hubType == hub.ArtifactHubType { + assert.Equal(t, tc.wantSupportTier, obj.GetLabels()[artifactHubSupportTierLabel]) + assert.Equal(t, tc.wantCatalog, obj.GetLabels()[artifactHubCatalogLabel]) + assert.Equal(t, tc.wantOrg, obj.GetLabels()[artifactHubOrgLabel]) + assert.Equal(t, "", obj.GetLabels()[tektonHubCatalogLabel]) + } else { + assert.Equal(t, "tekton", obj.GetLabels()[tektonHubCatalogLabel]) + } + } +} + +func TestListInstalled(t *testing.T) { + existingTask := &v1beta1.Task{ + ObjectMeta: metav1.ObjectMeta{ + Name: "gvr", + Namespace: "hub", + Labels: map[string]string{ + "hub.tekton.dev/catalog": "tekton", + "app.kubernetes.io/version": "0.1", + }}, + } + + version := "v1beta1" + dynamic := test.DynamicClient(cb.UnstructuredV1beta1T(existingTask, version)) + + cs, _ := test.SeedV1beta1TestData(t, test.Data{Tasks: []*v1beta1.Task{existingTask}}) + cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) + + clientSet := test.FakeClientSet(cs.Pipeline, dynamic, "hub") + + installer := New(clientSet) + list, _ := installer.ListInstalled("task", "hub") + + assert.Equal(t, len(list), 1) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/installer/config.go b/pkg/cmd/hub/installer/config.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/installer/config.go rename to pkg/cmd/hub/installer/config.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/installer/kube_action.go b/pkg/cmd/hub/installer/kube_action.go similarity index 98% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/installer/kube_action.go rename to pkg/cmd/hub/installer/kube_action.go index 72f786a2b6..2d2ff48942 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/installer/kube_action.go +++ b/pkg/cmd/hub/installer/kube_action.go @@ -19,7 +19,7 @@ import ( "fmt" "strings" - gvrRes "github.com/tektoncd/hub/api/pkg/cli/gvr" + gvrRes "github.com/tektoncd/cli/pkg/cmd/hub/gvr" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/kube/client.go b/pkg/cmd/hub/kube/client.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/kube/client.go rename to pkg/cmd/hub/kube/client.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/options/select_options.go b/pkg/cmd/hub/options/select_options.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/options/select_options.go rename to pkg/cmd/hub/options/select_options.go index 0bc3ce5817..587f26ae45 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/options/select_options.go +++ b/pkg/cmd/hub/options/select_options.go @@ -19,8 +19,8 @@ import ( "strings" "github.com/AlecAivazis/survey/v2" - "github.com/tektoncd/hub/api/pkg/cli/app" - "github.com/tektoncd/hub/api/pkg/cli/hub" + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" ) type Options struct { diff --git a/pkg/cmd/hub/options/select_options_test.go b/pkg/cmd/hub/options/select_options_test.go new file mode 100644 index 0000000000..6c449b8577 --- /dev/null +++ b/pkg/cmd/hub/options/select_options_test.go @@ -0,0 +1,135 @@ +// Copyright © 2021 The Tekton 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 options + +import ( + "testing" + + "github.com/AlecAivazis/survey/v2/terminal" + goexpect "github.com/Netflix/go-expect" + "github.com/tektoncd/cli/pkg/cmd/hub/test/prompt" +) + +func TestOptions_Ask(t *testing.T) { + options := []string{ + "buildah", + "git-clone", + "tkn", + } + + options1 := []string{ + "foo", + "baar", + } + + options2 := []string{ + "0.1", + "0.2", + } + + testParams := []struct { + name string + resource string + prompt prompt.Prompt + options []string + want Options + }{ + { + name: "select task name", + resource: "task", + prompt: prompt.Prompt{ + CmdArgs: []string{}, + Procedure: func(c *goexpect.Console) error { + if _, err := c.ExpectString("Select task:"); err != nil { + return err + } + if _, err := c.SendLine(options[0]); err != nil { + return err + } + return nil + }, + }, + options: options, + want: Options{ + Name: "buildah", + From: "", + Version: "", + }, + }, + { + name: "select catalog", + resource: "catalog", + prompt: prompt.Prompt{ + CmdArgs: []string{}, + Procedure: func(c *goexpect.Console) error { + if _, err := c.ExpectString("Select catalog:"); err != nil { + return err + } + if _, err := c.SendLine(options1[0]); err != nil { + return err + } + return nil + }, + }, + options: options1, + want: Options{ + Name: "", + From: "foo", + Version: "", + }, + }, + { + name: "select version", + resource: "version", + prompt: prompt.Prompt{ + CmdArgs: []string{}, + Procedure: func(c *goexpect.Console) error { + if _, err := c.ExpectString("Select version:"); err != nil { + return err + } + if _, err := c.SendLine(options2[0]); err != nil { + return err + } + return nil + }, + }, + options: options2, + want: Options{ + Name: "", + From: "", + Version: "0.1", + }, + }, + } + + for _, tp := range testParams { + t.Run(tp.name, func(t *testing.T) { + opts := &Options{} + tp.prompt.RunTest(t, tp.prompt.Procedure, func(stdio terminal.Stdio) error { + opts.AskOpts = prompt.WithStdio(stdio) + return opts.Ask(tp.resource, tp.options) + }) + if opts.Name != tp.want.Name { + t.Errorf("Unexpected Task Name") + } + if opts.From != tp.want.From { + t.Errorf("Unexpected Catalog Name") + } + if opts.Version != tp.want.Version { + t.Errorf("Unexpected Version") + } + }) + } +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/parser/catalog.go b/pkg/cmd/hub/parser/catalog.go similarity index 99% rename from vendor/github.com/tektoncd/hub/api/pkg/parser/catalog.go rename to pkg/cmd/hub/parser/catalog.go index efee177cc8..b32219eadf 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/parser/catalog.go +++ b/pkg/cmd/hub/parser/catalog.go @@ -24,7 +24,7 @@ import ( "strings" "time" - "github.com/tektoncd/hub/api/pkg/git" + "github.com/tektoncd/cli/pkg/cmd/hub/git" v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "go.uber.org/zap" diff --git a/pkg/cmd/hub/parser/catalog_test.go b/pkg/cmd/hub/parser/catalog_test.go new file mode 100644 index 0000000000..b86815a1e2 --- /dev/null +++ b/pkg/cmd/hub/parser/catalog_test.go @@ -0,0 +1,137 @@ +// Copyright © 2020 The Tekton 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 parser + +import ( + "path/filepath" + "testing" + "time" + + "github.com/tektoncd/cli/pkg/cmd/hub/git" + "go.uber.org/zap" + "gotest.tools/v3/assert" + "gotest.tools/v3/assert/cmp" +) + +type fakeRepo struct { + path string + head string + modifiedTime map[string]time.Time +} + +var _ git.Repo = (*fakeRepo)(nil) + +func (r fakeRepo) Path() string { + return r.path +} + +func (r fakeRepo) Head() string { + return r.head +} + +func (r fakeRepo) ModifiedTime(path string) (time.Time, error) { + + rp, _ := r.RelPath(path) + return r.modifiedTime[rp], nil +} + +func (r fakeRepo) RelPath(f string) (string, error) { + return filepath.Rel(r.path, f) +} + +func TestParse_NonExistentRepo(t *testing.T) { + repo := fakeRepo{ + path: "./testdata/catalogs/non-existent", + } + + p := ForCatalog(zap.NewNop().Sugar(), repo, "") + _, result := p.Parse() + assert.Equal(t, 1, len(result.Errors)) + assert.Equal(t, "no resources found in repo", result.Error()) + +} + +func TestParse_ValidRepo(t *testing.T) { + now := time.Now() + repo := fakeRepo{ + path: "./testdata/catalogs/valid", + modifiedTime: map[string]time.Time{ + "task/maven/0.1/maven.yaml": now, + }, + } + + p := ForCatalog(zap.NewNop().Sugar(), repo, "") + res, result := p.Parse() + + assert.Equal(t, 0, len(result.Errors)) + assert.Equal(t, "", result.Error()) + + assert.Equal(t, 2, len(res)) + gitCLI := res[0] + assert.Equal(t, "git-cli", gitCLI.Name) + assert.Equal(t, 1, len(gitCLI.Versions)) + assert.Equal(t, "linux/s390x", gitCLI.Versions[0].Platforms[0]) + + maven := res[1] + assert.Equal(t, "maven", maven.Name) + assert.Equal(t, 2, len(maven.Versions)) + assert.Equal(t, now, maven.Versions[0].ModifiedAt) + assert.Equal(t, "linux/amd64", maven.Versions[0].Platforms[0]) + assert.Equal(t, "linux/ppc64le", maven.Versions[1].Platforms[0]) + assert.Equal(t, "linux/s390x", maven.Versions[1].Platforms[1]) +} + +func TestParse_InvalidTask(t *testing.T) { + // invalid task is ignored but result must have the issue it found + repo := fakeRepo{ + path: "./testdata/catalogs/invalid-task", + } + + p := ForCatalog(zap.NewNop().Sugar(), repo, "") + res, result := p.Parse() + + assert.Equal(t, 0, len(res)) + + assert.Equal(t, 1, len(result.Errors)) + assert.Equal(t, "no resources found in repo", result.Error()) + + assert.Equal(t, 1, len(result.Issues)) + issue := result.Issues[0] + assert.Assert(t, cmp.Contains(issue.Message, "git-cli is missing mandatory version label")) + assert.Equal(t, Critical, issue.Type) +} + +func TestParse_InvalidFilename(t *testing.T) { + // invalid task is ignored but result must have the issue it found + repo := fakeRepo{ + path: "./testdata/catalogs/invalid-taskname", + } + + p := ForCatalog(zap.NewNop().Sugar(), repo, "") + res, result := p.Parse() + + assert.Equal(t, 2, len(res)) // one maven should be found and a git-clone + + assert.Equal(t, 0, len(result.Errors)) + + assert.Equal(t, 2, len(result.Issues)) + gitCLI := result.Issues[0] + assert.Assert(t, cmp.Contains(gitCLI.Message, "failed to find any resource matching")) + assert.Equal(t, Critical, gitCLI.Type) + + gitClone := result.Issues[1] + assert.Assert(t, cmp.Contains(gitClone.Message, "expected to find 2 versions but found only 1")) + assert.Equal(t, Critical, gitClone.Type) +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/parser/kind.go b/pkg/cmd/hub/parser/kind.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/parser/kind.go rename to pkg/cmd/hub/parser/kind.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/parser/parser.go b/pkg/cmd/hub/parser/parser.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/parser/parser.go rename to pkg/cmd/hub/parser/parser.go index 3982b28a3a..803eb4670a 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/parser/parser.go +++ b/pkg/cmd/hub/parser/parser.go @@ -18,7 +18,7 @@ import ( "fmt" "sync" - "github.com/tektoncd/hub/api/pkg/git" + "github.com/tektoncd/cli/pkg/cmd/hub/git" v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "go.uber.org/zap" diff --git a/vendor/github.com/tektoncd/hub/api/pkg/parser/resource.go b/pkg/cmd/hub/parser/resource.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/parser/resource.go rename to pkg/cmd/hub/parser/resource.go diff --git a/vendor/github.com/tektoncd/hub/api/pkg/parser/result.go b/pkg/cmd/hub/parser/result.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/parser/result.go rename to pkg/cmd/hub/parser/result.go diff --git a/pkg/cmd/hub/parser/testdata/catalogs/invalid-task/task/git-cli/0.1/git-cli.yaml b/pkg/cmd/hub/parser/testdata/catalogs/invalid-task/task/git-cli/0.1/git-cli.yaml new file mode 100644 index 0000000000..30f5b3c64f --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/invalid-task/task/git-cli/0.1/git-cli.yaml @@ -0,0 +1,73 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: git-cli +# labels: +# app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: git + tekton.dev/displayName: "git cli" +spec: + description: >- + This task can be used to perform git operations. + + Git command that needs to be run can be passed as a script to + the task.This task needs authentication to git in order to push + after the git operation. + + workspaces: + - name: source + description: A workspace that contains the fetched git repository. + - name: input + description: A workspace that contains file that needs to be added to git. + params: + - name: BASE_IMAGE + description: | + The base image for the task. + type: string + default: alpine/git:latest + + - name: GIT_USER_NAME + type: string + description: | + Git user name for performing git operation. + default: "" + + - name: GIT_USER_EMAIL + type: string + description: | + Git user email for performing git operation. + default: "" + + - name: GIT_SCRIPT + description: The git script to run. + type: string + default: | + git help + + results: + - name: commit + description: The precise commit SHA after the git operation. + + steps: + - name: git + image: $(params.BASE_IMAGE) + workingDir: $(workspaces.source.path) + script: | + + # Setting up the config for the git. + git config --global user.email "$(params.GIT_USER_EMAIL)" + git config --global user.name "$(params.GIT_USER_NAME)" + + $(params.GIT_SCRIPT) + + RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" + EXIT_CODE="$?" + if [ "$EXIT_CODE" != 0 ] + then + exit $EXIT_CODE + fi + # Make sure we don't add a trailing newline to the result! + echo -n "$RESULT_SHA" > $(results.commit.path) diff --git a/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-cli/0.1/git-invalid-name.yaml b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-cli/0.1/git-invalid-name.yaml new file mode 100644 index 0000000000..30f5b3c64f --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-cli/0.1/git-invalid-name.yaml @@ -0,0 +1,73 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: git-cli +# labels: +# app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: git + tekton.dev/displayName: "git cli" +spec: + description: >- + This task can be used to perform git operations. + + Git command that needs to be run can be passed as a script to + the task.This task needs authentication to git in order to push + after the git operation. + + workspaces: + - name: source + description: A workspace that contains the fetched git repository. + - name: input + description: A workspace that contains file that needs to be added to git. + params: + - name: BASE_IMAGE + description: | + The base image for the task. + type: string + default: alpine/git:latest + + - name: GIT_USER_NAME + type: string + description: | + Git user name for performing git operation. + default: "" + + - name: GIT_USER_EMAIL + type: string + description: | + Git user email for performing git operation. + default: "" + + - name: GIT_SCRIPT + description: The git script to run. + type: string + default: | + git help + + results: + - name: commit + description: The precise commit SHA after the git operation. + + steps: + - name: git + image: $(params.BASE_IMAGE) + workingDir: $(workspaces.source.path) + script: | + + # Setting up the config for the git. + git config --global user.email "$(params.GIT_USER_EMAIL)" + git config --global user.name "$(params.GIT_USER_NAME)" + + $(params.GIT_SCRIPT) + + RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" + EXIT_CODE="$?" + if [ "$EXIT_CODE" != 0 ] + then + exit $EXIT_CODE + fi + # Make sure we don't add a trailing newline to the result! + echo -n "$RESULT_SHA" > $(results.commit.path) diff --git a/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-clone/0.1/got-cloned.yaml b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-clone/0.1/got-cloned.yaml new file mode 100644 index 0000000000..1a44270be6 --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-clone/0.1/got-cloned.yaml @@ -0,0 +1,119 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: git-clone + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: git + tekton.dev/displayName: "git clone" +spec: + description: >- + These Tasks are Git tasks to work with repositories used by other tasks + in your Pipeline. + + The git-clone Task will clone a repo from the provided url into the + output Workspace. By default the repo will be cloned into the root of + your Workspace. You can clone into a subdirectory by setting this Task's + subdirectory param. + + workspaces: + - name: output + description: The git repo will be cloned onto the volume backing this workspace + params: + - name: url + description: git url to clone + type: string + - name: revision + description: git revision to checkout (branch, tag, sha, ref…) + type: string + default: master + - name: refspec + description: (optional) git refspec to fetch before checking out revision + default: "" + - name: submodules + description: defines if the resource should initialize and fetch the submodules + type: string + default: "true" + - name: depth + description: performs a shallow clone where only the most recent commit(s) will be fetched + type: string + default: "1" + - name: sslVerify + description: defines if http.sslVerify should be set to true or false in the global git config + type: string + default: "true" + - name: subdirectory + description: subdirectory inside the "output" workspace to clone the git repo into + type: string + default: "" + - name: deleteExisting + description: clean out the contents of the repo's destination directory (if it already exists) before trying to clone the repo there + type: string + default: "false" + - name: httpProxy + description: git HTTP proxy server for non-SSL requests + type: string + default: "" + - name: httpsProxy + description: git HTTPS proxy server for SSL requests + type: string + default: "" + - name: noProxy + description: git no proxy - opt out of proxying HTTP/HTTPS requests + type: string + default: "" + - name: gitInitImage + description: The image used where the git-init binary is. + default: "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.15.2" + type: string + results: + - name: commit + description: The precise commit SHA that was fetched by this Task + steps: + - name: clone + image: $(params.gitInitImage) + script: | + CHECKOUT_DIR="$(workspaces.output.path)/$(params.subdirectory)" + + cleandir() { + # Delete any existing contents of the repo directory if it exists. + # + # We don't just "rm -rf $CHECKOUT_DIR" because $CHECKOUT_DIR might be "/" + # or the root of a mounted volume. + if [[ -d "$CHECKOUT_DIR" ]] ; then + # Delete non-hidden files and directories + rm -rf "$CHECKOUT_DIR"/* + # Delete files and directories starting with . but excluding .. + rm -rf "$CHECKOUT_DIR"/.[!.]* + # Delete files and directories starting with .. plus any other character + rm -rf "$CHECKOUT_DIR"/..?* + fi + } + + if [[ "$(params.deleteExisting)" == "true" ]] ; then + cleandir + fi + + test -z "$(params.httpProxy)" || export HTTP_PROXY=$(params.httpProxy) + test -z "$(params.httpsProxy)" || export HTTPS_PROXY=$(params.httpsProxy) + test -z "$(params.noProxy)" || export NO_PROXY=$(params.noProxy) + + /ko-app/git-init \ + -url "$(params.url)" \ + -revision "$(params.revision)" \ + -refspec "$(params.refspec)" \ + -path "$CHECKOUT_DIR" \ + -sslVerify="$(params.sslVerify)" \ + -submodules="$(params.submodules)" \ + -depth "$(params.depth)" + cd "$CHECKOUT_DIR" + RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" + EXIT_CODE="$?" + if [ "$EXIT_CODE" != 0 ] + then + exit $EXIT_CODE + fi + # Make sure we don't add a trailing newline to the result! + echo -n "$RESULT_SHA" > $(results.commit.path) \ No newline at end of file diff --git a/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-clone/0.2/git-clone.yaml b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-clone/0.2/git-clone.yaml new file mode 100644 index 0000000000..ec1ed25897 --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/git-clone/0.2/git-clone.yaml @@ -0,0 +1,128 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: git-clone + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: git + tekton.dev/displayName: "git clone" +spec: + description: >- + These Tasks are Git tasks to work with repositories used by other tasks + in your Pipeline. + + The git-clone Task will clone a repo from the provided url into the + output Workspace. By default the repo will be cloned into the root of + your Workspace. You can clone into a subdirectory by setting this Task's + subdirectory param. + + workspaces: + - name: output + description: The git repo will be cloned onto the volume backing this workspace + params: + - name: url + description: git url to clone + type: string + - name: revision + description: git revision to checkout (branch, tag, sha, ref…) + type: string + default: "" + - name: refspec + description: (optional) git refspec to fetch before checking out revision + default: "" + - name: submodules + description: defines if the resource should initialize and fetch the submodules + type: string + default: "true" + - name: depth + description: performs a shallow clone where only the most recent commit(s) will be fetched + type: string + default: "1" + - name: sslVerify + description: defines if http.sslVerify should be set to true or false in the global git config + type: string + default: "true" + - name: subdirectory + description: subdirectory inside the "output" workspace to clone the git repo into + type: string + default: "" + - name: deleteExisting + description: clean out the contents of the repo's destination directory (if it already exists) before trying to clone the repo there + type: string + default: "true" + - name: httpProxy + description: git HTTP proxy server for non-SSL requests + type: string + default: "" + - name: httpsProxy + description: git HTTPS proxy server for SSL requests + type: string + default: "" + - name: noProxy + description: git no proxy - opt out of proxying HTTP/HTTPS requests + type: string + default: "" + - name: verbose + description: log the commands used during execution + type: string + default: "true" + results: + - name: commit + description: The precise commit SHA that was fetched by this Task + - name: url + description: The precise URL that was fetched by this Task + steps: + - name: clone + image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.16.2 + script: | + #!/bin/sh + set -eu -o pipefail + + if [[ "$(params.verbose)" == "true" ]] ; then + set -x + fi + + CHECKOUT_DIR="$(workspaces.output.path)/$(params.subdirectory)" + + cleandir() { + # Delete any existing contents of the repo directory if it exists. + # + # We don't just "rm -rf $CHECKOUT_DIR" because $CHECKOUT_DIR might be "/" + # or the root of a mounted volume. + if [[ -d "$CHECKOUT_DIR" ]] ; then + # Delete non-hidden files and directories + rm -rf "$CHECKOUT_DIR"/* + # Delete files and directories starting with . but excluding .. + rm -rf "$CHECKOUT_DIR"/.[!.]* + # Delete files and directories starting with .. plus any other character + rm -rf "$CHECKOUT_DIR"/..?* + fi + } + + if [[ "$(params.deleteExisting)" == "true" ]] ; then + cleandir + fi + + test -z "$(params.httpProxy)" || export HTTP_PROXY=$(params.httpProxy) + test -z "$(params.httpsProxy)" || export HTTPS_PROXY=$(params.httpsProxy) + test -z "$(params.noProxy)" || export NO_PROXY=$(params.noProxy) + + /ko-app/git-init \ + -url "$(params.url)" \ + -revision "$(params.revision)" \ + -refspec "$(params.refspec)" \ + -path "$CHECKOUT_DIR" \ + -sslVerify="$(params.sslVerify)" \ + -submodules="$(params.submodules)" \ + -depth "$(params.depth)" + cd "$CHECKOUT_DIR" + RESULT_SHA="$(git rev-parse HEAD)" + EXIT_CODE="$?" + if [ "$EXIT_CODE" != 0 ] ; then + exit $EXIT_CODE + fi + # ensure we don't add a trailing newline to the result + echo -n "$RESULT_SHA" > $(results.commit.path) + echo -n "$(params.url)" > $(results.url.path) diff --git a/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/maven/0.1/maven.yaml b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/maven/0.1/maven.yaml new file mode 100644 index 0000000000..a49a96b1ae --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/maven/0.1/maven.yaml @@ -0,0 +1,121 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: maven + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: build-tool + tekton.dev/displayName: "Maven Build Tool" + +spec: + description: >- + This Task can be used to run a Maven build. + + workspaces: + - name: source + description: The workspace consisting of maven project. + - name: maven-settings + description: >- + The workspace consisting of the custom maven settings + provided by the user. + params: + - name: MAVEN_IMAGE + type: string + description: Maven base image + default: gcr.io/cloud-builders/mvn + - name: GOALS + description: maven goals to run + type: array + default: + - "package" + - name: MAVEN_MIRROR_URL + description: The Maven repository mirror url + type: string + default: "" + - name: PROXY_USER + description: The username for the proxy server + type: string + default: "" + - name: PROXY_PASSWORD + description: The password for the proxy server + type: string + default: "" + - name: PROXY_PORT + description: Port number for the proxy server + type: string + default: "" + - name: PROXY_HOST + description: Proxy server Host + type: string + default: "" + - name: PROXY_NON_PROXY_HOSTS + description: Non proxy server host + type: string + default: "" + - name: PROXY_PROTOCOL + description: Protocol for the proxy ie http or https + type: string + default: "http" + steps: + - name: mvn-settings + image: registry.access.redhat.com/ubi8/ubi-minimal:latest + script: | + #!/usr/bin/env bash + + [[ -f $(workspaces.maven-settings.path)/settings.xml ]] && \ + echo 'using existing $(workspaces.maven-settings.path)/settings.xml' && exit 0 + + cat > $(workspaces.maven-settings.path)/settings.xml < + + + + + + + + + + EOF + + xml="" + if [ -n "$(params.PROXY_HOST)" -a -n "$(params.PROXY_PORT)" ]; then + xml="\ + genproxy\ + true\ + $(params.PROXY_PROTOCOL)\ + $(params.PROXY_HOST)\ + $(params.PROXY_PORT)" + if [ -n "$(params.PROXY_USER)" -a -n "$(params.PROXY_PASSWORD)" ]; then + xml="$xml\ + $(params.PROXY_USER)\ + $(params.PROXY_PASSWORD)" + fi + if [ -n "$(params.PROXY_NON_PROXY_HOSTS)" ]; then + xml="$xml\ + $(params.PROXY_NON_PROXY_HOSTS)" + fi + xml="$xml\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + if [ -n "$(params.MAVEN_MIRROR_URL)" ]; then + xml=" \ + mirror.default\ + $(params.MAVEN_MIRROR_URL)\ + central\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + - name: mvn-goals + image: $(params.MAVEN_IMAGE) + workingDir: $(workspaces.source.path) + command: ["/usr/bin/mvn"] + args: + - -s + - $(workspaces.maven-settings.path)/settings.xml + - "$(params.GOALS)" diff --git a/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/maven/0.2/maven.yaml b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/maven/0.2/maven.yaml new file mode 100644 index 0000000000..504aed7e57 --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/invalid-taskname/task/maven/0.2/maven.yaml @@ -0,0 +1,149 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: maven + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: build-tool + tekton.dev/displayName: "Maven Build Tool" +spec: + description: >- + This Task can be used to run a Maven build. + + workspaces: + - name: source + description: The workspace consisting of maven project. + - name: maven-settings + description: >- + The workspace consisting of the custom maven settings + provided by the user. + params: + - name: MAVEN_IMAGE + type: string + description: Maven base image + default: gcr.io/cloud-builders/mvn + - name: GOALS + description: maven goals to run + type: array + default: + - "package" + - name: MAVEN_MIRROR_URL + description: The Maven repository mirror url + type: string + default: "" + - name: SERVER_USER + description: The username for the server + type: string + default: "" + - name: SERVER_PASSWORD + description: The password for the server + type: string + default: "" + - name: PROXY_USER + description: The username for the proxy server + type: string + default: "" + - name: PROXY_PASSWORD + description: The password for the proxy server + type: string + default: "" + - name: PROXY_PORT + description: Port number for the proxy server + type: string + default: "" + - name: PROXY_HOST + description: Proxy server Host + type: string + default: "" + - name: PROXY_NON_PROXY_HOSTS + description: Non proxy server host + type: string + default: "" + - name: PROXY_PROTOCOL + description: Protocol for the proxy ie http or https + type: string + default: "http" + - name: CONTEXT_DIR + type: string + description: >- + The context directory within the repository for sources on + which we want to execute maven goals. + default: "." + steps: + - name: mvn-settings + image: registry.access.redhat.com/ubi8/ubi-minimal:latest + script: | + #!/usr/bin/env bash + + [[ -f $(workspaces.maven-settings.path)/settings.xml ]] && \ + echo 'using existing $(workspaces.maven-settings.path)/settings.xml' && exit 0 + + cat > $(workspaces.maven-settings.path)/settings.xml < + + + + + + + + + + + + + + EOF + + xml="" + if [ -n "$(params.PROXY_HOST)" -a -n "$(params.PROXY_PORT)" ]; then + xml="\ + genproxy\ + true\ + $(params.PROXY_PROTOCOL)\ + $(params.PROXY_HOST)\ + $(params.PROXY_PORT)" + if [ -n "$(params.PROXY_USER)" -a -n "$(params.PROXY_PASSWORD)" ]; then + xml="$xml\ + $(params.PROXY_USER)\ + $(params.PROXY_PASSWORD)" + fi + if [ -n "$(params.PROXY_NON_PROXY_HOSTS)" ]; then + xml="$xml\ + $(params.PROXY_NON_PROXY_HOSTS)" + fi + xml="$xml\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + if [ -n "$(params.SERVER_USER)" -a -n "$(params.SERVER_PASSWORD)" ]; then + xml="\ + serverid" + xml="$xml\ + $(params.SERVER_USER)\ + $(params.SERVER_PASSWORD)" + xml="$xml\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + if [ -n "$(params.MAVEN_MIRROR_URL)" ]; then + xml=" \ + mirror.default\ + $(params.MAVEN_MIRROR_URL)\ + central\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + - name: mvn-goals + image: $(params.MAVEN_IMAGE) + workingDir: $(workspaces.source.path)/$(params.CONTEXT_DIR) + command: ["/usr/bin/mvn"] + args: + - -s + - $(workspaces.maven-settings.path)/settings.xml + - "$(params.GOALS)" diff --git a/pkg/cmd/hub/parser/testdata/catalogs/valid/task/git-cli/0.1/git-cli.yaml b/pkg/cmd/hub/parser/testdata/catalogs/valid/task/git-cli/0.1/git-cli.yaml new file mode 100644 index 0000000000..cdc1c4056f --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/valid/task/git-cli/0.1/git-cli.yaml @@ -0,0 +1,74 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: git-cli + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: git + tekton.dev/displayName: "git cli" + tekton.dev/platforms: "linux/s390x" +spec: + description: >- + This task can be used to perform git operations. + + Git command that needs to be run can be passed as a script to + the task.This task needs authentication to git in order to push + after the git operation. + + workspaces: + - name: source + description: A workspace that contains the fetched git repository. + - name: input + description: A workspace that contains file that needs to be added to git. + params: + - name: BASE_IMAGE + description: | + The base image for the task. + type: string + default: alpine/git:latest + + - name: GIT_USER_NAME + type: string + description: | + Git user name for performing git operation. + default: "" + + - name: GIT_USER_EMAIL + type: string + description: | + Git user email for performing git operation. + default: "" + + - name: GIT_SCRIPT + description: The git script to run. + type: string + default: | + git help + + results: + - name: commit + description: The precise commit SHA after the git operation. + + steps: + - name: git + image: $(params.BASE_IMAGE) + workingDir: $(workspaces.source.path) + script: | + + # Setting up the config for the git. + git config --global user.email "$(params.GIT_USER_EMAIL)" + git config --global user.name "$(params.GIT_USER_NAME)" + + $(params.GIT_SCRIPT) + + RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" + EXIT_CODE="$?" + if [ "$EXIT_CODE" != 0 ] + then + exit $EXIT_CODE + fi + # Make sure we don't add a trailing newline to the result! + echo -n "$RESULT_SHA" > $(results.commit.path) diff --git a/pkg/cmd/hub/parser/testdata/catalogs/valid/task/maven/0.1/maven.yaml b/pkg/cmd/hub/parser/testdata/catalogs/valid/task/maven/0.1/maven.yaml new file mode 100644 index 0000000000..631ccbdc76 --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/valid/task/maven/0.1/maven.yaml @@ -0,0 +1,119 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: maven + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: build-tool +spec: + description: >- + This Task can be used to run a Maven build. + + workspaces: + - name: source + description: The workspace consisting of maven project. + - name: maven-settings + description: >- + The workspace consisting of the custom maven settings + provided by the user. + params: + - name: MAVEN_IMAGE + type: string + description: Maven base image + default: gcr.io/cloud-builders/mvn + - name: GOALS + description: maven goals to run + type: array + default: + - "package" + - name: MAVEN_MIRROR_URL + description: The Maven repository mirror url + type: string + default: "" + - name: PROXY_USER + description: The username for the proxy server + type: string + default: "" + - name: PROXY_PASSWORD + description: The password for the proxy server + type: string + default: "" + - name: PROXY_PORT + description: Port number for the proxy server + type: string + default: "" + - name: PROXY_HOST + description: Proxy server Host + type: string + default: "" + - name: PROXY_NON_PROXY_HOSTS + description: Non proxy server host + type: string + default: "" + - name: PROXY_PROTOCOL + description: Protocol for the proxy ie http or https + type: string + default: "http" + steps: + - name: mvn-settings + image: registry.access.redhat.com/ubi8/ubi-minimal:latest + script: | + #!/usr/bin/env bash + + [[ -f $(workspaces.maven-settings.path)/settings.xml ]] && \ + echo 'using existing $(workspaces.maven-settings.path)/settings.xml' && exit 0 + + cat > $(workspaces.maven-settings.path)/settings.xml < + + + + + + + + + + EOF + + xml="" + if [ -n "$(params.PROXY_HOST)" -a -n "$(params.PROXY_PORT)" ]; then + xml="\ + genproxy\ + true\ + $(params.PROXY_PROTOCOL)\ + $(params.PROXY_HOST)\ + $(params.PROXY_PORT)" + if [ -n "$(params.PROXY_USER)" -a -n "$(params.PROXY_PASSWORD)" ]; then + xml="$xml\ + $(params.PROXY_USER)\ + $(params.PROXY_PASSWORD)" + fi + if [ -n "$(params.PROXY_NON_PROXY_HOSTS)" ]; then + xml="$xml\ + $(params.PROXY_NON_PROXY_HOSTS)" + fi + xml="$xml\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + if [ -n "$(params.MAVEN_MIRROR_URL)" ]; then + xml=" \ + mirror.default\ + $(params.MAVEN_MIRROR_URL)\ + central\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + - name: mvn-goals + image: $(params.MAVEN_IMAGE) + workingDir: $(workspaces.source.path) + command: ["/usr/bin/mvn"] + args: + - -s + - $(workspaces.maven-settings.path)/settings.xml + - "$(params.GOALS)" diff --git a/pkg/cmd/hub/parser/testdata/catalogs/valid/task/maven/0.2/maven.yaml b/pkg/cmd/hub/parser/testdata/catalogs/valid/task/maven/0.2/maven.yaml new file mode 100644 index 0000000000..242efb4747 --- /dev/null +++ b/pkg/cmd/hub/parser/testdata/catalogs/valid/task/maven/0.2/maven.yaml @@ -0,0 +1,149 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: maven + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: build-tool + tekton.dev/platforms: "linux/ppc64le,linux/s390x" +spec: + description: >- + This Task can be used to run a Maven build. + + workspaces: + - name: source + description: The workspace consisting of maven project. + - name: maven-settings + description: >- + The workspace consisting of the custom maven settings + provided by the user. + params: + - name: MAVEN_IMAGE + type: string + description: Maven base image + default: gcr.io/cloud-builders/mvn + - name: GOALS + description: maven goals to run + type: array + default: + - "package" + - name: MAVEN_MIRROR_URL + description: The Maven repository mirror url + type: string + default: "" + - name: SERVER_USER + description: The username for the server + type: string + default: "" + - name: SERVER_PASSWORD + description: The password for the server + type: string + default: "" + - name: PROXY_USER + description: The username for the proxy server + type: string + default: "" + - name: PROXY_PASSWORD + description: The password for the proxy server + type: string + default: "" + - name: PROXY_PORT + description: Port number for the proxy server + type: string + default: "" + - name: PROXY_HOST + description: Proxy server Host + type: string + default: "" + - name: PROXY_NON_PROXY_HOSTS + description: Non proxy server host + type: string + default: "" + - name: PROXY_PROTOCOL + description: Protocol for the proxy ie http or https + type: string + default: "http" + - name: CONTEXT_DIR + type: string + description: >- + The context directory within the repository for sources on + which we want to execute maven goals. + default: "." + steps: + - name: mvn-settings + image: registry.access.redhat.com/ubi8/ubi-minimal:latest + script: | + #!/usr/bin/env bash + + [[ -f $(workspaces.maven-settings.path)/settings.xml ]] && \ + echo 'using existing $(workspaces.maven-settings.path)/settings.xml' && exit 0 + + cat > $(workspaces.maven-settings.path)/settings.xml < + + + + + + + + + + + + + + EOF + + xml="" + if [ -n "$(params.PROXY_HOST)" -a -n "$(params.PROXY_PORT)" ]; then + xml="\ + genproxy\ + true\ + $(params.PROXY_PROTOCOL)\ + $(params.PROXY_HOST)\ + $(params.PROXY_PORT)" + if [ -n "$(params.PROXY_USER)" -a -n "$(params.PROXY_PASSWORD)" ]; then + xml="$xml\ + $(params.PROXY_USER)\ + $(params.PROXY_PASSWORD)" + fi + if [ -n "$(params.PROXY_NON_PROXY_HOSTS)" ]; then + xml="$xml\ + $(params.PROXY_NON_PROXY_HOSTS)" + fi + xml="$xml\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + if [ -n "$(params.SERVER_USER)" -a -n "$(params.SERVER_PASSWORD)" ]; then + xml="\ + serverid" + xml="$xml\ + $(params.SERVER_USER)\ + $(params.SERVER_PASSWORD)" + xml="$xml\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + if [ -n "$(params.MAVEN_MIRROR_URL)" ]; then + xml=" \ + mirror.default\ + $(params.MAVEN_MIRROR_URL)\ + central\ + " + sed -i "s||$xml|" $(workspaces.maven-settings.path)/settings.xml + fi + + - name: mvn-goals + image: $(params.MAVEN_IMAGE) + workingDir: $(workspaces.source.path)/$(params.CONTEXT_DIR) + command: ["/usr/bin/mvn"] + args: + - -s + - $(workspaces.maven-settings.path)/settings.xml + - "$(params.GOALS)" diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/printer/print.go b/pkg/cmd/hub/printer/print.go similarity index 97% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/printer/print.go rename to pkg/cmd/hub/printer/print.go index 6d8331c2ca..be7cff8194 100644 --- a/vendor/github.com/tektoncd/hub/api/pkg/cli/printer/print.go +++ b/pkg/cmd/hub/printer/print.go @@ -20,7 +20,7 @@ import ( "text/tabwriter" "text/template" - "github.com/tektoncd/hub/api/pkg/cli/formatter" + "github.com/tektoncd/cli/pkg/cmd/hub/formatter" ) type Printer struct { diff --git a/pkg/cmd/hub/test/builder/apiresource.go b/pkg/cmd/hub/test/builder/apiresource.go new file mode 100644 index 0000000000..76f3d3c06a --- /dev/null +++ b/pkg/cmd/hub/test/builder/apiresource.go @@ -0,0 +1,52 @@ +// Copyright © 2020 The Tekton 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 builder + +import ( + "strings" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + group string = "tekton.dev" +) + +func APIResourceList(version string, kinds []string) []*metav1.APIResourceList { + return []*metav1.APIResourceList{ + { + GroupVersion: group + "/" + version, + APIResources: apiresources(version, kinds), + }, + } +} + +func apiresources(version string, kinds []string) []metav1.APIResource { + apires := make([]metav1.APIResource, 0) + for _, kind := range kinds { + namespaced := true + if strings.Contains(kind, "cluster") { + namespaced = false + } + apires = append(apires, metav1.APIResource{ + Name: kind + "s", + Group: group, + Kind: kind, + Version: version, + Namespaced: namespaced, + }) + } + return apires +} diff --git a/pkg/cmd/hub/test/builder/unstructured.go b/pkg/cmd/hub/test/builder/unstructured.go new file mode 100644 index 0000000000..c9d2c90903 --- /dev/null +++ b/pkg/cmd/hub/test/builder/unstructured.go @@ -0,0 +1,40 @@ +// Copyright © 2020 The Tekton 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 builder + +import ( + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" +) + +func UnstructuredV1beta1T(task *v1beta1.Task, version string) *unstructured.Unstructured { + task.APIVersion = "tekton.dev/" + version + task.Kind = "task" + object, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(task) + return &unstructured.Unstructured{ + Object: object, + } +} + +func UnstructuredV1(task *v1.Task, version string) *unstructured.Unstructured { + task.APIVersion = "tekton.dev/" + version + task.Kind = "task" + object, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(task) + return &unstructured.Unstructured{ + Object: object, + } +} diff --git a/pkg/cmd/hub/test/client.go b/pkg/cmd/hub/test/client.go new file mode 100644 index 0000000000..6ec520f2c6 --- /dev/null +++ b/pkg/cmd/hub/test/client.go @@ -0,0 +1,55 @@ +// Copyright © 2020 The Tekton 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 test + +import ( + "github.com/tektoncd/cli/pkg/cmd/hub/gvr" + "github.com/tektoncd/cli/pkg/cmd/hub/kube" + "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" + "k8s.io/client-go/dynamic" +) + +type fakeClients struct { + tekton versioned.Interface + dynamic dynamic.Interface + namespace string +} + +var _ kube.ClientSet = (*fakeClients)(nil) + +func (p *fakeClients) Dynamic() dynamic.Interface { + return p.dynamic +} + +func (p *fakeClients) Tekton() versioned.Interface { + return p.tekton +} +func (p *fakeClients) Namespace() string { + return p.namespace +} + +func FakeClientSet(tekton versioned.Interface, dynamic dynamic.Interface, namespace string) kube.ClientSet { + if tekton != nil { + if err := gvr.InitializeAPIGroupRes(tekton.Discovery()); err != nil { + return nil + } + } + + return &fakeClients{ + tekton: tekton, + dynamic: dynamic, + namespace: namespace, + } +} diff --git a/pkg/cmd/hub/test/config.go b/pkg/cmd/hub/test/config.go new file mode 100644 index 0000000000..97aa246338 --- /dev/null +++ b/pkg/cmd/hub/test/config.go @@ -0,0 +1,79 @@ +// Copyright © 2020 The Tekton 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 test + +import ( + "fmt" + "io" + + "github.com/tektoncd/cli/pkg/cmd/hub/app" + "github.com/tektoncd/cli/pkg/cmd/hub/hub" +) + +// API is test URL +const API string = "http://test.hub.cli" + +type cli struct { + hub hub.Client + stream app.Stream +} + +var _ app.CLI = (*cli)(nil) + +func NewCLI(hubType string) app.CLI { + var h hub.Client + switch hubType { + case hub.TektonHubType: + h = hub.NewTektonHubClient() + case hub.ArtifactHubType: + h = hub.NewArtifactHubClient() + default: + fmt.Printf("invalid hub type: %s, using default type: %s to continue", hubType, hub.TektonHubType) + h = hub.NewTektonHubClient() + } + + if err := h.SetURL(API); err != nil { + fmt.Printf("Failed validate and set the hub apiURL server URL %v", err) + } + + return &cli{ + stream: app.Stream{}, + hub: h, + } +} + +func (c *cli) Stream() app.Stream { + return c.stream +} + +func (c *cli) SetStream(out, err io.Writer) { + c.stream = app.Stream{Out: out, Err: err} +} + +func (c *cli) Hub() hub.Client { + return c.hub +} + +func (c *cli) SetHub(hubType string) error { + if hubType == hub.TektonHubType { + c.hub = hub.NewTektonHubClient() + return nil + } else if hubType == hub.ArtifactHubType { + c.hub = hub.NewArtifactHubClient() + return nil + } + + return fmt.Errorf("invalid hub type: %s", hubType) +} diff --git a/pkg/cmd/hub/test/dynamictestclient.go b/pkg/cmd/hub/test/dynamictestclient.go new file mode 100644 index 0000000000..28ad9a1bec --- /dev/null +++ b/pkg/cmd/hub/test/dynamictestclient.go @@ -0,0 +1,36 @@ +// Copyright © 2022 The Tekton 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 test + +import ( + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic/fake" +) + +func DynamicClient(objects ...runtime.Object) *fake.FakeDynamicClient { + dynamicClient := fake.NewSimpleDynamicClientWithCustomListKinds( + runtime.NewScheme(), + map[schema.GroupVersionResource]string{ + {Group: "tekton.dev", Version: "v1", Resource: "tasks"}: "TaskList", + {Group: "tekton.dev", Version: "v1alpha1", Resource: "tasks"}: "TaskList", + {Group: "tekton.dev", Version: "v1beta1", Resource: "tasks"}: "TaskList", + {Group: "apps", Version: "v1", Resource: "deployments"}: "DeploymentList", + }, + objects..., + ) + + return dynamicClient +} diff --git a/pkg/cmd/hub/test/helpers.go b/pkg/cmd/hub/test/helpers.go new file mode 100644 index 0000000000..b57b29730c --- /dev/null +++ b/pkg/cmd/hub/test/helpers.go @@ -0,0 +1,111 @@ +// Copyright © 2020 The Tekton 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 test + +import ( + "context" + "fmt" + "testing" + + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing" + pipelinetest "github.com/tektoncd/pipeline/test" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" +) + +type Data struct { + Pipelines []*v1beta1.Pipeline + Tasks []*v1beta1.Task +} + +func SeedTestData(t *testing.T, d pipelinetest.Data) (pipelinetest.Clients, pipelinetest.Informers) { + ctx, _ := ttesting.SetupFakeContext(t) + return pipelinetest.SeedTestData(t, ctx, d) +} + +func SeedV1beta1TestData(t *testing.T, d Data) (Clients, Informers) { + ctx, _ := ttesting.SetupFakeContext(t) + return seedTestData(t, ctx, d) +} + +func GetDeploymentData(name, version string) *unstructured.Unstructured { + deployment := &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "apps/v1", + "kind": "Deployment", + "metadata": map[string]interface{}{ + "name": name, + "labels": map[string]interface{}{ + "app.kubernetes.io/part-of": "tekton-pipelines", + "app.kubernetes.io/component": "controller", + "app.kubernetes.io/name": "controller", + }, + }, + "spec": map[string]interface{}{ + "template": map[string]interface{}{ + "metadata": map[string]interface{}{ + "labels": map[string]interface{}{ + "app.kubernetes.io/version": version, + }, + "annotations": map[string]interface{}{}, + }, + "spec": map[string]interface{}{ + "containers": []interface{}{ + map[string]interface{}{ + "name": "web", + "image": "nginx:1.12", + }, + }, + }, + }, + }, + }, + } + return deployment +} + +func GetConfigMapData(name string, version string) *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": name, + "labels": map[string]interface{}{ + "app.kubernetes.io/part-of": "tekton-pipelines", + "app.kubernetes.io/instance": "default", + }, + }, + "data": map[string]interface{}{ + "version": version, + }, + }, + } +} + +func CreateTektonPipelineController(dynamic dynamic.Interface, version string) error { + + deployment := GetDeploymentData("test", version) + + deploymentRes := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"} + _, err := dynamic.Resource(deploymentRes).Namespace("tekton-pipelines").Create(context.TODO(), deployment, metav1.CreateOptions{}) + if err != nil { + return fmt.Errorf("failed to create deployment: %v", err) + } + return nil +} diff --git a/pkg/cmd/hub/test/prompt/prompt.go b/pkg/cmd/hub/test/prompt/prompt.go new file mode 100644 index 0000000000..688a51f2b0 --- /dev/null +++ b/pkg/cmd/hub/test/prompt/prompt.go @@ -0,0 +1,76 @@ +// Copyright © 2021 The Tekton 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 prompt + +import ( + "bytes" + "testing" + + "github.com/ActiveState/vt10x" + "github.com/AlecAivazis/survey/v2" + "github.com/AlecAivazis/survey/v2/terminal" + goexpect "github.com/Netflix/go-expect" + "gotest.tools/v3/assert" +) + +// Prompt provides test utility for prompt test. +type Prompt struct { + CmdArgs []string + Procedure func(*goexpect.Console) error +} + +// RunTest run the test cases of some prompt test. +// It will start given procedure in background and eval the test. +func (pt *Prompt) RunTest(t *testing.T, procedure func(*goexpect.Console) error, test func(terminal.Stdio) error) { + // Multiplex output to a buffer as well for the raw bytes. + buf := new(bytes.Buffer) + c, state, err := vt10x.NewVT10XConsole(goexpect.WithStdout(buf)) + assert.NilError(t, err) + defer c.Close() + + donec := make(chan struct{}) + go func() { + defer close(donec) + if err := procedure(c); err != nil { + t.Logf("procedure failed: %v", err) + } + }() + + assert.NilError(t, test(stdio(c))) + + // Close the slave end of the pty, and read the remaining bytes from the master end. + _ = c.Tty().Close() + <-donec + + t.Logf("Raw output: %q", buf.String()) + + // Dump the terminal's screen. + t.Logf("\n%s", goexpect.StripTrailingEmptyLines(state.String())) +} + +// WithStdio helps to test interactive command +// by setting stdio for the ask function +func WithStdio(stdio terminal.Stdio) survey.AskOpt { + return func(options *survey.AskOptions) error { + options.Stdio.In = stdio.In + options.Stdio.Out = stdio.Out + options.Stdio.Err = stdio.Err + return nil + } +} + +func stdio(c *goexpect.Console) terminal.Stdio { + return terminal.Stdio{In: c.Tty(), Out: c.Tty(), Err: c.Tty()} +} diff --git a/pkg/cmd/hub/test/v1beta.go b/pkg/cmd/hub/test/v1beta.go new file mode 100644 index 0000000000..0e03be7f0a --- /dev/null +++ b/pkg/cmd/hub/test/v1beta.go @@ -0,0 +1,67 @@ +package test + +import ( + "context" + "testing" + + fakepipelineclientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/fake" + informersv1beta1 "github.com/tektoncd/pipeline/pkg/client/informers/externalversions/pipeline/v1beta1" + fakepipelineclient "github.com/tektoncd/pipeline/pkg/client/injection/client/fake" + fakepipelineinformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/pipeline/fake" + faketaskinformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/task/fake" + "github.com/tektoncd/pipeline/test" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + fakekubeclientset "k8s.io/client-go/kubernetes/fake" + fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" +) + +type Clients struct { + Pipeline *fakepipelineclientset.Clientset + Kube *fakekubeclientset.Clientset +} + +// Informers holds references to informers which are useful for reconciler tests. +type Informers struct { + Pipeline informersv1beta1.PipelineInformer + Task informersv1beta1.TaskInformer +} + +// seedTestData returns Clients and Informers populated with the +// given Data. +// nolint: revive +func seedTestData(t *testing.T, ctx context.Context, d Data) (Clients, Informers) { + c := Clients{ + + Kube: fakekubeclient.Get(ctx), + Pipeline: fakepipelineclient.Get(ctx), + } + + // Every time a resource is modified, change the metadata.resourceVersion. + test.PrependResourceVersionReactor(&c.Pipeline.Fake) + + i := Informers{ + Pipeline: fakepipelineinformer.Get(ctx), + Task: faketaskinformer.Get(ctx), + } + + // Attach reactors that add resource mutations to the appropriate + // informer index, and simulate optimistic concurrency failures when + // the resource version is mismatched. + c.Pipeline.PrependReactor("*", "pipelines", test.AddToInformer(t, i.Pipeline.Informer().GetIndexer())) + for _, p := range d.Pipelines { + p := p.DeepCopy() // Avoid assumptions that the informer's copy is modified. + if _, err := c.Pipeline.TektonV1beta1().Pipelines(p.Namespace).Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatal(err) + } + } + c.Pipeline.PrependReactor("*", "tasks", test.AddToInformer(t, i.Task.Informer().GetIndexer())) + for _, ta := range d.Tasks { + ta := ta.DeepCopy() // Avoid assumptions that the informer's copy is modified. + if _, err := c.Pipeline.TektonV1beta1().Tasks(ta.Namespace).Create(ctx, ta, metav1.CreateOptions{}); err != nil { + t.Fatal(err) + } + } + c.Pipeline.ClearActions() + c.Kube.ClearActions() + return c, i +} diff --git a/vendor/github.com/tektoncd/hub/api/pkg/cli/version/version.go b/pkg/cmd/hub/version/version.go similarity index 100% rename from vendor/github.com/tektoncd/hub/api/pkg/cli/version/version.go rename to pkg/cmd/hub/version/version.go diff --git a/pkg/cmd/hub/version/version_test.go b/pkg/cmd/hub/version/version_test.go new file mode 100644 index 0000000000..e6e990d1ad --- /dev/null +++ b/pkg/cmd/hub/version/version_test.go @@ -0,0 +1,92 @@ +// Copyright © 2021 The Tekton 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 version + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tektoncd/cli/pkg/cmd/hub/test" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func TestGetPipelineVersion(t *testing.T) { + + testParams := []struct { + name string + namespace string + userProvidedNamespace string + deployment *unstructured.Unstructured + want string + }{{ + name: "empty deployment items", + namespace: "tekton-pipelines", + deployment: &unstructured.Unstructured{}, + want: "", + }, { + name: "deployment spec have labels specific to master version (new labels)", + namespace: "tekton-pipelines", + deployment: test.GetDeploymentData("dep5", "master-tekton-pipelines"), + want: "master-tekton-pipelines", + }} + for _, tp := range testParams { + t.Run(tp.name, func(t *testing.T) { + dynamic := test.DynamicClient() + deploymentRes := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"} + _, err := dynamic.Resource(deploymentRes).Namespace(tp.namespace).Create(context.TODO(), tp.deployment, metav1.CreateOptions{}) + if err != nil { + t.Errorf("failed to create deployment: %v", err) + } + version, _ := GetPipelineVersion(dynamic) + assert.Equal(t, tp.want, version) + }) + } +} + +func TestGetPipelineVersionViaConfigMap(t *testing.T) { + + testParams := []struct { + name string + namespace string + userProvidedNamespace string + configMap *unstructured.Unstructured + want string + }{{ + name: "empty deployment items", + namespace: "tekton-pipelines", + configMap: &unstructured.Unstructured{}, + want: "", + }, { + name: "deployment spec have labels specific to master version (new labels)", + namespace: "tekton-pipelines", + configMap: test.GetConfigMapData("pipelines-info", "master-tekton-pipelines"), + want: "master-tekton-pipelines", + }} + for _, tp := range testParams { + t.Run(tp.name, func(t *testing.T) { + dynamic := test.DynamicClient() + deploymentRes := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"} + _, err := dynamic.Resource(deploymentRes).Namespace(tp.namespace).Create(context.TODO(), tp.configMap, metav1.CreateOptions{}) + if err != nil { + t.Errorf("failed to create deployment: %v", err) + } + version, _ := GetPipelineVersion(dynamic) + assert.Equal(t, tp.want, version) + }) + } +} diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index b779b3e4e4..5cfb806ecd 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -27,6 +27,8 @@ import ( "github.com/tektoncd/cli/pkg/cmd/completion" "github.com/tektoncd/cli/pkg/cmd/customrun" "github.com/tektoncd/cli/pkg/cmd/eventlistener" + hubApp "github.com/tektoncd/cli/pkg/cmd/hub/app" + hub "github.com/tektoncd/cli/pkg/cmd/hub/cmd" "github.com/tektoncd/cli/pkg/cmd/pipeline" "github.com/tektoncd/cli/pkg/cmd/pipelinerun" "github.com/tektoncd/cli/pkg/cmd/task" @@ -36,8 +38,6 @@ import ( "github.com/tektoncd/cli/pkg/cmd/version" "github.com/tektoncd/cli/pkg/plugins" "github.com/tektoncd/cli/pkg/suggestion" - hubApp "github.com/tektoncd/hub/api/pkg/cli/app" - hub "github.com/tektoncd/hub/api/pkg/cli/cmd" ) const usageTemplate = `Usage:{{if .Runnable}} diff --git a/pkg/cmd/testdata/TestPluginList.golden b/pkg/cmd/testdata/TestPluginList.golden index 8101fcf81c..ed08e97d9a 100644 --- a/pkg/cmd/testdata/TestPluginList.golden +++ b/pkg/cmd/testdata/TestPluginList.golden @@ -10,7 +10,7 @@ Available Commands: clustertriggerbinding Manage ClusterTriggerBindings customrun Manage CustomRuns eventlistener Manage EventListeners - hub Interact with tekton hub + hub Interact with artifacthub pipeline Manage pipelines pipelinerun Manage PipelineRuns task Manage Tasks diff --git a/test/e2e-tests.sh b/test/e2e-tests.sh index 55fe92ad2f..6f88f84384 100755 --- a/test/e2e-tests.sh +++ b/test/e2e-tests.sh @@ -147,10 +147,9 @@ must_fail "describe deleted eventlistener" tkn eventlistener describe github-li echo "Hub" echo "....................." run_test "hub command exists" tkn hub --help -run_test "hub search exists" tkn hub search --help -run_test "hub get exists" tkn hub get --help -must_fail "hub search cli" tkn hub --api-server=api.nonexistent.server search cli -must_fail "hub get task cli" tkn hub --api-server=api.nonexistent.server get task cli +run_test "hub install exists" tkn hub install --help +must_fail "hub search cli" tkn hub --type tekton --api-server=api.nonexistent.server search cli +must_fail "hub get task cli" tkn hub --type tekton --api-server=api.nonexistent.server get task cli # Make sure that eveything is cleaned up in the current namespace. for res in tasks pipelines taskruns pipelineruns; do diff --git a/vendor/github.com/ActiveState/vt10x/.travis.yml b/vendor/github.com/ActiveState/vt10x/.travis.yml new file mode 100644 index 0000000000..8c2998f7db --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/.travis.yml @@ -0,0 +1,5 @@ +language: go + +go: + - "1.10.2" + - master diff --git a/vendor/github.com/ActiveState/vt10x/LICENSE b/vendor/github.com/ActiveState/vt10x/LICENSE new file mode 100644 index 0000000000..a5976d65d3 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2013 James Gray + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without liitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and thismssion notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/ActiveState/vt10x/README.md b/vendor/github.com/ActiveState/vt10x/README.md new file mode 100644 index 0000000000..420318f675 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/README.md @@ -0,0 +1,9 @@ +# vt10x + +[![Build Status](https://travis-ci.org/hinshun/vt10x.svg?branch=master)](https://travis-ci.org/hinshun/vt10x) +[![GoDoc](https://godoc.org/github.com/hinshun/vt10x?status.svg)](https://godoc.org/github.com/hinshun/vt10x) + +Package vt10x is a vt10x terminal emulation backend, influenced +largely by st, rxvt, xterm, and iTerm as reference. Use it for terminal +muxing, a terminal emulation frontend, or wherever else you need +terminal emulation. diff --git a/vendor/github.com/ActiveState/vt10x/color.go b/vendor/github.com/ActiveState/vt10x/color.go new file mode 100644 index 0000000000..4ce0d8f323 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/color.go @@ -0,0 +1,37 @@ +package vt10x + +// ANSI color values +const ( + Black Color = iota + Red + Green + Yellow + Blue + Magenta + Cyan + LightGrey + DarkGrey + LightRed + LightGreen + LightYellow + LightBlue + LightMagenta + LightCyan + White +) + +// Default colors are potentially distinct to allow for special behavior. +// For example, a transparent background. Otherwise, the simple case is to +// map default colors to another color. +const ( + DefaultFG Color = 0xff80 + iota + DefaultBG +) + +// Color maps to the ANSI colors [0, 16) and the xterm colors [16, 256). +type Color uint16 + +// ANSI returns true if Color is within [0, 16). +func (c Color) ANSI() bool { + return (c < 16) +} diff --git a/vendor/github.com/ActiveState/vt10x/csi.go b/vendor/github.com/ActiveState/vt10x/csi.go new file mode 100644 index 0000000000..138cff9078 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/csi.go @@ -0,0 +1,189 @@ +package vt10x + +import ( + "fmt" + "strconv" + "strings" +) + +// CSI (Control Sequence Introducer) +// ESC+[ +type csiEscape struct { + buf []byte + args []int + mode byte + priv bool +} + +func (c *csiEscape) reset() { + c.buf = c.buf[:0] + c.args = c.args[:0] + c.mode = 0 + c.priv = false +} + +func (c *csiEscape) put(b byte) bool { + c.buf = append(c.buf, b) + if b >= 0x40 && b <= 0x7E || len(c.buf) >= 256 { + c.parse() + return true + } + return false +} + +func (c *csiEscape) parse() { + c.mode = c.buf[len(c.buf)-1] + if len(c.buf) == 1 { + return + } + s := string(c.buf) + c.args = c.args[:0] + if s[0] == '?' { + c.priv = true + s = s[1:] + } + s = s[:len(s)-1] + ss := strings.Split(s, ";") + for _, p := range ss { + i, err := strconv.Atoi(p) + if err != nil { + //t.logf("invalid CSI arg '%s'\n", p) + break + } + c.args = append(c.args, i) + } +} + +func (c *csiEscape) arg(i, def int) int { + if i >= len(c.args) || i < 0 { + return def + } + return c.args[i] +} + +// maxarg takes the maximum of arg(i, def) and def +func (c *csiEscape) maxarg(i, def int) int { + return max(c.arg(i, def), def) +} + +func (t *State) handleCSI() { + c := &t.csi + switch c.mode { + default: + goto unknown + case '@': // ICH - insert blank char + t.insertBlanks(c.arg(0, 1)) + case 'A': // CUU - cursor up + t.moveTo(t.cur.x, t.cur.y-c.maxarg(0, 1)) + case 'B', 'e': // CUD, VPR - cursor down + t.moveTo(t.cur.x, t.cur.y+c.maxarg(0, 1)) + case 'c': // DA - device attributes + if c.arg(0, 0) == 0 { + // TODO: write vt102 id + } + case 'C', 'a': // CUF, HPR - cursor forward + t.moveTo(t.cur.x+c.maxarg(0, 1), t.cur.y) + case 'D': // CUB - cursor backward + t.moveTo(t.cur.x-c.maxarg(0, 1), t.cur.y) + case 'E': // CNL - cursor down and first col + t.moveTo(0, t.cur.y+c.arg(0, 1)) + case 'F': // CPL - cursor up and first col + t.moveTo(0, t.cur.y-c.arg(0, 1)) + case 'g': // TBC - tabulation clear + switch c.arg(0, 0) { + // clear current tab stop + case 0: + t.tabs[t.cur.x] = false + // clear all tabs + case 3: + for i := range t.tabs { + t.tabs[i] = false + } + default: + goto unknown + } + case 'G', '`': // CHA, HPA - Move to + t.moveTo(c.arg(0, 1)-1, t.cur.y) + case 'H', 'f': // CUP, HVP - move to + t.moveAbsTo(c.arg(1, 1)-1, c.arg(0, 1)-1) + case 'I': // CHT - cursor forward tabulation tab stops + n := c.arg(0, 1) + for i := 0; i < n; i++ { + t.putTab(true) + } + case 'J': // ED - clear screen + // TODO: sel.ob.x = -1 + switch c.arg(0, 0) { + case 0: // below + t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y) + if t.cur.y < t.rows-1 { + t.clear(0, t.cur.y+1, t.cols-1, t.rows-1) + } + case 1: // above + if t.cur.y > 1 { + t.clear(0, 0, t.cols-1, t.cur.y-1) + } + t.clear(0, t.cur.y, t.cur.x, t.cur.y) + case 2: // all + t.clear(0, 0, t.cols-1, t.rows-1) + default: + goto unknown + } + case 'K': // EL - clear line + switch c.arg(0, 0) { + case 0: // right + t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y) + case 1: // left + t.clear(0, t.cur.y, t.cur.x, t.cur.y) + case 2: // all + t.clear(0, t.cur.y, t.cols-1, t.cur.y) + } + case 'S': // SU - scroll lines up + t.scrollUp(t.top, c.arg(0, 1)) + case 'T': // SD - scroll lines down + t.scrollDown(t.top, c.arg(0, 1)) + case 'L': // IL - insert blank lines + t.insertBlankLines(c.arg(0, 1)) + case 'l': // RM - reset mode + t.setMode(c.priv, false, c.args) + case 'M': // DL - delete lines + t.deleteLines(c.arg(0, 1)) + case 'X': // ECH - erase chars + t.clear(t.cur.x, t.cur.y, t.cur.x+c.arg(0, 1)-1, t.cur.y) + case 'P': // DCH - delete chars + t.deleteChars(c.arg(0, 1)) + case 'Z': // CBT - cursor backward tabulation tab stops + n := c.arg(0, 1) + for i := 0; i < n; i++ { + t.putTab(false) + } + case 'd': // VPA - move to + t.moveAbsTo(t.cur.x, c.arg(0, 1)-1) + case 'h': // SM - set terminal mode + t.setMode(c.priv, true, c.args) + case 'm': // SGR - terminal attribute (color) + t.setAttr(c.args) + case 'n': + switch c.arg(0, 0) { + case 5: // DSR - device status report + t.w.Write([]byte("\033[0n")) + case 6: // CPR - cursor position report + t.w.Write([]byte(fmt.Sprintf("\033[%d;%dR", t.cur.y+1, t.cur.x+1))) + } + case 'r': // DECSTBM - set scrolling region + if c.priv { + goto unknown + } else { + t.setScroll(c.arg(0, 1)-1, c.arg(1, t.rows)-1) + t.moveAbsTo(0, 0) + } + case 's': // DECSC - save cursor position (ANSI.SYS) + t.saveCursor() + case 'u': // DECRC - restore cursor position (ANSI.SYS) + t.restoreCursor() + } + return +unknown: // TODO: get rid of this goto + t.logf("unknown CSI sequence '%c'\n", c.mode) + // TODO: c.dump() +} diff --git a/vendor/github.com/ActiveState/vt10x/doc.go b/vendor/github.com/ActiveState/vt10x/doc.go new file mode 100644 index 0000000000..8205207d6e --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/doc.go @@ -0,0 +1,9 @@ +/* +Package terminal is a vt10x terminal emulation backend, influenced +largely by st, rxvt, xterm, and iTerm as reference. Use it for terminal +muxing, a terminal emulation frontend, or wherever else you need +terminal emulation. + +In development, but very usable. +*/ +package vt10x diff --git a/vendor/github.com/ActiveState/vt10x/expect.go b/vendor/github.com/ActiveState/vt10x/expect.go new file mode 100644 index 0000000000..7ae52f321e --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/expect.go @@ -0,0 +1,31 @@ +// +build !windows + +package vt10x + +import ( + expect "github.com/Netflix/go-expect" + "github.com/kr/pty" +) + +// NewVT10XConsole returns a new expect.Console that multiplexes the +// Stdin/Stdout to a VT10X terminal, allowing Console to interact with an +// application sending ANSI escape sequences. +func NewVT10XConsole(opts ...expect.ConsoleOpt) (*expect.Console, *State, error) { + ptm, pts, err := pty.Open() + if err != nil { + return nil, nil, err + } + + var state State + term, err := Create(&state, pts) + if err != nil { + return nil, nil, err + } + + c, err := expect.NewConsole(append(opts, expect.WithStdin(ptm), expect.WithStdout(term), expect.WithCloser(pts, ptm, term))...) + if err != nil { + return nil, nil, err + } + + return c, &state, nil +} diff --git a/vendor/github.com/ActiveState/vt10x/ioctl_other.go b/vendor/github.com/ActiveState/vt10x/ioctl_other.go new file mode 100644 index 0000000000..0aa1868754 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/ioctl_other.go @@ -0,0 +1,15 @@ +// +build plan9 nacl windows + +package vt10x + +import ( + "os" +) + +func ioctl(f *os.File, cmd, p uintptr) error { + return nil +} + +func ResizePty(*os.File) error { + return nil +} diff --git a/vendor/github.com/ActiveState/vt10x/ioctl_posix.go b/vendor/github.com/ActiveState/vt10x/ioctl_posix.go new file mode 100644 index 0000000000..7b81b3a1c2 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/ioctl_posix.go @@ -0,0 +1,31 @@ +// +build linux darwin dragonfly solaris openbsd netbsd freebsd + +package vt10x + +import ( + "os" + "syscall" + "unsafe" +) + +func ioctl(f *os.File, cmd, p uintptr) error { + _, _, errno := syscall.Syscall( + syscall.SYS_IOCTL, + f.Fd(), + syscall.TIOCSWINSZ, + p) + if errno != 0 { + return syscall.Errno(errno) + } + return nil +} + +func ResizePty(pty *os.File, cols, rows int) error { + var w struct{ row, col, xpix, ypix uint16 } + w.row = uint16(rows) + w.col = uint16(cols) + w.xpix = 16 * uint16(cols) + w.ypix = 16 * uint16(rows) + return ioctl(pty, syscall.TIOCSWINSZ, + uintptr(unsafe.Pointer(&w))) +} diff --git a/vendor/github.com/ActiveState/vt10x/parse.go b/vendor/github.com/ActiveState/vt10x/parse.go new file mode 100644 index 0000000000..b6eee4847a --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/parse.go @@ -0,0 +1,222 @@ +package vt10x + +func isControlCode(c rune) bool { + return c < 0x20 || c == 0177 +} + +func (t *State) parse(c rune) bool { + t.logf("%q", string(c)) + if isControlCode(c) { + wasHandled, isPrintable := t.handleControlCodes(c) + if wasHandled || t.cur.attr.mode&attrGfx == 0 { + return isPrintable + } + } + // TODO: update selection; see st.c:2450 + + if t.mode&ModeWrap != 0 && t.cur.state&cursorWrapNext != 0 { + t.lines[t.cur.y][t.cur.x].mode |= attrWrap + t.newline(true) + } + + if t.mode&ModeInsert != 0 && t.cur.x+1 < t.cols { + // TODO: move shiz, look at st.c:2458 + t.logln("insert mode not implemented") + } + + t.setChar(c, &t.cur.attr, t.cur.x, t.cur.y) + if t.cur.x+1 < t.cols { + t.moveTo(t.cur.x+1, t.cur.y) + } else { + t.cur.state |= cursorWrapNext + } + + return true +} + +func (t *State) parseEsc(c rune) bool { + if wasHandled, isPrintable := t.handleControlCodes(c); wasHandled { + return isPrintable + } + next := t.parse + t.logf("%q", string(c)) + switch c { + case '[': + next = t.parseEscCSI + case '#': + next = t.parseEscTest + case 'P', // DCS - Device Control String + '_', // APC - Application Program Command + '^', // PM - Privacy Message + ']', // OSC - Operating System Command + 'k': // old title set compatibility + t.str.reset() + t.str.typ = c + next = t.parseEscStr + case '(': // set primary charset G0 + next = t.parseEscAltCharset + case ')', // set secondary charset G1 (ignored) + '*', // set tertiary charset G2 (ignored) + '+': // set quaternary charset G3 (ignored) + case 'D': // IND - linefeed + if t.cur.y == t.bottom { + t.scrollUp(t.top, 1) + } else { + t.moveTo(t.cur.x, t.cur.y+1) + } + case 'E': // NEL - next line + t.newline(true) + case 'H': // HTS - horizontal tab stop + t.tabs[t.cur.x] = true + case 'M': // RI - reverse index + if t.cur.y == t.top { + t.scrollDown(t.top, 1) + } else { + t.moveTo(t.cur.x, t.cur.y-1) + } + case 'Z': // DECID - identify terminal + // TODO: write to our writer our id + case 'c': // RIS - reset to initial state + t.reset() + case '=': // DECPAM - application keypad + t.mode |= ModeAppKeypad + case '>': // DECPNM - normal keypad + t.mode &^= ModeAppKeypad + case '7': // DECSC - save cursor + t.saveCursor() + case '8': // DECRC - restore cursor + t.restoreCursor() + case '\\': // ST - stop + default: + t.logf("unknown ESC sequence '%c'\n", c) + } + t.state = next + return false +} + +func (t *State) parseEscCSI(c rune) bool { + if wasHandled, isPrintable := t.handleControlCodes(c); wasHandled { + return isPrintable + } + t.logf("%q", string(c)) + if t.csi.put(byte(c)) { + t.state = t.parse + t.handleCSI() + } + return false +} + +func (t *State) parseEscStr(c rune) bool { + t.logf("%q", string(c)) + switch c { + case '\033': + t.state = t.parseEscStrEnd + case '\a': // backwards compatiblity to xterm + t.state = t.parse + t.handleSTR() + default: + t.str.put(c) + } + return false +} + +func (t *State) parseEscStrEnd(c rune) bool { + if wasHandled, isPrintable := t.handleControlCodes(c); wasHandled { + return isPrintable + } + t.logf("%q", string(c)) + t.state = t.parse + if c == '\\' { + t.handleSTR() + } + return false +} + +func (t *State) parseEscAltCharset(c rune) bool { + if wasHandled, isPrintable := t.handleControlCodes(c); wasHandled { + return isPrintable + } + t.logf("%q", string(c)) + switch c { + case '0': // line drawing set + t.cur.attr.mode |= attrGfx + case 'B': // USASCII + t.cur.attr.mode &^= attrGfx + case 'A', // UK (ignored) + '<', // multinational (ignored) + '5', // Finnish (ignored) + 'C', // Finnish (ignored) + 'K': // German (ignored) + default: + t.logf("unknown alt. charset '%c'\n", c) + } + t.state = t.parse + return false +} + +func (t *State) parseEscTest(c rune) bool { + if wasHandled, isPrintable := t.handleControlCodes(c); wasHandled { + return isPrintable + } + // DEC screen alignment test + if c == '8' { + for y := 0; y < t.rows; y++ { + for x := 0; x < t.cols; x++ { + t.setChar('E', &t.cur.attr, x, y) + } + } + } + t.state = t.parse + return false +} + +// handleControlCodes handles control codes and returns two booleans +// The first boolean indicates whether the control code was handled, the second one whether +// the rune was printable +func (t *State) handleControlCodes(c rune) (bool, bool) { + if !isControlCode(c) { + return false, true + } + isPrintable := false + switch c { + // HT + case '\t': + t.putTab(true) + isPrintable = true + // BS + case '\b': + if t.cur.x == t.cols-1 && t.Mode(ModeWrap) && t.cur.state&cursorWrapNext != 0 { + t.cur.state &^= cursorWrapNext + } else { + t.moveTo(t.cur.x-1, t.cur.y) + } + // CR + case '\r': + t.moveTo(0, t.cur.y) + // LF, VT, LF + case '\f', '\v', '\n': + // go to first col if mode is set + t.newline(t.mode&ModeCRLF != 0) + isPrintable = true + // BEL + case '\a': + // TODO: emit sound + // TODO: window alert if not focused + // ESC + case 033: + t.csi.reset() + t.state = t.parseEsc + // SO, SI + case 016, 017: + // different charsets not supported. apps should use the correct + // alt charset escapes, probably for line drawing + // SUB, CAN + case 032, 030: + t.csi.reset() + // ignore ENQ, NUL, XON, XOFF, DEL + case 005, 000, 021, 023, 0177: + default: + return false, true + } + return true, isPrintable +} diff --git a/vendor/github.com/ActiveState/vt10x/state.go b/vendor/github.com/ActiveState/vt10x/state.go new file mode 100644 index 0000000000..589c866da4 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/state.go @@ -0,0 +1,894 @@ +package vt10x + +import ( + "io" + "log" + "sync" +) + +const ( + tabspaces = 8 +) + +const ( + attrReverse = 1 << iota + attrUnderline + attrBold + attrGfx + attrItalic + attrBlink + attrWrap +) + +const ( + cursorDefault = 1 << iota + cursorWrapNext + cursorOrigin +) + +// ModeFlag represents various terminal mode states. +type ModeFlag uint32 + +// Terminal modes +const ( + ModeWrap ModeFlag = 1 << iota + ModeInsert + ModeAppKeypad + ModeAltScreen + ModeCRLF + ModeMouseButton + ModeMouseMotion + ModeReverse + ModeKeyboardLock + ModeHide + ModeEcho + ModeAppCursor + ModeMouseSgr + Mode8bit + ModeBlink + ModeFBlink + ModeFocus + ModeMouseX10 + ModeMouseMany + ModeMouseMask = ModeMouseButton | ModeMouseMotion | ModeMouseX10 | ModeMouseMany +) + +// ChangeFlag represents possible state changes of the terminal. +type ChangeFlag uint32 + +// Terminal changes to occur in VT.ReadState +const ( + ChangedScreen ChangeFlag = 1 << iota + ChangedTitle +) + +type glyph struct { + c rune + mode int16 + fg, bg Color +} + +type line []glyph + +type cursor struct { + attr glyph + x, y int + state uint8 +} + +type parseState func(c rune) bool + +// State represents the terminal emulation state. Use Lock/Unlock +// methods to synchronize data access with VT. +type State struct { + DebugLogger *log.Logger + // RecordHistory is a flag that when set to true keeps a history of all lines that are scrolled out of view + RecordHistory bool + + w io.Writer + mu sync.Mutex + changed ChangeFlag + cols, rows int + lines []line + altLines []line + dirty []bool // line dirtiness + anydirty bool + cur, curSaved cursor + top, bottom int // scroll limits + mode ModeFlag + state parseState + str strEscape + csi csiEscape + numlock bool + tabs []bool + title string + history []line +} + +func (t *State) logf(format string, args ...interface{}) { + if t.DebugLogger != nil { + t.DebugLogger.Printf(format, args...) + } +} + +func (t *State) logln(s string) { + if t.DebugLogger != nil { + t.DebugLogger.Println(s) + } +} + +func (t *State) lock() { + t.mu.Lock() +} + +func (t *State) unlock() { + t.mu.Unlock() +} + +// Lock locks the state object's mutex. +func (t *State) Lock() { + t.mu.Lock() +} + +// Unlock resets change flags and unlocks the state object's mutex. +func (t *State) Unlock() { + t.resetChanges() + t.mu.Unlock() +} + +// Cell returns the character code, foreground color, and background +// color at position (x, y) relative to the top left of the terminal. +func (t *State) Cell(x, y int) (ch rune, fg Color, bg Color) { + return t.lines[y][x].c, Color(t.lines[y][x].fg), Color(t.lines[y][x].bg) +} + +// Cursor returns the current position of the cursor. +func (t *State) Cursor() (int, int) { + return t.cur.x, t.cur.y +} + +// CursorVisible returns the visible state of the cursor. +func (t *State) CursorVisible() bool { + return t.mode&ModeHide == 0 +} + +// Mode tests if mode is currently set. +func (t *State) Mode(mode ModeFlag) bool { + return t.mode&mode != 0 +} + +// Title returns the current title set via the tty. +func (t *State) Title() string { + return t.title +} + +/* +// ChangeMask returns a bitfield of changes that have occured by VT. +func (t *State) ChangeMask() ChangeFlag { + return t.changed +} +*/ + +// Changed returns true if change has occured. +func (t *State) Changed(change ChangeFlag) bool { + return t.changed&change != 0 +} + +// resetChanges resets the change mask and dirtiness. +func (t *State) resetChanges() { + for i := range t.dirty { + t.dirty[i] = false + } + t.anydirty = false + t.changed = 0 +} + +func (t *State) saveCursor() { + t.curSaved = t.cur +} + +func (t *State) restoreCursor() { + t.cur = t.curSaved + t.moveTo(t.cur.x, t.cur.y) +} + +// WriteString processes the given string and updates the state +// This function is usually used for testing, as it also initializes the states, +// so previous state modifications are lost +func (t *State) WriteString(s string, rows, cols int) { + t.numlock = true + t.state = t.parse + t.cur.attr.fg = DefaultBG + t.cur.attr.bg = DefaultBG + t.resize(rows, cols) + t.reset() + for _, c := range []rune(s) { + t.put(c) + } +} + +func (t *State) put(c rune) bool { + return t.state(c) +} + +func (t *State) putTab(forward bool) { + x := t.cur.x + if forward { + if x == t.cols { + return + } + for x++; x < t.cols && !t.tabs[x]; x++ { + } + } else { + if x == 0 { + return + } + for x--; x > 0 && !t.tabs[x]; x-- { + } + } + t.moveTo(x, t.cur.y) +} + +func (t *State) newline(firstCol bool) { + y := t.cur.y + if y == t.bottom { + cur := t.cur + t.cur = t.defaultCursor() + t.scrollUp(t.top, 1) + t.cur = cur + } else { + y++ + } + if firstCol { + t.moveTo(0, y) + } else { + t.moveTo(t.cur.x, y) + } +} + +// table from st, which in turn is from rxvt :) +var gfxCharTable = [62]rune{ + '↑', '↓', '→', '←', '█', '▚', '☃', // A - G + 0, 0, 0, 0, 0, 0, 0, 0, // H - O + 0, 0, 0, 0, 0, 0, 0, 0, // P - W + 0, 0, 0, 0, 0, 0, 0, ' ', // X - _ + '◆', '▒', '␉', '␌', '␍', '␊', '°', '±', // ` - g + '␤', '␋', '┘', '┐', '┌', '└', '┼', '⎺', // h - o + '⎻', '─', '⎼', '⎽', '├', '┤', '┴', '┬', // p - w + '│', '≤', '≥', 'π', '≠', '£', '·', // x - ~ +} + +func (t *State) setChar(c rune, attr *glyph, x, y int) { + if attr.mode&attrGfx != 0 { + if c >= 0x41 && c <= 0x7e && gfxCharTable[c-0x41] != 0 { + c = gfxCharTable[c-0x41] + } + } + t.changed |= ChangedScreen + t.dirty[y] = true + t.lines[y][x] = *attr + t.lines[y][x].c = c + //if t.options.BrightBold && attr.mode&attrBold != 0 && attr.fg < 8 { + if attr.mode&attrBold != 0 && attr.fg < 8 { + t.lines[y][x].fg = attr.fg + 8 + } + if attr.mode&attrReverse != 0 { + t.lines[y][x].fg = attr.bg + t.lines[y][x].bg = attr.fg + } +} + +func (t *State) defaultCursor() cursor { + c := cursor{} + c.attr.fg = DefaultFG + c.attr.bg = DefaultBG + return c +} + +func (t *State) reset() { + t.cur = t.defaultCursor() + t.saveCursor() + for i := range t.tabs { + t.tabs[i] = false + } + for i := tabspaces; i < len(t.tabs); i += tabspaces { + t.tabs[i] = true + } + t.top = 0 + t.bottom = t.rows - 1 + t.mode = ModeWrap + t.clear(0, 0, t.rows-1, t.cols-1) + t.moveTo(0, 0) + t.history = make([]line, 0) +} + +// TODO: definitely can improve allocs +func (t *State) resize(cols, rows int) bool { + if cols == t.cols && rows == t.rows { + return false + } + if cols < 1 || rows < 1 { + return false + } + slide := t.cur.y - rows + 1 + if slide > 0 { + copy(t.lines, t.lines[slide:slide+rows]) + copy(t.altLines, t.altLines[slide:slide+rows]) + } + + lines, altLines, tabs := t.lines, t.altLines, t.tabs + t.lines = make([]line, rows) + t.altLines = make([]line, rows) + t.dirty = make([]bool, rows) + t.tabs = make([]bool, cols) + + minrows := min(rows, t.rows) + mincols := min(cols, t.cols) + t.changed |= ChangedScreen + for i := 0; i < rows; i++ { + t.dirty[i] = true + t.lines[i] = make(line, cols) + t.altLines[i] = make(line, cols) + } + for i := 0; i < minrows; i++ { + copy(t.lines[i], lines[i]) + copy(t.altLines[i], altLines[i]) + } + copy(t.tabs, tabs) + if cols > t.cols { + i := t.cols - 1 + for i > 0 && !tabs[i] { + i-- + } + for i += tabspaces; i < len(tabs); i += tabspaces { + tabs[i] = true + } + } + + t.cols = cols + t.rows = rows + t.setScroll(0, rows-1) + t.moveTo(t.cur.x, t.cur.y) + for i := 0; i < 2; i++ { + if mincols < cols && minrows > 0 { + t.clear(mincols, 0, cols-1, minrows-1) + } + if cols > 0 && minrows < rows { + t.clear(0, minrows, cols-1, rows-1) + } + t.swapScreen() + } + return slide > 0 +} + +func (t *State) clear(x0, y0, x1, y1 int) { + if x0 > x1 { + x0, x1 = x1, x0 + } + if y0 > y1 { + y0, y1 = y1, y0 + } + x0 = clamp(x0, 0, t.cols-1) + x1 = clamp(x1, 0, t.cols-1) + y0 = clamp(y0, 0, t.rows-1) + y1 = clamp(y1, 0, t.rows-1) + t.changed |= ChangedScreen + for y := y0; y <= y1; y++ { + t.dirty[y] = true + for x := x0; x <= x1; x++ { + t.lines[y][x] = t.cur.attr + t.lines[y][x].c = ' ' + } + } +} + +func (t *State) clearAll() { + t.clear(0, 0, t.cols-1, t.rows-1) +} + +func (t *State) moveAbsTo(x, y int) { + if t.cur.state&cursorOrigin != 0 { + y += t.top + } + t.moveTo(x, y) +} + +func (t *State) moveTo(x, y int) { + var miny, maxy int + if t.cur.state&cursorOrigin != 0 { + miny = t.top + maxy = t.bottom + } else { + miny = 0 + maxy = t.rows - 1 + } + x = clamp(x, 0, t.cols-1) + y = clamp(y, miny, maxy) + t.changed |= ChangedScreen + t.cur.state &^= cursorWrapNext + t.cur.x = x + t.cur.y = y +} + +func (t *State) swapScreen() { + t.lines, t.altLines = t.altLines, t.lines + t.mode ^= ModeAltScreen + t.dirtyAll() +} + +func (t *State) dirtyAll() { + t.changed |= ChangedScreen + for y := 0; y < t.rows; y++ { + t.dirty[y] = true + } +} + +func (t *State) setScroll(top, bottom int) { + top = clamp(top, 0, t.rows-1) + bottom = clamp(bottom, 0, t.rows-1) + if top > bottom { + top, bottom = bottom, top + } + t.top = top + t.bottom = bottom +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func clamp(val, min, max int) int { + if val < min { + return min + } else if val > max { + return max + } + return val +} + +func between(val, min, max int) bool { + if val < min || val > max { + return false + } + return true +} + +func (t *State) scrollDown(orig, n int) { + n = clamp(n, 0, t.bottom-orig+1) + t.clear(0, t.bottom-n+1, t.cols-1, t.bottom) + t.changed |= ChangedScreen + for i := t.bottom; i >= orig+n; i-- { + t.lines[i], t.lines[i-n] = t.lines[i-n], t.lines[i] + t.dirty[i] = true + t.dirty[i-n] = true + } + + // TODO: selection scroll +} + +func (t *State) scrollUp(orig, n int) { + n = clamp(n, 0, t.bottom-orig+1) + if t.RecordHistory && orig == t.top { + for i := orig; i < orig+n; i++ { + l := make([]glyph, len(t.lines[i])) + copy(l, t.lines[i]) + t.history = append(t.history, l) + } + } + t.clear(0, orig, t.cols-1, orig+n-1) + t.changed |= ChangedScreen + for i := orig; i <= t.bottom-n; i++ { + t.lines[i], t.lines[i+n] = t.lines[i+n], t.lines[i] + t.dirty[i] = true + t.dirty[i+n] = true + } + + // TODO: selection scroll +} + +func (t *State) modMode(set bool, bit ModeFlag) { + if set { + t.mode |= bit + } else { + t.mode &^= bit + } +} + +func (t *State) setMode(priv bool, set bool, args []int) { + if priv { + for _, a := range args { + switch a { + case 1: // DECCKM - cursor key + t.modMode(set, ModeAppCursor) + case 5: // DECSCNM - reverse video + mode := t.mode + t.modMode(set, ModeReverse) + if mode != t.mode { + // TODO: redraw + } + case 6: // DECOM - origin + if set { + t.cur.state |= cursorOrigin + } else { + t.cur.state &^= cursorOrigin + } + t.moveAbsTo(0, 0) + case 7: // DECAWM - auto wrap + t.modMode(set, ModeWrap) + // IGNORED: + case 0, // error + 2, // DECANM - ANSI/VT52 + 3, // DECCOLM - column + 4, // DECSCLM - scroll + 8, // DECARM - auto repeat + 18, // DECPFF - printer feed + 19, // DECPEX - printer extent + 42, // DECNRCM - national characters + 12: // att610 - start blinking cursor + break + case 25: // DECTCEM - text cursor enable mode + t.modMode(!set, ModeHide) + case 9: // X10 mouse compatibility mode + t.modMode(false, ModeMouseMask) + t.modMode(set, ModeMouseX10) + case 1000: // report button press + t.modMode(false, ModeMouseMask) + t.modMode(set, ModeMouseButton) + case 1002: // report motion on button press + t.modMode(false, ModeMouseMask) + t.modMode(set, ModeMouseMotion) + case 1003: // enable all mouse motions + t.modMode(false, ModeMouseMask) + t.modMode(set, ModeMouseMany) + case 1004: // send focus events to tty + t.modMode(set, ModeFocus) + case 1006: // extended reporting mode + t.modMode(set, ModeMouseSgr) + case 1034: + t.modMode(set, Mode8bit) + case 1049, // = 1047 and 1048 + 47, 1047: + alt := t.mode&ModeAltScreen != 0 + if alt { + t.clear(0, 0, t.cols-1, t.rows-1) + } + if !set || !alt { + t.swapScreen() + } + if a != 1049 { + break + } + fallthrough + case 1048: + if set { + t.saveCursor() + } else { + t.restoreCursor() + } + case 1001: + // mouse highlight mode; can hang the terminal by design when + // implemented + case 1005: + // utf8 mouse mode; will confuse applications not supporting + // utf8 and luit + case 1015: + // urxvt mangled mouse mode; incompatiblt and can be mistaken + // for other control codes + default: + t.logf("unknown private set/reset mode %d\n", a) + } + } + } else { + for _, a := range args { + switch a { + case 0: // Error (ignored) + case 2: // KAM - keyboard action + t.modMode(set, ModeKeyboardLock) + case 4: // IRM - insertion-replacement + t.modMode(set, ModeInsert) + t.logln("insert mode not implemented") + case 12: // SRM - send/receive + t.modMode(set, ModeEcho) + case 20: // LNM - linefeed/newline + t.modMode(set, ModeCRLF) + case 34: + t.logln("right-to-left mode not implemented") + case 96: + t.logln("right-to-left copy mode not implemented") + default: + t.logf("unknown set/reset mode %d\n", a) + } + } + } +} + +func (t *State) setAttr(attr []int) { + if len(attr) == 0 { + attr = []int{0} + } + for i := 0; i < len(attr); i++ { + a := attr[i] + switch a { + case 0: + t.cur.attr.mode &^= attrReverse | attrUnderline | attrBold | attrItalic | attrBlink + t.cur.attr.fg = DefaultFG + t.cur.attr.bg = DefaultBG + case 1: + t.cur.attr.mode |= attrBold + case 3: + t.cur.attr.mode |= attrItalic + case 4: + t.cur.attr.mode |= attrUnderline + case 5, 6: // slow, rapid blink + t.cur.attr.mode |= attrBlink + case 7: + t.cur.attr.mode |= attrReverse + case 21, 22: + t.cur.attr.mode &^= attrBold + case 23: + t.cur.attr.mode &^= attrItalic + case 24: + t.cur.attr.mode &^= attrUnderline + case 25, 26: + t.cur.attr.mode &^= attrBlink + case 27: + t.cur.attr.mode &^= attrReverse + case 38: + if i+2 < len(attr) && attr[i+1] == 5 { + i += 2 + if between(attr[i], 0, 255) { + t.cur.attr.fg = Color(attr[i]) + } else { + t.logf("bad fgcolor %d\n", attr[i]) + } + } else { + t.logf("gfx attr %d unknown\n", a) + } + case 39: + t.cur.attr.fg = DefaultFG + case 48: + if i+2 < len(attr) && attr[i+1] == 5 { + i += 2 + if between(attr[i], 0, 255) { + t.cur.attr.bg = Color(attr[i]) + } else { + t.logf("bad bgcolor %d\n", attr[i]) + } + } else { + t.logf("gfx attr %d unknown\n", a) + } + case 49: + t.cur.attr.bg = DefaultBG + default: + if between(a, 30, 37) { + t.cur.attr.fg = Color(a - 30) + } else if between(a, 40, 47) { + t.cur.attr.bg = Color(a - 40) + } else if between(a, 90, 97) { + t.cur.attr.fg = Color(a - 90 + 8) + } else if between(a, 100, 107) { + t.cur.attr.bg = Color(a - 100 + 8) + } else { + t.logf("gfx attr %d unknown\n", a) + } + } + } +} + +func (t *State) insertBlanks(n int) { + src := t.cur.x + dst := src + n + size := t.cols - dst + t.changed |= ChangedScreen + t.dirty[t.cur.y] = true + + if dst >= t.cols { + t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y) + } else { + copy(t.lines[t.cur.y][dst:dst+size], t.lines[t.cur.y][src:src+size]) + t.clear(src, t.cur.y, dst-1, t.cur.y) + } +} + +func (t *State) insertBlankLines(n int) { + if t.cur.y < t.top || t.cur.y > t.bottom { + return + } + t.scrollDown(t.cur.y, n) +} + +func (t *State) deleteLines(n int) { + if t.cur.y < t.top || t.cur.y > t.bottom { + return + } + t.scrollUp(t.cur.y, n) +} + +func (t *State) deleteChars(n int) { + src := t.cur.x + n + dst := t.cur.x + size := t.cols - src + t.changed |= ChangedScreen + t.dirty[t.cur.y] = true + + if src >= t.cols { + t.clear(t.cur.x, t.cur.y, t.cols-1, t.cur.y) + } else { + copy(t.lines[t.cur.y][dst:dst+size], t.lines[t.cur.y][src:src+size]) + t.clear(t.cols-n, t.cur.y, t.cols-1, t.cur.y) + } +} + +func (t *State) setTitle(title string) { + t.changed |= ChangedTitle + t.title = title +} + +// GlobalCursor returns the current position including the history +func (t *State) GlobalCursor() (int, int) { + cx := t.cur.x + if t.cur.state&cursorWrapNext != 0 { + cx++ + } + return cx, t.cur.y + len(t.history) +} + +// Size returns rows and columns of state +func (t *State) Size() (rows int, cols int) { + return t.rows, t.cols +} + +// String returns a string representation of the terminal output +func (t *State) String() string { + return t.string(false, false, -1, 0) +} + +// StringBeforeCursor returns the terminal output in front of the cursor +func (t *State) StringBeforeCursor() string { + return t.string(false, true, -1, 0) +} + +// UnwrappedStringBeforeCursor returns the terminal output in front of the cursor without the automatic line wrapping +func (t *State) UnwrappedStringBeforeCursor() string { + return t.string(true, true, -1, 0) +} + +// StringToCursorFrom returns the string before the cursor starting from the global position row and col +func (t *State) StringToCursorFrom(row int, col int) string { + return t.string(false, true, row, col) +} + +// UnwrappedStringToCursorFrom returns the string before the cursor starting from the global position row and col without the automatic line wrapping +func (t *State) UnwrappedStringToCursorFrom(row int, col int) string { + return t.string(true, true, row, col) +} + +// matchRune checks if the rune `expected` matches the rune `got` +// it also returns the updated index `i` assuming that we are going backwards in an array of of expected runes (as is done in HasStringBeforeCursor()) +// if `ignoreNewlinesAndSpaces` is true, newlines and spaces that mismatch are skipped over. +func matchRune(got rune, expected []rune, i int, ignoreNewlinesAndSpaces bool) (bool, int) { + exactMatch := got == expected[i] + if exactMatch { + return true, i - 1 + } + if !ignoreNewlinesAndSpaces { + return false, i + } + + if got == ' ' { + return true, i + } + + if expected[i] == ' ' || expected[i] == '\n' || expected[i] == '\r' { + if i == 0 { + return true, -1 + } + return matchRune(got, expected, i-1, true) + } + + return false, i +} + +// HasStringBeforeCursor checks whether `m` matches the string before the cursor position +// If ignoreNewlinesAndSpaces is set to true, newline and space characters are skipped over +func (t *State) HasStringBeforeCursor(m string, ignoreNewlinesAndSpaces bool) bool { + runesToMatch := []rune(m) + // set index of current rune to be matched + i := len(runesToMatch) - 1 + + // quick check if there actually is enough data written to the terminal + if len(runesToMatch) > (len(t.history)+t.cur.y+1)*t.cols { + return false + } + + // if we are in the last column and in `cursorWrapNext` mode, + // the current character is in front of the cursor ... + onWrap := t.cur.state&cursorWrapNext != 0 + x := t.cur.x + if !onWrap { + // ... otherwise go one character back + x-- + } + y := t.cur.y + // first search for matching characters on the current screen + for ; y >= 0 && i >= 0; y-- { + for ; x >= 0 && i >= 0; x-- { + c, _, _ := t.Cell(x, y) + var isOk bool + isOk, i = matchRune(c, runesToMatch, i, ignoreNewlinesAndSpaces) + if !isOk { + return false + } + } + x = t.cols - 1 + } + // then search for matching characters in the scroll buffer (history) + for y = len(t.history) - 1; y >= 0 && i >= 0; y-- { + for x = t.cols - 1; x >= 0 && i >= 0; x-- { + c := t.history[y][x].c + var isOk bool + isOk, i = matchRune(c, runesToMatch, i, ignoreNewlinesAndSpaces) + if !isOk { + return false + } + } + } + + // ensure that we matched all the characters that we were looking for + return i == -1 +} + +func (t *State) string(unwrap bool, toCursor bool, fromRow int, fromCol int) string { + t.Lock() + defer t.Unlock() + + lh := len(t.history) + if fromRow == -1 { + fromRow = lh + } + + var view []rune + x := fromCol + for y := fromRow; y < lh; y++ { + for ; x < t.cols; x++ { + c := t.history[y][x].c + view = append(view, c) + } + x = 0 + if !unwrap { + view = append(view, '\n') + } + fromRow = lh + } + + onWrap := t.cur.state&cursorWrapNext != 0 + curX := t.cur.x + if onWrap { + curX++ + } + for y := fromRow - lh; y < t.rows && (!toCursor || y <= t.cur.y); y++ { + for ; x < t.cols; x++ { + if toCursor && x == curX && y == t.cur.y { + break + } + c, _, _ := t.Cell(x, y) + view = append(view, c) + } + x = 0 + if !unwrap { + view = append(view, '\n') + } + } + + return string(view) +} diff --git a/vendor/github.com/ActiveState/vt10x/str.go b/vendor/github.com/ActiveState/vt10x/str.go new file mode 100644 index 0000000000..c8ca50cbcb --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/str.go @@ -0,0 +1,94 @@ +package vt10x + +import ( + "strconv" + "strings" +) + +// STR sequences are similar to CSI sequences, but have string arguments (and +// as far as I can tell, don't really have a name; STR is the name I took from +// suckless which I imagine comes from rxvt or xterm). +type strEscape struct { + typ rune + buf []rune + args []string +} + +func (s *strEscape) reset() { + s.typ = 0 + s.buf = s.buf[:0] + s.args = nil +} + +func (s *strEscape) put(c rune) { + // TODO: improve allocs with an array backed slice; bench first + if len(s.buf) < 256 { + s.buf = append(s.buf, c) + } + // Going by st, it is better to remain silent when the STR sequence is not + // ended so that it is apparent to users something is wrong. The length sanity + // check ensures we don't absorb the entire stream into memory. + // TODO: see what rxvt or xterm does +} + +func (s *strEscape) parse() { + s.args = strings.Split(string(s.buf), ";") +} + +func (s *strEscape) arg(i, def int) int { + if i >= len(s.args) || i < 0 { + return def + } + i, err := strconv.Atoi(s.args[i]) + if err != nil { + return def + } + return i +} + +func (s *strEscape) argString(i int, def string) string { + if i >= len(s.args) || i < 0 { + return def + } + return s.args[i] +} + +func (t *State) handleSTR() { + s := &t.str + s.parse() + + switch s.typ { + case ']': // OSC - operating system command + switch d := s.arg(0, 0); d { + case 0, 1, 2: + title := s.argString(1, "") + if title != "" { + t.setTitle(title) + } + case 4: // color set + if len(s.args) < 3 { + break + } + // setcolorname(s.arg(1, 0), s.argString(2, "")) + case 104: // color reset + // TODO: complain about invalid color, redraw, etc. + // setcolorname(s.arg(1, 0), nil) + default: + t.logf("unknown OSC command %d\n", d) + // TODO: s.dump() + } + case 'k': // old title set compatibility + title := s.argString(0, "") + if title != "" { + t.setTitle(title) + } + default: + // TODO: Ignore these codes instead of complain? + // 'P': // DSC - device control string + // '_': // APC - application program command + // '^': // PM - privacy message + + t.logf("unhandled STR sequence '%c'\n", s.typ) + // t.str.dump() + } +} diff --git a/vendor/github.com/ActiveState/vt10x/strip.go b/vendor/github.com/ActiveState/vt10x/strip.go new file mode 100644 index 0000000000..232f90dc24 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/strip.go @@ -0,0 +1,55 @@ +package vt10x + +import ( + "unicode" + "unicode/utf8" +) + +type VTStrip struct { + VT +} + +func NewStrip() *VTStrip { + t := &VTStrip{ + VT{ + dest: &State{}, + rwc: nil, + }, + } + t.init() + return t +} + +// Strip returns in with all VT10x escape sequences stripped. An error is +// also returned if one or more of the stripped escape sequences are invalid. +func (t *VTStrip) Strip(in []byte) ([]byte, error) { + var locked bool + defer func() { + if locked { + t.dest.unlock() + } + }() + out := make([]byte, len(in)) + nout := 0 + s := string(in) + for i, w := 0, 0; i < len(s); i += w { + c, sz := utf8.DecodeRuneInString(s[i:]) + w = sz + if c == unicode.ReplacementChar && sz == 1 { + t.dest.logln("invalid utf8 sequence") + break + } + if !locked { + t.dest.lock() + locked = true + } + + // put rune for parsing and update state + isPrintable := t.dest.put(c) + if isPrintable { + copy(out[nout:nout+w], in[i:i+w]) + nout += w + } + } + return out[:nout], nil +} diff --git a/vendor/github.com/ActiveState/vt10x/vt.go b/vendor/github.com/ActiveState/vt10x/vt.go new file mode 100644 index 0000000000..954a51a3f6 --- /dev/null +++ b/vendor/github.com/ActiveState/vt10x/vt.go @@ -0,0 +1,152 @@ +package vt10x + +import ( + "bufio" + "bytes" + "io" + "unicode" + "unicode/utf8" +) + +// VT represents the virtual terminal emulator. +type VT struct { + dest *State + in io.Reader + out io.Writer + rwc io.ReadWriteCloser + br *bufio.Reader +} + +// Create initializes a virtual terminal emulator with the target state +// and io.ReadWriteCloser input. +func Create(state *State, rwc io.ReadWriteCloser) (*VT, error) { + t := &VT{ + dest: state, + rwc: rwc, + } + t.init() + return t, nil +} + +func New(state *State, in io.Reader, out io.Writer) (*VT, error) { + t := &VT{ + dest: state, + in: in, + out: out, + } + t.init() + return t, nil +} + +func (t *VT) init() { + if t.rwc != nil { + t.br = bufio.NewReader(t.rwc) + t.dest.w = t.rwc + } else { + t.br = bufio.NewReader(t.in) + t.dest.w = t.out + } + t.dest.numlock = true + t.dest.state = t.dest.parse + t.dest.cur.attr.fg = DefaultFG + t.dest.cur.attr.bg = DefaultBG + t.Resize(80, 24) + t.dest.reset() +} + +// WriteRune writes a single rune to the terminal +func (t *VT) WriteRune(r rune) { + t.dest.lock() + defer t.dest.unlock() + t.dest.put(r) +} + +// Write parses input and writes terminal changes to state. +func (t *VT) Write(p []byte) (int, error) { + var written int + r := bytes.NewReader(p) + t.dest.lock() + defer t.dest.unlock() + for { + c, sz, err := r.ReadRune() + if err != nil { + if err == io.EOF { + break + } + return written, err + } + written += sz + if c == unicode.ReplacementChar && sz == 1 { + if r.Len() == 0 { + // not enough bytes for a full rune + return written - 1, nil + } + t.dest.logln("invalid utf8 sequence") + continue + } + t.dest.put(c) + } + return written, nil +} + +// Close closes the io.ReadWriteCloser. +func (t *VT) Close() error { + if t.rwc == nil { + return nil + } + return t.rwc.Close() +} + +// Parse blocks on read on pty or io.ReadCloser, then parses sequences until +// buffer empties. State is locked as soon as first rune is read, and unlocked +// when buffer is empty. +// TODO: add tests for expected blocking behavior +func (t *VT) Parse() error { + var locked bool + defer func() { + if locked { + t.dest.unlock() + } + }() + for { + c, sz, err := t.br.ReadRune() + if err != nil { + return err + } + if c == unicode.ReplacementChar && sz == 1 { + t.dest.logln("invalid utf8 sequence") + break + } + if !locked { + t.dest.lock() + locked = true + } + + // put rune for parsing and update state + t.dest.put(c) + + // break if our buffer is empty, or if buffer contains an + // incomplete rune. + n := t.br.Buffered() + if n == 0 || (n < 4 && !fullRuneBuffered(t.br)) { + break + } + } + return nil +} + +func fullRuneBuffered(br *bufio.Reader) bool { + n := br.Buffered() + buf, err := br.Peek(n) + if err != nil { + return false + } + return utf8.FullRune(buf) +} + +// Resize reports new size to pty and updates state. +func (t *VT) Resize(cols, rows int) { + t.dest.lock() + defer t.dest.unlock() + _ = t.dest.resize(cols, rows) +} diff --git a/vendor/github.com/h2non/parth/LICENSE b/vendor/github.com/h2non/parth/LICENSE new file mode 100644 index 0000000000..a5325703a0 --- /dev/null +++ b/vendor/github.com/h2non/parth/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2018 codemodus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/github.com/h2non/parth/README.md b/vendor/github.com/h2non/parth/README.md new file mode 100644 index 0000000000..f8dd8df8e6 --- /dev/null +++ b/vendor/github.com/h2non/parth/README.md @@ -0,0 +1,196 @@ +# parth + + go get -u github.com/h2non/parth + +Package parth provides path parsing for segment unmarshaling and slicing. In +other words, parth provides simple and flexible access to (URL) path parameters. + +Along with string, all basic non-alias types are supported. An interface is +available for implementation by user-defined types. When handling an int, uint, +or float of any size, the first valid value within the specified segment will be +used. + +## Usage + +```go +Variables +func Segment(path string, i int, v interface{}) error +func Sequent(path, key string, v interface{}) error +func Span(path string, i, j int) (string, error) +func SubSeg(path, key string, i int, v interface{}) error +func SubSpan(path, key string, i, j int) (string, error) +type Parth + func New(path string) *Parth + func NewBySpan(path string, i, j int) *Parth + func NewBySubSpan(path, key string, i, j int) *Parth + func (p *Parth) Err() error + func (p *Parth) Segment(i int, v interface{}) + func (p *Parth) Sequent(key string, v interface{}) + func (p *Parth) Span(i, j int) string + func (p *Parth) SubSeg(key string, i int, v interface{}) + func (p *Parth) SubSpan(key string, i, j int) string +type Unmarshaler +``` + +### Setup ("By Index") + +```go +import ( + "fmt" + + "github.com/codemodus/parth" +) + +func handler(w http.ResponseWriter, r *http.Request) { + var s string + if err := parth.Segment(r.URL.Path, 4, &s); err != nil { + fmt.Fprintln(os.Stderr, err) + } + + fmt.Println(r.URL.Path) + fmt.Printf("%v (%T)\n", s, s) + + // Output: + // /some/path/things/42/others/3 + // others (string) +} +``` + +### Setup ("By Key") + +```go +import ( + "fmt" + + "github.com/codemodus/parth" +) + +func handler(w http.ResponseWriter, r *http.Request) { + var i int64 + if err := parth.Sequent(r.URL.Path, "things", &i); err != nil { + fmt.Fprintln(os.Stderr, err) + } + + fmt.Println(r.URL.Path) + fmt.Printf("%v (%T)\n", i, i) + + // Output: + // /some/path/things/42/others/3 + // 42 (int64) +} +``` + +### Setup (Parth Type) + +```go +import ( + "fmt" + + "github.com/codemodus/parth" +) + +func handler(w http.ResponseWriter, r *http.Request) { + var s string + var f float32 + + p := parth.New(r.URL.Path) + p.Segment(2, &s) + p.SubSeg("key", 1, &f) + if err := p.Err(); err != nil { + fmt.Fprintln(os.Stderr, err) + } + + fmt.Println(r.URL.Path) + fmt.Printf("%v (%T)\n", s, s) + fmt.Printf("%v (%T)\n", f, f) + + // Output: + // /zero/one/two/key/four/5.5/six + // two (string) + // 5.5 (float32) +} +``` + +### Setup (Unmarshaler) + +```go +import ( + "fmt" + + "github.com/codemodus/parth" +) + +func handler(w http.ResponseWriter, r *http.Request) { + /* + type mytype []byte + + func (m *mytype) UnmarshalSegment(seg string) error { + *m = []byte(seg) + } + */ + + var m mytype + if err := parth.Segment(r.URL.Path, 4, &m); err != nil { + fmt.Fprintln(os.Stderr, err) + } + + fmt.Println(r.URL.Path) + fmt.Printf("%v == %q (%T)\n", m, m, m) + + // Output: + // /zero/one/two/key/four/5.5/six + // [102 111 117 114] == "four" (mypkg.mytype) +} +``` + +## More Info + +### Keep Using http.HandlerFunc And Minimize context.Context Usage + +The most obvious use case for parth is when working with any URL path such as +the one found at http.Request.URL.Path. parth is fast enough that it can be used +multiple times in place of a single use of similar router-parameter schemes or +even context.Context. There is no need to use an alternate http handler function +definition in order to pass data that is already being passed. The http.Request +type already holds URL data and parth is great at handling it. Additionally, +parth takes care of parsing selected path segments into the types actually +needed. Parth not only does more, it's usually faster and less intrusive than +the alternatives. + +### Indexes + +If an index is negative, the negative count begins with the last segment. +Providing a 0 for the second index is a special case which acts as an alias for +the end of the path. An error is returned if: 1. Any index is out of range of +the path; 2. When there are two indexes, the first index does not precede the +second index. + +### Keys + +If a key is involved, functions will only handle the portion of the path +subsequent to the provided key. An error is returned if the key cannot be found +in the path. + +### First Whole, First Decimal (Restated - Important!) + +When handling an int, uint, or float of any size, the first valid value within +the specified segment will be used. + +## Documentation + +View the [GoDoc](http://godoc.org/github.com/codemodus/parth) + +## Benchmarks + + Go 1.11 + benchmark iter time/iter bytes alloc allocs + --------- ---- --------- ----------- ------ + BenchmarkSegmentString-8 30000000 39.60 ns/op 0 B/op 0 allocs/op + BenchmarkSegmentInt-8 20000000 65.60 ns/op 0 B/op 0 allocs/op + BenchmarkSegmentIntNegIndex-8 20000000 86.60 ns/op 0 B/op 0 allocs/op + BenchmarkSpan-8 100000000 18.20 ns/op 0 B/op 0 allocs/op + BenchmarkStdlibSegmentString-8 5000000 454.00 ns/op 50 B/op 2 allocs/op + BenchmarkStdlibSegmentInt-8 3000000 526.00 ns/op 50 B/op 2 allocs/op + BenchmarkStdlibSpan-8 3000000 518.00 ns/op 69 B/op 2 allocs/op + BenchmarkContextLookupSetGet-8 1000000 1984.00 ns/op 480 B/op 6 allocs/op + diff --git a/vendor/github.com/h2non/parth/parth.go b/vendor/github.com/h2non/parth/parth.go new file mode 100644 index 0000000000..3e64d8feee --- /dev/null +++ b/vendor/github.com/h2non/parth/parth.go @@ -0,0 +1,349 @@ +// Package parth provides path parsing for segment unmarshaling and slicing. In +// other words, parth provides simple and flexible access to (URL) path +// parameters. +// +// Along with string, all basic non-alias types are supported. An interface is +// available for implementation by user-defined types. When handling an int, +// uint, or float of any size, the first valid value within the specified +// segment will be used. +package parth + +import ( + "errors" +) + +// Unmarshaler is the interface implemented by types that can unmarshal a path +// segment representation of themselves. It is safe to assume that the segment +// data will not include slashes. +type Unmarshaler interface { + UnmarshalSegment(string) error +} + +// Err{Name} values facilitate error identification. +var ( + ErrUnknownType = errors.New("unknown type provided") + + ErrFirstSegNotFound = errors.New("first segment not found by index") + ErrLastSegNotFound = errors.New("last segment not found by index") + ErrSegOrderReversed = errors.New("first segment must precede last segment") + ErrKeySegNotFound = errors.New("segment not found by key") + + ErrDataUnparsable = errors.New("data cannot be parsed") +) + +// Segment locates the path segment indicated by the index i and unmarshals it +// into the provided type v. If the index is negative, the negative count +// begins with the last segment. An error is returned if: 1. The type is not a +// pointer to an instance of one of the basic non-alias types and does not +// implement the Unmarshaler interface; 2. The index is out of range of the +// path; 3. The located path segment data cannot be parsed as the provided type +// or if an error is returned when using a provided Unmarshaler implementation. +func Segment(path string, i int, v interface{}) error { //nolint + var err error + + switch v := v.(type) { + case *bool: + *v, err = segmentToBool(path, i) + + case *float32: + var f float64 + f, err = segmentToFloatN(path, i, 32) + *v = float32(f) + + case *float64: + *v, err = segmentToFloatN(path, i, 64) + + case *int: + var n int64 + n, err = segmentToIntN(path, i, 0) + *v = int(n) + + case *int16: + var n int64 + n, err = segmentToIntN(path, i, 16) + *v = int16(n) + + case *int32: + var n int64 + n, err = segmentToIntN(path, i, 32) + *v = int32(n) + + case *int64: + *v, err = segmentToIntN(path, i, 64) + + case *int8: + var n int64 + n, err = segmentToIntN(path, i, 8) + *v = int8(n) + + case *string: + *v, err = segmentToString(path, i) + + case *uint: + var n uint64 + n, err = segmentToUintN(path, i, 0) + *v = uint(n) + + case *uint16: + var n uint64 + n, err = segmentToUintN(path, i, 16) + *v = uint16(n) + + case *uint32: + var n uint64 + n, err = segmentToUintN(path, i, 32) + *v = uint32(n) + + case *uint64: + *v, err = segmentToUintN(path, i, 64) + + case *uint8: + var n uint64 + n, err = segmentToUintN(path, i, 8) + *v = uint8(n) + + case Unmarshaler: + var s string + s, err = segmentToString(path, i) + if err == nil { + err = v.UnmarshalSegment(s) + } + + default: + err = ErrUnknownType + } + + return err +} + +// Sequent is similar to Segment, but uses a key to locate a segment and then +// unmarshal the subsequent segment. It is a simple wrapper over SubSeg with an +// index of 0. +func Sequent(path, key string, v interface{}) error { + return SubSeg(path, key, 0, v) +} + +// Span returns the path segments between two segment indexes i and j including +// the first segment. If an index is negative, the negative count begins with +// the last segment. Providing a 0 for the last index j is a special case which +// acts as an alias for the end of the path. If the first segment does not begin +// with a slash and it is part of the requested span, no slash will be added. An +// error is returned if: 1. Either index is out of range of the path; 2. The +// first index i does not precede the last index j. +func Span(path string, i, j int) (string, error) { + var f, l int + var ok bool + + if i < 0 { + f, ok = segStartIndexFromEnd(path, i) + } else { + f, ok = segStartIndexFromStart(path, i) + } + if !ok { + return "", ErrFirstSegNotFound + } + + if j > 0 { + l, ok = segEndIndexFromStart(path, j) + } else { + l, ok = segEndIndexFromEnd(path, j) + } + if !ok { + return "", ErrLastSegNotFound + } + + if f == l { + return "", nil + } + + if f > l { + return "", ErrSegOrderReversed + } + + return path[f:l], nil +} + +// SubSeg is similar to Segment, but only handles the portion of the path +// subsequent to the provided key. For example, to access the segment +// immediately after a key, an index of 0 should be provided (see Sequent). An +// error is returned if the key cannot be found in the path. +func SubSeg(path, key string, i int, v interface{}) error { //nolint + var err error + + switch v := v.(type) { + case *bool: + *v, err = subSegToBool(path, key, i) + + case *float32: + var f float64 + f, err = subSegToFloatN(path, key, i, 32) + *v = float32(f) + + case *float64: + *v, err = subSegToFloatN(path, key, i, 64) + + case *int: + var n int64 + n, err = subSegToIntN(path, key, i, 0) + *v = int(n) + + case *int16: + var n int64 + n, err = subSegToIntN(path, key, i, 16) + *v = int16(n) + + case *int32: + var n int64 + n, err = subSegToIntN(path, key, i, 32) + *v = int32(n) + + case *int64: + *v, err = subSegToIntN(path, key, i, 64) + + case *int8: + var n int64 + n, err = subSegToIntN(path, key, i, 8) + *v = int8(n) + + case *string: + *v, err = subSegToString(path, key, i) + + case *uint: + var n uint64 + n, err = subSegToUintN(path, key, i, 0) + *v = uint(n) + + case *uint16: + var n uint64 + n, err = subSegToUintN(path, key, i, 16) + *v = uint16(n) + + case *uint32: + var n uint64 + n, err = subSegToUintN(path, key, i, 32) + *v = uint32(n) + + case *uint64: + *v, err = subSegToUintN(path, key, i, 64) + + case *uint8: + var n uint64 + n, err = subSegToUintN(path, key, i, 8) + *v = uint8(n) + + case Unmarshaler: + var s string + s, err = subSegToString(path, key, i) + if err == nil { + err = v.UnmarshalSegment(s) + } + + default: + err = ErrUnknownType + } + + return err +} + +// SubSpan is similar to Span, but only handles the portion of the path +// subsequent to the provided key. An error is returned if the key cannot be +// found in the path. +func SubSpan(path, key string, i, j int) (string, error) { + si, ok := segIndexByKey(path, key) + if !ok { + return "", ErrKeySegNotFound + } + + if i >= 0 { + i++ + } + if j > 0 { + j++ + } + + s, err := Span(path[si:], i, j) + if err != nil { + return "", err + } + + return s, nil +} + +// Parth manages path and error data for processing a single path multiple +// times while error checking only once. Only the first encountered error is +// stored as all subsequent calls to Parth methods that can error are elided. +type Parth struct { + path string + err error +} + +// New constructs a pointer to an instance of Parth around the provided path. +func New(path string) *Parth { + return &Parth{path: path} +} + +// NewBySpan constructs a pointer to an instance of Parth after preprocessing +// the provided path with Span. +func NewBySpan(path string, i, j int) *Parth { + s, err := Span(path, i, j) + return &Parth{s, err} +} + +// NewBySubSpan constructs a pointer to an instance of Parth after +// preprocessing the provided path with SubSpan. +func NewBySubSpan(path, key string, i, j int) *Parth { + s, err := SubSpan(path, key, i, j) + return &Parth{s, err} +} + +// Err returns the first error encountered by the *Parth receiver. +func (p *Parth) Err() error { + return p.err +} + +// Segment operates the same as the package-level function Segment. +func (p *Parth) Segment(i int, v interface{}) { + if p.err != nil { + return + } + + p.err = Segment(p.path, i, v) +} + +// Sequent operates the same as the package-level function Sequent. +func (p *Parth) Sequent(key string, v interface{}) { + p.SubSeg(key, 0, v) +} + +// Span operates the same as the package-level function Span. +func (p *Parth) Span(i, j int) string { + if p.err != nil { + return "" + } + + s, err := Span(p.path, i, j) + p.err = err + + return s +} + +// SubSeg operates the same as the package-level function SubSeg. +func (p *Parth) SubSeg(key string, i int, v interface{}) { + if p.err != nil { + return + } + + p.err = SubSeg(p.path, key, i, v) +} + +// SubSpan operates the same as the package-level function SubSpan. +func (p *Parth) SubSpan(key string, i, j int) string { + if p.err != nil { + return "" + } + + s, err := SubSpan(p.path, key, i, j) + p.err = err + + return s +} diff --git a/vendor/github.com/h2non/parth/segindex.go b/vendor/github.com/h2non/parth/segindex.go new file mode 100644 index 0000000000..77861f99ae --- /dev/null +++ b/vendor/github.com/h2non/parth/segindex.go @@ -0,0 +1,118 @@ +package parth + +func segStartIndexFromStart(path string, seg int) (int, bool) { + if seg < 0 { + return 0, false + } + + for n, ct := 0, 0; n < len(path); n++ { + if n > 0 && path[n] == '/' { + ct++ + } + + if ct == seg { + return n, true + } + } + + return 0, false +} + +func segStartIndexFromEnd(path string, seg int) (int, bool) { + if seg > -1 { + return 0, false + } + + for n, ct := len(path)-1, 0; n >= 0; n-- { + if path[n] == '/' || n == 0 { + ct-- + } + + if ct == seg { + return n, true + } + } + + return 0, false +} + +func segEndIndexFromStart(path string, seg int) (int, bool) { + if seg < 1 { + return 0, false + } + + for n, ct := 0, 0; n < len(path); n++ { + if path[n] == '/' && n > 0 { + ct++ + } + + if ct == seg { + return n, true + } + + if n+1 == len(path) && ct+1 == seg { + return n + 1, true + } + } + + return 0, false +} + +func segEndIndexFromEnd(path string, seg int) (int, bool) { + if seg > 0 { + return 0, false + } + + if seg == 0 { + return len(path), true + } + + if len(path) == 1 && path[0] == '/' { + return 0, true + } + + for n, ct := len(path)-1, 0; n >= 0; n-- { + if n == 0 || path[n] == '/' { + ct-- + } + + if ct == seg { + return n, true + } + + } + + return 0, false +} + +func segIndexByKey(path, key string) (int, bool) { //nolint + if path == "" || key == "" { + return 0, false + } + + for n := 0; n < len(path); n++ { + si, ok := segStartIndexFromStart(path, n) + if !ok { + return 0, false + } + + if len(path[si:]) == len(key)+1 { + if path[si+1:] == key { + return si, true + } + + return 0, false + } + + tmpEI, ok := segStartIndexFromStart(path[si:], 1) + if !ok { + return 0, false + } + + if path[si+1:tmpEI+si] == key || n == 0 && path[0] != '/' && path[si:tmpEI+si] == key { + return si, true + } + } + + return 0, false +} diff --git a/vendor/github.com/h2non/parth/segtostr.go b/vendor/github.com/h2non/parth/segtostr.go new file mode 100644 index 0000000000..6e16a911b0 --- /dev/null +++ b/vendor/github.com/h2non/parth/segtostr.go @@ -0,0 +1,305 @@ +package parth + +import ( + "strconv" + "unicode" +) + +func segmentToBool(path string, i int) (bool, error) { + s, err := segmentToString(path, i) + if err != nil { + return false, err + } + + v, err := strconv.ParseBool(s) + if err != nil { + return false, ErrDataUnparsable + } + + return v, nil +} + +func segmentToFloatN(path string, i, size int) (float64, error) { + ss, err := segmentToString(path, i) + if err != nil { + return 0.0, err + } + + s, ok := firstFloatFromString(ss) + if !ok { + return 0.0, err + } + + v, err := strconv.ParseFloat(s, size) + if err != nil { + return 0.0, ErrDataUnparsable + } + + return v, nil +} + +func segmentToIntN(path string, i, size int) (int64, error) { + ss, err := segmentToString(path, i) + if err != nil { + return 0, err + } + + s, ok := firstIntFromString(ss) + if !ok { + return 0, ErrDataUnparsable + } + + v, err := strconv.ParseInt(s, 10, size) + if err != nil { + return 0, ErrDataUnparsable + } + + return v, nil +} + +func segmentToString(path string, i int) (string, error) { + j := i + 1 + if i < 0 { + i-- + } + + s, err := Span(path, i, j) + if err != nil { + return "", err + } + + if s[0] == '/' { + s = s[1:] + } + + return s, nil +} + +func segmentToUintN(path string, i, size int) (uint64, error) { + ss, err := segmentToString(path, i) + if err != nil { + return 0, err + } + + s, ok := firstUintFromString(ss) + if !ok { + return 0, ErrDataUnparsable + } + + v, err := strconv.ParseUint(s, 10, size) + if err != nil { + return 0, ErrDataUnparsable + } + + return v, nil +} + +func subSegToBool(path, key string, i int) (bool, error) { + s, err := subSegToString(path, key, i) + if err != nil { + return false, err + } + + v, err := strconv.ParseBool(s) + if err != nil { + return false, ErrDataUnparsable + } + + return v, nil +} + +func subSegToFloatN(path, key string, i, size int) (float64, error) { + ss, err := subSegToString(path, key, i) + if err != nil { + return 0.0, err + } + + s, ok := firstFloatFromString(ss) + if !ok { + return 0.0, ErrDataUnparsable + } + + v, err := strconv.ParseFloat(s, size) + if err != nil { + return 0.0, ErrDataUnparsable + } + + return v, nil +} + +func subSegToIntN(path, key string, i, size int) (int64, error) { + ss, err := subSegToString(path, key, i) + if err != nil { + return 0, err + } + + s, ok := firstIntFromString(ss) + if !ok { + return 0, ErrDataUnparsable + } + + v, err := strconv.ParseInt(s, 10, size) + if err != nil { + return 0, ErrDataUnparsable + } + + return v, nil +} + +func subSegToString(path, key string, i int) (string, error) { + ki, ok := segIndexByKey(path, key) + if !ok { + return "", ErrKeySegNotFound + } + + i++ + + s, err := segmentToString(path[ki:], i) + if err != nil { + return "", err + } + + return s, nil +} + +func subSegToUintN(path, key string, i, size int) (uint64, error) { + ss, err := subSegToString(path, key, i) + if err != nil { + return 0, err + } + + s, ok := firstUintFromString(ss) + if !ok { + return 0, ErrDataUnparsable + } + + v, err := strconv.ParseUint(s, 10, size) + if err != nil { + return 0, ErrDataUnparsable + } + + return v, nil +} + +func firstUintFromString(s string) (string, bool) { + ind, l := 0, 0 + + for n := 0; n < len(s); n++ { + if unicode.IsDigit(rune(s[n])) { + if l == 0 { + ind = n + } + + l++ + } else { + if l == 0 && s[n] == '.' { + if n+1 < len(s) && unicode.IsDigit(rune(s[n+1])) { + return "0", true + } + + break + } + + if l > 0 { + break + } + } + } + + if l == 0 { + return "", false + } + + return s[ind : ind+l], true +} + +func firstIntFromString(s string) (string, bool) { //nolint + ind, l := 0, 0 + + for n := 0; n < len(s); n++ { + if unicode.IsDigit(rune(s[n])) { + if l == 0 { + ind = n + } + + l++ + } else if s[n] == '-' { + if l == 0 { + ind = n + l++ + } else { + break + } + } else { + if l == 0 && s[n] == '.' { + if n+1 < len(s) && unicode.IsDigit(rune(s[n+1])) { + return "0", true + } + + break + } + + if l > 0 { + break + } + } + } + + if l == 0 { + return "", false + } + + return s[ind : ind+l], true +} + +func firstFloatFromString(s string) (string, bool) { //nolint + c, ind, l := 0, 0, 0 + + for n := 0; n < len(s); n++ { + if unicode.IsDigit(rune(s[n])) { + if l == 0 { + ind = n + } + + l++ + } else if s[n] == '-' { + if l == 0 { + ind = n + l++ + } else { + break + } + } else if s[n] == '.' { + if l == 0 { + ind = n + } + + if c > 0 { + break + } + + l++ + c++ + } else if s[n] == 'e' && l > 0 && n+1 < len(s) && s[n+1] == '+' { + l++ + } else if s[n] == '+' && l > 0 && s[n-1] == 'e' { + if n+1 < len(s) && unicode.IsDigit(rune(s[n+1])) { + l++ + continue + } + + l-- + break + } else { + if l > 0 { + break + } + } + } + + if l == 0 || s[ind:ind+l] == "." { + return "", false + } + + return s[ind : ind+l], true +} diff --git a/vendor/github.com/kr/pty/.gitignore b/vendor/github.com/kr/pty/.gitignore new file mode 100644 index 0000000000..1f0a99f2f2 --- /dev/null +++ b/vendor/github.com/kr/pty/.gitignore @@ -0,0 +1,4 @@ +[568].out +_go* +_test* +_obj diff --git a/vendor/github.com/kr/pty/License b/vendor/github.com/kr/pty/License new file mode 100644 index 0000000000..6b7558b6b4 --- /dev/null +++ b/vendor/github.com/kr/pty/License @@ -0,0 +1,23 @@ +Copyright (c) 2011 Keith Rarick + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall +be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/kr/pty/README.md b/vendor/github.com/kr/pty/README.md new file mode 100644 index 0000000000..f9bb002e03 --- /dev/null +++ b/vendor/github.com/kr/pty/README.md @@ -0,0 +1,100 @@ +# pty + +Pty is a Go package for using unix pseudo-terminals. + +## Install + + go get github.com/kr/pty + +## Example + +### Command + +```go +package main + +import ( + "github.com/kr/pty" + "io" + "os" + "os/exec" +) + +func main() { + c := exec.Command("grep", "--color=auto", "bar") + f, err := pty.Start(c) + if err != nil { + panic(err) + } + + go func() { + f.Write([]byte("foo\n")) + f.Write([]byte("bar\n")) + f.Write([]byte("baz\n")) + f.Write([]byte{4}) // EOT + }() + io.Copy(os.Stdout, f) +} +``` + +### Shell + +```go +package main + +import ( + "io" + "log" + "os" + "os/exec" + "os/signal" + "syscall" + + "github.com/kr/pty" + "golang.org/x/crypto/ssh/terminal" +) + +func test() error { + // Create arbitrary command. + c := exec.Command("bash") + + // Start the command with a pty. + ptmx, err := pty.Start(c) + if err != nil { + return err + } + // Make sure to close the pty at the end. + defer func() { _ = ptmx.Close() }() // Best effort. + + // Handle pty size. + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGWINCH) + go func() { + for range ch { + if err := pty.InheritSize(os.Stdin, ptmx); err != nil { + log.Printf("error resizing pty: %s", err) + } + } + }() + ch <- syscall.SIGWINCH // Initial resize. + + // Set stdin in raw mode. + oldState, err := terminal.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort. + + // Copy stdin to the pty and the pty to stdout. + go func() { _, _ = io.Copy(ptmx, os.Stdin) }() + _, _ = io.Copy(os.Stdout, ptmx) + + return nil +} + +func main() { + if err := test(); err != nil { + log.Fatal(err) + } +} +``` diff --git a/vendor/github.com/kr/pty/doc.go b/vendor/github.com/kr/pty/doc.go new file mode 100644 index 0000000000..190cfbea92 --- /dev/null +++ b/vendor/github.com/kr/pty/doc.go @@ -0,0 +1,16 @@ +// Package pty provides functions for working with Unix terminals. +package pty + +import ( + "errors" + "os" +) + +// ErrUnsupported is returned if a function is not +// available on the current platform. +var ErrUnsupported = errors.New("unsupported") + +// Opens a pty and its corresponding tty. +func Open() (pty, tty *os.File, err error) { + return open() +} diff --git a/vendor/github.com/kr/pty/ioctl.go b/vendor/github.com/kr/pty/ioctl.go new file mode 100644 index 0000000000..c57c19e7e2 --- /dev/null +++ b/vendor/github.com/kr/pty/ioctl.go @@ -0,0 +1,13 @@ +// +build !windows + +package pty + +import "syscall" + +func ioctl(fd, cmd, ptr uintptr) error { + _, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, ptr) + if e != 0 { + return e + } + return nil +} diff --git a/vendor/github.com/kr/pty/ioctl_bsd.go b/vendor/github.com/kr/pty/ioctl_bsd.go new file mode 100644 index 0000000000..73b12c53cf --- /dev/null +++ b/vendor/github.com/kr/pty/ioctl_bsd.go @@ -0,0 +1,39 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package pty + +// from +const ( + _IOC_VOID uintptr = 0x20000000 + _IOC_OUT uintptr = 0x40000000 + _IOC_IN uintptr = 0x80000000 + _IOC_IN_OUT uintptr = _IOC_OUT | _IOC_IN + _IOC_DIRMASK = _IOC_VOID | _IOC_OUT | _IOC_IN + + _IOC_PARAM_SHIFT = 13 + _IOC_PARAM_MASK = (1 << _IOC_PARAM_SHIFT) - 1 +) + +func _IOC_PARM_LEN(ioctl uintptr) uintptr { + return (ioctl >> 16) & _IOC_PARAM_MASK +} + +func _IOC(inout uintptr, group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return inout | (param_len&_IOC_PARAM_MASK)<<16 | uintptr(group)<<8 | ioctl_num +} + +func _IO(group byte, ioctl_num uintptr) uintptr { + return _IOC(_IOC_VOID, group, ioctl_num, 0) +} + +func _IOR(group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return _IOC(_IOC_OUT, group, ioctl_num, param_len) +} + +func _IOW(group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return _IOC(_IOC_IN, group, ioctl_num, param_len) +} + +func _IOWR(group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return _IOC(_IOC_IN_OUT, group, ioctl_num, param_len) +} diff --git a/vendor/github.com/kr/pty/mktypes.bash b/vendor/github.com/kr/pty/mktypes.bash new file mode 100644 index 0000000000..82ee16721c --- /dev/null +++ b/vendor/github.com/kr/pty/mktypes.bash @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +GOOSARCH="${GOOS}_${GOARCH}" +case "$GOOSARCH" in +_* | *_ | _) + echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2 + exit 1 + ;; +esac + +GODEFS="go tool cgo -godefs" + +$GODEFS types.go |gofmt > ztypes_$GOARCH.go + +case $GOOS in +freebsd|dragonfly|openbsd) + $GODEFS types_$GOOS.go |gofmt > ztypes_$GOOSARCH.go + ;; +esac diff --git a/vendor/github.com/kr/pty/pty_darwin.go b/vendor/github.com/kr/pty/pty_darwin.go new file mode 100644 index 0000000000..6344b6b0ef --- /dev/null +++ b/vendor/github.com/kr/pty/pty_darwin.go @@ -0,0 +1,65 @@ +package pty + +import ( + "errors" + "os" + "syscall" + "unsafe" +) + +func open() (pty, tty *os.File, err error) { + pFD, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|syscall.O_CLOEXEC, 0) + if err != nil { + return nil, nil, err + } + p := os.NewFile(uintptr(pFD), "/dev/ptmx") + // In case of error after this point, make sure we close the ptmx fd. + defer func() { + if err != nil { + _ = p.Close() // Best effort. + } + }() + + sname, err := ptsname(p) + if err != nil { + return nil, nil, err + } + + if err := grantpt(p); err != nil { + return nil, nil, err + } + + if err := unlockpt(p); err != nil { + return nil, nil, err + } + + t, err := os.OpenFile(sname, os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + return p, t, nil +} + +func ptsname(f *os.File) (string, error) { + n := make([]byte, _IOC_PARM_LEN(syscall.TIOCPTYGNAME)) + + err := ioctl(f.Fd(), syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n[0]))) + if err != nil { + return "", err + } + + for i, c := range n { + if c == 0 { + return string(n[:i]), nil + } + } + return "", errors.New("TIOCPTYGNAME string not NUL-terminated") +} + +func grantpt(f *os.File) error { + return ioctl(f.Fd(), syscall.TIOCPTYGRANT, 0) +} + +func unlockpt(f *os.File) error { + return ioctl(f.Fd(), syscall.TIOCPTYUNLK, 0) +} diff --git a/vendor/github.com/kr/pty/pty_dragonfly.go b/vendor/github.com/kr/pty/pty_dragonfly.go new file mode 100644 index 0000000000..b7d1f20f29 --- /dev/null +++ b/vendor/github.com/kr/pty/pty_dragonfly.go @@ -0,0 +1,80 @@ +package pty + +import ( + "errors" + "os" + "strings" + "syscall" + "unsafe" +) + +// same code as pty_darwin.go +func open() (pty, tty *os.File, err error) { + p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + // In case of error after this point, make sure we close the ptmx fd. + defer func() { + if err != nil { + _ = p.Close() // Best effort. + } + }() + + sname, err := ptsname(p) + if err != nil { + return nil, nil, err + } + + if err := grantpt(p); err != nil { + return nil, nil, err + } + + if err := unlockpt(p); err != nil { + return nil, nil, err + } + + t, err := os.OpenFile(sname, os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + return p, t, nil +} + +func grantpt(f *os.File) error { + _, err := isptmaster(f.Fd()) + return err +} + +func unlockpt(f *os.File) error { + _, err := isptmaster(f.Fd()) + return err +} + +func isptmaster(fd uintptr) (bool, error) { + err := ioctl(fd, syscall.TIOCISPTMASTER, 0) + return err == nil, err +} + +var ( + emptyFiodgnameArg fiodgnameArg + ioctl_FIODNAME = _IOW('f', 120, unsafe.Sizeof(emptyFiodgnameArg)) +) + +func ptsname(f *os.File) (string, error) { + name := make([]byte, _C_SPECNAMELEN) + fa := fiodgnameArg{Name: (*byte)(unsafe.Pointer(&name[0])), Len: _C_SPECNAMELEN, Pad_cgo_0: [4]byte{0, 0, 0, 0}} + + err := ioctl(f.Fd(), ioctl_FIODNAME, uintptr(unsafe.Pointer(&fa))) + if err != nil { + return "", err + } + + for i, c := range name { + if c == 0 { + s := "/dev/" + string(name[:i]) + return strings.Replace(s, "ptm", "pts", -1), nil + } + } + return "", errors.New("TIOCPTYGNAME string not NUL-terminated") +} diff --git a/vendor/github.com/kr/pty/pty_freebsd.go b/vendor/github.com/kr/pty/pty_freebsd.go new file mode 100644 index 0000000000..63b6d91337 --- /dev/null +++ b/vendor/github.com/kr/pty/pty_freebsd.go @@ -0,0 +1,78 @@ +package pty + +import ( + "errors" + "os" + "syscall" + "unsafe" +) + +func posixOpenpt(oflag int) (fd int, err error) { + r0, _, e1 := syscall.Syscall(syscall.SYS_POSIX_OPENPT, uintptr(oflag), 0, 0) + fd = int(r0) + if e1 != 0 { + err = e1 + } + return fd, err +} + +func open() (pty, tty *os.File, err error) { + fd, err := posixOpenpt(syscall.O_RDWR | syscall.O_CLOEXEC) + if err != nil { + return nil, nil, err + } + p := os.NewFile(uintptr(fd), "/dev/pts") + // In case of error after this point, make sure we close the pts fd. + defer func() { + if err != nil { + _ = p.Close() // Best effort. + } + }() + + sname, err := ptsname(p) + if err != nil { + return nil, nil, err + } + + t, err := os.OpenFile("/dev/"+sname, os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + return p, t, nil +} + +func isptmaster(fd uintptr) (bool, error) { + err := ioctl(fd, syscall.TIOCPTMASTER, 0) + return err == nil, err +} + +var ( + emptyFiodgnameArg fiodgnameArg + ioctlFIODGNAME = _IOW('f', 120, unsafe.Sizeof(emptyFiodgnameArg)) +) + +func ptsname(f *os.File) (string, error) { + master, err := isptmaster(f.Fd()) + if err != nil { + return "", err + } + if !master { + return "", syscall.EINVAL + } + + const n = _C_SPECNAMELEN + 1 + var ( + buf = make([]byte, n) + arg = fiodgnameArg{Len: n, Buf: (*byte)(unsafe.Pointer(&buf[0]))} + ) + if err := ioctl(f.Fd(), ioctlFIODGNAME, uintptr(unsafe.Pointer(&arg))); err != nil { + return "", err + } + + for i, c := range buf { + if c == 0 { + return string(buf[:i]), nil + } + } + return "", errors.New("FIODGNAME string not NUL-terminated") +} diff --git a/vendor/github.com/kr/pty/pty_linux.go b/vendor/github.com/kr/pty/pty_linux.go new file mode 100644 index 0000000000..296dd21298 --- /dev/null +++ b/vendor/github.com/kr/pty/pty_linux.go @@ -0,0 +1,51 @@ +package pty + +import ( + "os" + "strconv" + "syscall" + "unsafe" +) + +func open() (pty, tty *os.File, err error) { + p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + // In case of error after this point, make sure we close the ptmx fd. + defer func() { + if err != nil { + _ = p.Close() // Best effort. + } + }() + + sname, err := ptsname(p) + if err != nil { + return nil, nil, err + } + + if err := unlockpt(p); err != nil { + return nil, nil, err + } + + t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0) + if err != nil { + return nil, nil, err + } + return p, t, nil +} + +func ptsname(f *os.File) (string, error) { + var n _C_uint + err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))) + if err != nil { + return "", err + } + return "/dev/pts/" + strconv.Itoa(int(n)), nil +} + +func unlockpt(f *os.File) error { + var u _C_int + // use TIOCSPTLCK with a zero valued arg to clear the slave pty lock + return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))) +} diff --git a/vendor/github.com/kr/pty/pty_openbsd.go b/vendor/github.com/kr/pty/pty_openbsd.go new file mode 100644 index 0000000000..6e7aeae7c0 --- /dev/null +++ b/vendor/github.com/kr/pty/pty_openbsd.go @@ -0,0 +1,33 @@ +package pty + +import ( + "os" + "syscall" + "unsafe" +) + +func open() (pty, tty *os.File, err error) { + /* + * from ptm(4): + * The PTMGET command allocates a free pseudo terminal, changes its + * ownership to the caller, revokes the access privileges for all previous + * users, opens the file descriptors for the master and slave devices and + * returns them to the caller in struct ptmget. + */ + + p, err := os.OpenFile("/dev/ptm", os.O_RDWR|syscall.O_CLOEXEC, 0) + if err != nil { + return nil, nil, err + } + defer p.Close() + + var ptm ptmget + if err := ioctl(p.Fd(), uintptr(ioctl_PTMGET), uintptr(unsafe.Pointer(&ptm))); err != nil { + return nil, nil, err + } + + pty = os.NewFile(uintptr(ptm.Cfd), "/dev/ptm") + tty = os.NewFile(uintptr(ptm.Sfd), "/dev/ptm") + + return pty, tty, nil +} diff --git a/vendor/github.com/kr/pty/pty_unsupported.go b/vendor/github.com/kr/pty/pty_unsupported.go new file mode 100644 index 0000000000..9a3e721bc4 --- /dev/null +++ b/vendor/github.com/kr/pty/pty_unsupported.go @@ -0,0 +1,11 @@ +// +build !linux,!darwin,!freebsd,!dragonfly,!openbsd + +package pty + +import ( + "os" +) + +func open() (pty, tty *os.File, err error) { + return nil, nil, ErrUnsupported +} diff --git a/vendor/github.com/kr/pty/run.go b/vendor/github.com/kr/pty/run.go new file mode 100644 index 0000000000..baecca8af9 --- /dev/null +++ b/vendor/github.com/kr/pty/run.go @@ -0,0 +1,34 @@ +// +build !windows + +package pty + +import ( + "os" + "os/exec" + "syscall" +) + +// Start assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout, +// and c.Stderr, calls c.Start, and returns the File of the tty's +// corresponding pty. +func Start(c *exec.Cmd) (pty *os.File, err error) { + pty, tty, err := Open() + if err != nil { + return nil, err + } + defer tty.Close() + c.Stdout = tty + c.Stdin = tty + c.Stderr = tty + if c.SysProcAttr == nil { + c.SysProcAttr = &syscall.SysProcAttr{} + } + c.SysProcAttr.Setctty = true + c.SysProcAttr.Setsid = true + err = c.Start() + if err != nil { + pty.Close() + return nil, err + } + return pty, err +} diff --git a/vendor/github.com/kr/pty/util.go b/vendor/github.com/kr/pty/util.go new file mode 100644 index 0000000000..68a8584cfe --- /dev/null +++ b/vendor/github.com/kr/pty/util.go @@ -0,0 +1,64 @@ +// +build !windows + +package pty + +import ( + "os" + "syscall" + "unsafe" +) + +// InheritSize applies the terminal size of master to slave. This should be run +// in a signal handler for syscall.SIGWINCH to automatically resize the slave when +// the master receives a window size change notification. +func InheritSize(master, slave *os.File) error { + size, err := GetsizeFull(master) + if err != nil { + return err + } + err = Setsize(slave, size) + if err != nil { + return err + } + return nil +} + +// Setsize resizes t to s. +func Setsize(t *os.File, ws *Winsize) error { + return windowRectCall(ws, t.Fd(), syscall.TIOCSWINSZ) +} + +// GetsizeFull returns the full terminal size description. +func GetsizeFull(t *os.File) (size *Winsize, err error) { + var ws Winsize + err = windowRectCall(&ws, t.Fd(), syscall.TIOCGWINSZ) + return &ws, err +} + +// Getsize returns the number of rows (lines) and cols (positions +// in each line) in terminal t. +func Getsize(t *os.File) (rows, cols int, err error) { + ws, err := GetsizeFull(t) + return int(ws.Rows), int(ws.Cols), err +} + +// Winsize describes the terminal size. +type Winsize struct { + Rows uint16 // ws_row: Number of rows (in cells) + Cols uint16 // ws_col: Number of columns (in cells) + X uint16 // ws_xpixel: Width in pixels + Y uint16 // ws_ypixel: Height in pixels +} + +func windowRectCall(ws *Winsize, fd, a2 uintptr) error { + _, _, errno := syscall.Syscall( + syscall.SYS_IOCTL, + fd, + a2, + uintptr(unsafe.Pointer(ws)), + ) + if errno != 0 { + return syscall.Errno(errno) + } + return nil +} diff --git a/vendor/github.com/kr/pty/ztypes_386.go b/vendor/github.com/kr/pty/ztypes_386.go new file mode 100644 index 0000000000..ff0b8fd838 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_386.go @@ -0,0 +1,9 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/kr/pty/ztypes_amd64.go b/vendor/github.com/kr/pty/ztypes_amd64.go new file mode 100644 index 0000000000..ff0b8fd838 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_amd64.go @@ -0,0 +1,9 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/kr/pty/ztypes_arm.go b/vendor/github.com/kr/pty/ztypes_arm.go new file mode 100644 index 0000000000..ff0b8fd838 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_arm.go @@ -0,0 +1,9 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/kr/pty/ztypes_arm64.go b/vendor/github.com/kr/pty/ztypes_arm64.go new file mode 100644 index 0000000000..6c29a4b918 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_arm64.go @@ -0,0 +1,11 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +// +build arm64 + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/kr/pty/ztypes_dragonfly_amd64.go b/vendor/github.com/kr/pty/ztypes_dragonfly_amd64.go new file mode 100644 index 0000000000..6b0ba037f8 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_dragonfly_amd64.go @@ -0,0 +1,14 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_dragonfly.go + +package pty + +const ( + _C_SPECNAMELEN = 0x3f +) + +type fiodgnameArg struct { + Name *byte + Len uint32 + Pad_cgo_0 [4]byte +} diff --git a/vendor/github.com/kr/pty/ztypes_freebsd_386.go b/vendor/github.com/kr/pty/ztypes_freebsd_386.go new file mode 100644 index 0000000000..d9975374e3 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_freebsd_386.go @@ -0,0 +1,13 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package pty + +const ( + _C_SPECNAMELEN = 0x3f +) + +type fiodgnameArg struct { + Len int32 + Buf *byte +} diff --git a/vendor/github.com/kr/pty/ztypes_freebsd_amd64.go b/vendor/github.com/kr/pty/ztypes_freebsd_amd64.go new file mode 100644 index 0000000000..5fa102fcdf --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_freebsd_amd64.go @@ -0,0 +1,14 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package pty + +const ( + _C_SPECNAMELEN = 0x3f +) + +type fiodgnameArg struct { + Len int32 + Pad_cgo_0 [4]byte + Buf *byte +} diff --git a/vendor/github.com/kr/pty/ztypes_freebsd_arm.go b/vendor/github.com/kr/pty/ztypes_freebsd_arm.go new file mode 100644 index 0000000000..d9975374e3 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_freebsd_arm.go @@ -0,0 +1,13 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package pty + +const ( + _C_SPECNAMELEN = 0x3f +) + +type fiodgnameArg struct { + Len int32 + Buf *byte +} diff --git a/vendor/github.com/kr/pty/ztypes_mipsx.go b/vendor/github.com/kr/pty/ztypes_mipsx.go new file mode 100644 index 0000000000..f0ce74086a --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_mipsx.go @@ -0,0 +1,12 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +// +build linux +// +build mips mipsle mips64 mips64le + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/kr/pty/ztypes_openbsd_amd64.go b/vendor/github.com/kr/pty/ztypes_openbsd_amd64.go new file mode 100644 index 0000000000..e67051688f --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_openbsd_amd64.go @@ -0,0 +1,13 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_openbsd.go + +package pty + +type ptmget struct { + Cfd int32 + Sfd int32 + Cn [16]int8 + Sn [16]int8 +} + +var ioctl_PTMGET = 0x40287401 diff --git a/vendor/github.com/kr/pty/ztypes_ppc64.go b/vendor/github.com/kr/pty/ztypes_ppc64.go new file mode 100644 index 0000000000..4e1af84312 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_ppc64.go @@ -0,0 +1,11 @@ +// +build ppc64 + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/kr/pty/ztypes_ppc64le.go b/vendor/github.com/kr/pty/ztypes_ppc64le.go new file mode 100644 index 0000000000..e6780f4e23 --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_ppc64le.go @@ -0,0 +1,11 @@ +// +build ppc64le + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/kr/pty/ztypes_s390x.go b/vendor/github.com/kr/pty/ztypes_s390x.go new file mode 100644 index 0000000000..a7452b61cb --- /dev/null +++ b/vendor/github.com/kr/pty/ztypes_s390x.go @@ -0,0 +1,11 @@ +// +build s390x + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/vendor/github.com/stretchr/testify/LICENSE b/vendor/github.com/stretchr/testify/LICENSE new file mode 100644 index 0000000000..4b0421cf9e --- /dev/null +++ b/vendor/github.com/stretchr/testify/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2012-2020 Mat Ryer, Tyler Bunnell and contributors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/stretchr/testify/assert/assertion_compare.go b/vendor/github.com/stretchr/testify/assert/assertion_compare.go new file mode 100644 index 0000000000..ffb24e8e31 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_compare.go @@ -0,0 +1,495 @@ +package assert + +import ( + "bytes" + "fmt" + "reflect" + "time" +) + +// Deprecated: CompareType has only ever been for internal use and has accidentally been published since v1.6.0. Do not use it. +type CompareType = compareResult + +type compareResult int + +const ( + compareLess compareResult = iota - 1 + compareEqual + compareGreater +) + +var ( + intType = reflect.TypeOf(int(1)) + int8Type = reflect.TypeOf(int8(1)) + int16Type = reflect.TypeOf(int16(1)) + int32Type = reflect.TypeOf(int32(1)) + int64Type = reflect.TypeOf(int64(1)) + + uintType = reflect.TypeOf(uint(1)) + uint8Type = reflect.TypeOf(uint8(1)) + uint16Type = reflect.TypeOf(uint16(1)) + uint32Type = reflect.TypeOf(uint32(1)) + uint64Type = reflect.TypeOf(uint64(1)) + + uintptrType = reflect.TypeOf(uintptr(1)) + + float32Type = reflect.TypeOf(float32(1)) + float64Type = reflect.TypeOf(float64(1)) + + stringType = reflect.TypeOf("") + + timeType = reflect.TypeOf(time.Time{}) + bytesType = reflect.TypeOf([]byte{}) +) + +func compare(obj1, obj2 interface{}, kind reflect.Kind) (compareResult, bool) { + obj1Value := reflect.ValueOf(obj1) + obj2Value := reflect.ValueOf(obj2) + + // throughout this switch we try and avoid calling .Convert() if possible, + // as this has a pretty big performance impact + switch kind { + case reflect.Int: + { + intobj1, ok := obj1.(int) + if !ok { + intobj1 = obj1Value.Convert(intType).Interface().(int) + } + intobj2, ok := obj2.(int) + if !ok { + intobj2 = obj2Value.Convert(intType).Interface().(int) + } + if intobj1 > intobj2 { + return compareGreater, true + } + if intobj1 == intobj2 { + return compareEqual, true + } + if intobj1 < intobj2 { + return compareLess, true + } + } + case reflect.Int8: + { + int8obj1, ok := obj1.(int8) + if !ok { + int8obj1 = obj1Value.Convert(int8Type).Interface().(int8) + } + int8obj2, ok := obj2.(int8) + if !ok { + int8obj2 = obj2Value.Convert(int8Type).Interface().(int8) + } + if int8obj1 > int8obj2 { + return compareGreater, true + } + if int8obj1 == int8obj2 { + return compareEqual, true + } + if int8obj1 < int8obj2 { + return compareLess, true + } + } + case reflect.Int16: + { + int16obj1, ok := obj1.(int16) + if !ok { + int16obj1 = obj1Value.Convert(int16Type).Interface().(int16) + } + int16obj2, ok := obj2.(int16) + if !ok { + int16obj2 = obj2Value.Convert(int16Type).Interface().(int16) + } + if int16obj1 > int16obj2 { + return compareGreater, true + } + if int16obj1 == int16obj2 { + return compareEqual, true + } + if int16obj1 < int16obj2 { + return compareLess, true + } + } + case reflect.Int32: + { + int32obj1, ok := obj1.(int32) + if !ok { + int32obj1 = obj1Value.Convert(int32Type).Interface().(int32) + } + int32obj2, ok := obj2.(int32) + if !ok { + int32obj2 = obj2Value.Convert(int32Type).Interface().(int32) + } + if int32obj1 > int32obj2 { + return compareGreater, true + } + if int32obj1 == int32obj2 { + return compareEqual, true + } + if int32obj1 < int32obj2 { + return compareLess, true + } + } + case reflect.Int64: + { + int64obj1, ok := obj1.(int64) + if !ok { + int64obj1 = obj1Value.Convert(int64Type).Interface().(int64) + } + int64obj2, ok := obj2.(int64) + if !ok { + int64obj2 = obj2Value.Convert(int64Type).Interface().(int64) + } + if int64obj1 > int64obj2 { + return compareGreater, true + } + if int64obj1 == int64obj2 { + return compareEqual, true + } + if int64obj1 < int64obj2 { + return compareLess, true + } + } + case reflect.Uint: + { + uintobj1, ok := obj1.(uint) + if !ok { + uintobj1 = obj1Value.Convert(uintType).Interface().(uint) + } + uintobj2, ok := obj2.(uint) + if !ok { + uintobj2 = obj2Value.Convert(uintType).Interface().(uint) + } + if uintobj1 > uintobj2 { + return compareGreater, true + } + if uintobj1 == uintobj2 { + return compareEqual, true + } + if uintobj1 < uintobj2 { + return compareLess, true + } + } + case reflect.Uint8: + { + uint8obj1, ok := obj1.(uint8) + if !ok { + uint8obj1 = obj1Value.Convert(uint8Type).Interface().(uint8) + } + uint8obj2, ok := obj2.(uint8) + if !ok { + uint8obj2 = obj2Value.Convert(uint8Type).Interface().(uint8) + } + if uint8obj1 > uint8obj2 { + return compareGreater, true + } + if uint8obj1 == uint8obj2 { + return compareEqual, true + } + if uint8obj1 < uint8obj2 { + return compareLess, true + } + } + case reflect.Uint16: + { + uint16obj1, ok := obj1.(uint16) + if !ok { + uint16obj1 = obj1Value.Convert(uint16Type).Interface().(uint16) + } + uint16obj2, ok := obj2.(uint16) + if !ok { + uint16obj2 = obj2Value.Convert(uint16Type).Interface().(uint16) + } + if uint16obj1 > uint16obj2 { + return compareGreater, true + } + if uint16obj1 == uint16obj2 { + return compareEqual, true + } + if uint16obj1 < uint16obj2 { + return compareLess, true + } + } + case reflect.Uint32: + { + uint32obj1, ok := obj1.(uint32) + if !ok { + uint32obj1 = obj1Value.Convert(uint32Type).Interface().(uint32) + } + uint32obj2, ok := obj2.(uint32) + if !ok { + uint32obj2 = obj2Value.Convert(uint32Type).Interface().(uint32) + } + if uint32obj1 > uint32obj2 { + return compareGreater, true + } + if uint32obj1 == uint32obj2 { + return compareEqual, true + } + if uint32obj1 < uint32obj2 { + return compareLess, true + } + } + case reflect.Uint64: + { + uint64obj1, ok := obj1.(uint64) + if !ok { + uint64obj1 = obj1Value.Convert(uint64Type).Interface().(uint64) + } + uint64obj2, ok := obj2.(uint64) + if !ok { + uint64obj2 = obj2Value.Convert(uint64Type).Interface().(uint64) + } + if uint64obj1 > uint64obj2 { + return compareGreater, true + } + if uint64obj1 == uint64obj2 { + return compareEqual, true + } + if uint64obj1 < uint64obj2 { + return compareLess, true + } + } + case reflect.Float32: + { + float32obj1, ok := obj1.(float32) + if !ok { + float32obj1 = obj1Value.Convert(float32Type).Interface().(float32) + } + float32obj2, ok := obj2.(float32) + if !ok { + float32obj2 = obj2Value.Convert(float32Type).Interface().(float32) + } + if float32obj1 > float32obj2 { + return compareGreater, true + } + if float32obj1 == float32obj2 { + return compareEqual, true + } + if float32obj1 < float32obj2 { + return compareLess, true + } + } + case reflect.Float64: + { + float64obj1, ok := obj1.(float64) + if !ok { + float64obj1 = obj1Value.Convert(float64Type).Interface().(float64) + } + float64obj2, ok := obj2.(float64) + if !ok { + float64obj2 = obj2Value.Convert(float64Type).Interface().(float64) + } + if float64obj1 > float64obj2 { + return compareGreater, true + } + if float64obj1 == float64obj2 { + return compareEqual, true + } + if float64obj1 < float64obj2 { + return compareLess, true + } + } + case reflect.String: + { + stringobj1, ok := obj1.(string) + if !ok { + stringobj1 = obj1Value.Convert(stringType).Interface().(string) + } + stringobj2, ok := obj2.(string) + if !ok { + stringobj2 = obj2Value.Convert(stringType).Interface().(string) + } + if stringobj1 > stringobj2 { + return compareGreater, true + } + if stringobj1 == stringobj2 { + return compareEqual, true + } + if stringobj1 < stringobj2 { + return compareLess, true + } + } + // Check for known struct types we can check for compare results. + case reflect.Struct: + { + // All structs enter here. We're not interested in most types. + if !obj1Value.CanConvert(timeType) { + break + } + + // time.Time can be compared! + timeObj1, ok := obj1.(time.Time) + if !ok { + timeObj1 = obj1Value.Convert(timeType).Interface().(time.Time) + } + + timeObj2, ok := obj2.(time.Time) + if !ok { + timeObj2 = obj2Value.Convert(timeType).Interface().(time.Time) + } + + if timeObj1.Before(timeObj2) { + return compareLess, true + } + if timeObj1.Equal(timeObj2) { + return compareEqual, true + } + return compareGreater, true + } + case reflect.Slice: + { + // We only care about the []byte type. + if !obj1Value.CanConvert(bytesType) { + break + } + + // []byte can be compared! + bytesObj1, ok := obj1.([]byte) + if !ok { + bytesObj1 = obj1Value.Convert(bytesType).Interface().([]byte) + + } + bytesObj2, ok := obj2.([]byte) + if !ok { + bytesObj2 = obj2Value.Convert(bytesType).Interface().([]byte) + } + + return compareResult(bytes.Compare(bytesObj1, bytesObj2)), true + } + case reflect.Uintptr: + { + uintptrObj1, ok := obj1.(uintptr) + if !ok { + uintptrObj1 = obj1Value.Convert(uintptrType).Interface().(uintptr) + } + uintptrObj2, ok := obj2.(uintptr) + if !ok { + uintptrObj2 = obj2Value.Convert(uintptrType).Interface().(uintptr) + } + if uintptrObj1 > uintptrObj2 { + return compareGreater, true + } + if uintptrObj1 == uintptrObj2 { + return compareEqual, true + } + if uintptrObj1 < uintptrObj2 { + return compareLess, true + } + } + } + + return compareEqual, false +} + +// Greater asserts that the first element is greater than the second +// +// assert.Greater(t, 2, 1) +// assert.Greater(t, float64(2), float64(1)) +// assert.Greater(t, "b", "a") +func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + failMessage := fmt.Sprintf("\"%v\" is not greater than \"%v\"", e1, e2) + return compareTwoValues(t, e1, e2, []compareResult{compareGreater}, failMessage, msgAndArgs...) +} + +// GreaterOrEqual asserts that the first element is greater than or equal to the second +// +// assert.GreaterOrEqual(t, 2, 1) +// assert.GreaterOrEqual(t, 2, 2) +// assert.GreaterOrEqual(t, "b", "a") +// assert.GreaterOrEqual(t, "b", "b") +func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + failMessage := fmt.Sprintf("\"%v\" is not greater than or equal to \"%v\"", e1, e2) + return compareTwoValues(t, e1, e2, []compareResult{compareGreater, compareEqual}, failMessage, msgAndArgs...) +} + +// Less asserts that the first element is less than the second +// +// assert.Less(t, 1, 2) +// assert.Less(t, float64(1), float64(2)) +// assert.Less(t, "a", "b") +func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + failMessage := fmt.Sprintf("\"%v\" is not less than \"%v\"", e1, e2) + return compareTwoValues(t, e1, e2, []compareResult{compareLess}, failMessage, msgAndArgs...) +} + +// LessOrEqual asserts that the first element is less than or equal to the second +// +// assert.LessOrEqual(t, 1, 2) +// assert.LessOrEqual(t, 2, 2) +// assert.LessOrEqual(t, "a", "b") +// assert.LessOrEqual(t, "b", "b") +func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + failMessage := fmt.Sprintf("\"%v\" is not less than or equal to \"%v\"", e1, e2) + return compareTwoValues(t, e1, e2, []compareResult{compareLess, compareEqual}, failMessage, msgAndArgs...) +} + +// Positive asserts that the specified element is positive +// +// assert.Positive(t, 1) +// assert.Positive(t, 1.23) +func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + zero := reflect.Zero(reflect.TypeOf(e)) + failMessage := fmt.Sprintf("\"%v\" is not positive", e) + return compareTwoValues(t, e, zero.Interface(), []compareResult{compareGreater}, failMessage, msgAndArgs...) +} + +// Negative asserts that the specified element is negative +// +// assert.Negative(t, -1) +// assert.Negative(t, -1.23) +func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + zero := reflect.Zero(reflect.TypeOf(e)) + failMessage := fmt.Sprintf("\"%v\" is not negative", e) + return compareTwoValues(t, e, zero.Interface(), []compareResult{compareLess}, failMessage, msgAndArgs...) +} + +func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []compareResult, failMessage string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + e1Kind := reflect.ValueOf(e1).Kind() + e2Kind := reflect.ValueOf(e2).Kind() + if e1Kind != e2Kind { + return Fail(t, "Elements should be the same type", msgAndArgs...) + } + + compareResult, isComparable := compare(e1, e2, e1Kind) + if !isComparable { + return Fail(t, fmt.Sprintf(`Can not compare type "%T"`, e1), msgAndArgs...) + } + + if !containsValue(allowedComparesResults, compareResult) { + return Fail(t, failMessage, msgAndArgs...) + } + + return true +} + +func containsValue(values []compareResult, value compareResult) bool { + for _, v := range values { + if v == value { + return true + } + } + + return false +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_format.go b/vendor/github.com/stretchr/testify/assert/assertion_format.go new file mode 100644 index 0000000000..c592f6ad5f --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_format.go @@ -0,0 +1,866 @@ +// Code generated with github.com/stretchr/testify/_codegen; DO NOT EDIT. + +package assert + +import ( + http "net/http" + url "net/url" + time "time" +) + +// Conditionf uses a Comparison to assert a complex condition. +func Conditionf(t TestingT, comp Comparison, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Condition(t, comp, append([]interface{}{msg}, args...)...) +} + +// Containsf asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// assert.Containsf(t, "Hello World", "World", "error message %s", "formatted") +// assert.Containsf(t, ["Hello", "World"], "World", "error message %s", "formatted") +// assert.Containsf(t, {"Hello": "World"}, "Hello", "error message %s", "formatted") +func Containsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Contains(t, s, contains, append([]interface{}{msg}, args...)...) +} + +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func DirExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return DirExists(t, path, append([]interface{}{msg}, args...)...) +} + +// ElementsMatchf asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// assert.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +func ElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return ElementsMatch(t, listA, listB, append([]interface{}{msg}, args...)...) +} + +// Emptyf asserts that the given value is "empty". +// +// [Zero values] are "empty". +// +// Arrays are "empty" if every element is the zero value of the type (stricter than "empty"). +// +// Slices, maps and channels with zero length are "empty". +// +// Pointer values are "empty" if the pointer is nil or if the pointed value is "empty". +// +// assert.Emptyf(t, obj, "error message %s", "formatted") +// +// [Zero values]: https://go.dev/ref/spec#The_zero_value +func Emptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Empty(t, object, append([]interface{}{msg}, args...)...) +} + +// Equalf asserts that two objects are equal. +// +// assert.Equalf(t, 123, 123, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func Equalf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Equal(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// EqualErrorf asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// assert.EqualErrorf(t, err, expectedErrorString, "error message %s", "formatted") +func EqualErrorf(t TestingT, theError error, errString string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return EqualError(t, theError, errString, append([]interface{}{msg}, args...)...) +} + +// EqualExportedValuesf asserts that the types of two objects are equal and their public +// fields are also equal. This is useful for comparing structs that have private fields +// that could potentially differ. +// +// type S struct { +// Exported int +// notExported int +// } +// assert.EqualExportedValuesf(t, S{1, 2}, S{1, 3}, "error message %s", "formatted") => true +// assert.EqualExportedValuesf(t, S{1, 2}, S{2, 3}, "error message %s", "formatted") => false +func EqualExportedValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return EqualExportedValues(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// EqualValuesf asserts that two objects are equal or convertible to the larger +// type and equal. +// +// assert.EqualValuesf(t, uint32(123), int32(123), "error message %s", "formatted") +func EqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return EqualValues(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Errorf asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// assert.Errorf(t, err, "error message %s", "formatted") +func Errorf(t TestingT, err error, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Error(t, err, append([]interface{}{msg}, args...)...) +} + +// ErrorAsf asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. +// This is a wrapper for errors.As. +func ErrorAsf(t TestingT, err error, target interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return ErrorAs(t, err, target, append([]interface{}{msg}, args...)...) +} + +// ErrorContainsf asserts that a function returned an error (i.e. not `nil`) +// and that the error contains the specified substring. +// +// actualObj, err := SomeFunction() +// assert.ErrorContainsf(t, err, expectedErrorSubString, "error message %s", "formatted") +func ErrorContainsf(t TestingT, theError error, contains string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return ErrorContains(t, theError, contains, append([]interface{}{msg}, args...)...) +} + +// ErrorIsf asserts that at least one of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func ErrorIsf(t TestingT, err error, target error, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return ErrorIs(t, err, target, append([]interface{}{msg}, args...)...) +} + +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventuallyf(t, func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Eventually(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...) +} + +// EventuallyWithTf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. In contrast to Eventually, +// it supplies a CollectT to the condition function, so that the condition +// function can use the CollectT to call other assertions. +// The condition is considered "met" if no errors are raised in a tick. +// The supplied CollectT collects all errors from one tick (if there are any). +// If the condition is not met before waitFor, the collected errors of +// the last tick are copied to t. +// +// externalValue := false +// go func() { +// time.Sleep(8*time.Second) +// externalValue = true +// }() +// assert.EventuallyWithTf(t, func(c *assert.CollectT, "error message %s", "formatted") { +// // add assertions as needed; any assertion failure will fail the current tick +// assert.True(c, externalValue, "expected 'externalValue' to be true") +// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false") +func EventuallyWithTf(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return EventuallyWithT(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...) +} + +// Exactlyf asserts that two objects are equal in value and type. +// +// assert.Exactlyf(t, int32(123), int64(123), "error message %s", "formatted") +func Exactlyf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Exactly(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Failf reports a failure through +func Failf(t TestingT, failureMessage string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, failureMessage, append([]interface{}{msg}, args...)...) +} + +// FailNowf fails test +func FailNowf(t TestingT, failureMessage string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return FailNow(t, failureMessage, append([]interface{}{msg}, args...)...) +} + +// Falsef asserts that the specified value is false. +// +// assert.Falsef(t, myBool, "error message %s", "formatted") +func Falsef(t TestingT, value bool, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return False(t, value, append([]interface{}{msg}, args...)...) +} + +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func FileExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return FileExists(t, path, append([]interface{}{msg}, args...)...) +} + +// Greaterf asserts that the first element is greater than the second +// +// assert.Greaterf(t, 2, 1, "error message %s", "formatted") +// assert.Greaterf(t, float64(2), float64(1), "error message %s", "formatted") +// assert.Greaterf(t, "b", "a", "error message %s", "formatted") +func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Greater(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// GreaterOrEqualf asserts that the first element is greater than or equal to the second +// +// assert.GreaterOrEqualf(t, 2, 1, "error message %s", "formatted") +// assert.GreaterOrEqualf(t, 2, 2, "error message %s", "formatted") +// assert.GreaterOrEqualf(t, "b", "a", "error message %s", "formatted") +// assert.GreaterOrEqualf(t, "b", "b", "error message %s", "formatted") +func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return GreaterOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// HTTPBodyContainsf asserts that a specified handler returns a +// body that contains a string. +// +// assert.HTTPBodyContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPBodyContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...) +} + +// HTTPBodyNotContainsf asserts that a specified handler returns a +// body that does not contain a string. +// +// assert.HTTPBodyNotContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPBodyNotContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...) +} + +// HTTPErrorf asserts that a specified handler returns an error status code. +// +// assert.HTTPErrorf(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPErrorf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPError(t, handler, method, url, values, append([]interface{}{msg}, args...)...) +} + +// HTTPRedirectf asserts that a specified handler returns a redirect status code. +// +// assert.HTTPRedirectf(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPRedirectf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPRedirect(t, handler, method, url, values, append([]interface{}{msg}, args...)...) +} + +// HTTPStatusCodef asserts that a specified handler returns a specified status code. +// +// assert.HTTPStatusCodef(t, myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPStatusCodef(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPStatusCode(t, handler, method, url, values, statuscode, append([]interface{}{msg}, args...)...) +} + +// HTTPSuccessf asserts that a specified handler returns a success status code. +// +// assert.HTTPSuccessf(t, myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPSuccessf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPSuccess(t, handler, method, url, values, append([]interface{}{msg}, args...)...) +} + +// Implementsf asserts that an object is implemented by the specified interface. +// +// assert.Implementsf(t, (*MyInterface)(nil), new(MyObject), "error message %s", "formatted") +func Implementsf(t TestingT, interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Implements(t, interfaceObject, object, append([]interface{}{msg}, args...)...) +} + +// InDeltaf asserts that the two numerals are within delta of each other. +// +// assert.InDeltaf(t, math.Pi, 22/7.0, 0.01, "error message %s", "formatted") +func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InDelta(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func InDeltaMapValuesf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InDeltaMapValues(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// InDeltaSlicef is the same as InDelta, except it compares two slices. +func InDeltaSlicef(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InDeltaSlice(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// InEpsilonf asserts that expected and actual have a relative error less than epsilon +func InEpsilonf(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InEpsilon(t, expected, actual, epsilon, append([]interface{}{msg}, args...)...) +} + +// InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices. +func InEpsilonSlicef(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InEpsilonSlice(t, expected, actual, epsilon, append([]interface{}{msg}, args...)...) +} + +// IsDecreasingf asserts that the collection is decreasing +// +// assert.IsDecreasingf(t, []int{2, 1, 0}, "error message %s", "formatted") +// assert.IsDecreasingf(t, []float{2, 1}, "error message %s", "formatted") +// assert.IsDecreasingf(t, []string{"b", "a"}, "error message %s", "formatted") +func IsDecreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return IsDecreasing(t, object, append([]interface{}{msg}, args...)...) +} + +// IsIncreasingf asserts that the collection is increasing +// +// assert.IsIncreasingf(t, []int{1, 2, 3}, "error message %s", "formatted") +// assert.IsIncreasingf(t, []float{1, 2}, "error message %s", "formatted") +// assert.IsIncreasingf(t, []string{"a", "b"}, "error message %s", "formatted") +func IsIncreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return IsIncreasing(t, object, append([]interface{}{msg}, args...)...) +} + +// IsNonDecreasingf asserts that the collection is not decreasing +// +// assert.IsNonDecreasingf(t, []int{1, 1, 2}, "error message %s", "formatted") +// assert.IsNonDecreasingf(t, []float{1, 2}, "error message %s", "formatted") +// assert.IsNonDecreasingf(t, []string{"a", "b"}, "error message %s", "formatted") +func IsNonDecreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return IsNonDecreasing(t, object, append([]interface{}{msg}, args...)...) +} + +// IsNonIncreasingf asserts that the collection is not increasing +// +// assert.IsNonIncreasingf(t, []int{2, 1, 1}, "error message %s", "formatted") +// assert.IsNonIncreasingf(t, []float{2, 1}, "error message %s", "formatted") +// assert.IsNonIncreasingf(t, []string{"b", "a"}, "error message %s", "formatted") +func IsNonIncreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return IsNonIncreasing(t, object, append([]interface{}{msg}, args...)...) +} + +// IsNotTypef asserts that the specified objects are not of the same type. +// +// assert.IsNotTypef(t, &NotMyStruct{}, &MyStruct{}, "error message %s", "formatted") +func IsNotTypef(t TestingT, theType interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return IsNotType(t, theType, object, append([]interface{}{msg}, args...)...) +} + +// IsTypef asserts that the specified objects are of the same type. +// +// assert.IsTypef(t, &MyStruct{}, &MyStruct{}, "error message %s", "formatted") +func IsTypef(t TestingT, expectedType interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return IsType(t, expectedType, object, append([]interface{}{msg}, args...)...) +} + +// JSONEqf asserts that two JSON strings are equivalent. +// +// assert.JSONEqf(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted") +func JSONEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return JSONEq(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Lenf asserts that the specified object has specific length. +// Lenf also fails if the object has a type that len() not accept. +// +// assert.Lenf(t, mySlice, 3, "error message %s", "formatted") +func Lenf(t TestingT, object interface{}, length int, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Len(t, object, length, append([]interface{}{msg}, args...)...) +} + +// Lessf asserts that the first element is less than the second +// +// assert.Lessf(t, 1, 2, "error message %s", "formatted") +// assert.Lessf(t, float64(1), float64(2), "error message %s", "formatted") +// assert.Lessf(t, "a", "b", "error message %s", "formatted") +func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Less(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// LessOrEqualf asserts that the first element is less than or equal to the second +// +// assert.LessOrEqualf(t, 1, 2, "error message %s", "formatted") +// assert.LessOrEqualf(t, 2, 2, "error message %s", "formatted") +// assert.LessOrEqualf(t, "a", "b", "error message %s", "formatted") +// assert.LessOrEqualf(t, "b", "b", "error message %s", "formatted") +func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return LessOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// Negativef asserts that the specified element is negative +// +// assert.Negativef(t, -1, "error message %s", "formatted") +// assert.Negativef(t, -1.23, "error message %s", "formatted") +func Negativef(t TestingT, e interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Negative(t, e, append([]interface{}{msg}, args...)...) +} + +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Neverf(t, func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Neverf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Never(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...) +} + +// Nilf asserts that the specified object is nil. +// +// assert.Nilf(t, err, "error message %s", "formatted") +func Nilf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Nil(t, object, append([]interface{}{msg}, args...)...) +} + +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoDirExists(t, path, append([]interface{}{msg}, args...)...) +} + +// NoErrorf asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if assert.NoErrorf(t, err, "error message %s", "formatted") { +// assert.Equal(t, expectedObj, actualObj) +// } +func NoErrorf(t TestingT, err error, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoError(t, err, append([]interface{}{msg}, args...)...) +} + +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoFileExists(t, path, append([]interface{}{msg}, args...)...) +} + +// NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// assert.NotContainsf(t, "Hello World", "Earth", "error message %s", "formatted") +// assert.NotContainsf(t, ["Hello", "World"], "Earth", "error message %s", "formatted") +// assert.NotContainsf(t, {"Hello": "World"}, "Earth", "error message %s", "formatted") +func NotContainsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotContains(t, s, contains, append([]interface{}{msg}, args...)...) +} + +// NotElementsMatchf asserts that the specified listA(array, slice...) is NOT equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should not match. +// This is an inverse of ElementsMatch. +// +// assert.NotElementsMatchf(t, [1, 1, 2, 3], [1, 1, 2, 3], "error message %s", "formatted") -> false +// +// assert.NotElementsMatchf(t, [1, 1, 2, 3], [1, 2, 3], "error message %s", "formatted") -> true +// +// assert.NotElementsMatchf(t, [1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true +func NotElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotElementsMatch(t, listA, listB, append([]interface{}{msg}, args...)...) +} + +// NotEmptyf asserts that the specified object is NOT [Empty]. +// +// if assert.NotEmptyf(t, obj, "error message %s", "formatted") { +// assert.Equal(t, "two", obj[1]) +// } +func NotEmptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotEmpty(t, object, append([]interface{}{msg}, args...)...) +} + +// NotEqualf asserts that the specified values are NOT equal. +// +// assert.NotEqualf(t, obj1, obj2, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func NotEqualf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotEqual(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// NotEqualValuesf asserts that two objects are not equal even when converted to the same type +// +// assert.NotEqualValuesf(t, obj1, obj2, "error message %s", "formatted") +func NotEqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotEqualValues(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// NotErrorAsf asserts that none of the errors in err's chain matches target, +// but if so, sets target to that error value. +func NotErrorAsf(t TestingT, err error, target interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotErrorAs(t, err, target, append([]interface{}{msg}, args...)...) +} + +// NotErrorIsf asserts that none of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func NotErrorIsf(t TestingT, err error, target error, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotErrorIs(t, err, target, append([]interface{}{msg}, args...)...) +} + +// NotImplementsf asserts that an object does not implement the specified interface. +// +// assert.NotImplementsf(t, (*MyInterface)(nil), new(MyObject), "error message %s", "formatted") +func NotImplementsf(t TestingT, interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotImplements(t, interfaceObject, object, append([]interface{}{msg}, args...)...) +} + +// NotNilf asserts that the specified object is not nil. +// +// assert.NotNilf(t, err, "error message %s", "formatted") +func NotNilf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotNil(t, object, append([]interface{}{msg}, args...)...) +} + +// NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// assert.NotPanicsf(t, func(){ RemainCalm() }, "error message %s", "formatted") +func NotPanicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotPanics(t, f, append([]interface{}{msg}, args...)...) +} + +// NotRegexpf asserts that a specified regexp does not match a string. +// +// assert.NotRegexpf(t, regexp.MustCompile("starts"), "it's starting", "error message %s", "formatted") +// assert.NotRegexpf(t, "^start", "it's not starting", "error message %s", "formatted") +func NotRegexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotRegexp(t, rx, str, append([]interface{}{msg}, args...)...) +} + +// NotSamef asserts that two pointers do not reference the same object. +// +// assert.NotSamef(t, ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSamef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotSame(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// NotSubsetf asserts that the list (array, slice, or map) does NOT contain all +// elements given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// assert.NotSubsetf(t, [1, 3, 4], [1, 2], "error message %s", "formatted") +// assert.NotSubsetf(t, {"x": 1, "y": 2}, {"z": 3}, "error message %s", "formatted") +// assert.NotSubsetf(t, [1, 3, 4], {1: "one", 2: "two"}, "error message %s", "formatted") +// assert.NotSubsetf(t, {"x": 1, "y": 2}, ["z"], "error message %s", "formatted") +func NotSubsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotSubset(t, list, subset, append([]interface{}{msg}, args...)...) +} + +// NotZerof asserts that i is not the zero value for its type. +func NotZerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotZero(t, i, append([]interface{}{msg}, args...)...) +} + +// Panicsf asserts that the code inside the specified PanicTestFunc panics. +// +// assert.Panicsf(t, func(){ GoCrazy() }, "error message %s", "formatted") +func Panicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Panics(t, f, append([]interface{}{msg}, args...)...) +} + +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithErrorf(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func PanicsWithErrorf(t TestingT, errString string, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return PanicsWithError(t, errString, f, append([]interface{}{msg}, args...)...) +} + +// PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// assert.PanicsWithValuef(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func PanicsWithValuef(t TestingT, expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return PanicsWithValue(t, expected, f, append([]interface{}{msg}, args...)...) +} + +// Positivef asserts that the specified element is positive +// +// assert.Positivef(t, 1, "error message %s", "formatted") +// assert.Positivef(t, 1.23, "error message %s", "formatted") +func Positivef(t TestingT, e interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Positive(t, e, append([]interface{}{msg}, args...)...) +} + +// Regexpf asserts that a specified regexp matches a string. +// +// assert.Regexpf(t, regexp.MustCompile("start"), "it's starting", "error message %s", "formatted") +// assert.Regexpf(t, "start...$", "it's not starting", "error message %s", "formatted") +func Regexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Regexp(t, rx, str, append([]interface{}{msg}, args...)...) +} + +// Samef asserts that two pointers reference the same object. +// +// assert.Samef(t, ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func Samef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Same(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Subsetf asserts that the list (array, slice, or map) contains all elements +// given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// assert.Subsetf(t, [1, 2, 3], [1, 2], "error message %s", "formatted") +// assert.Subsetf(t, {"x": 1, "y": 2}, {"x": 1}, "error message %s", "formatted") +// assert.Subsetf(t, [1, 2, 3], {1: "one", 2: "two"}, "error message %s", "formatted") +// assert.Subsetf(t, {"x": 1, "y": 2}, ["x"], "error message %s", "formatted") +func Subsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Subset(t, list, subset, append([]interface{}{msg}, args...)...) +} + +// Truef asserts that the specified value is true. +// +// assert.Truef(t, myBool, "error message %s", "formatted") +func Truef(t TestingT, value bool, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return True(t, value, append([]interface{}{msg}, args...)...) +} + +// WithinDurationf asserts that the two times are within duration delta of each other. +// +// assert.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return WithinDuration(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// WithinRangef asserts that a time is within a time range (inclusive). +// +// assert.WithinRangef(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted") +func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return WithinRange(t, actual, start, end, append([]interface{}{msg}, args...)...) +} + +// YAMLEqf asserts that two YAML strings are equivalent. +func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return YAMLEq(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Zerof asserts that i is the zero value for its type. +func Zerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Zero(t, i, append([]interface{}{msg}, args...)...) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl b/vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl new file mode 100644 index 0000000000..d2bb0b8177 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl @@ -0,0 +1,5 @@ +{{.CommentFormat}} +func {{.DocInfo.Name}}f(t TestingT, {{.ParamsFormat}}) bool { + if h, ok := t.(tHelper); ok { h.Helper() } + return {{.DocInfo.Name}}(t, {{.ForwardedParamsFormat}}) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_forward.go b/vendor/github.com/stretchr/testify/assert/assertion_forward.go new file mode 100644 index 0000000000..58db928450 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_forward.go @@ -0,0 +1,1723 @@ +// Code generated with github.com/stretchr/testify/_codegen; DO NOT EDIT. + +package assert + +import ( + http "net/http" + url "net/url" + time "time" +) + +// Condition uses a Comparison to assert a complex condition. +func (a *Assertions) Condition(comp Comparison, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Condition(a.t, comp, msgAndArgs...) +} + +// Conditionf uses a Comparison to assert a complex condition. +func (a *Assertions) Conditionf(comp Comparison, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Conditionf(a.t, comp, msg, args...) +} + +// Contains asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// a.Contains("Hello World", "World") +// a.Contains(["Hello", "World"], "World") +// a.Contains({"Hello": "World"}, "Hello") +func (a *Assertions) Contains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Contains(a.t, s, contains, msgAndArgs...) +} + +// Containsf asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// a.Containsf("Hello World", "World", "error message %s", "formatted") +// a.Containsf(["Hello", "World"], "World", "error message %s", "formatted") +// a.Containsf({"Hello": "World"}, "Hello", "error message %s", "formatted") +func (a *Assertions) Containsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Containsf(a.t, s, contains, msg, args...) +} + +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return DirExists(a.t, path, msgAndArgs...) +} + +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func (a *Assertions) DirExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return DirExistsf(a.t, path, msg, args...) +} + +// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// a.ElementsMatch([1, 3, 2, 3], [1, 3, 3, 2]) +func (a *Assertions) ElementsMatch(listA interface{}, listB interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ElementsMatch(a.t, listA, listB, msgAndArgs...) +} + +// ElementsMatchf asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +func (a *Assertions) ElementsMatchf(listA interface{}, listB interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ElementsMatchf(a.t, listA, listB, msg, args...) +} + +// Empty asserts that the given value is "empty". +// +// [Zero values] are "empty". +// +// Arrays are "empty" if every element is the zero value of the type (stricter than "empty"). +// +// Slices, maps and channels with zero length are "empty". +// +// Pointer values are "empty" if the pointer is nil or if the pointed value is "empty". +// +// a.Empty(obj) +// +// [Zero values]: https://go.dev/ref/spec#The_zero_value +func (a *Assertions) Empty(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Empty(a.t, object, msgAndArgs...) +} + +// Emptyf asserts that the given value is "empty". +// +// [Zero values] are "empty". +// +// Arrays are "empty" if every element is the zero value of the type (stricter than "empty"). +// +// Slices, maps and channels with zero length are "empty". +// +// Pointer values are "empty" if the pointer is nil or if the pointed value is "empty". +// +// a.Emptyf(obj, "error message %s", "formatted") +// +// [Zero values]: https://go.dev/ref/spec#The_zero_value +func (a *Assertions) Emptyf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Emptyf(a.t, object, msg, args...) +} + +// Equal asserts that two objects are equal. +// +// a.Equal(123, 123) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Equal(a.t, expected, actual, msgAndArgs...) +} + +// EqualError asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// a.EqualError(err, expectedErrorString) +func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualError(a.t, theError, errString, msgAndArgs...) +} + +// EqualErrorf asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// a.EqualErrorf(err, expectedErrorString, "error message %s", "formatted") +func (a *Assertions) EqualErrorf(theError error, errString string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualErrorf(a.t, theError, errString, msg, args...) +} + +// EqualExportedValues asserts that the types of two objects are equal and their public +// fields are also equal. This is useful for comparing structs that have private fields +// that could potentially differ. +// +// type S struct { +// Exported int +// notExported int +// } +// a.EqualExportedValues(S{1, 2}, S{1, 3}) => true +// a.EqualExportedValues(S{1, 2}, S{2, 3}) => false +func (a *Assertions) EqualExportedValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualExportedValues(a.t, expected, actual, msgAndArgs...) +} + +// EqualExportedValuesf asserts that the types of two objects are equal and their public +// fields are also equal. This is useful for comparing structs that have private fields +// that could potentially differ. +// +// type S struct { +// Exported int +// notExported int +// } +// a.EqualExportedValuesf(S{1, 2}, S{1, 3}, "error message %s", "formatted") => true +// a.EqualExportedValuesf(S{1, 2}, S{2, 3}, "error message %s", "formatted") => false +func (a *Assertions) EqualExportedValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualExportedValuesf(a.t, expected, actual, msg, args...) +} + +// EqualValues asserts that two objects are equal or convertible to the larger +// type and equal. +// +// a.EqualValues(uint32(123), int32(123)) +func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualValues(a.t, expected, actual, msgAndArgs...) +} + +// EqualValuesf asserts that two objects are equal or convertible to the larger +// type and equal. +// +// a.EqualValuesf(uint32(123), int32(123), "error message %s", "formatted") +func (a *Assertions) EqualValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualValuesf(a.t, expected, actual, msg, args...) +} + +// Equalf asserts that two objects are equal. +// +// a.Equalf(123, 123, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func (a *Assertions) Equalf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Equalf(a.t, expected, actual, msg, args...) +} + +// Error asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// a.Error(err) +func (a *Assertions) Error(err error, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Error(a.t, err, msgAndArgs...) +} + +// ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. +// This is a wrapper for errors.As. +func (a *Assertions) ErrorAs(err error, target interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ErrorAs(a.t, err, target, msgAndArgs...) +} + +// ErrorAsf asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. +// This is a wrapper for errors.As. +func (a *Assertions) ErrorAsf(err error, target interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ErrorAsf(a.t, err, target, msg, args...) +} + +// ErrorContains asserts that a function returned an error (i.e. not `nil`) +// and that the error contains the specified substring. +// +// actualObj, err := SomeFunction() +// a.ErrorContains(err, expectedErrorSubString) +func (a *Assertions) ErrorContains(theError error, contains string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ErrorContains(a.t, theError, contains, msgAndArgs...) +} + +// ErrorContainsf asserts that a function returned an error (i.e. not `nil`) +// and that the error contains the specified substring. +// +// actualObj, err := SomeFunction() +// a.ErrorContainsf(err, expectedErrorSubString, "error message %s", "formatted") +func (a *Assertions) ErrorContainsf(theError error, contains string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ErrorContainsf(a.t, theError, contains, msg, args...) +} + +// ErrorIs asserts that at least one of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ErrorIs(a.t, err, target, msgAndArgs...) +} + +// ErrorIsf asserts that at least one of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func (a *Assertions) ErrorIsf(err error, target error, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ErrorIsf(a.t, err, target, msg, args...) +} + +// Errorf asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// a.Errorf(err, "error message %s", "formatted") +func (a *Assertions) Errorf(err error, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Errorf(a.t, err, msg, args...) +} + +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventually(func() bool { return true; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Eventually(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// EventuallyWithT asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. In contrast to Eventually, +// it supplies a CollectT to the condition function, so that the condition +// function can use the CollectT to call other assertions. +// The condition is considered "met" if no errors are raised in a tick. +// The supplied CollectT collects all errors from one tick (if there are any). +// If the condition is not met before waitFor, the collected errors of +// the last tick are copied to t. +// +// externalValue := false +// go func() { +// time.Sleep(8*time.Second) +// externalValue = true +// }() +// a.EventuallyWithT(func(c *assert.CollectT) { +// // add assertions as needed; any assertion failure will fail the current tick +// assert.True(c, externalValue, "expected 'externalValue' to be true") +// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false") +func (a *Assertions) EventuallyWithT(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EventuallyWithT(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// EventuallyWithTf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. In contrast to Eventually, +// it supplies a CollectT to the condition function, so that the condition +// function can use the CollectT to call other assertions. +// The condition is considered "met" if no errors are raised in a tick. +// The supplied CollectT collects all errors from one tick (if there are any). +// If the condition is not met before waitFor, the collected errors of +// the last tick are copied to t. +// +// externalValue := false +// go func() { +// time.Sleep(8*time.Second) +// externalValue = true +// }() +// a.EventuallyWithTf(func(c *assert.CollectT, "error message %s", "formatted") { +// // add assertions as needed; any assertion failure will fail the current tick +// assert.True(c, externalValue, "expected 'externalValue' to be true") +// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false") +func (a *Assertions) EventuallyWithTf(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EventuallyWithTf(a.t, condition, waitFor, tick, msg, args...) +} + +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventuallyf(func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Eventuallyf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Eventuallyf(a.t, condition, waitFor, tick, msg, args...) +} + +// Exactly asserts that two objects are equal in value and type. +// +// a.Exactly(int32(123), int64(123)) +func (a *Assertions) Exactly(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Exactly(a.t, expected, actual, msgAndArgs...) +} + +// Exactlyf asserts that two objects are equal in value and type. +// +// a.Exactlyf(int32(123), int64(123), "error message %s", "formatted") +func (a *Assertions) Exactlyf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Exactlyf(a.t, expected, actual, msg, args...) +} + +// Fail reports a failure through +func (a *Assertions) Fail(failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Fail(a.t, failureMessage, msgAndArgs...) +} + +// FailNow fails test +func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FailNow(a.t, failureMessage, msgAndArgs...) +} + +// FailNowf fails test +func (a *Assertions) FailNowf(failureMessage string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FailNowf(a.t, failureMessage, msg, args...) +} + +// Failf reports a failure through +func (a *Assertions) Failf(failureMessage string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Failf(a.t, failureMessage, msg, args...) +} + +// False asserts that the specified value is false. +// +// a.False(myBool) +func (a *Assertions) False(value bool, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return False(a.t, value, msgAndArgs...) +} + +// Falsef asserts that the specified value is false. +// +// a.Falsef(myBool, "error message %s", "formatted") +func (a *Assertions) Falsef(value bool, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Falsef(a.t, value, msg, args...) +} + +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileExists(a.t, path, msgAndArgs...) +} + +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileExistsf(a.t, path, msg, args...) +} + +// Greater asserts that the first element is greater than the second +// +// a.Greater(2, 1) +// a.Greater(float64(2), float64(1)) +// a.Greater("b", "a") +func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Greater(a.t, e1, e2, msgAndArgs...) +} + +// GreaterOrEqual asserts that the first element is greater than or equal to the second +// +// a.GreaterOrEqual(2, 1) +// a.GreaterOrEqual(2, 2) +// a.GreaterOrEqual("b", "a") +// a.GreaterOrEqual("b", "b") +func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return GreaterOrEqual(a.t, e1, e2, msgAndArgs...) +} + +// GreaterOrEqualf asserts that the first element is greater than or equal to the second +// +// a.GreaterOrEqualf(2, 1, "error message %s", "formatted") +// a.GreaterOrEqualf(2, 2, "error message %s", "formatted") +// a.GreaterOrEqualf("b", "a", "error message %s", "formatted") +// a.GreaterOrEqualf("b", "b", "error message %s", "formatted") +func (a *Assertions) GreaterOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return GreaterOrEqualf(a.t, e1, e2, msg, args...) +} + +// Greaterf asserts that the first element is greater than the second +// +// a.Greaterf(2, 1, "error message %s", "formatted") +// a.Greaterf(float64(2), float64(1), "error message %s", "formatted") +// a.Greaterf("b", "a", "error message %s", "formatted") +func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Greaterf(a.t, e1, e2, msg, args...) +} + +// HTTPBodyContains asserts that a specified handler returns a +// body that contains a string. +// +// a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyContains(a.t, handler, method, url, values, str, msgAndArgs...) +} + +// HTTPBodyContainsf asserts that a specified handler returns a +// body that contains a string. +// +// a.HTTPBodyContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyContainsf(a.t, handler, method, url, values, str, msg, args...) +} + +// HTTPBodyNotContains asserts that a specified handler returns a +// body that does not contain a string. +// +// a.HTTPBodyNotContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyNotContains(a.t, handler, method, url, values, str, msgAndArgs...) +} + +// HTTPBodyNotContainsf asserts that a specified handler returns a +// body that does not contain a string. +// +// a.HTTPBodyNotContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyNotContainsf(a.t, handler, method, url, values, str, msg, args...) +} + +// HTTPError asserts that a specified handler returns an error status code. +// +// a.HTTPError(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPError(a.t, handler, method, url, values, msgAndArgs...) +} + +// HTTPErrorf asserts that a specified handler returns an error status code. +// +// a.HTTPErrorf(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPErrorf(a.t, handler, method, url, values, msg, args...) +} + +// HTTPRedirect asserts that a specified handler returns a redirect status code. +// +// a.HTTPRedirect(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPRedirect(a.t, handler, method, url, values, msgAndArgs...) +} + +// HTTPRedirectf asserts that a specified handler returns a redirect status code. +// +// a.HTTPRedirectf(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPRedirectf(a.t, handler, method, url, values, msg, args...) +} + +// HTTPStatusCode asserts that a specified handler returns a specified status code. +// +// a.HTTPStatusCode(myHandler, "GET", "/notImplemented", nil, 501) +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPStatusCode(a.t, handler, method, url, values, statuscode, msgAndArgs...) +} + +// HTTPStatusCodef asserts that a specified handler returns a specified status code. +// +// a.HTTPStatusCodef(myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPStatusCodef(a.t, handler, method, url, values, statuscode, msg, args...) +} + +// HTTPSuccess asserts that a specified handler returns a success status code. +// +// a.HTTPSuccess(myHandler, "POST", "http://www.google.com", nil) +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPSuccess(a.t, handler, method, url, values, msgAndArgs...) +} + +// HTTPSuccessf asserts that a specified handler returns a success status code. +// +// a.HTTPSuccessf(myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPSuccessf(a.t, handler, method, url, values, msg, args...) +} + +// Implements asserts that an object is implemented by the specified interface. +// +// a.Implements((*MyInterface)(nil), new(MyObject)) +func (a *Assertions) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Implements(a.t, interfaceObject, object, msgAndArgs...) +} + +// Implementsf asserts that an object is implemented by the specified interface. +// +// a.Implementsf((*MyInterface)(nil), new(MyObject), "error message %s", "formatted") +func (a *Assertions) Implementsf(interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Implementsf(a.t, interfaceObject, object, msg, args...) +} + +// InDelta asserts that the two numerals are within delta of each other. +// +// a.InDelta(math.Pi, 22/7.0, 0.01) +func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDelta(a.t, expected, actual, delta, msgAndArgs...) +} + +// InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func (a *Assertions) InDeltaMapValues(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaMapValues(a.t, expected, actual, delta, msgAndArgs...) +} + +// InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func (a *Assertions) InDeltaMapValuesf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaMapValuesf(a.t, expected, actual, delta, msg, args...) +} + +// InDeltaSlice is the same as InDelta, except it compares two slices. +func (a *Assertions) InDeltaSlice(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaSlice(a.t, expected, actual, delta, msgAndArgs...) +} + +// InDeltaSlicef is the same as InDelta, except it compares two slices. +func (a *Assertions) InDeltaSlicef(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaSlicef(a.t, expected, actual, delta, msg, args...) +} + +// InDeltaf asserts that the two numerals are within delta of each other. +// +// a.InDeltaf(math.Pi, 22/7.0, 0.01, "error message %s", "formatted") +func (a *Assertions) InDeltaf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaf(a.t, expected, actual, delta, msg, args...) +} + +// InEpsilon asserts that expected and actual have a relative error less than epsilon +func (a *Assertions) InEpsilon(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilon(a.t, expected, actual, epsilon, msgAndArgs...) +} + +// InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices. +func (a *Assertions) InEpsilonSlice(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilonSlice(a.t, expected, actual, epsilon, msgAndArgs...) +} + +// InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices. +func (a *Assertions) InEpsilonSlicef(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilonSlicef(a.t, expected, actual, epsilon, msg, args...) +} + +// InEpsilonf asserts that expected and actual have a relative error less than epsilon +func (a *Assertions) InEpsilonf(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilonf(a.t, expected, actual, epsilon, msg, args...) +} + +// IsDecreasing asserts that the collection is decreasing +// +// a.IsDecreasing([]int{2, 1, 0}) +// a.IsDecreasing([]float{2, 1}) +// a.IsDecreasing([]string{"b", "a"}) +func (a *Assertions) IsDecreasing(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsDecreasing(a.t, object, msgAndArgs...) +} + +// IsDecreasingf asserts that the collection is decreasing +// +// a.IsDecreasingf([]int{2, 1, 0}, "error message %s", "formatted") +// a.IsDecreasingf([]float{2, 1}, "error message %s", "formatted") +// a.IsDecreasingf([]string{"b", "a"}, "error message %s", "formatted") +func (a *Assertions) IsDecreasingf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsDecreasingf(a.t, object, msg, args...) +} + +// IsIncreasing asserts that the collection is increasing +// +// a.IsIncreasing([]int{1, 2, 3}) +// a.IsIncreasing([]float{1, 2}) +// a.IsIncreasing([]string{"a", "b"}) +func (a *Assertions) IsIncreasing(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsIncreasing(a.t, object, msgAndArgs...) +} + +// IsIncreasingf asserts that the collection is increasing +// +// a.IsIncreasingf([]int{1, 2, 3}, "error message %s", "formatted") +// a.IsIncreasingf([]float{1, 2}, "error message %s", "formatted") +// a.IsIncreasingf([]string{"a", "b"}, "error message %s", "formatted") +func (a *Assertions) IsIncreasingf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsIncreasingf(a.t, object, msg, args...) +} + +// IsNonDecreasing asserts that the collection is not decreasing +// +// a.IsNonDecreasing([]int{1, 1, 2}) +// a.IsNonDecreasing([]float{1, 2}) +// a.IsNonDecreasing([]string{"a", "b"}) +func (a *Assertions) IsNonDecreasing(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsNonDecreasing(a.t, object, msgAndArgs...) +} + +// IsNonDecreasingf asserts that the collection is not decreasing +// +// a.IsNonDecreasingf([]int{1, 1, 2}, "error message %s", "formatted") +// a.IsNonDecreasingf([]float{1, 2}, "error message %s", "formatted") +// a.IsNonDecreasingf([]string{"a", "b"}, "error message %s", "formatted") +func (a *Assertions) IsNonDecreasingf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsNonDecreasingf(a.t, object, msg, args...) +} + +// IsNonIncreasing asserts that the collection is not increasing +// +// a.IsNonIncreasing([]int{2, 1, 1}) +// a.IsNonIncreasing([]float{2, 1}) +// a.IsNonIncreasing([]string{"b", "a"}) +func (a *Assertions) IsNonIncreasing(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsNonIncreasing(a.t, object, msgAndArgs...) +} + +// IsNonIncreasingf asserts that the collection is not increasing +// +// a.IsNonIncreasingf([]int{2, 1, 1}, "error message %s", "formatted") +// a.IsNonIncreasingf([]float{2, 1}, "error message %s", "formatted") +// a.IsNonIncreasingf([]string{"b", "a"}, "error message %s", "formatted") +func (a *Assertions) IsNonIncreasingf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsNonIncreasingf(a.t, object, msg, args...) +} + +// IsNotType asserts that the specified objects are not of the same type. +// +// a.IsNotType(&NotMyStruct{}, &MyStruct{}) +func (a *Assertions) IsNotType(theType interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsNotType(a.t, theType, object, msgAndArgs...) +} + +// IsNotTypef asserts that the specified objects are not of the same type. +// +// a.IsNotTypef(&NotMyStruct{}, &MyStruct{}, "error message %s", "formatted") +func (a *Assertions) IsNotTypef(theType interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsNotTypef(a.t, theType, object, msg, args...) +} + +// IsType asserts that the specified objects are of the same type. +// +// a.IsType(&MyStruct{}, &MyStruct{}) +func (a *Assertions) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsType(a.t, expectedType, object, msgAndArgs...) +} + +// IsTypef asserts that the specified objects are of the same type. +// +// a.IsTypef(&MyStruct{}, &MyStruct{}, "error message %s", "formatted") +func (a *Assertions) IsTypef(expectedType interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsTypef(a.t, expectedType, object, msg, args...) +} + +// JSONEq asserts that two JSON strings are equivalent. +// +// a.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) +func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return JSONEq(a.t, expected, actual, msgAndArgs...) +} + +// JSONEqf asserts that two JSON strings are equivalent. +// +// a.JSONEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted") +func (a *Assertions) JSONEqf(expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return JSONEqf(a.t, expected, actual, msg, args...) +} + +// Len asserts that the specified object has specific length. +// Len also fails if the object has a type that len() not accept. +// +// a.Len(mySlice, 3) +func (a *Assertions) Len(object interface{}, length int, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Len(a.t, object, length, msgAndArgs...) +} + +// Lenf asserts that the specified object has specific length. +// Lenf also fails if the object has a type that len() not accept. +// +// a.Lenf(mySlice, 3, "error message %s", "formatted") +func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Lenf(a.t, object, length, msg, args...) +} + +// Less asserts that the first element is less than the second +// +// a.Less(1, 2) +// a.Less(float64(1), float64(2)) +// a.Less("a", "b") +func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Less(a.t, e1, e2, msgAndArgs...) +} + +// LessOrEqual asserts that the first element is less than or equal to the second +// +// a.LessOrEqual(1, 2) +// a.LessOrEqual(2, 2) +// a.LessOrEqual("a", "b") +// a.LessOrEqual("b", "b") +func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return LessOrEqual(a.t, e1, e2, msgAndArgs...) +} + +// LessOrEqualf asserts that the first element is less than or equal to the second +// +// a.LessOrEqualf(1, 2, "error message %s", "formatted") +// a.LessOrEqualf(2, 2, "error message %s", "formatted") +// a.LessOrEqualf("a", "b", "error message %s", "formatted") +// a.LessOrEqualf("b", "b", "error message %s", "formatted") +func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return LessOrEqualf(a.t, e1, e2, msg, args...) +} + +// Lessf asserts that the first element is less than the second +// +// a.Lessf(1, 2, "error message %s", "formatted") +// a.Lessf(float64(1), float64(2), "error message %s", "formatted") +// a.Lessf("a", "b", "error message %s", "formatted") +func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Lessf(a.t, e1, e2, msg, args...) +} + +// Negative asserts that the specified element is negative +// +// a.Negative(-1) +// a.Negative(-1.23) +func (a *Assertions) Negative(e interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Negative(a.t, e, msgAndArgs...) +} + +// Negativef asserts that the specified element is negative +// +// a.Negativef(-1, "error message %s", "formatted") +// a.Negativef(-1.23, "error message %s", "formatted") +func (a *Assertions) Negativef(e interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Negativef(a.t, e, msg, args...) +} + +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Never(func() bool { return false; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Never(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Neverf(func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Neverf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Neverf(a.t, condition, waitFor, tick, msg, args...) +} + +// Nil asserts that the specified object is nil. +// +// a.Nil(err) +func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Nil(a.t, object, msgAndArgs...) +} + +// Nilf asserts that the specified object is nil. +// +// a.Nilf(err, "error message %s", "formatted") +func (a *Assertions) Nilf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Nilf(a.t, object, msg, args...) +} + +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoDirExists(a.t, path, msgAndArgs...) +} + +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoDirExistsf(a.t, path, msg, args...) +} + +// NoError asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if a.NoError(err) { +// assert.Equal(t, expectedObj, actualObj) +// } +func (a *Assertions) NoError(err error, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoError(a.t, err, msgAndArgs...) +} + +// NoErrorf asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if a.NoErrorf(err, "error message %s", "formatted") { +// assert.Equal(t, expectedObj, actualObj) +// } +func (a *Assertions) NoErrorf(err error, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoErrorf(a.t, err, msg, args...) +} + +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoFileExists(a.t, path, msgAndArgs...) +} + +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoFileExistsf(a.t, path, msg, args...) +} + +// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// a.NotContains("Hello World", "Earth") +// a.NotContains(["Hello", "World"], "Earth") +// a.NotContains({"Hello": "World"}, "Earth") +func (a *Assertions) NotContains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotContains(a.t, s, contains, msgAndArgs...) +} + +// NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// a.NotContainsf("Hello World", "Earth", "error message %s", "formatted") +// a.NotContainsf(["Hello", "World"], "Earth", "error message %s", "formatted") +// a.NotContainsf({"Hello": "World"}, "Earth", "error message %s", "formatted") +func (a *Assertions) NotContainsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotContainsf(a.t, s, contains, msg, args...) +} + +// NotElementsMatch asserts that the specified listA(array, slice...) is NOT equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should not match. +// This is an inverse of ElementsMatch. +// +// a.NotElementsMatch([1, 1, 2, 3], [1, 1, 2, 3]) -> false +// +// a.NotElementsMatch([1, 1, 2, 3], [1, 2, 3]) -> true +// +// a.NotElementsMatch([1, 2, 3], [1, 2, 4]) -> true +func (a *Assertions) NotElementsMatch(listA interface{}, listB interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotElementsMatch(a.t, listA, listB, msgAndArgs...) +} + +// NotElementsMatchf asserts that the specified listA(array, slice...) is NOT equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should not match. +// This is an inverse of ElementsMatch. +// +// a.NotElementsMatchf([1, 1, 2, 3], [1, 1, 2, 3], "error message %s", "formatted") -> false +// +// a.NotElementsMatchf([1, 1, 2, 3], [1, 2, 3], "error message %s", "formatted") -> true +// +// a.NotElementsMatchf([1, 2, 3], [1, 2, 4], "error message %s", "formatted") -> true +func (a *Assertions) NotElementsMatchf(listA interface{}, listB interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotElementsMatchf(a.t, listA, listB, msg, args...) +} + +// NotEmpty asserts that the specified object is NOT [Empty]. +// +// if a.NotEmpty(obj) { +// assert.Equal(t, "two", obj[1]) +// } +func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEmpty(a.t, object, msgAndArgs...) +} + +// NotEmptyf asserts that the specified object is NOT [Empty]. +// +// if a.NotEmptyf(obj, "error message %s", "formatted") { +// assert.Equal(t, "two", obj[1]) +// } +func (a *Assertions) NotEmptyf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEmptyf(a.t, object, msg, args...) +} + +// NotEqual asserts that the specified values are NOT equal. +// +// a.NotEqual(obj1, obj2) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func (a *Assertions) NotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqual(a.t, expected, actual, msgAndArgs...) +} + +// NotEqualValues asserts that two objects are not equal even when converted to the same type +// +// a.NotEqualValues(obj1, obj2) +func (a *Assertions) NotEqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqualValues(a.t, expected, actual, msgAndArgs...) +} + +// NotEqualValuesf asserts that two objects are not equal even when converted to the same type +// +// a.NotEqualValuesf(obj1, obj2, "error message %s", "formatted") +func (a *Assertions) NotEqualValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqualValuesf(a.t, expected, actual, msg, args...) +} + +// NotEqualf asserts that the specified values are NOT equal. +// +// a.NotEqualf(obj1, obj2, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func (a *Assertions) NotEqualf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqualf(a.t, expected, actual, msg, args...) +} + +// NotErrorAs asserts that none of the errors in err's chain matches target, +// but if so, sets target to that error value. +func (a *Assertions) NotErrorAs(err error, target interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotErrorAs(a.t, err, target, msgAndArgs...) +} + +// NotErrorAsf asserts that none of the errors in err's chain matches target, +// but if so, sets target to that error value. +func (a *Assertions) NotErrorAsf(err error, target interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotErrorAsf(a.t, err, target, msg, args...) +} + +// NotErrorIs asserts that none of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func (a *Assertions) NotErrorIs(err error, target error, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotErrorIs(a.t, err, target, msgAndArgs...) +} + +// NotErrorIsf asserts that none of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func (a *Assertions) NotErrorIsf(err error, target error, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotErrorIsf(a.t, err, target, msg, args...) +} + +// NotImplements asserts that an object does not implement the specified interface. +// +// a.NotImplements((*MyInterface)(nil), new(MyObject)) +func (a *Assertions) NotImplements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotImplements(a.t, interfaceObject, object, msgAndArgs...) +} + +// NotImplementsf asserts that an object does not implement the specified interface. +// +// a.NotImplementsf((*MyInterface)(nil), new(MyObject), "error message %s", "formatted") +func (a *Assertions) NotImplementsf(interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotImplementsf(a.t, interfaceObject, object, msg, args...) +} + +// NotNil asserts that the specified object is not nil. +// +// a.NotNil(err) +func (a *Assertions) NotNil(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotNil(a.t, object, msgAndArgs...) +} + +// NotNilf asserts that the specified object is not nil. +// +// a.NotNilf(err, "error message %s", "formatted") +func (a *Assertions) NotNilf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotNilf(a.t, object, msg, args...) +} + +// NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// a.NotPanics(func(){ RemainCalm() }) +func (a *Assertions) NotPanics(f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotPanics(a.t, f, msgAndArgs...) +} + +// NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// a.NotPanicsf(func(){ RemainCalm() }, "error message %s", "formatted") +func (a *Assertions) NotPanicsf(f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotPanicsf(a.t, f, msg, args...) +} + +// NotRegexp asserts that a specified regexp does not match a string. +// +// a.NotRegexp(regexp.MustCompile("starts"), "it's starting") +// a.NotRegexp("^start", "it's not starting") +func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotRegexp(a.t, rx, str, msgAndArgs...) +} + +// NotRegexpf asserts that a specified regexp does not match a string. +// +// a.NotRegexpf(regexp.MustCompile("starts"), "it's starting", "error message %s", "formatted") +// a.NotRegexpf("^start", "it's not starting", "error message %s", "formatted") +func (a *Assertions) NotRegexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotRegexpf(a.t, rx, str, msg, args...) +} + +// NotSame asserts that two pointers do not reference the same object. +// +// a.NotSame(ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSame(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSame(a.t, expected, actual, msgAndArgs...) +} + +// NotSamef asserts that two pointers do not reference the same object. +// +// a.NotSamef(ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSamef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSamef(a.t, expected, actual, msg, args...) +} + +// NotSubset asserts that the list (array, slice, or map) does NOT contain all +// elements given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// a.NotSubset([1, 3, 4], [1, 2]) +// a.NotSubset({"x": 1, "y": 2}, {"z": 3}) +// a.NotSubset([1, 3, 4], {1: "one", 2: "two"}) +// a.NotSubset({"x": 1, "y": 2}, ["z"]) +func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSubset(a.t, list, subset, msgAndArgs...) +} + +// NotSubsetf asserts that the list (array, slice, or map) does NOT contain all +// elements given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// a.NotSubsetf([1, 3, 4], [1, 2], "error message %s", "formatted") +// a.NotSubsetf({"x": 1, "y": 2}, {"z": 3}, "error message %s", "formatted") +// a.NotSubsetf([1, 3, 4], {1: "one", 2: "two"}, "error message %s", "formatted") +// a.NotSubsetf({"x": 1, "y": 2}, ["z"], "error message %s", "formatted") +func (a *Assertions) NotSubsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSubsetf(a.t, list, subset, msg, args...) +} + +// NotZero asserts that i is not the zero value for its type. +func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotZero(a.t, i, msgAndArgs...) +} + +// NotZerof asserts that i is not the zero value for its type. +func (a *Assertions) NotZerof(i interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotZerof(a.t, i, msg, args...) +} + +// Panics asserts that the code inside the specified PanicTestFunc panics. +// +// a.Panics(func(){ GoCrazy() }) +func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Panics(a.t, f, msgAndArgs...) +} + +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithError("crazy error", func(){ GoCrazy() }) +func (a *Assertions) PanicsWithError(errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithError(a.t, errString, f, msgAndArgs...) +} + +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithErrorf("crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) PanicsWithErrorf(errString string, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithErrorf(a.t, errString, f, msg, args...) +} + +// PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// a.PanicsWithValue("crazy error", func(){ GoCrazy() }) +func (a *Assertions) PanicsWithValue(expected interface{}, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithValue(a.t, expected, f, msgAndArgs...) +} + +// PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// a.PanicsWithValuef("crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) PanicsWithValuef(expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithValuef(a.t, expected, f, msg, args...) +} + +// Panicsf asserts that the code inside the specified PanicTestFunc panics. +// +// a.Panicsf(func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) Panicsf(f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Panicsf(a.t, f, msg, args...) +} + +// Positive asserts that the specified element is positive +// +// a.Positive(1) +// a.Positive(1.23) +func (a *Assertions) Positive(e interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Positive(a.t, e, msgAndArgs...) +} + +// Positivef asserts that the specified element is positive +// +// a.Positivef(1, "error message %s", "formatted") +// a.Positivef(1.23, "error message %s", "formatted") +func (a *Assertions) Positivef(e interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Positivef(a.t, e, msg, args...) +} + +// Regexp asserts that a specified regexp matches a string. +// +// a.Regexp(regexp.MustCompile("start"), "it's starting") +// a.Regexp("start...$", "it's not starting") +func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Regexp(a.t, rx, str, msgAndArgs...) +} + +// Regexpf asserts that a specified regexp matches a string. +// +// a.Regexpf(regexp.MustCompile("start"), "it's starting", "error message %s", "formatted") +// a.Regexpf("start...$", "it's not starting", "error message %s", "formatted") +func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Regexpf(a.t, rx, str, msg, args...) +} + +// Same asserts that two pointers reference the same object. +// +// a.Same(ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Same(a.t, expected, actual, msgAndArgs...) +} + +// Samef asserts that two pointers reference the same object. +// +// a.Samef(ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) Samef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Samef(a.t, expected, actual, msg, args...) +} + +// Subset asserts that the list (array, slice, or map) contains all elements +// given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// a.Subset([1, 2, 3], [1, 2]) +// a.Subset({"x": 1, "y": 2}, {"x": 1}) +// a.Subset([1, 2, 3], {1: "one", 2: "two"}) +// a.Subset({"x": 1, "y": 2}, ["x"]) +func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Subset(a.t, list, subset, msgAndArgs...) +} + +// Subsetf asserts that the list (array, slice, or map) contains all elements +// given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// a.Subsetf([1, 2, 3], [1, 2], "error message %s", "formatted") +// a.Subsetf({"x": 1, "y": 2}, {"x": 1}, "error message %s", "formatted") +// a.Subsetf([1, 2, 3], {1: "one", 2: "two"}, "error message %s", "formatted") +// a.Subsetf({"x": 1, "y": 2}, ["x"], "error message %s", "formatted") +func (a *Assertions) Subsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Subsetf(a.t, list, subset, msg, args...) +} + +// True asserts that the specified value is true. +// +// a.True(myBool) +func (a *Assertions) True(value bool, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return True(a.t, value, msgAndArgs...) +} + +// Truef asserts that the specified value is true. +// +// a.Truef(myBool, "error message %s", "formatted") +func (a *Assertions) Truef(value bool, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Truef(a.t, value, msg, args...) +} + +// WithinDuration asserts that the two times are within duration delta of each other. +// +// a.WithinDuration(time.Now(), time.Now(), 10*time.Second) +func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return WithinDuration(a.t, expected, actual, delta, msgAndArgs...) +} + +// WithinDurationf asserts that the two times are within duration delta of each other. +// +// a.WithinDurationf(time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return WithinDurationf(a.t, expected, actual, delta, msg, args...) +} + +// WithinRange asserts that a time is within a time range (inclusive). +// +// a.WithinRange(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second)) +func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return WithinRange(a.t, actual, start, end, msgAndArgs...) +} + +// WithinRangef asserts that a time is within a time range (inclusive). +// +// a.WithinRangef(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted") +func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return WithinRangef(a.t, actual, start, end, msg, args...) +} + +// YAMLEq asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return YAMLEq(a.t, expected, actual, msgAndArgs...) +} + +// YAMLEqf asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return YAMLEqf(a.t, expected, actual, msg, args...) +} + +// Zero asserts that i is the zero value for its type. +func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Zero(a.t, i, msgAndArgs...) +} + +// Zerof asserts that i is the zero value for its type. +func (a *Assertions) Zerof(i interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Zerof(a.t, i, msg, args...) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl b/vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl new file mode 100644 index 0000000000..188bb9e174 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl @@ -0,0 +1,5 @@ +{{.CommentWithoutT "a"}} +func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) bool { + if h, ok := a.t.(tHelper); ok { h.Helper() } + return {{.DocInfo.Name}}(a.t, {{.ForwardedParams}}) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_order.go b/vendor/github.com/stretchr/testify/assert/assertion_order.go new file mode 100644 index 0000000000..2fdf80fdd3 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_order.go @@ -0,0 +1,81 @@ +package assert + +import ( + "fmt" + "reflect" +) + +// isOrdered checks that collection contains orderable elements. +func isOrdered(t TestingT, object interface{}, allowedComparesResults []compareResult, failMessage string, msgAndArgs ...interface{}) bool { + objKind := reflect.TypeOf(object).Kind() + if objKind != reflect.Slice && objKind != reflect.Array { + return false + } + + objValue := reflect.ValueOf(object) + objLen := objValue.Len() + + if objLen <= 1 { + return true + } + + value := objValue.Index(0) + valueInterface := value.Interface() + firstValueKind := value.Kind() + + for i := 1; i < objLen; i++ { + prevValue := value + prevValueInterface := valueInterface + + value = objValue.Index(i) + valueInterface = value.Interface() + + compareResult, isComparable := compare(prevValueInterface, valueInterface, firstValueKind) + + if !isComparable { + return Fail(t, fmt.Sprintf(`Can not compare type "%T" and "%T"`, value, prevValue), msgAndArgs...) + } + + if !containsValue(allowedComparesResults, compareResult) { + return Fail(t, fmt.Sprintf(failMessage, prevValue, value), msgAndArgs...) + } + } + + return true +} + +// IsIncreasing asserts that the collection is increasing +// +// assert.IsIncreasing(t, []int{1, 2, 3}) +// assert.IsIncreasing(t, []float{1, 2}) +// assert.IsIncreasing(t, []string{"a", "b"}) +func IsIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + return isOrdered(t, object, []compareResult{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs...) +} + +// IsNonIncreasing asserts that the collection is not increasing +// +// assert.IsNonIncreasing(t, []int{2, 1, 1}) +// assert.IsNonIncreasing(t, []float{2, 1}) +// assert.IsNonIncreasing(t, []string{"b", "a"}) +func IsNonIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + return isOrdered(t, object, []compareResult{compareEqual, compareGreater}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs...) +} + +// IsDecreasing asserts that the collection is decreasing +// +// assert.IsDecreasing(t, []int{2, 1, 0}) +// assert.IsDecreasing(t, []float{2, 1}) +// assert.IsDecreasing(t, []string{"b", "a"}) +func IsDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + return isOrdered(t, object, []compareResult{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs...) +} + +// IsNonDecreasing asserts that the collection is not decreasing +// +// assert.IsNonDecreasing(t, []int{1, 1, 2}) +// assert.IsNonDecreasing(t, []float{1, 2}) +// assert.IsNonDecreasing(t, []string{"a", "b"}) +func IsNonDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + return isOrdered(t, object, []compareResult{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs...) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertions.go b/vendor/github.com/stretchr/testify/assert/assertions.go new file mode 100644 index 0000000000..de8de0cb6c --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertions.go @@ -0,0 +1,2295 @@ +package assert + +import ( + "bufio" + "bytes" + "encoding/json" + "errors" + "fmt" + "math" + "os" + "reflect" + "regexp" + "runtime" + "runtime/debug" + "strings" + "time" + "unicode" + "unicode/utf8" + + "github.com/davecgh/go-spew/spew" + "github.com/pmezard/go-difflib/difflib" + + // Wrapper around gopkg.in/yaml.v3 + "github.com/stretchr/testify/assert/yaml" +) + +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_format.go.tmpl" + +// TestingT is an interface wrapper around *testing.T +type TestingT interface { + Errorf(format string, args ...interface{}) +} + +// ComparisonAssertionFunc is a common function prototype when comparing two values. Can be useful +// for table driven tests. +type ComparisonAssertionFunc func(TestingT, interface{}, interface{}, ...interface{}) bool + +// ValueAssertionFunc is a common function prototype when validating a single value. Can be useful +// for table driven tests. +type ValueAssertionFunc func(TestingT, interface{}, ...interface{}) bool + +// BoolAssertionFunc is a common function prototype when validating a bool value. Can be useful +// for table driven tests. +type BoolAssertionFunc func(TestingT, bool, ...interface{}) bool + +// ErrorAssertionFunc is a common function prototype when validating an error value. Can be useful +// for table driven tests. +type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool + +// PanicAssertionFunc is a common function prototype when validating a panic value. Can be useful +// for table driven tests. +type PanicAssertionFunc = func(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool + +// Comparison is a custom function that returns true on success and false on failure +type Comparison func() (success bool) + +/* + Helper functions +*/ + +// ObjectsAreEqual determines if two objects are considered equal. +// +// This function does no assertion of any kind. +func ObjectsAreEqual(expected, actual interface{}) bool { + if expected == nil || actual == nil { + return expected == actual + } + + exp, ok := expected.([]byte) + if !ok { + return reflect.DeepEqual(expected, actual) + } + + act, ok := actual.([]byte) + if !ok { + return false + } + if exp == nil || act == nil { + return exp == nil && act == nil + } + return bytes.Equal(exp, act) +} + +// copyExportedFields iterates downward through nested data structures and creates a copy +// that only contains the exported struct fields. +func copyExportedFields(expected interface{}) interface{} { + if isNil(expected) { + return expected + } + + expectedType := reflect.TypeOf(expected) + expectedKind := expectedType.Kind() + expectedValue := reflect.ValueOf(expected) + + switch expectedKind { + case reflect.Struct: + result := reflect.New(expectedType).Elem() + for i := 0; i < expectedType.NumField(); i++ { + field := expectedType.Field(i) + isExported := field.IsExported() + if isExported { + fieldValue := expectedValue.Field(i) + if isNil(fieldValue) || isNil(fieldValue.Interface()) { + continue + } + newValue := copyExportedFields(fieldValue.Interface()) + result.Field(i).Set(reflect.ValueOf(newValue)) + } + } + return result.Interface() + + case reflect.Ptr: + result := reflect.New(expectedType.Elem()) + unexportedRemoved := copyExportedFields(expectedValue.Elem().Interface()) + result.Elem().Set(reflect.ValueOf(unexportedRemoved)) + return result.Interface() + + case reflect.Array, reflect.Slice: + var result reflect.Value + if expectedKind == reflect.Array { + result = reflect.New(reflect.ArrayOf(expectedValue.Len(), expectedType.Elem())).Elem() + } else { + result = reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len()) + } + for i := 0; i < expectedValue.Len(); i++ { + index := expectedValue.Index(i) + if isNil(index) { + continue + } + unexportedRemoved := copyExportedFields(index.Interface()) + result.Index(i).Set(reflect.ValueOf(unexportedRemoved)) + } + return result.Interface() + + case reflect.Map: + result := reflect.MakeMap(expectedType) + for _, k := range expectedValue.MapKeys() { + index := expectedValue.MapIndex(k) + unexportedRemoved := copyExportedFields(index.Interface()) + result.SetMapIndex(k, reflect.ValueOf(unexportedRemoved)) + } + return result.Interface() + + default: + return expected + } +} + +// ObjectsExportedFieldsAreEqual determines if the exported (public) fields of two objects are +// considered equal. This comparison of only exported fields is applied recursively to nested data +// structures. +// +// This function does no assertion of any kind. +// +// Deprecated: Use [EqualExportedValues] instead. +func ObjectsExportedFieldsAreEqual(expected, actual interface{}) bool { + expectedCleaned := copyExportedFields(expected) + actualCleaned := copyExportedFields(actual) + return ObjectsAreEqualValues(expectedCleaned, actualCleaned) +} + +// ObjectsAreEqualValues gets whether two objects are equal, or if their +// values are equal. +func ObjectsAreEqualValues(expected, actual interface{}) bool { + if ObjectsAreEqual(expected, actual) { + return true + } + + expectedValue := reflect.ValueOf(expected) + actualValue := reflect.ValueOf(actual) + if !expectedValue.IsValid() || !actualValue.IsValid() { + return false + } + + expectedType := expectedValue.Type() + actualType := actualValue.Type() + if !expectedType.ConvertibleTo(actualType) { + return false + } + + if !isNumericType(expectedType) || !isNumericType(actualType) { + // Attempt comparison after type conversion + return reflect.DeepEqual( + expectedValue.Convert(actualType).Interface(), actual, + ) + } + + // If BOTH values are numeric, there are chances of false positives due + // to overflow or underflow. So, we need to make sure to always convert + // the smaller type to a larger type before comparing. + if expectedType.Size() >= actualType.Size() { + return actualValue.Convert(expectedType).Interface() == expected + } + + return expectedValue.Convert(actualType).Interface() == actual +} + +// isNumericType returns true if the type is one of: +// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, +// float32, float64, complex64, complex128 +func isNumericType(t reflect.Type) bool { + return t.Kind() >= reflect.Int && t.Kind() <= reflect.Complex128 +} + +/* CallerInfo is necessary because the assert functions use the testing object +internally, causing it to print the file:line of the assert method, rather than where +the problem actually occurred in calling code.*/ + +// CallerInfo returns an array of strings containing the file and line number +// of each stack frame leading from the current test to the assert call that +// failed. +func CallerInfo() []string { + var pc uintptr + var file string + var line int + var name string + + const stackFrameBufferSize = 10 + pcs := make([]uintptr, stackFrameBufferSize) + + callers := []string{} + offset := 1 + + for { + n := runtime.Callers(offset, pcs) + + if n == 0 { + break + } + + frames := runtime.CallersFrames(pcs[:n]) + + for { + frame, more := frames.Next() + pc = frame.PC + file = frame.File + line = frame.Line + + // This is a huge edge case, but it will panic if this is the case, see #180 + if file == "" { + break + } + + f := runtime.FuncForPC(pc) + if f == nil { + break + } + name = f.Name() + + // testing.tRunner is the standard library function that calls + // tests. Subtests are called directly by tRunner, without going through + // the Test/Benchmark/Example function that contains the t.Run calls, so + // with subtests we should break when we hit tRunner, without adding it + // to the list of callers. + if name == "testing.tRunner" { + break + } + + parts := strings.Split(file, "/") + if len(parts) > 1 { + filename := parts[len(parts)-1] + dir := parts[len(parts)-2] + if (dir != "assert" && dir != "mock" && dir != "require") || filename == "mock_test.go" { + callers = append(callers, fmt.Sprintf("%s:%d", file, line)) + } + } + + // Drop the package + dotPos := strings.LastIndexByte(name, '.') + name = name[dotPos+1:] + if isTest(name, "Test") || + isTest(name, "Benchmark") || + isTest(name, "Example") { + break + } + + if !more { + break + } + } + + // Next batch + offset += cap(pcs) + } + + return callers +} + +// Stolen from the `go test` tool. +// isTest tells whether name looks like a test (or benchmark, according to prefix). +// It is a Test (say) if there is a character after Test that is not a lower-case letter. +// We don't want TesticularCancer. +func isTest(name, prefix string) bool { + if !strings.HasPrefix(name, prefix) { + return false + } + if len(name) == len(prefix) { // "Test" is ok + return true + } + r, _ := utf8.DecodeRuneInString(name[len(prefix):]) + return !unicode.IsLower(r) +} + +func messageFromMsgAndArgs(msgAndArgs ...interface{}) string { + if len(msgAndArgs) == 0 || msgAndArgs == nil { + return "" + } + if len(msgAndArgs) == 1 { + msg := msgAndArgs[0] + if msgAsStr, ok := msg.(string); ok { + return msgAsStr + } + return fmt.Sprintf("%+v", msg) + } + if len(msgAndArgs) > 1 { + return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) + } + return "" +} + +// Aligns the provided message so that all lines after the first line start at the same location as the first line. +// Assumes that the first line starts at the correct location (after carriage return, tab, label, spacer and tab). +// The longestLabelLen parameter specifies the length of the longest label in the output (required because this is the +// basis on which the alignment occurs). +func indentMessageLines(message string, longestLabelLen int) string { + outBuf := new(bytes.Buffer) + + for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ { + // no need to align first line because it starts at the correct location (after the label) + if i != 0 { + // append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab + outBuf.WriteString("\n\t" + strings.Repeat(" ", longestLabelLen+1) + "\t") + } + outBuf.WriteString(scanner.Text()) + } + + return outBuf.String() +} + +type failNower interface { + FailNow() +} + +// FailNow fails test +func FailNow(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + Fail(t, failureMessage, msgAndArgs...) + + // We cannot extend TestingT with FailNow() and + // maintain backwards compatibility, so we fallback + // to panicking when FailNow is not available in + // TestingT. + // See issue #263 + + if t, ok := t.(failNower); ok { + t.FailNow() + } else { + panic("test failed and t is missing `FailNow()`") + } + return false +} + +// Fail reports a failure through +func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + content := []labeledContent{ + {"Error Trace", strings.Join(CallerInfo(), "\n\t\t\t")}, + {"Error", failureMessage}, + } + + // Add test name if the Go version supports it + if n, ok := t.(interface { + Name() string + }); ok { + content = append(content, labeledContent{"Test", n.Name()}) + } + + message := messageFromMsgAndArgs(msgAndArgs...) + if len(message) > 0 { + content = append(content, labeledContent{"Messages", message}) + } + + t.Errorf("\n%s", ""+labeledOutput(content...)) + + return false +} + +type labeledContent struct { + label string + content string +} + +// labeledOutput returns a string consisting of the provided labeledContent. Each labeled output is appended in the following manner: +// +// \t{{label}}:{{align_spaces}}\t{{content}}\n +// +// The initial carriage return is required to undo/erase any padding added by testing.T.Errorf. The "\t{{label}}:" is for the label. +// If a label is shorter than the longest label provided, padding spaces are added to make all the labels match in length. Once this +// alignment is achieved, "\t{{content}}\n" is added for the output. +// +// If the content of the labeledOutput contains line breaks, the subsequent lines are aligned so that they start at the same location as the first line. +func labeledOutput(content ...labeledContent) string { + longestLabel := 0 + for _, v := range content { + if len(v.label) > longestLabel { + longestLabel = len(v.label) + } + } + var output string + for _, v := range content { + output += "\t" + v.label + ":" + strings.Repeat(" ", longestLabel-len(v.label)) + "\t" + indentMessageLines(v.content, longestLabel) + "\n" + } + return output +} + +// Implements asserts that an object is implemented by the specified interface. +// +// assert.Implements(t, (*MyInterface)(nil), new(MyObject)) +func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + interfaceType := reflect.TypeOf(interfaceObject).Elem() + + if object == nil { + return Fail(t, fmt.Sprintf("Cannot check if nil implements %v", interfaceType), msgAndArgs...) + } + if !reflect.TypeOf(object).Implements(interfaceType) { + return Fail(t, fmt.Sprintf("%T must implement %v", object, interfaceType), msgAndArgs...) + } + + return true +} + +// NotImplements asserts that an object does not implement the specified interface. +// +// assert.NotImplements(t, (*MyInterface)(nil), new(MyObject)) +func NotImplements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + interfaceType := reflect.TypeOf(interfaceObject).Elem() + + if object == nil { + return Fail(t, fmt.Sprintf("Cannot check if nil does not implement %v", interfaceType), msgAndArgs...) + } + if reflect.TypeOf(object).Implements(interfaceType) { + return Fail(t, fmt.Sprintf("%T implements %v", object, interfaceType), msgAndArgs...) + } + + return true +} + +func isType(expectedType, object interface{}) bool { + return ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) +} + +// IsType asserts that the specified objects are of the same type. +// +// assert.IsType(t, &MyStruct{}, &MyStruct{}) +func IsType(t TestingT, expectedType, object interface{}, msgAndArgs ...interface{}) bool { + if isType(expectedType, object) { + return true + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, fmt.Sprintf("Object expected to be of type %T, but was %T", expectedType, object), msgAndArgs...) +} + +// IsNotType asserts that the specified objects are not of the same type. +// +// assert.IsNotType(t, &NotMyStruct{}, &MyStruct{}) +func IsNotType(t TestingT, theType, object interface{}, msgAndArgs ...interface{}) bool { + if !isType(theType, object) { + return true + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, fmt.Sprintf("Object type expected to be different than %T", theType), msgAndArgs...) +} + +// Equal asserts that two objects are equal. +// +// assert.Equal(t, 123, 123) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if err := validateEqualArgs(expected, actual); err != nil { + return Fail(t, fmt.Sprintf("Invalid operation: %#v == %#v (%s)", + expected, actual, err), msgAndArgs...) + } + + if !ObjectsAreEqual(expected, actual) { + diff := diff(expected, actual) + expected, actual = formatUnequalValues(expected, actual) + return Fail(t, fmt.Sprintf("Not equal: \n"+ + "expected: %s\n"+ + "actual : %s%s", expected, actual, diff), msgAndArgs...) + } + + return true +} + +// validateEqualArgs checks whether provided arguments can be safely used in the +// Equal/NotEqual functions. +func validateEqualArgs(expected, actual interface{}) error { + if expected == nil && actual == nil { + return nil + } + + if isFunction(expected) || isFunction(actual) { + return errors.New("cannot take func type as argument") + } + return nil +} + +// Same asserts that two pointers reference the same object. +// +// assert.Same(t, ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + same, ok := samePointers(expected, actual) + if !ok { + return Fail(t, "Both arguments must be pointers", msgAndArgs...) + } + + if !same { + // both are pointers but not the same type & pointing to the same address + return Fail(t, fmt.Sprintf("Not same: \n"+ + "expected: %p %#[1]v\n"+ + "actual : %p %#[2]v", + expected, actual), msgAndArgs...) + } + + return true +} + +// NotSame asserts that two pointers do not reference the same object. +// +// assert.NotSame(t, ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + same, ok := samePointers(expected, actual) + if !ok { + // fails when the arguments are not pointers + return !(Fail(t, "Both arguments must be pointers", msgAndArgs...)) + } + + if same { + return Fail(t, fmt.Sprintf( + "Expected and actual point to the same object: %p %#[1]v", + expected), msgAndArgs...) + } + return true +} + +// samePointers checks if two generic interface objects are pointers of the same +// type pointing to the same object. It returns two values: same indicating if +// they are the same type and point to the same object, and ok indicating that +// both inputs are pointers. +func samePointers(first, second interface{}) (same bool, ok bool) { + firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second) + if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr { + return false, false // not both are pointers + } + + firstType, secondType := reflect.TypeOf(first), reflect.TypeOf(second) + if firstType != secondType { + return false, true // both are pointers, but of different types + } + + // compare pointer addresses + return first == second, true +} + +// formatUnequalValues takes two values of arbitrary types and returns string +// representations appropriate to be presented to the user. +// +// If the values are not of like type, the returned strings will be prefixed +// with the type name, and the value will be enclosed in parentheses similar +// to a type conversion in the Go grammar. +func formatUnequalValues(expected, actual interface{}) (e string, a string) { + if reflect.TypeOf(expected) != reflect.TypeOf(actual) { + return fmt.Sprintf("%T(%s)", expected, truncatingFormat(expected)), + fmt.Sprintf("%T(%s)", actual, truncatingFormat(actual)) + } + switch expected.(type) { + case time.Duration: + return fmt.Sprintf("%v", expected), fmt.Sprintf("%v", actual) + } + return truncatingFormat(expected), truncatingFormat(actual) +} + +// truncatingFormat formats the data and truncates it if it's too long. +// +// This helps keep formatted error messages lines from exceeding the +// bufio.MaxScanTokenSize max line length that the go testing framework imposes. +func truncatingFormat(data interface{}) string { + value := fmt.Sprintf("%#v", data) + max := bufio.MaxScanTokenSize - 100 // Give us some space the type info too if needed. + if len(value) > max { + value = value[0:max] + "<... truncated>" + } + return value +} + +// EqualValues asserts that two objects are equal or convertible to the larger +// type and equal. +// +// assert.EqualValues(t, uint32(123), int32(123)) +func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if !ObjectsAreEqualValues(expected, actual) { + diff := diff(expected, actual) + expected, actual = formatUnequalValues(expected, actual) + return Fail(t, fmt.Sprintf("Not equal: \n"+ + "expected: %s\n"+ + "actual : %s%s", expected, actual, diff), msgAndArgs...) + } + + return true +} + +// EqualExportedValues asserts that the types of two objects are equal and their public +// fields are also equal. This is useful for comparing structs that have private fields +// that could potentially differ. +// +// type S struct { +// Exported int +// notExported int +// } +// assert.EqualExportedValues(t, S{1, 2}, S{1, 3}) => true +// assert.EqualExportedValues(t, S{1, 2}, S{2, 3}) => false +func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + aType := reflect.TypeOf(expected) + bType := reflect.TypeOf(actual) + + if aType != bType { + return Fail(t, fmt.Sprintf("Types expected to match exactly\n\t%v != %v", aType, bType), msgAndArgs...) + } + + expected = copyExportedFields(expected) + actual = copyExportedFields(actual) + + if !ObjectsAreEqualValues(expected, actual) { + diff := diff(expected, actual) + expected, actual = formatUnequalValues(expected, actual) + return Fail(t, fmt.Sprintf("Not equal (comparing only exported fields): \n"+ + "expected: %s\n"+ + "actual : %s%s", expected, actual, diff), msgAndArgs...) + } + + return true +} + +// Exactly asserts that two objects are equal in value and type. +// +// assert.Exactly(t, int32(123), int64(123)) +func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + aType := reflect.TypeOf(expected) + bType := reflect.TypeOf(actual) + + if aType != bType { + return Fail(t, fmt.Sprintf("Types expected to match exactly\n\t%v != %v", aType, bType), msgAndArgs...) + } + + return Equal(t, expected, actual, msgAndArgs...) +} + +// NotNil asserts that the specified object is not nil. +// +// assert.NotNil(t, err) +func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + if !isNil(object) { + return true + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "Expected value not to be nil.", msgAndArgs...) +} + +// isNil checks if a specified object is nil or not, without Failing. +func isNil(object interface{}) bool { + if object == nil { + return true + } + + value := reflect.ValueOf(object) + switch value.Kind() { + case + reflect.Chan, reflect.Func, + reflect.Interface, reflect.Map, + reflect.Ptr, reflect.Slice, reflect.UnsafePointer: + + return value.IsNil() + } + + return false +} + +// Nil asserts that the specified object is nil. +// +// assert.Nil(t, err) +func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + if isNil(object) { + return true + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, fmt.Sprintf("Expected nil, but got: %#v", object), msgAndArgs...) +} + +// isEmpty gets whether the specified object is considered empty or not. +func isEmpty(object interface{}) bool { + // get nil case out of the way + if object == nil { + return true + } + + return isEmptyValue(reflect.ValueOf(object)) +} + +// isEmptyValue gets whether the specified reflect.Value is considered empty or not. +func isEmptyValue(objValue reflect.Value) bool { + if objValue.IsZero() { + return true + } + // Special cases of non-zero values that we consider empty + switch objValue.Kind() { + // collection types are empty when they have no element + // Note: array types are empty when they match their zero-initialized state. + case reflect.Chan, reflect.Map, reflect.Slice: + return objValue.Len() == 0 + // non-nil pointers are empty if the value they point to is empty + case reflect.Ptr: + return isEmptyValue(objValue.Elem()) + } + return false +} + +// Empty asserts that the given value is "empty". +// +// [Zero values] are "empty". +// +// Arrays are "empty" if every element is the zero value of the type (stricter than "empty"). +// +// Slices, maps and channels with zero length are "empty". +// +// Pointer values are "empty" if the pointer is nil or if the pointed value is "empty". +// +// assert.Empty(t, obj) +// +// [Zero values]: https://go.dev/ref/spec#The_zero_value +func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + pass := isEmpty(object) + if !pass { + if h, ok := t.(tHelper); ok { + h.Helper() + } + Fail(t, fmt.Sprintf("Should be empty, but was %v", object), msgAndArgs...) + } + + return pass +} + +// NotEmpty asserts that the specified object is NOT [Empty]. +// +// if assert.NotEmpty(t, obj) { +// assert.Equal(t, "two", obj[1]) +// } +func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + pass := !isEmpty(object) + if !pass { + if h, ok := t.(tHelper); ok { + h.Helper() + } + Fail(t, fmt.Sprintf("Should NOT be empty, but was %v", object), msgAndArgs...) + } + + return pass +} + +// getLen tries to get the length of an object. +// It returns (0, false) if impossible. +func getLen(x interface{}) (length int, ok bool) { + v := reflect.ValueOf(x) + defer func() { + ok = recover() == nil + }() + return v.Len(), true +} + +// Len asserts that the specified object has specific length. +// Len also fails if the object has a type that len() not accept. +// +// assert.Len(t, mySlice, 3) +func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + l, ok := getLen(object) + if !ok { + return Fail(t, fmt.Sprintf("\"%v\" could not be applied builtin len()", object), msgAndArgs...) + } + + if l != length { + return Fail(t, fmt.Sprintf("\"%v\" should have %d item(s), but has %d", object, length, l), msgAndArgs...) + } + return true +} + +// True asserts that the specified value is true. +// +// assert.True(t, myBool) +func True(t TestingT, value bool, msgAndArgs ...interface{}) bool { + if !value { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "Should be true", msgAndArgs...) + } + + return true +} + +// False asserts that the specified value is false. +// +// assert.False(t, myBool) +func False(t TestingT, value bool, msgAndArgs ...interface{}) bool { + if value { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "Should be false", msgAndArgs...) + } + + return true +} + +// NotEqual asserts that the specified values are NOT equal. +// +// assert.NotEqual(t, obj1, obj2) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if err := validateEqualArgs(expected, actual); err != nil { + return Fail(t, fmt.Sprintf("Invalid operation: %#v != %#v (%s)", + expected, actual, err), msgAndArgs...) + } + + if ObjectsAreEqual(expected, actual) { + return Fail(t, fmt.Sprintf("Should not be: %#v\n", actual), msgAndArgs...) + } + + return true +} + +// NotEqualValues asserts that two objects are not equal even when converted to the same type +// +// assert.NotEqualValues(t, obj1, obj2) +func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if ObjectsAreEqualValues(expected, actual) { + return Fail(t, fmt.Sprintf("Should not be: %#v\n", actual), msgAndArgs...) + } + + return true +} + +// containsElement try loop over the list check if the list includes the element. +// return (false, false) if impossible. +// return (true, false) if element was not found. +// return (true, true) if element was found. +func containsElement(list interface{}, element interface{}) (ok, found bool) { + listValue := reflect.ValueOf(list) + listType := reflect.TypeOf(list) + if listType == nil { + return false, false + } + listKind := listType.Kind() + defer func() { + if e := recover(); e != nil { + ok = false + found = false + } + }() + + if listKind == reflect.String { + elementValue := reflect.ValueOf(element) + return true, strings.Contains(listValue.String(), elementValue.String()) + } + + if listKind == reflect.Map { + mapKeys := listValue.MapKeys() + for i := 0; i < len(mapKeys); i++ { + if ObjectsAreEqual(mapKeys[i].Interface(), element) { + return true, true + } + } + return true, false + } + + for i := 0; i < listValue.Len(); i++ { + if ObjectsAreEqual(listValue.Index(i).Interface(), element) { + return true, true + } + } + return true, false +} + +// Contains asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// assert.Contains(t, "Hello World", "World") +// assert.Contains(t, ["Hello", "World"], "World") +// assert.Contains(t, {"Hello": "World"}, "Hello") +func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ok, found := containsElement(s, contains) + if !ok { + return Fail(t, fmt.Sprintf("%#v could not be applied builtin len()", s), msgAndArgs...) + } + if !found { + return Fail(t, fmt.Sprintf("%#v does not contain %#v", s, contains), msgAndArgs...) + } + + return true +} + +// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// assert.NotContains(t, "Hello World", "Earth") +// assert.NotContains(t, ["Hello", "World"], "Earth") +// assert.NotContains(t, {"Hello": "World"}, "Earth") +func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ok, found := containsElement(s, contains) + if !ok { + return Fail(t, fmt.Sprintf("%#v could not be applied builtin len()", s), msgAndArgs...) + } + if found { + return Fail(t, fmt.Sprintf("%#v should not contain %#v", s, contains), msgAndArgs...) + } + + return true +} + +// Subset asserts that the list (array, slice, or map) contains all elements +// given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// assert.Subset(t, [1, 2, 3], [1, 2]) +// assert.Subset(t, {"x": 1, "y": 2}, {"x": 1}) +// assert.Subset(t, [1, 2, 3], {1: "one", 2: "two"}) +// assert.Subset(t, {"x": 1, "y": 2}, ["x"]) +func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if subset == nil { + return true // we consider nil to be equal to the nil set + } + + listKind := reflect.TypeOf(list).Kind() + if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...) + } + + subsetKind := reflect.TypeOf(subset).Kind() + if subsetKind != reflect.Array && subsetKind != reflect.Slice && subsetKind != reflect.Map { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...) + } + + if subsetKind == reflect.Map && listKind == reflect.Map { + subsetMap := reflect.ValueOf(subset) + actualMap := reflect.ValueOf(list) + + for _, k := range subsetMap.MapKeys() { + ev := subsetMap.MapIndex(k) + av := actualMap.MapIndex(k) + + if !av.IsValid() { + return Fail(t, fmt.Sprintf("%#v does not contain %#v", list, subset), msgAndArgs...) + } + if !ObjectsAreEqual(ev.Interface(), av.Interface()) { + return Fail(t, fmt.Sprintf("%#v does not contain %#v", list, subset), msgAndArgs...) + } + } + + return true + } + + subsetList := reflect.ValueOf(subset) + if subsetKind == reflect.Map { + keys := make([]interface{}, subsetList.Len()) + for idx, key := range subsetList.MapKeys() { + keys[idx] = key.Interface() + } + subsetList = reflect.ValueOf(keys) + } + for i := 0; i < subsetList.Len(); i++ { + element := subsetList.Index(i).Interface() + ok, found := containsElement(list, element) + if !ok { + return Fail(t, fmt.Sprintf("%#v could not be applied builtin len()", list), msgAndArgs...) + } + if !found { + return Fail(t, fmt.Sprintf("%#v does not contain %#v", list, element), msgAndArgs...) + } + } + + return true +} + +// NotSubset asserts that the list (array, slice, or map) does NOT contain all +// elements given in the subset (array, slice, or map). +// Map elements are key-value pairs unless compared with an array or slice where +// only the map key is evaluated. +// +// assert.NotSubset(t, [1, 3, 4], [1, 2]) +// assert.NotSubset(t, {"x": 1, "y": 2}, {"z": 3}) +// assert.NotSubset(t, [1, 3, 4], {1: "one", 2: "two"}) +// assert.NotSubset(t, {"x": 1, "y": 2}, ["z"]) +func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if subset == nil { + return Fail(t, "nil is the empty set which is a subset of every set", msgAndArgs...) + } + + listKind := reflect.TypeOf(list).Kind() + if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...) + } + + subsetKind := reflect.TypeOf(subset).Kind() + if subsetKind != reflect.Array && subsetKind != reflect.Slice && subsetKind != reflect.Map { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...) + } + + if subsetKind == reflect.Map && listKind == reflect.Map { + subsetMap := reflect.ValueOf(subset) + actualMap := reflect.ValueOf(list) + + for _, k := range subsetMap.MapKeys() { + ev := subsetMap.MapIndex(k) + av := actualMap.MapIndex(k) + + if !av.IsValid() { + return true + } + if !ObjectsAreEqual(ev.Interface(), av.Interface()) { + return true + } + } + + return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...) + } + + subsetList := reflect.ValueOf(subset) + if subsetKind == reflect.Map { + keys := make([]interface{}, subsetList.Len()) + for idx, key := range subsetList.MapKeys() { + keys[idx] = key.Interface() + } + subsetList = reflect.ValueOf(keys) + } + for i := 0; i < subsetList.Len(); i++ { + element := subsetList.Index(i).Interface() + ok, found := containsElement(list, element) + if !ok { + return Fail(t, fmt.Sprintf("%q could not be applied builtin len()", list), msgAndArgs...) + } + if !found { + return true + } + } + + return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...) +} + +// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// assert.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2]) +func ElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if isEmpty(listA) && isEmpty(listB) { + return true + } + + if !isList(t, listA, msgAndArgs...) || !isList(t, listB, msgAndArgs...) { + return false + } + + extraA, extraB := diffLists(listA, listB) + + if len(extraA) == 0 && len(extraB) == 0 { + return true + } + + return Fail(t, formatListDiff(listA, listB, extraA, extraB), msgAndArgs...) +} + +// isList checks that the provided value is array or slice. +func isList(t TestingT, list interface{}, msgAndArgs ...interface{}) (ok bool) { + kind := reflect.TypeOf(list).Kind() + if kind != reflect.Array && kind != reflect.Slice { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s, expecting array or slice", list, kind), + msgAndArgs...) + } + return true +} + +// diffLists diffs two arrays/slices and returns slices of elements that are only in A and only in B. +// If some element is present multiple times, each instance is counted separately (e.g. if something is 2x in A and +// 5x in B, it will be 0x in extraA and 3x in extraB). The order of items in both lists is ignored. +func diffLists(listA, listB interface{}) (extraA, extraB []interface{}) { + aValue := reflect.ValueOf(listA) + bValue := reflect.ValueOf(listB) + + aLen := aValue.Len() + bLen := bValue.Len() + + // Mark indexes in bValue that we already used + visited := make([]bool, bLen) + for i := 0; i < aLen; i++ { + element := aValue.Index(i).Interface() + found := false + for j := 0; j < bLen; j++ { + if visited[j] { + continue + } + if ObjectsAreEqual(bValue.Index(j).Interface(), element) { + visited[j] = true + found = true + break + } + } + if !found { + extraA = append(extraA, element) + } + } + + for j := 0; j < bLen; j++ { + if visited[j] { + continue + } + extraB = append(extraB, bValue.Index(j).Interface()) + } + + return +} + +func formatListDiff(listA, listB interface{}, extraA, extraB []interface{}) string { + var msg bytes.Buffer + + msg.WriteString("elements differ") + if len(extraA) > 0 { + msg.WriteString("\n\nextra elements in list A:\n") + msg.WriteString(spewConfig.Sdump(extraA)) + } + if len(extraB) > 0 { + msg.WriteString("\n\nextra elements in list B:\n") + msg.WriteString(spewConfig.Sdump(extraB)) + } + msg.WriteString("\n\nlistA:\n") + msg.WriteString(spewConfig.Sdump(listA)) + msg.WriteString("\n\nlistB:\n") + msg.WriteString(spewConfig.Sdump(listB)) + + return msg.String() +} + +// NotElementsMatch asserts that the specified listA(array, slice...) is NOT equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should not match. +// This is an inverse of ElementsMatch. +// +// assert.NotElementsMatch(t, [1, 1, 2, 3], [1, 1, 2, 3]) -> false +// +// assert.NotElementsMatch(t, [1, 1, 2, 3], [1, 2, 3]) -> true +// +// assert.NotElementsMatch(t, [1, 2, 3], [1, 2, 4]) -> true +func NotElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if isEmpty(listA) && isEmpty(listB) { + return Fail(t, "listA and listB contain the same elements", msgAndArgs) + } + + if !isList(t, listA, msgAndArgs...) { + return Fail(t, "listA is not a list type", msgAndArgs...) + } + if !isList(t, listB, msgAndArgs...) { + return Fail(t, "listB is not a list type", msgAndArgs...) + } + + extraA, extraB := diffLists(listA, listB) + if len(extraA) == 0 && len(extraB) == 0 { + return Fail(t, "listA and listB contain the same elements", msgAndArgs) + } + + return true +} + +// Condition uses a Comparison to assert a complex condition. +func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + result := comp() + if !result { + Fail(t, "Condition failed!", msgAndArgs...) + } + return result +} + +// PanicTestFunc defines a func that should be passed to the assert.Panics and assert.NotPanics +// methods, and represents a simple func that takes no arguments, and returns nothing. +type PanicTestFunc func() + +// didPanic returns true if the function passed to it panics. Otherwise, it returns false. +func didPanic(f PanicTestFunc) (didPanic bool, message interface{}, stack string) { + didPanic = true + + defer func() { + message = recover() + if didPanic { + stack = string(debug.Stack()) + } + }() + + // call the target function + f() + didPanic = false + + return +} + +// Panics asserts that the code inside the specified PanicTestFunc panics. +// +// assert.Panics(t, func(){ GoCrazy() }) +func Panics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if funcDidPanic, panicValue, _ := didPanic(f); !funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) + } + + return true +} + +// PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// assert.PanicsWithValue(t, "crazy error", func(){ GoCrazy() }) +func PanicsWithValue(t TestingT, expected interface{}, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + funcDidPanic, panicValue, panickedStack := didPanic(f) + if !funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) + } + if panicValue != expected { + return Fail(t, fmt.Sprintf("func %#v should panic with value:\t%#v\n\tPanic value:\t%#v\n\tPanic stack:\t%s", f, expected, panicValue, panickedStack), msgAndArgs...) + } + + return true +} + +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithError(t, "crazy error", func(){ GoCrazy() }) +func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + funcDidPanic, panicValue, panickedStack := didPanic(f) + if !funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) + } + panicErr, ok := panicValue.(error) + if !ok || panicErr.Error() != errString { + return Fail(t, fmt.Sprintf("func %#v should panic with error message:\t%#v\n\tPanic value:\t%#v\n\tPanic stack:\t%s", f, errString, panicValue, panickedStack), msgAndArgs...) + } + + return true +} + +// NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// assert.NotPanics(t, func(){ RemainCalm() }) +func NotPanics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if funcDidPanic, panicValue, panickedStack := didPanic(f); funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should not panic\n\tPanic value:\t%v\n\tPanic stack:\t%s", f, panicValue, panickedStack), msgAndArgs...) + } + + return true +} + +// WithinDuration asserts that the two times are within duration delta of each other. +// +// assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second) +func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + dt := expected.Sub(actual) + if dt < -delta || dt > delta { + return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...) + } + + return true +} + +// WithinRange asserts that a time is within a time range (inclusive). +// +// assert.WithinRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second)) +func WithinRange(t TestingT, actual, start, end time.Time, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if end.Before(start) { + return Fail(t, "Start should be before end", msgAndArgs...) + } + + if actual.Before(start) { + return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is before the range", actual, start, end), msgAndArgs...) + } else if actual.After(end) { + return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is after the range", actual, start, end), msgAndArgs...) + } + + return true +} + +func toFloat(x interface{}) (float64, bool) { + var xf float64 + xok := true + + switch xn := x.(type) { + case uint: + xf = float64(xn) + case uint8: + xf = float64(xn) + case uint16: + xf = float64(xn) + case uint32: + xf = float64(xn) + case uint64: + xf = float64(xn) + case int: + xf = float64(xn) + case int8: + xf = float64(xn) + case int16: + xf = float64(xn) + case int32: + xf = float64(xn) + case int64: + xf = float64(xn) + case float32: + xf = float64(xn) + case float64: + xf = xn + case time.Duration: + xf = float64(xn) + default: + xok = false + } + + return xf, xok +} + +// InDelta asserts that the two numerals are within delta of each other. +// +// assert.InDelta(t, math.Pi, 22/7.0, 0.01) +func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + af, aok := toFloat(expected) + bf, bok := toFloat(actual) + + if !aok || !bok { + return Fail(t, "Parameters must be numerical", msgAndArgs...) + } + + if math.IsNaN(af) && math.IsNaN(bf) { + return true + } + + if math.IsNaN(af) { + return Fail(t, "Expected must not be NaN", msgAndArgs...) + } + + if math.IsNaN(bf) { + return Fail(t, fmt.Sprintf("Expected %v with delta %v, but was NaN", expected, delta), msgAndArgs...) + } + + dt := af - bf + if dt < -delta || dt > delta { + return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...) + } + + return true +} + +// InDeltaSlice is the same as InDelta, except it compares two slices. +func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if expected == nil || actual == nil || + reflect.TypeOf(actual).Kind() != reflect.Slice || + reflect.TypeOf(expected).Kind() != reflect.Slice { + return Fail(t, "Parameters must be slice", msgAndArgs...) + } + + actualSlice := reflect.ValueOf(actual) + expectedSlice := reflect.ValueOf(expected) + + for i := 0; i < actualSlice.Len(); i++ { + result := InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta, msgAndArgs...) + if !result { + return result + } + } + + return true +} + +// InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if expected == nil || actual == nil || + reflect.TypeOf(actual).Kind() != reflect.Map || + reflect.TypeOf(expected).Kind() != reflect.Map { + return Fail(t, "Arguments must be maps", msgAndArgs...) + } + + expectedMap := reflect.ValueOf(expected) + actualMap := reflect.ValueOf(actual) + + if expectedMap.Len() != actualMap.Len() { + return Fail(t, "Arguments must have the same number of keys", msgAndArgs...) + } + + for _, k := range expectedMap.MapKeys() { + ev := expectedMap.MapIndex(k) + av := actualMap.MapIndex(k) + + if !ev.IsValid() { + return Fail(t, fmt.Sprintf("missing key %q in expected map", k), msgAndArgs...) + } + + if !av.IsValid() { + return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...) + } + + if !InDelta( + t, + ev.Interface(), + av.Interface(), + delta, + msgAndArgs..., + ) { + return false + } + } + + return true +} + +func calcRelativeError(expected, actual interface{}) (float64, error) { + af, aok := toFloat(expected) + bf, bok := toFloat(actual) + if !aok || !bok { + return 0, fmt.Errorf("Parameters must be numerical") + } + if math.IsNaN(af) && math.IsNaN(bf) { + return 0, nil + } + if math.IsNaN(af) { + return 0, errors.New("expected value must not be NaN") + } + if af == 0 { + return 0, fmt.Errorf("expected value must have a value other than zero to calculate the relative error") + } + if math.IsNaN(bf) { + return 0, errors.New("actual value must not be NaN") + } + + return math.Abs(af-bf) / math.Abs(af), nil +} + +// InEpsilon asserts that expected and actual have a relative error less than epsilon +func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if math.IsNaN(epsilon) { + return Fail(t, "epsilon must not be NaN", msgAndArgs...) + } + actualEpsilon, err := calcRelativeError(expected, actual) + if err != nil { + return Fail(t, err.Error(), msgAndArgs...) + } + if math.IsNaN(actualEpsilon) { + return Fail(t, "relative error is NaN", msgAndArgs...) + } + if actualEpsilon > epsilon { + return Fail(t, fmt.Sprintf("Relative error is too high: %#v (expected)\n"+ + " < %#v (actual)", epsilon, actualEpsilon), msgAndArgs...) + } + + return true +} + +// InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices. +func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if expected == nil || actual == nil { + return Fail(t, "Parameters must be slice", msgAndArgs...) + } + + expectedSlice := reflect.ValueOf(expected) + actualSlice := reflect.ValueOf(actual) + + if expectedSlice.Type().Kind() != reflect.Slice { + return Fail(t, "Expected value must be slice", msgAndArgs...) + } + + expectedLen := expectedSlice.Len() + if !IsType(t, expected, actual) || !Len(t, actual, expectedLen) { + return false + } + + for i := 0; i < expectedLen; i++ { + if !InEpsilon(t, expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface(), epsilon, "at index %d", i) { + return false + } + } + + return true +} + +/* + Errors +*/ + +// NoError asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if assert.NoError(t, err) { +// assert.Equal(t, expectedObj, actualObj) +// } +func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool { + if err != nil { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, fmt.Sprintf("Received unexpected error:\n%+v", err), msgAndArgs...) + } + + return true +} + +// Error asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// assert.Error(t, err) +func Error(t TestingT, err error, msgAndArgs ...interface{}) bool { + if err == nil { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "An error is expected but got nil.", msgAndArgs...) + } + + return true +} + +// EqualError asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// assert.EqualError(t, err, expectedErrorString) +func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if !Error(t, theError, msgAndArgs...) { + return false + } + expected := errString + actual := theError.Error() + // don't need to use deep equals here, we know they are both strings + if expected != actual { + return Fail(t, fmt.Sprintf("Error message not equal:\n"+ + "expected: %q\n"+ + "actual : %q", expected, actual), msgAndArgs...) + } + return true +} + +// ErrorContains asserts that a function returned an error (i.e. not `nil`) +// and that the error contains the specified substring. +// +// actualObj, err := SomeFunction() +// assert.ErrorContains(t, err, expectedErrorSubString) +func ErrorContains(t TestingT, theError error, contains string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if !Error(t, theError, msgAndArgs...) { + return false + } + + actual := theError.Error() + if !strings.Contains(actual, contains) { + return Fail(t, fmt.Sprintf("Error %#v does not contain %#v", actual, contains), msgAndArgs...) + } + + return true +} + +// matchRegexp return true if a specified regexp matches a string. +func matchRegexp(rx interface{}, str interface{}) bool { + var r *regexp.Regexp + if rr, ok := rx.(*regexp.Regexp); ok { + r = rr + } else { + r = regexp.MustCompile(fmt.Sprint(rx)) + } + + switch v := str.(type) { + case []byte: + return r.Match(v) + case string: + return r.MatchString(v) + default: + return r.MatchString(fmt.Sprint(v)) + } +} + +// Regexp asserts that a specified regexp matches a string. +// +// assert.Regexp(t, regexp.MustCompile("start"), "it's starting") +// assert.Regexp(t, "start...$", "it's not starting") +func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + match := matchRegexp(rx, str) + + if !match { + Fail(t, fmt.Sprintf("Expect \"%v\" to match \"%v\"", str, rx), msgAndArgs...) + } + + return match +} + +// NotRegexp asserts that a specified regexp does not match a string. +// +// assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting") +// assert.NotRegexp(t, "^start", "it's not starting") +func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + match := matchRegexp(rx, str) + + if match { + Fail(t, fmt.Sprintf("Expect \"%v\" to NOT match \"%v\"", str, rx), msgAndArgs...) + } + + return !match +} + +// Zero asserts that i is the zero value for its type. +func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if i != nil && !reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface()) { + return Fail(t, fmt.Sprintf("Should be zero, but was %v", i), msgAndArgs...) + } + return true +} + +// NotZero asserts that i is not the zero value for its type. +func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if i == nil || reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface()) { + return Fail(t, fmt.Sprintf("Should not be zero, but was %v", i), msgAndArgs...) + } + return true +} + +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func FileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return Fail(t, fmt.Sprintf("unable to find file %q", path), msgAndArgs...) + } + return Fail(t, fmt.Sprintf("error when running os.Lstat(%q): %s", path, err), msgAndArgs...) + } + if info.IsDir() { + return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...) + } + return true +} + +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + return true + } + if info.IsDir() { + return true + } + return Fail(t, fmt.Sprintf("file %q exists", path), msgAndArgs...) +} + +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func DirExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return Fail(t, fmt.Sprintf("unable to find file %q", path), msgAndArgs...) + } + return Fail(t, fmt.Sprintf("error when running os.Lstat(%q): %s", path, err), msgAndArgs...) + } + if !info.IsDir() { + return Fail(t, fmt.Sprintf("%q is a file", path), msgAndArgs...) + } + return true +} + +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return true + } + return true + } + if !info.IsDir() { + return true + } + return Fail(t, fmt.Sprintf("directory %q exists", path), msgAndArgs...) +} + +// JSONEq asserts that two JSON strings are equivalent. +// +// assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) +func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + var expectedJSONAsInterface, actualJSONAsInterface interface{} + + if err := json.Unmarshal([]byte(expected), &expectedJSONAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Expected value ('%s') is not valid json.\nJSON parsing error: '%s'", expected, err.Error()), msgAndArgs...) + } + + // Shortcut if same bytes + if actual == expected { + return true + } + + if err := json.Unmarshal([]byte(actual), &actualJSONAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Input ('%s') needs to be valid json.\nJSON parsing error: '%s'", actual, err.Error()), msgAndArgs...) + } + + return Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...) +} + +// YAMLEq asserts that two YAML strings are equivalent. +func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + var expectedYAMLAsInterface, actualYAMLAsInterface interface{} + + if err := yaml.Unmarshal([]byte(expected), &expectedYAMLAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Expected value ('%s') is not valid yaml.\nYAML parsing error: '%s'", expected, err.Error()), msgAndArgs...) + } + + // Shortcut if same bytes + if actual == expected { + return true + } + + if err := yaml.Unmarshal([]byte(actual), &actualYAMLAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Input ('%s') needs to be valid yaml.\nYAML error: '%s'", actual, err.Error()), msgAndArgs...) + } + + return Equal(t, expectedYAMLAsInterface, actualYAMLAsInterface, msgAndArgs...) +} + +func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) { + t := reflect.TypeOf(v) + k := t.Kind() + + if k == reflect.Ptr { + t = t.Elem() + k = t.Kind() + } + return t, k +} + +// diff returns a diff of both values as long as both are of the same type and +// are a struct, map, slice, array or string. Otherwise it returns an empty string. +func diff(expected interface{}, actual interface{}) string { + if expected == nil || actual == nil { + return "" + } + + et, ek := typeAndKind(expected) + at, _ := typeAndKind(actual) + + if et != at { + return "" + } + + if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String { + return "" + } + + var e, a string + + switch et { + case reflect.TypeOf(""): + e = reflect.ValueOf(expected).String() + a = reflect.ValueOf(actual).String() + case reflect.TypeOf(time.Time{}): + e = spewConfigStringerEnabled.Sdump(expected) + a = spewConfigStringerEnabled.Sdump(actual) + default: + e = spewConfig.Sdump(expected) + a = spewConfig.Sdump(actual) + } + + diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ + A: difflib.SplitLines(e), + B: difflib.SplitLines(a), + FromFile: "Expected", + FromDate: "", + ToFile: "Actual", + ToDate: "", + Context: 1, + }) + + return "\n\nDiff:\n" + diff +} + +func isFunction(arg interface{}) bool { + if arg == nil { + return false + } + return reflect.TypeOf(arg).Kind() == reflect.Func +} + +var spewConfig = spew.ConfigState{ + Indent: " ", + DisablePointerAddresses: true, + DisableCapacities: true, + SortKeys: true, + DisableMethods: true, + MaxDepth: 10, +} + +var spewConfigStringerEnabled = spew.ConfigState{ + Indent: " ", + DisablePointerAddresses: true, + DisableCapacities: true, + SortKeys: true, + MaxDepth: 10, +} + +type tHelper = interface { + Helper() +} + +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond) +func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ch := make(chan bool, 1) + checkCond := func() { ch <- condition() } + + timer := time.NewTimer(waitFor) + defer timer.Stop() + + ticker := time.NewTicker(tick) + defer ticker.Stop() + + var tickC <-chan time.Time + + // Check the condition once first on the initial call. + go checkCond() + + for { + select { + case <-timer.C: + return Fail(t, "Condition never satisfied", msgAndArgs...) + case <-tickC: + tickC = nil + go checkCond() + case v := <-ch: + if v { + return true + } + tickC = ticker.C + } + } +} + +// CollectT implements the TestingT interface and collects all errors. +type CollectT struct { + // A slice of errors. Non-nil slice denotes a failure. + // If it's non-nil but len(c.errors) == 0, this is also a failure + // obtained by direct c.FailNow() call. + errors []error +} + +// Helper is like [testing.T.Helper] but does nothing. +func (CollectT) Helper() {} + +// Errorf collects the error. +func (c *CollectT) Errorf(format string, args ...interface{}) { + c.errors = append(c.errors, fmt.Errorf(format, args...)) +} + +// FailNow stops execution by calling runtime.Goexit. +func (c *CollectT) FailNow() { + c.fail() + runtime.Goexit() +} + +// Deprecated: That was a method for internal usage that should not have been published. Now just panics. +func (*CollectT) Reset() { + panic("Reset() is deprecated") +} + +// Deprecated: That was a method for internal usage that should not have been published. Now just panics. +func (*CollectT) Copy(TestingT) { + panic("Copy() is deprecated") +} + +func (c *CollectT) fail() { + if !c.failed() { + c.errors = []error{} // Make it non-nil to mark a failure. + } +} + +func (c *CollectT) failed() bool { + return c.errors != nil +} + +// EventuallyWithT asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. In contrast to Eventually, +// it supplies a CollectT to the condition function, so that the condition +// function can use the CollectT to call other assertions. +// The condition is considered "met" if no errors are raised in a tick. +// The supplied CollectT collects all errors from one tick (if there are any). +// If the condition is not met before waitFor, the collected errors of +// the last tick are copied to t. +// +// externalValue := false +// go func() { +// time.Sleep(8*time.Second) +// externalValue = true +// }() +// assert.EventuallyWithT(t, func(c *assert.CollectT) { +// // add assertions as needed; any assertion failure will fail the current tick +// assert.True(c, externalValue, "expected 'externalValue' to be true") +// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false") +func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + var lastFinishedTickErrs []error + ch := make(chan *CollectT, 1) + + checkCond := func() { + collect := new(CollectT) + defer func() { + ch <- collect + }() + condition(collect) + } + + timer := time.NewTimer(waitFor) + defer timer.Stop() + + ticker := time.NewTicker(tick) + defer ticker.Stop() + + var tickC <-chan time.Time + + // Check the condition once first on the initial call. + go checkCond() + + for { + select { + case <-timer.C: + for _, err := range lastFinishedTickErrs { + t.Errorf("%v", err) + } + return Fail(t, "Condition never satisfied", msgAndArgs...) + case <-tickC: + tickC = nil + go checkCond() + case collect := <-ch: + if !collect.failed() { + return true + } + // Keep the errors from the last ended condition, so that they can be copied to t if timeout is reached. + lastFinishedTickErrs = collect.errors + tickC = ticker.C + } + } +} + +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond) +func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ch := make(chan bool, 1) + checkCond := func() { ch <- condition() } + + timer := time.NewTimer(waitFor) + defer timer.Stop() + + ticker := time.NewTicker(tick) + defer ticker.Stop() + + var tickC <-chan time.Time + + // Check the condition once first on the initial call. + go checkCond() + + for { + select { + case <-timer.C: + return true + case <-tickC: + tickC = nil + go checkCond() + case v := <-ch: + if v { + return Fail(t, "Condition satisfied", msgAndArgs...) + } + tickC = ticker.C + } + } +} + +// ErrorIs asserts that at least one of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func ErrorIs(t TestingT, err, target error, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if errors.Is(err, target) { + return true + } + + var expectedText string + if target != nil { + expectedText = target.Error() + if err == nil { + return Fail(t, fmt.Sprintf("Expected error with %q in chain but got nil.", expectedText), msgAndArgs...) + } + } + + chain := buildErrorChainString(err, false) + + return Fail(t, fmt.Sprintf("Target error should be in err chain:\n"+ + "expected: %q\n"+ + "in chain: %s", expectedText, chain, + ), msgAndArgs...) +} + +// NotErrorIs asserts that none of the errors in err's chain matches target. +// This is a wrapper for errors.Is. +func NotErrorIs(t TestingT, err, target error, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if !errors.Is(err, target) { + return true + } + + var expectedText string + if target != nil { + expectedText = target.Error() + } + + chain := buildErrorChainString(err, false) + + return Fail(t, fmt.Sprintf("Target error should not be in err chain:\n"+ + "found: %q\n"+ + "in chain: %s", expectedText, chain, + ), msgAndArgs...) +} + +// ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. +// This is a wrapper for errors.As. +func ErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if errors.As(err, target) { + return true + } + + expectedType := reflect.TypeOf(target).Elem().String() + if err == nil { + return Fail(t, fmt.Sprintf("An error is expected but got nil.\n"+ + "expected: %s", expectedType), msgAndArgs...) + } + + chain := buildErrorChainString(err, true) + + return Fail(t, fmt.Sprintf("Should be in error chain:\n"+ + "expected: %s\n"+ + "in chain: %s", expectedType, chain, + ), msgAndArgs...) +} + +// NotErrorAs asserts that none of the errors in err's chain matches target, +// but if so, sets target to that error value. +func NotErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if !errors.As(err, target) { + return true + } + + chain := buildErrorChainString(err, true) + + return Fail(t, fmt.Sprintf("Target error should not be in err chain:\n"+ + "found: %s\n"+ + "in chain: %s", reflect.TypeOf(target).Elem().String(), chain, + ), msgAndArgs...) +} + +func unwrapAll(err error) (errs []error) { + errs = append(errs, err) + switch x := err.(type) { + case interface{ Unwrap() error }: + err = x.Unwrap() + if err == nil { + return + } + errs = append(errs, unwrapAll(err)...) + case interface{ Unwrap() []error }: + for _, err := range x.Unwrap() { + errs = append(errs, unwrapAll(err)...) + } + } + return +} + +func buildErrorChainString(err error, withType bool) string { + if err == nil { + return "" + } + + var chain string + errs := unwrapAll(err) + for i := range errs { + if i != 0 { + chain += "\n\t" + } + chain += fmt.Sprintf("%q", errs[i].Error()) + if withType { + chain += fmt.Sprintf(" (%T)", errs[i]) + } + } + return chain +} diff --git a/vendor/github.com/stretchr/testify/assert/doc.go b/vendor/github.com/stretchr/testify/assert/doc.go new file mode 100644 index 0000000000..a0b953aa5c --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/doc.go @@ -0,0 +1,50 @@ +// Package assert provides a set of comprehensive testing tools for use with the normal Go testing system. +// +// # Note +// +// All functions in this package return a bool value indicating whether the assertion has passed. +// +// # Example Usage +// +// The following is a complete example using assert in a standard test function: +// +// import ( +// "testing" +// "github.com/stretchr/testify/assert" +// ) +// +// func TestSomething(t *testing.T) { +// +// var a string = "Hello" +// var b string = "Hello" +// +// assert.Equal(t, a, b, "The two words should be the same.") +// +// } +// +// if you assert many times, use the format below: +// +// import ( +// "testing" +// "github.com/stretchr/testify/assert" +// ) +// +// func TestSomething(t *testing.T) { +// assert := assert.New(t) +// +// var a string = "Hello" +// var b string = "Hello" +// +// assert.Equal(a, b, "The two words should be the same.") +// } +// +// # Assertions +// +// Assertions allow you to easily write test code, and are global funcs in the `assert` package. +// All assertion functions take, as the first argument, the `*testing.T` object provided by the +// testing framework. This allows the assertion funcs to write the failings and other details to +// the correct place. +// +// Every assertion function also takes an optional string message as the final argument, +// allowing custom error messages to be appended to the message the assertion method outputs. +package assert diff --git a/vendor/github.com/stretchr/testify/assert/errors.go b/vendor/github.com/stretchr/testify/assert/errors.go new file mode 100644 index 0000000000..ac9dc9d1d6 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/errors.go @@ -0,0 +1,10 @@ +package assert + +import ( + "errors" +) + +// AnError is an error instance useful for testing. If the code does not care +// about error specifics, and only needs to return the error for example, this +// error should be used to make the test code more readable. +var AnError = errors.New("assert.AnError general error for testing") diff --git a/vendor/github.com/stretchr/testify/assert/forward_assertions.go b/vendor/github.com/stretchr/testify/assert/forward_assertions.go new file mode 100644 index 0000000000..df189d2348 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/forward_assertions.go @@ -0,0 +1,16 @@ +package assert + +// Assertions provides assertion methods around the +// TestingT interface. +type Assertions struct { + t TestingT +} + +// New makes a new Assertions object for the specified TestingT. +func New(t TestingT) *Assertions { + return &Assertions{ + t: t, + } +} + +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs" diff --git a/vendor/github.com/stretchr/testify/assert/http_assertions.go b/vendor/github.com/stretchr/testify/assert/http_assertions.go new file mode 100644 index 0000000000..5a6bb75f2c --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/http_assertions.go @@ -0,0 +1,165 @@ +package assert + +import ( + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "strings" +) + +// httpCode is a helper that returns HTTP code of the response. It returns -1 and +// an error if building a new request fails. +func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) { + w := httptest.NewRecorder() + req, err := http.NewRequest(method, url, http.NoBody) + if err != nil { + return -1, err + } + req.URL.RawQuery = values.Encode() + handler(w, req) + return w.Code, nil +} + +// HTTPSuccess asserts that a specified handler returns a success status code. +// +// assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil) +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...) + } + + isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent + if !isSuccessCode { + Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...) + } + + return isSuccessCode +} + +// HTTPRedirect asserts that a specified handler returns a redirect status code. +// +// assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...) + } + + isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect + if !isRedirectCode { + Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...) + } + + return isRedirectCode +} + +// HTTPError asserts that a specified handler returns an error status code. +// +// assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...) + } + + isErrorCode := code >= http.StatusBadRequest + if !isErrorCode { + Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...) + } + + return isErrorCode +} + +// HTTPStatusCode asserts that a specified handler returns a specified status code. +// +// assert.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501) +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...) + } + + successful := code == statuscode + if !successful { + Fail(t, fmt.Sprintf("Expected HTTP status code %d for %q but received %d", statuscode, url+"?"+values.Encode(), code), msgAndArgs...) + } + + return successful +} + +// HTTPBody is a helper that returns HTTP body of the response. It returns +// empty string if building a new request fails. +func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string { + w := httptest.NewRecorder() + if len(values) > 0 { + url += "?" + values.Encode() + } + req, err := http.NewRequest(method, url, http.NoBody) + if err != nil { + return "" + } + handler(w, req) + return w.Body.String() +} + +// HTTPBodyContains asserts that a specified handler returns a +// body that contains a string. +// +// assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + body := HTTPBody(handler, method, url, values) + + contains := strings.Contains(body, fmt.Sprint(str)) + if !contains { + Fail(t, fmt.Sprintf("Expected response body for %q to contain %q but found %q", url+"?"+values.Encode(), str, body), msgAndArgs...) + } + + return contains +} + +// HTTPBodyNotContains asserts that a specified handler returns a +// body that does not contain a string. +// +// assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + body := HTTPBody(handler, method, url, values) + + contains := strings.Contains(body, fmt.Sprint(str)) + if contains { + Fail(t, fmt.Sprintf("Expected response body for %q to NOT contain %q but found %q", url+"?"+values.Encode(), str, body), msgAndArgs...) + } + + return !contains +} diff --git a/vendor/github.com/stretchr/testify/assert/yaml/yaml_custom.go b/vendor/github.com/stretchr/testify/assert/yaml/yaml_custom.go new file mode 100644 index 0000000000..5a74c4f4d5 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/yaml/yaml_custom.go @@ -0,0 +1,24 @@ +//go:build testify_yaml_custom && !testify_yaml_fail && !testify_yaml_default + +// Package yaml is an implementation of YAML functions that calls a pluggable implementation. +// +// This implementation is selected with the testify_yaml_custom build tag. +// +// go test -tags testify_yaml_custom +// +// This implementation can be used at build time to replace the default implementation +// to avoid linking with [gopkg.in/yaml.v3]. +// +// In your test package: +// +// import assertYaml "github.com/stretchr/testify/assert/yaml" +// +// func init() { +// assertYaml.Unmarshal = func (in []byte, out interface{}) error { +// // ... +// return nil +// } +// } +package yaml + +var Unmarshal func(in []byte, out interface{}) error diff --git a/vendor/github.com/stretchr/testify/assert/yaml/yaml_default.go b/vendor/github.com/stretchr/testify/assert/yaml/yaml_default.go new file mode 100644 index 0000000000..0bae80e34a --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/yaml/yaml_default.go @@ -0,0 +1,36 @@ +//go:build !testify_yaml_fail && !testify_yaml_custom + +// Package yaml is just an indirection to handle YAML deserialization. +// +// This package is just an indirection that allows the builder to override the +// indirection with an alternative implementation of this package that uses +// another implementation of YAML deserialization. This allows to not either not +// use YAML deserialization at all, or to use another implementation than +// [gopkg.in/yaml.v3] (for example for license compatibility reasons, see [PR #1120]). +// +// Alternative implementations are selected using build tags: +// +// - testify_yaml_fail: [Unmarshal] always fails with an error +// - testify_yaml_custom: [Unmarshal] is a variable. Caller must initialize it +// before calling any of [github.com/stretchr/testify/assert.YAMLEq] or +// [github.com/stretchr/testify/assert.YAMLEqf]. +// +// Usage: +// +// go test -tags testify_yaml_fail +// +// You can check with "go list" which implementation is linked: +// +// go list -f '{{.Imports}}' github.com/stretchr/testify/assert/yaml +// go list -tags testify_yaml_fail -f '{{.Imports}}' github.com/stretchr/testify/assert/yaml +// go list -tags testify_yaml_custom -f '{{.Imports}}' github.com/stretchr/testify/assert/yaml +// +// [PR #1120]: https://github.com/stretchr/testify/pull/1120 +package yaml + +import goyaml "gopkg.in/yaml.v3" + +// Unmarshal is just a wrapper of [gopkg.in/yaml.v3.Unmarshal]. +func Unmarshal(in []byte, out interface{}) error { + return goyaml.Unmarshal(in, out) +} diff --git a/vendor/github.com/stretchr/testify/assert/yaml/yaml_fail.go b/vendor/github.com/stretchr/testify/assert/yaml/yaml_fail.go new file mode 100644 index 0000000000..8041803fd2 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/yaml/yaml_fail.go @@ -0,0 +1,17 @@ +//go:build testify_yaml_fail && !testify_yaml_custom && !testify_yaml_default + +// Package yaml is an implementation of YAML functions that always fail. +// +// This implementation can be used at build time to replace the default implementation +// to avoid linking with [gopkg.in/yaml.v3]: +// +// go test -tags testify_yaml_fail +package yaml + +import "errors" + +var errNotImplemented = errors.New("YAML functions are not available (see https://pkg.go.dev/github.com/stretchr/testify/assert/yaml)") + +func Unmarshal([]byte, interface{}) error { + return errNotImplemented +} diff --git a/vendor/github.com/tektoncd/hub/LICENSE b/vendor/github.com/tektoncd/hub/LICENSE deleted file mode 100644 index 261eeb9e9f..0000000000 --- a/vendor/github.com/tektoncd/hub/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - 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. diff --git a/vendor/goa.design/goa/v3/http/mux.go b/vendor/goa.design/goa/v3/http/mux.go index a7d533c874..a2d9421a48 100644 --- a/vendor/goa.design/goa/v3/http/mux.go +++ b/vendor/goa.design/goa/v3/http/mux.go @@ -82,11 +82,19 @@ type ( ) // NewMuxer returns a Muxer implementation based on a Chi router. +// +// The returned muxer sets r.Pattern (Go 1.22+) on every dispatched request +// before middlewares run. This allows observability middleware such as +// otelhttp to read the matched route from r.Pattern for span attributes and +// metrics. To take advantage of this, register otelhttp as a mux middleware +// rather than wrapping the mux externally: +// +// mux := goahttp.NewMuxer() +// mux.Use(otelhttp.NewMiddleware("service")) func NewMuxer() ResolverMuxer { return &mux{ - Router: chi.NewRouter(), - wildcards: make(map[string]string), - middlewares: nil, + Router: chi.NewRouter(), + wildcards: make(map[string]string), } } @@ -94,6 +102,10 @@ func NewMuxer() ResolverMuxer { var wildPath = regexp.MustCompile(`/{\*([a-zA-Z0-9_]+)}`) // Handle registers the handler function for the given method and pattern. +// It sets r.Pattern on every matched request to "METHOD /path" (matching the +// Go 1.22+ convention used by http.ServeMux), enabling observability +// middleware such as otelhttp to automatically tag spans and metrics with +// the matched route. func (m *mux) Handle(method, pattern string, handler http.HandlerFunc) { m.mu.Lock() defer m.mu.Unlock() @@ -109,6 +121,9 @@ func (m *mux) Handle(method, pattern string, handler http.HandlerFunc) { })) m.middlewares = nil } + // Capture the registered pattern before wildcard rewriting so we can + // populate r.Pattern for downstream consumers. + reqPattern := method + " " + pattern if wildcards := wildPath.FindStringSubmatch(pattern); len(wildcards) > 0 { if len(wildcards) > 2 { panic("too many wildcards") @@ -116,7 +131,22 @@ func (m *mux) Handle(method, pattern string, handler http.HandlerFunc) { pattern = wildPath.ReplaceAllString(pattern, "/*") m.wildcards[method+"::"+pattern] = wildcards[1] } - m.Method(method, pattern, handler) + m.Method(method, pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.Pattern = reqPattern + handler(w, r) + })) +} + +// ServeHTTP resolves the matched route and sets r.Pattern before dispatching +// the request through chi's middleware chain and handler. This ensures that +// middlewares registered via Use() — such as otelhttp.NewMiddleware — can +// read r.Pattern to tag spans and metrics with the http.route attribute. +func (m *mux) ServeHTTP(w http.ResponseWriter, r *http.Request) { + rctx := chi.NewRouteContext() + if m.Match(rctx, r.Method, r.URL.Path) { + r.Pattern = r.Method + " " + m.resolveWildcard(r.Method, rctx.RoutePattern()) + } + m.Router.ServeHTTP(w, r) } // Vars extracts the path variables from the request context. diff --git a/vendor/goa.design/goa/v3/pkg/version.go b/vendor/goa.design/goa/v3/pkg/version.go index f95e0b7a60..88fcb00d00 100644 --- a/vendor/goa.design/goa/v3/pkg/version.go +++ b/vendor/goa.design/goa/v3/pkg/version.go @@ -10,9 +10,9 @@ const ( // Major version number Major = 3 // Minor version number - Minor = 23 + Minor = 26 // Build number - Build = 4 + Build = 0 // Suffix - set to empty string in release tag commits. Suffix = "" ) diff --git a/vendor/gopkg.in/h2non/gock.v1/.editorconfig b/vendor/gopkg.in/h2non/gock.v1/.editorconfig new file mode 100644 index 0000000000..b570cb1698 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = tab +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/vendor/gopkg.in/h2non/gock.v1/.gitignore b/vendor/gopkg.in/h2non/gock.v1/.gitignore new file mode 100644 index 0000000000..173b0298be --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/.gitignore @@ -0,0 +1,30 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so +go.sum + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof + +.idea/ +*.iml +*.out +*.tmp diff --git a/vendor/gopkg.in/h2non/gock.v1/.travis.yml b/vendor/gopkg.in/h2non/gock.v1/.travis.yml new file mode 100644 index 0000000000..d8ea0713df --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/.travis.yml @@ -0,0 +1,26 @@ +language: go + +go: + - "1.15" + - "1.14" + - "stable" + +before_install: + - go get -u github.com/nbio/st + - go get -u github.com/codemodus/parth + - go get -u gopkg.in/h2non/gentleman.v1 + - go get -u -v github.com/axw/gocov/gocov + - go get -u -v github.com/mattn/goveralls + - go get -v -u golang.org/x/lint/golint + - mkdir -p $GOPATH/src/gopkg.in/h2non/gock.v1 + - cp -r . $GOPATH/src/gopkg.in/h2non/gock.v1 + +script: + - diff -u <(echo -n) <(gofmt -s -d ./) + - diff -u <(echo -n) <(go vet ./...) + - diff -u <(echo -n) <(golint ./...) + - go test -v -race -covermode=atomic -coverprofile=coverage.out + - go test ./_examples/*/ -v -race + +after_success: + - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN diff --git a/vendor/gopkg.in/h2non/gock.v1/History.md b/vendor/gopkg.in/h2non/gock.v1/History.md new file mode 100644 index 0000000000..4329310c91 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/History.md @@ -0,0 +1,139 @@ +## v1.1.2 / 2021-08-03 + + * fix(mock): fix race condition in mock.go file (#92) + +## v1.1.1 / 2021-07-14 + + * feat(matchers): Support custom MIME types (#88) + +## v1.1.0 / 2021-06-02 + + * Add context expiration cancellation support (#86) + +## v1.0.16 / 2020-11-23 + + * Fix regexp matching issues in headers (#59) + +## v1.0.15 / 2019-07-03 + + * NewMatcher() will now return objects that completely separate one another. (#55) + * add request Options (#49) + * fix typo: function -> func (#52) + * feat(docs): change note + * feat(docs): add net/http support + * Add Basic Auth (#47) + * Update LICENSE (#46) + +## v1.0.14 / 2019-01-31 + + * feat(version): bump to v1.0.14 + * feat: add go.mod + +## v1.0.13 / 2019-01-30 + + * Add PathParam matcher (#42) + +## v1.0.12 / 2018-11-13 + + * Fix possible data race. (#41) + +## v1.0.11 / 2018-10-29 + + * Do not reset response body (#40) + * refactor(travis): remove unsupported versions for golint based on Go release policy support + * feat(gock): add gock.Observe to support inspection of the outgoing intercepted HTTP traffic (#38) + +## v1.0.10 / 2018-09-09 + + * Support multiple response headers with same name #35 (#36) + +## v1.0.9 / 2018-06-14 + + * fix(url-encoding) add exact match test in MatchPath (#34) + * fix(travis): use string notation for Go versions + +## v1.0.8 / 2018-02-28 + + * chore(LICENSE): update year ;) + * feat(docs): add additional tips and examples + * feat(gock): ignore already intercepted http.Client + +## v1.0.7 / 2017-12-21 + + * Make MatchHost case insensitive. (#31) + * refactor(docs): remove codesponsor :( + * add example when request reply with error (#28) + * feat(docs): add sponsor ad + * Add example networking partially enabled (#23) + +## v1.0.6 / 2017-07-27 + + * fix(#23): mock transport deadlock + +## v1.0.5 / 2017-07-26 + + * feat(#25, #24): use content type only if missing while matching JSON/XML + * feat(#24): add CleanUnmatchedRequests() and OffAll() public functions + * feat(version): bump to v1.0.5 + * fix(store): use proper indent style + * fix(mutex): use different mutex for store + * feat(travis): add Go 1.8 CI support + +## v1.0.4 / 2017-02-14 + + * Update README to include most up to date version (#17) + * Update MatchBody() to compare if key + value pairs of JSON match regardless of order they are in. (#16) + * feat(examples): add new example for unmatch case + * refactor(docs): add pook reference + +## 1.0.3 / 14-11-2016 + +- feat(#13): adds `GetUnmatchedRequests()` and `HasUnmatchedRequests()` API functions. + +## 1.0.2 / 10-11-2016 + +- fix(#11): adds `Compression()` method for output HTTP traffic body compression processing and matching. + +## 1.0.1 / 07-09-2016 + +- fix(#9): missing URL query param matcher. + +## 1.0.0 / 19-04-2016 + +- feat(version): first major version release. + +## 0.1.6 / 19-04-2016 + +- fix(#7): if error configured, RoundTripper should reply with `nil` response. + +## 0.1.5 / 09-04-2016 + +- feat(#5): support `ReplyFunc` for convenience. + +## 0.1.4 / 16-03-2016 + +- feat(api): add `IsDone()` method. +- fix(responder): return mock error if present. +- feat(#4): support define request/response body from file disk. + +## 0.1.3 / 09-03-2016 + +- feat(matcher): add content type matcher helper method supporting aliases. +- feat(interceptor): add function to restore HTTP client transport. +- feat(matcher): add URL scheme matcher function. +- fix(request): ignore base slash path. +- feat(api): add Off() method for easier restore and clean up. +- feat(store): add public API for pending mocks. + +## 0.1.2 / 04-03-2016 + +- fix(matcher): body matchers no used by default. +- feat(matcher): add matcher factories for multiple cases. + +## 0.1.1 / 04-03-2016 + +- fix(params): persist query params accordingly. + +## 0.1.0 / 02-03-2016 + +- First release. diff --git a/vendor/gopkg.in/h2non/gock.v1/LICENSE b/vendor/gopkg.in/h2non/gock.v1/LICENSE new file mode 100644 index 0000000000..1911b17782 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/LICENSE @@ -0,0 +1,24 @@ +The MIT License + +Copyright (c) 2016-2019 Tomas Aparicio + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/gopkg.in/h2non/gock.v1/README.md b/vendor/gopkg.in/h2non/gock.v1/README.md new file mode 100644 index 0000000000..a0d38a3da9 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/README.md @@ -0,0 +1,373 @@ +# gock [![Build Status](https://travis-ci.org/h2non/gock.svg?branch=master)](https://travis-ci.org/h2non/gock) [![GitHub release](https://img.shields.io/badge/version-v1.0-orange.svg?style=flat)](https://github.com/h2non/gock/releases) [![GoDoc](https://godoc.org/github.com/h2non/gock?status.svg)](https://godoc.org/github.com/h2non/gock) [![Coverage Status](https://coveralls.io/repos/github/h2non/gock/badge.svg?branch=master)](https://coveralls.io/github/h2non/gock?branch=master) [![Go Report Card](https://img.shields.io/badge/go_report-A+-brightgreen.svg)](https://goreportcard.com/report/github.com/h2non/gock) [![license](https://img.shields.io/badge/license-MIT-blue.svg)]() + +Versatile HTTP mocking made easy in [Go](https://golang.org) that works with any `net/http` based stdlib implementation. + +Heavily inspired by [nock](https://github.com/node-nock/nock). +There is also its Python port, [pook](https://github.com/h2non/pook). + +To get started, take a look to the [examples](#examples). + +## Features + +- Simple, expressive, fluent API. +- Semantic API DSL for declarative HTTP mock declarations. +- Built-in helpers for easy JSON/XML mocking. +- Supports persistent and volatile TTL-limited mocks. +- Full regular expressions capable HTTP request mock matching. +- Designed for both testing and runtime scenarios. +- Match request by method, URL params, headers and bodies. +- Extensible and pluggable HTTP matching rules. +- Ability to switch between mock and real networking modes. +- Ability to filter/map HTTP requests for accurate mock matching. +- Supports map and filters to handle mocks easily. +- Wide compatible HTTP interceptor using `http.RoundTripper` interface. +- Works with any `net/http` compatible client, such as [gentleman](https://github.com/h2non/gentleman). +- Network timeout/cancelation delay simulation. +- Extensible and hackable API. +- Dependency free. + +## Installation + +```bash +go get -u gopkg.in/h2non/gock.v1 +``` + +## API + +See [godoc reference](https://godoc.org/github.com/h2non/gock) for detailed API documentation. + +## How it mocks + +1. Intercepts any HTTP outgoing request via `http.DefaultTransport` or custom `http.Transport` used by any `http.Client`. +2. Matches outgoing HTTP requests against a pool of defined HTTP mock expectations in FIFO declaration order. +3. If at least one mock matches, it will be used in order to compose the mock HTTP response. +4. If no mock can be matched, it will resolve the request with an error, unless real networking mode is enable, in which case a real HTTP request will be performed. + +## Tips + +#### Testing + +Declare your mocks before you start declaring the concrete test logic: + +```go +func TestFoo(t *testing.T) { + defer gock.Off() // Flush pending mocks after test execution + + gock.New("http://server.com"). + Get("/bar"). + Reply(200). + JSON(map[string]string{"foo": "bar"}) + + // Your test code starts here... +} +``` + +#### Race conditions + +If you're running concurrent code, be aware that your mocks are declared first to avoid unexpected +race conditions while configuring `gock` or intercepting custom HTTP clients. + +`gock` is not fully thread-safe, but sensible parts are. +Any help making `gock` more reliable in this sense is appreciated. + +#### Define complex mocks first + +If you're mocking a bunch of mocks in the same test suite, it's recommended to define the more +concrete mocks first, and then the generic ones. + +This approach usually avoids matching unexpected generic mocks (e.g: specific header, body payload...) instead of the generic ones that performs less complex matches. + +#### Disable `gock` traffic interception once done + +In other to minimize potential side effects within your test code, it's a good practice +disabling `gock` once you are done with your HTTP testing logic. + +A Go idiomatic approach for doing this can be using it in a `defer` statement, such as: + +```go +func TestGock (t *testing.T) { + defer gock.Off() + + // ... my test code goes here +} +``` + +#### Intercept an `http.Client` just once + +You don't need to intercept multiple times the same `http.Client` instance. + +Just call `gock.InterceptClient(client)` once, typically at the beginning of your test scenarios. + +#### Restore an `http.Client` after interception + +**NOTE**: this is not required is you are using `http.DefaultClient` or `http.DefaultTransport`. + +As a good testing pattern, you should call `gock.RestoreClient(client)` after running your test scenario, typically as after clean up hook. + +You can also use a `defer` statement for doing it, as you do with `gock.Off()`, such as: + +```go +func TestGock (t *testing.T) { + defer gock.Off() + defer gock.RestoreClient(client) + + // ... my test code goes here +} +``` + +## Examples + +See [examples](https://github.com/h2non/gock/tree/master/_examples) directory for more featured use cases. + +#### Simple mocking via tests + +```go +package test + +import ( + "github.com/nbio/st" + "gopkg.in/h2non/gock.v1" + "io/ioutil" + "net/http" + "testing" +) + +func TestSimple(t *testing.T) { + defer gock.Off() + + gock.New("http://foo.com"). + Get("/bar"). + Reply(200). + JSON(map[string]string{"foo": "bar"}) + + res, err := http.Get("http://foo.com/bar") + st.Expect(t, err, nil) + st.Expect(t, res.StatusCode, 200) + + body, _ := ioutil.ReadAll(res.Body) + st.Expect(t, string(body)[:13], `{"foo":"bar"}`) + + // Verify that we don't have pending mocks + st.Expect(t, gock.IsDone(), true) +} +``` + +#### Request headers matching + +```go +package test + +import ( + "github.com/nbio/st" + "gopkg.in/h2non/gock.v1" + "io/ioutil" + "net/http" + "testing" +) + +func TestMatchHeaders(t *testing.T) { + defer gock.Off() + + gock.New("http://foo.com"). + MatchHeader("Authorization", "^foo bar$"). + MatchHeader("API", "1.[0-9]+"). + HeaderPresent("Accept"). + Reply(200). + BodyString("foo foo") + + req, err := http.NewRequest("GET", "http://foo.com", nil) + req.Header.Set("Authorization", "foo bar") + req.Header.Set("API", "1.0") + req.Header.Set("Accept", "text/plain") + + res, err := (&http.Client{}).Do(req) + st.Expect(t, err, nil) + st.Expect(t, res.StatusCode, 200) + body, _ := ioutil.ReadAll(res.Body) + st.Expect(t, string(body), "foo foo") + + // Verify that we don't have pending mocks + st.Expect(t, gock.IsDone(), true) +} +``` + +#### Request param matching + +```go +package test + +import ( + "github.com/nbio/st" + "gopkg.in/h2non/gock.v1" + "io/ioutil" + "net/http" + "testing" +) + +func TestMatchParams(t *testing.T) { + defer gock.Off() + + gock.New("http://foo.com"). + MatchParam("page", "1"). + MatchParam("per_page", "10"). + Reply(200). + BodyString("foo foo") + + req, err := http.NewRequest("GET", "http://foo.com?page=1&per_page=10", nil) + + res, err := (&http.Client{}).Do(req) + st.Expect(t, err, nil) + st.Expect(t, res.StatusCode, 200) + body, _ := ioutil.ReadAll(res.Body) + st.Expect(t, string(body), "foo foo") + + // Verify that we don't have pending mocks + st.Expect(t, gock.IsDone(), true) +} +``` + +#### JSON body matching and response + +```go +package test + +import ( + "bytes" + "github.com/nbio/st" + "gopkg.in/h2non/gock.v1" + "io/ioutil" + "net/http" + "testing" +) + +func TestMockSimple(t *testing.T) { + defer gock.Off() + + gock.New("http://foo.com"). + Post("/bar"). + MatchType("json"). + JSON(map[string]string{"foo": "bar"}). + Reply(201). + JSON(map[string]string{"bar": "foo"}) + + body := bytes.NewBuffer([]byte(`{"foo":"bar"}`)) + res, err := http.Post("http://foo.com/bar", "application/json", body) + st.Expect(t, err, nil) + st.Expect(t, res.StatusCode, 201) + + resBody, _ := ioutil.ReadAll(res.Body) + st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`) + + // Verify that we don't have pending mocks + st.Expect(t, gock.IsDone(), true) +} +``` + +#### Mocking a custom http.Client and http.RoundTripper + +```go +package test + +import ( + "github.com/nbio/st" + "gopkg.in/h2non/gock.v1" + "io/ioutil" + "net/http" + "testing" +) + +func TestClient(t *testing.T) { + defer gock.Off() + + gock.New("http://foo.com"). + Reply(200). + BodyString("foo foo") + + req, err := http.NewRequest("GET", "http://foo.com", nil) + client := &http.Client{Transport: &http.Transport{}} + gock.InterceptClient(client) + + res, err := client.Do(req) + st.Expect(t, err, nil) + st.Expect(t, res.StatusCode, 200) + body, _ := ioutil.ReadAll(res.Body) + st.Expect(t, string(body), "foo foo") + + // Verify that we don't have pending mocks + st.Expect(t, gock.IsDone(), true) +} +``` + +#### Enable real networking + +```go +package main + +import ( + "fmt" + "gopkg.in/h2non/gock.v1" + "io/ioutil" + "net/http" +) + +func main() { + defer gock.Off() + defer gock.DisableNetworking() + + gock.EnableNetworking() + gock.New("http://httpbin.org"). + Get("/get"). + Reply(201). + SetHeader("Server", "gock") + + res, err := http.Get("http://httpbin.org/get") + if err != nil { + fmt.Errorf("Error: %s", err) + } + + // The response status comes from the mock + fmt.Printf("Status: %d\n", res.StatusCode) + // The server header comes from mock as well + fmt.Printf("Server header: %s\n", res.Header.Get("Server")) + // Response body is the original + body, _ := ioutil.ReadAll(res.Body) + fmt.Printf("Body: %s", string(body)) +} +``` + +#### Debug intercepted http requests + +```go +package main + +import ( + "bytes" + "gopkg.in/h2non/gock.v1" + "net/http" +) + +func main() { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + gock.New("http://foo.com"). + Post("/bar"). + MatchType("json"). + JSON(map[string]string{"foo": "bar"}). + Reply(200) + + body := bytes.NewBuffer([]byte(`{"foo":"bar"}`)) + http.Post("http://foo.com/bar", "application/json", body) +} + +``` + +## Hacking it! + +You can easily hack `gock` defining custom matcher functions with own matching rules. + +See [add matcher functions](https://github.com/h2non/gock/blob/master/_examples/add_matchers/matchers.go) and [custom matching layer](https://github.com/h2non/gock/blob/master/_examples/custom_matcher/matcher.go) examples for further details. + +## License + +MIT - Tomas Aparicio diff --git a/vendor/gopkg.in/h2non/gock.v1/gock.go b/vendor/gopkg.in/h2non/gock.v1/gock.go new file mode 100644 index 0000000000..c3a8333b57 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/gock.go @@ -0,0 +1,178 @@ +package gock + +import ( + "fmt" + "net/http" + "net/http/httputil" + "net/url" + "regexp" + "sync" +) + +// mutex is used interally for locking thread-sensitive functions. +var mutex = &sync.Mutex{} + +// config global singleton store. +var config = struct { + Networking bool + NetworkingFilters []FilterRequestFunc + Observer ObserverFunc +}{} + +// ObserverFunc is implemented by users to inspect the outgoing intercepted HTTP traffic +type ObserverFunc func(*http.Request, Mock) + +// DumpRequest is a default implementation of ObserverFunc that dumps +// the HTTP/1.x wire representation of the http request +var DumpRequest ObserverFunc = func(request *http.Request, mock Mock) { + bytes, _ := httputil.DumpRequestOut(request, true) + fmt.Println(string(bytes)) + fmt.Printf("\nMatches: %v\n---\n", mock != nil) +} + +// track unmatched requests so they can be tested for +var unmatchedRequests = []*http.Request{} + +// New creates and registers a new HTTP mock with +// default settings and returns the Request DSL for HTTP mock +// definition and set up. +func New(uri string) *Request { + Intercept() + + res := NewResponse() + req := NewRequest() + req.URLStruct, res.Error = url.Parse(normalizeURI(uri)) + + // Create the new mock expectation + exp := NewMock(req, res) + Register(exp) + + return req +} + +// Intercepting returns true if gock is currently able to intercept. +func Intercepting() bool { + mutex.Lock() + defer mutex.Unlock() + return http.DefaultTransport == DefaultTransport +} + +// Intercept enables HTTP traffic interception via http.DefaultTransport. +// If you are using a custom HTTP transport, you have to use `gock.Transport()` +func Intercept() { + if !Intercepting() { + mutex.Lock() + http.DefaultTransport = DefaultTransport + mutex.Unlock() + } +} + +// InterceptClient allows the developer to intercept HTTP traffic using +// a custom http.Client who uses a non default http.Transport/http.RoundTripper implementation. +func InterceptClient(cli *http.Client) { + _, ok := cli.Transport.(*Transport) + if ok { + return // if transport already intercepted, just ignore it + } + trans := NewTransport() + trans.Transport = cli.Transport + cli.Transport = trans +} + +// RestoreClient allows the developer to disable and restore the +// original transport in the given http.Client. +func RestoreClient(cli *http.Client) { + trans, ok := cli.Transport.(*Transport) + if !ok { + return + } + cli.Transport = trans.Transport +} + +// Disable disables HTTP traffic interception by gock. +func Disable() { + mutex.Lock() + defer mutex.Unlock() + http.DefaultTransport = NativeTransport +} + +// Off disables the default HTTP interceptors and removes +// all the registered mocks, even if they has not been intercepted yet. +func Off() { + Flush() + Disable() +} + +// OffAll is like `Off()`, but it also removes the unmatched requests registry. +func OffAll() { + Flush() + Disable() + CleanUnmatchedRequest() +} + +// Observe provides a hook to support inspection of the request and matched mock +func Observe(fn ObserverFunc) { + mutex.Lock() + defer mutex.Unlock() + config.Observer = fn +} + +// EnableNetworking enables real HTTP networking +func EnableNetworking() { + mutex.Lock() + defer mutex.Unlock() + config.Networking = true +} + +// DisableNetworking disables real HTTP networking +func DisableNetworking() { + mutex.Lock() + defer mutex.Unlock() + config.Networking = false +} + +// NetworkingFilter determines if an http.Request should be triggered or not. +func NetworkingFilter(fn FilterRequestFunc) { + mutex.Lock() + defer mutex.Unlock() + config.NetworkingFilters = append(config.NetworkingFilters, fn) +} + +// DisableNetworkingFilters disables registered networking filters. +func DisableNetworkingFilters() { + mutex.Lock() + defer mutex.Unlock() + config.NetworkingFilters = []FilterRequestFunc{} +} + +// GetUnmatchedRequests returns all requests that have been received but haven't matched any mock +func GetUnmatchedRequests() []*http.Request { + mutex.Lock() + defer mutex.Unlock() + return unmatchedRequests +} + +// HasUnmatchedRequest returns true if gock has received any requests that didn't match a mock +func HasUnmatchedRequest() bool { + return len(GetUnmatchedRequests()) > 0 +} + +// CleanUnmatchedRequest cleans the unmatched requests internal registry. +func CleanUnmatchedRequest() { + mutex.Lock() + defer mutex.Unlock() + unmatchedRequests = []*http.Request{} +} + +func trackUnmatchedRequest(req *http.Request) { + mutex.Lock() + defer mutex.Unlock() + unmatchedRequests = append(unmatchedRequests, req) +} + +func normalizeURI(uri string) string { + if ok, _ := regexp.MatchString("^http[s]?", uri); !ok { + return "http://" + uri + } + return uri +} diff --git a/vendor/gopkg.in/h2non/gock.v1/matcher.go b/vendor/gopkg.in/h2non/gock.v1/matcher.go new file mode 100644 index 0000000000..11a1d7eac7 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/matcher.go @@ -0,0 +1,137 @@ +package gock + +import "net/http" + +// MatchersHeader exposes an slice of HTTP header specific mock matchers. +var MatchersHeader = []MatchFunc{ + MatchMethod, + MatchScheme, + MatchHost, + MatchPath, + MatchHeaders, + MatchQueryParams, + MatchPathParams, +} + +// MatchersBody exposes an slice of HTTP body specific built-in mock matchers. +var MatchersBody = []MatchFunc{ + MatchBody, +} + +// Matchers stores all the built-in mock matchers. +var Matchers = append(MatchersHeader, MatchersBody...) + +// DefaultMatcher stores the default Matcher instance used to match mocks. +var DefaultMatcher = NewMatcher() + +// MatchFunc represents the required function +// interface implemented by matchers. +type MatchFunc func(*http.Request, *Request) (bool, error) + +// Matcher represents the required interface implemented by mock matchers. +type Matcher interface { + // Get returns a slice of registered function matchers. + Get() []MatchFunc + + // Add adds a new matcher function. + Add(MatchFunc) + + // Set sets the matchers functions stack. + Set([]MatchFunc) + + // Flush flushes the current matchers function stack. + Flush() + + // Match matches the given http.Request with a mock Request. + Match(*http.Request, *Request) (bool, error) +} + +// MockMatcher implements a mock matcher +type MockMatcher struct { + Matchers []MatchFunc +} + +// NewMatcher creates a new mock matcher +// using the default matcher functions. +func NewMatcher() *MockMatcher { + m := NewEmptyMatcher() + for _, matchFn := range Matchers { + m.Add(matchFn) + } + return m +} + +// NewBasicMatcher creates a new matcher with header only mock matchers. +func NewBasicMatcher() *MockMatcher { + m := NewEmptyMatcher() + for _, matchFn := range MatchersHeader { + m.Add(matchFn) + } + return m +} + +// NewEmptyMatcher creates a new empty matcher without default matchers. +func NewEmptyMatcher() *MockMatcher { + return &MockMatcher{Matchers: []MatchFunc{}} +} + +// Get returns a slice of registered function matchers. +func (m *MockMatcher) Get() []MatchFunc { + mutex.Lock() + defer mutex.Unlock() + return m.Matchers +} + +// Add adds a new function matcher. +func (m *MockMatcher) Add(fn MatchFunc) { + m.Matchers = append(m.Matchers, fn) +} + +// Set sets a new stack of matchers functions. +func (m *MockMatcher) Set(stack []MatchFunc) { + m.Matchers = stack +} + +// Flush flushes the current matcher +func (m *MockMatcher) Flush() { + m.Matchers = []MatchFunc{} +} + +// Clone returns a separate MockMatcher instance that has a copy of the same MatcherFuncs +func (m *MockMatcher) Clone() *MockMatcher { + m2 := NewEmptyMatcher() + for _, mFn := range m.Get() { + m2.Add(mFn) + } + return m2 +} + +// Match matches the given http.Request with a mock request +// returning true in case that the request matches, otherwise false. +func (m *MockMatcher) Match(req *http.Request, ereq *Request) (bool, error) { + for _, matcher := range m.Matchers { + matches, err := matcher(req, ereq) + if err != nil { + return false, err + } + if !matches { + return false, nil + } + } + return true, nil +} + +// MatchMock is a helper function that matches the given http.Request +// in the list of registered mocks, returning it if matches or error if it fails. +func MatchMock(req *http.Request) (Mock, error) { + for _, mock := range GetAll() { + matches, err := mock.Match(req) + if err != nil { + return nil, err + } + if matches { + return mock, nil + } + } + return nil, nil +} diff --git a/vendor/gopkg.in/h2non/gock.v1/matchers.go b/vendor/gopkg.in/h2non/gock.v1/matchers.go new file mode 100644 index 0000000000..658c9a6e60 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/matchers.go @@ -0,0 +1,266 @@ +package gock + +import ( + "compress/gzip" + "encoding/json" + "io" + "io/ioutil" + "net/http" + "reflect" + "regexp" + "strings" + + "github.com/h2non/parth" +) + +// EOL represents the end of line character. +const EOL = 0xa + +// BodyTypes stores the supported MIME body types for matching. +// Currently only text-based types. +var BodyTypes = []string{ + "text/html", + "text/plain", + "application/json", + "application/xml", + "multipart/form-data", + "application/x-www-form-urlencoded", +} + +// BodyTypeAliases stores a generic MIME type by alias. +var BodyTypeAliases = map[string]string{ + "html": "text/html", + "text": "text/plain", + "json": "application/json", + "xml": "application/xml", + "form": "multipart/form-data", + "url": "application/x-www-form-urlencoded", +} + +// CompressionSchemes stores the supported Content-Encoding types for decompression. +var CompressionSchemes = []string{ + "gzip", +} + +// MatchMethod matches the HTTP method of the given request. +func MatchMethod(req *http.Request, ereq *Request) (bool, error) { + return ereq.Method == "" || req.Method == ereq.Method, nil +} + +// MatchScheme matches the request URL protocol scheme. +func MatchScheme(req *http.Request, ereq *Request) (bool, error) { + return ereq.URLStruct.Scheme == "" || req.URL.Scheme == "" || ereq.URLStruct.Scheme == req.URL.Scheme, nil +} + +// MatchHost matches the HTTP host header field of the given request. +func MatchHost(req *http.Request, ereq *Request) (bool, error) { + url := ereq.URLStruct + if strings.EqualFold(url.Host, req.URL.Host) { + return true, nil + } + if !ereq.Options.DisableRegexpHost { + return regexp.MatchString(url.Host, req.URL.Host) + } + return false, nil +} + +// MatchPath matches the HTTP URL path of the given request. +func MatchPath(req *http.Request, ereq *Request) (bool, error) { + if req.URL.Path == ereq.URLStruct.Path { + return true, nil + } + return regexp.MatchString(ereq.URLStruct.Path, req.URL.Path) +} + +// MatchHeaders matches the headers fields of the given request. +func MatchHeaders(req *http.Request, ereq *Request) (bool, error) { + for key, value := range ereq.Header { + var err error + var match bool + var matchEscaped bool + + for _, field := range req.Header[key] { + match, err = regexp.MatchString(value[0], field) + // Some values may contain reserved regex params e.g. "()", try matching with these escaped. + matchEscaped, err = regexp.MatchString(regexp.QuoteMeta(value[0]), field) + + if err != nil { + return false, err + } + if match || matchEscaped { + break + } + + } + + if !match && !matchEscaped { + return false, nil + } + } + return true, nil +} + +// MatchQueryParams matches the URL query params fields of the given request. +func MatchQueryParams(req *http.Request, ereq *Request) (bool, error) { + for key, value := range ereq.URLStruct.Query() { + var err error + var match bool + + for _, field := range req.URL.Query()[key] { + match, err = regexp.MatchString(value[0], field) + if err != nil { + return false, err + } + if match { + break + } + } + + if !match { + return false, nil + } + } + return true, nil +} + +// MatchPathParams matches the URL path parameters of the given request. +func MatchPathParams(req *http.Request, ereq *Request) (bool, error) { + for key, value := range ereq.PathParams { + var s string + + if err := parth.Sequent(req.URL.Path, key, &s); err != nil { + return false, nil + } + + if s != value { + return false, nil + } + } + return true, nil +} + +// MatchBody tries to match the request body. +// TODO: not too smart now, needs several improvements. +func MatchBody(req *http.Request, ereq *Request) (bool, error) { + // If match body is empty, just continue + if req.Method == "HEAD" || len(ereq.BodyBuffer) == 0 { + return true, nil + } + + // Only can match certain MIME body types + if !supportedType(req, ereq) { + return false, nil + } + + // Can only match certain compression schemes + if !supportedCompressionScheme(req) { + return false, nil + } + + // Create a reader for the body depending on compression type + bodyReader := req.Body + if ereq.CompressionScheme != "" { + if ereq.CompressionScheme != req.Header.Get("Content-Encoding") { + return false, nil + } + compressedBodyReader, err := compressionReader(req.Body, ereq.CompressionScheme) + if err != nil { + return false, err + } + bodyReader = compressedBodyReader + } + + // Read the whole request body + body, err := ioutil.ReadAll(bodyReader) + if err != nil { + return false, err + } + + // Restore body reader stream + req.Body = createReadCloser(body) + + // If empty, ignore the match + if len(body) == 0 && len(ereq.BodyBuffer) != 0 { + return false, nil + } + + // Match body by atomic string comparison + bodyStr := castToString(body) + matchStr := castToString(ereq.BodyBuffer) + if bodyStr == matchStr { + return true, nil + } + + // Match request body by regexp + match, _ := regexp.MatchString(matchStr, bodyStr) + if match == true { + return true, nil + } + + // todo - add conditional do only perform the conversion of body bytes + // representation of JSON to a map and then compare them for equality. + + // Check if the key + value pairs match + var bodyMap map[string]interface{} + var matchMap map[string]interface{} + + // Ensure that both byte bodies that that should be JSON can be converted to maps. + umErr := json.Unmarshal(body, &bodyMap) + umErr2 := json.Unmarshal(ereq.BodyBuffer, &matchMap) + if umErr == nil && umErr2 == nil && reflect.DeepEqual(bodyMap, matchMap) { + return true, nil + } + + return false, nil +} + +func supportedType(req *http.Request, ereq *Request) bool { + mime := req.Header.Get("Content-Type") + if mime == "" { + return true + } + + mimeToMatch := ereq.Header.Get("Content-Type") + if mimeToMatch != "" { + return mime == mimeToMatch + } + + for _, kind := range BodyTypes { + if match, _ := regexp.MatchString(kind, mime); match { + return true + } + } + return false +} + +func supportedCompressionScheme(req *http.Request) bool { + encoding := req.Header.Get("Content-Encoding") + if encoding == "" { + return true + } + + for _, kind := range CompressionSchemes { + if match, _ := regexp.MatchString(kind, encoding); match { + return true + } + } + return false +} + +func castToString(buf []byte) string { + str := string(buf) + tail := len(str) - 1 + if str[tail] == EOL { + str = str[:tail] + } + return str +} + +func compressionReader(r io.ReadCloser, scheme string) (io.ReadCloser, error) { + switch scheme { + case "gzip": + return gzip.NewReader(r) + default: + return r, nil + } +} diff --git a/vendor/gopkg.in/h2non/gock.v1/mock.go b/vendor/gopkg.in/h2non/gock.v1/mock.go new file mode 100644 index 0000000000..d28875bf37 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/mock.go @@ -0,0 +1,172 @@ +package gock + +import ( + "net/http" + "sync" +) + +// Mock represents the required interface that must +// be implemented by HTTP mock instances. +type Mock interface { + // Disable disables the current mock manually. + Disable() + + // Done returns true if the current mock is disabled. + Done() bool + + // Request returns the mock Request instance. + Request() *Request + + // Response returns the mock Response instance. + Response() *Response + + // Match matches the given http.Request with the current mock. + Match(*http.Request) (bool, error) + + // AddMatcher adds a new matcher function. + AddMatcher(MatchFunc) + + // SetMatcher uses a new matcher implementation. + SetMatcher(Matcher) +} + +// Mocker implements a Mock capable interface providing +// a default mock configuration used internally to store mocks. +type Mocker struct { + // disabler stores a disabler for thread safety checking current mock is disabled + disabler *disabler + + // mutex stores the mock mutex for thread safety. + mutex sync.Mutex + + // matcher stores a Matcher capable instance to match the given http.Request. + matcher Matcher + + // request stores the mock Request to match. + request *Request + + // response stores the mock Response to use in case of match. + response *Response +} + +type disabler struct { + // disabled stores if the current mock is disabled. + disabled bool + + // mutex stores the disabler mutex for thread safety. + mutex sync.RWMutex +} + +func (d *disabler) isDisabled() bool { + d.mutex.RLock() + defer d.mutex.RUnlock() + return d.disabled +} + +func (d *disabler) Disable() { + d.mutex.Lock() + defer d.mutex.Unlock() + d.disabled = true +} + +// NewMock creates a new HTTP mock based on the given request and response instances. +// It's mostly used internally. +func NewMock(req *Request, res *Response) *Mocker { + mock := &Mocker{ + disabler: new(disabler), + request: req, + response: res, + matcher: DefaultMatcher.Clone(), + } + res.Mock = mock + req.Mock = mock + req.Response = res + return mock +} + +// Disable disables the current mock manually. +func (m *Mocker) Disable() { + m.disabler.Disable() +} + +// Done returns true in case that the current mock +// instance is disabled and therefore must be removed. +func (m *Mocker) Done() bool { + // prevent deadlock with m.mutex + if m.disabler.isDisabled() { + return true + } + + m.mutex.Lock() + defer m.mutex.Unlock() + return !m.request.Persisted && m.request.Counter == 0 +} + +// Request returns the Request instance +// configured for the current HTTP mock. +func (m *Mocker) Request() *Request { + return m.request +} + +// Response returns the Response instance +// configured for the current HTTP mock. +func (m *Mocker) Response() *Response { + return m.response +} + +// Match matches the given http.Request with the current Request +// mock expectation, returning true if matches. +func (m *Mocker) Match(req *http.Request) (bool, error) { + if m.disabler.isDisabled() { + return false, nil + } + + // Filter + for _, filter := range m.request.Filters { + if !filter(req) { + return false, nil + } + } + + // Map + for _, mapper := range m.request.Mappers { + if treq := mapper(req); treq != nil { + req = treq + } + } + + // Match + matches, err := m.matcher.Match(req, m.request) + if matches { + m.decrement() + } + + return matches, err +} + +// SetMatcher sets a new matcher implementation +// for the current mock expectation. +func (m *Mocker) SetMatcher(matcher Matcher) { + m.matcher = matcher +} + +// AddMatcher adds a new matcher function +// for the current mock expectation. +func (m *Mocker) AddMatcher(fn MatchFunc) { + m.matcher.Add(fn) +} + +// decrement decrements the current mock Request counter. +func (m *Mocker) decrement() { + if m.request.Persisted { + return + } + + m.mutex.Lock() + defer m.mutex.Unlock() + + m.request.Counter-- + if m.request.Counter == 0 { + m.disabler.Disable() + } +} diff --git a/vendor/gopkg.in/h2non/gock.v1/options.go b/vendor/gopkg.in/h2non/gock.v1/options.go new file mode 100644 index 0000000000..188aa58d8b --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/options.go @@ -0,0 +1,8 @@ +package gock + +// Options represents customized option for gock +type Options struct { + // DisableRegexpHost stores if the host is only a plain string rather than regular expression, + // if DisableRegexpHost is true, host sets in gock.New(...) will be treated as plain string + DisableRegexpHost bool +} diff --git a/vendor/gopkg.in/h2non/gock.v1/request.go b/vendor/gopkg.in/h2non/gock.v1/request.go new file mode 100644 index 0000000000..5702417b2b --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/request.go @@ -0,0 +1,325 @@ +package gock + +import ( + "encoding/base64" + "io" + "io/ioutil" + "net/http" + "net/url" + "strings" +) + +// MapRequestFunc represents the required function interface for request mappers. +type MapRequestFunc func(*http.Request) *http.Request + +// FilterRequestFunc represents the required function interface for request filters. +type FilterRequestFunc func(*http.Request) bool + +// Request represents the high-level HTTP request used to store +// request fields used to match intercepted requests. +type Request struct { + // Mock stores the parent mock reference for the current request mock used for method delegation. + Mock Mock + + // Response stores the current Response instance for the current matches Request. + Response *Response + + // Error stores the latest mock request configuration error. + Error error + + // Counter stores the pending times that the current mock should be active. + Counter int + + // Persisted stores if the current mock should be always active. + Persisted bool + + // Options stores options for current Request. + Options Options + + // URLStruct stores the parsed URL as *url.URL struct. + URLStruct *url.URL + + // Method stores the Request HTTP method to match. + Method string + + // CompressionScheme stores the Request Compression scheme to match and use for decompression. + CompressionScheme string + + // Header stores the HTTP header fields to match. + Header http.Header + + // Cookies stores the Request HTTP cookies values to match. + Cookies []*http.Cookie + + // PathParams stores the path parameters to match. + PathParams map[string]string + + // BodyBuffer stores the body data to match. + BodyBuffer []byte + + // Mappers stores the request functions mappers used for matching. + Mappers []MapRequestFunc + + // Filters stores the request functions filters used for matching. + Filters []FilterRequestFunc +} + +// NewRequest creates a new Request instance. +func NewRequest() *Request { + return &Request{ + Counter: 1, + URLStruct: &url.URL{}, + Header: make(http.Header), + PathParams: make(map[string]string), + } +} + +// URL defines the mock URL to match. +func (r *Request) URL(uri string) *Request { + r.URLStruct, r.Error = url.Parse(uri) + return r +} + +// SetURL defines the url.URL struct to be used for matching. +func (r *Request) SetURL(u *url.URL) *Request { + r.URLStruct = u + return r +} + +// Path defines the mock URL path value to match. +func (r *Request) Path(path string) *Request { + r.URLStruct.Path = path + return r +} + +// Get specifies the GET method and the given URL path to match. +func (r *Request) Get(path string) *Request { + return r.method("GET", path) +} + +// Post specifies the POST method and the given URL path to match. +func (r *Request) Post(path string) *Request { + return r.method("POST", path) +} + +// Put specifies the PUT method and the given URL path to match. +func (r *Request) Put(path string) *Request { + return r.method("PUT", path) +} + +// Delete specifies the DELETE method and the given URL path to match. +func (r *Request) Delete(path string) *Request { + return r.method("DELETE", path) +} + +// Patch specifies the PATCH method and the given URL path to match. +func (r *Request) Patch(path string) *Request { + return r.method("PATCH", path) +} + +// Head specifies the HEAD method and the given URL path to match. +func (r *Request) Head(path string) *Request { + return r.method("HEAD", path) +} + +// method is a DRY shortcut used to declare the expected HTTP method and URL path. +func (r *Request) method(method, path string) *Request { + if path != "/" { + r.URLStruct.Path = path + } + r.Method = strings.ToUpper(method) + return r +} + +// Body defines the body data to match based on a io.Reader interface. +func (r *Request) Body(body io.Reader) *Request { + r.BodyBuffer, r.Error = ioutil.ReadAll(body) + return r +} + +// BodyString defines the body to match based on a given string. +func (r *Request) BodyString(body string) *Request { + r.BodyBuffer = []byte(body) + return r +} + +// File defines the body to match based on the given file path string. +func (r *Request) File(path string) *Request { + r.BodyBuffer, r.Error = ioutil.ReadFile(path) + return r +} + +// Compression defines the request compression scheme, and enables automatic body decompression. +// Supports only the "gzip" scheme so far. +func (r *Request) Compression(scheme string) *Request { + r.Header.Set("Content-Encoding", scheme) + r.CompressionScheme = scheme + return r +} + +// JSON defines the JSON body to match based on a given structure. +func (r *Request) JSON(data interface{}) *Request { + if r.Header.Get("Content-Type") == "" { + r.Header.Set("Content-Type", "application/json") + } + r.BodyBuffer, r.Error = readAndDecode(data, "json") + return r +} + +// XML defines the XML body to match based on a given structure. +func (r *Request) XML(data interface{}) *Request { + if r.Header.Get("Content-Type") == "" { + r.Header.Set("Content-Type", "application/xml") + } + r.BodyBuffer, r.Error = readAndDecode(data, "xml") + return r +} + +// MatchType defines the request Content-Type MIME header field. +// Supports custom MIME types and type aliases. E.g: json, xml, form, text... +func (r *Request) MatchType(kind string) *Request { + mime := BodyTypeAliases[kind] + if mime != "" { + kind = mime + } + r.Header.Set("Content-Type", kind) + return r +} + +// BasicAuth defines a username and password for HTTP Basic Authentication +func (r *Request) BasicAuth(username, password string) *Request { + r.Header.Set("Authorization", "Basic "+basicAuth(username, password)) + return r +} + +// MatchHeader defines a new key and value header to match. +func (r *Request) MatchHeader(key, value string) *Request { + r.Header.Set(key, value) + return r +} + +// HeaderPresent defines that a header field must be present in the request. +func (r *Request) HeaderPresent(key string) *Request { + r.Header.Set(key, ".*") + return r +} + +// MatchHeaders defines a map of key-value headers to match. +func (r *Request) MatchHeaders(headers map[string]string) *Request { + for key, value := range headers { + r.Header.Set(key, value) + } + return r +} + +// MatchParam defines a new key and value URL query param to match. +func (r *Request) MatchParam(key, value string) *Request { + query := r.URLStruct.Query() + query.Set(key, value) + r.URLStruct.RawQuery = query.Encode() + return r +} + +// MatchParams defines a map of URL query param key-value to match. +func (r *Request) MatchParams(params map[string]string) *Request { + query := r.URLStruct.Query() + for key, value := range params { + query.Set(key, value) + } + r.URLStruct.RawQuery = query.Encode() + return r +} + +// ParamPresent matches if the given query param key is present in the URL. +func (r *Request) ParamPresent(key string) *Request { + r.MatchParam(key, ".*") + return r +} + +// PathParam matches if a given path parameter key is present in the URL. +// +// The value is representative of the restful resource the key defines, e.g. +// // /users/123/name +// r.PathParam("users", "123") +// would match. +func (r *Request) PathParam(key, val string) *Request { + r.PathParams[key] = val + + return r +} + +// Persist defines the current HTTP mock as persistent and won't be removed after intercepting it. +func (r *Request) Persist() *Request { + r.Persisted = true + return r +} + +// WithOptions sets the options for the request. +func (r *Request) WithOptions(options Options) *Request { + r.Options = options + return r +} + +// Times defines the number of times that the current HTTP mock should remain active. +func (r *Request) Times(num int) *Request { + r.Counter = num + return r +} + +// AddMatcher adds a new matcher function to match the request. +func (r *Request) AddMatcher(fn MatchFunc) *Request { + r.Mock.AddMatcher(fn) + return r +} + +// SetMatcher sets a new matcher function to match the request. +func (r *Request) SetMatcher(matcher Matcher) *Request { + r.Mock.SetMatcher(matcher) + return r +} + +// Map adds a new request mapper function to map http.Request before the matching process. +func (r *Request) Map(fn MapRequestFunc) *Request { + r.Mappers = append(r.Mappers, fn) + return r +} + +// Filter filters a new request filter function to filter http.Request before the matching process. +func (r *Request) Filter(fn FilterRequestFunc) *Request { + r.Filters = append(r.Filters, fn) + return r +} + +// EnableNetworking enables the use real networking for the current mock. +func (r *Request) EnableNetworking() *Request { + if r.Response != nil { + r.Response.UseNetwork = true + } + return r +} + +// Reply defines the Response status code and returns the mock Response DSL. +func (r *Request) Reply(status int) *Response { + return r.Response.Status(status) +} + +// ReplyError defines the Response simulated error. +func (r *Request) ReplyError(err error) *Response { + return r.Response.SetError(err) +} + +// ReplyFunc allows the developer to define the mock response via a custom function. +func (r *Request) ReplyFunc(replier func(*Response)) *Response { + replier(r.Response) + return r.Response +} + +// See 2 (end of page 4) https://www.ietf.org/rfc/rfc2617.txt +// "To receive authorization, the client sends the userid and password, +// separated by a single colon (":") character, within a base64 +// encoded string in the credentials." +// It is not meant to be urlencoded. +func basicAuth(username, password string) string { + auth := username + ":" + password + return base64.StdEncoding.EncodeToString([]byte(auth)) +} diff --git a/vendor/gopkg.in/h2non/gock.v1/responder.go b/vendor/gopkg.in/h2non/gock.v1/responder.go new file mode 100644 index 0000000000..b71c7424fd --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/responder.go @@ -0,0 +1,105 @@ +package gock + +import ( + "bytes" + "io" + "io/ioutil" + "net/http" + "strconv" + "time" +) + +// Responder builds a mock http.Response based on the given Response mock. +func Responder(req *http.Request, mock *Response, res *http.Response) (*http.Response, error) { + // If error present, reply it + err := mock.Error + if err != nil { + return nil, err + } + + if res == nil { + res = createResponse(req) + } + + // Apply response filter + for _, filter := range mock.Filters { + if !filter(res) { + return res, nil + } + } + + // Define mock status code + if mock.StatusCode != 0 { + res.Status = strconv.Itoa(mock.StatusCode) + " " + http.StatusText(mock.StatusCode) + res.StatusCode = mock.StatusCode + } + + // Define headers by merging fields + res.Header = mergeHeaders(res, mock) + + // Define mock body, if present + if len(mock.BodyBuffer) > 0 { + res.ContentLength = int64(len(mock.BodyBuffer)) + res.Body = createReadCloser(mock.BodyBuffer) + } + + // Apply response mappers + for _, mapper := range mock.Mappers { + if tres := mapper(res); tres != nil { + res = tres + } + } + + // Sleep to simulate delay, if necessary + if mock.ResponseDelay > 0 { + // allow escaping from sleep due to request context expiration or cancellation + t := time.NewTimer(mock.ResponseDelay) + select { + case <-t.C: + case <-req.Context().Done(): + // cleanly stop the timer + if !t.Stop() { + <-t.C + } + } + } + + // check if the request context has ended. we could put this up in the delay code above, but putting it here + // has the added benefit of working even when there is no delay (very small timeouts, already-done contexts, etc.) + if err = req.Context().Err(); err != nil { + // cleanly close the response and return the context error + io.Copy(ioutil.Discard, res.Body) + res.Body.Close() + return nil, err + } + + return res, err +} + +// createResponse creates a new http.Response with default fields. +func createResponse(req *http.Request) *http.Response { + return &http.Response{ + ProtoMajor: 1, + ProtoMinor: 1, + Proto: "HTTP/1.1", + Request: req, + Header: make(http.Header), + Body: createReadCloser([]byte{}), + } +} + +// mergeHeaders copies the mock headers. +func mergeHeaders(res *http.Response, mres *Response) http.Header { + for key, values := range mres.Header { + for _, value := range values { + res.Header.Add(key, value) + } + } + return res.Header +} + +// createReadCloser creates an io.ReadCloser from a byte slice that is suitable for use as an +// http response body. +func createReadCloser(body []byte) io.ReadCloser { + return ioutil.NopCloser(bytes.NewReader(body)) +} diff --git a/vendor/gopkg.in/h2non/gock.v1/response.go b/vendor/gopkg.in/h2non/gock.v1/response.go new file mode 100644 index 0000000000..07b7158091 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/response.go @@ -0,0 +1,186 @@ +package gock + +import ( + "bytes" + "encoding/json" + "encoding/xml" + "io" + "io/ioutil" + "net/http" + "time" +) + +// MapResponseFunc represents the required function interface impletemed by response mappers. +type MapResponseFunc func(*http.Response) *http.Response + +// FilterResponseFunc represents the required function interface impletemed by response filters. +type FilterResponseFunc func(*http.Response) bool + +// Response represents high-level HTTP fields to configure +// and define HTTP responses intercepted by gock. +type Response struct { + // Mock stores the parent mock reference for the current response mock used for method delegation. + Mock Mock + + // Error stores the latest response configuration or injected error. + Error error + + // UseNetwork enables the use of real network for the current mock. + UseNetwork bool + + // StatusCode stores the response status code. + StatusCode int + + // Headers stores the response headers. + Header http.Header + + // Cookies stores the response cookie fields. + Cookies []*http.Cookie + + // BodyBuffer stores the array of bytes to use as body. + BodyBuffer []byte + + // ResponseDelay stores the simulated response delay. + ResponseDelay time.Duration + + // Mappers stores the request functions mappers used for matching. + Mappers []MapResponseFunc + + // Filters stores the request functions filters used for matching. + Filters []FilterResponseFunc +} + +// NewResponse creates a new Response. +func NewResponse() *Response { + return &Response{Header: make(http.Header)} +} + +// Status defines the desired HTTP status code to reply in the current response. +func (r *Response) Status(code int) *Response { + r.StatusCode = code + return r +} + +// Type defines the response Content-Type MIME header field. +// Supports type alias. E.g: json, xml, form, text... +func (r *Response) Type(kind string) *Response { + mime := BodyTypeAliases[kind] + if mime != "" { + kind = mime + } + r.Header.Set("Content-Type", kind) + return r +} + +// SetHeader sets a new header field in the mock response. +func (r *Response) SetHeader(key, value string) *Response { + r.Header.Set(key, value) + return r +} + +// AddHeader adds a new header field in the mock response +// with out removing an existent one. +func (r *Response) AddHeader(key, value string) *Response { + r.Header.Add(key, value) + return r +} + +// SetHeaders sets a map of header fields in the mock response. +func (r *Response) SetHeaders(headers map[string]string) *Response { + for key, value := range headers { + r.Header.Add(key, value) + } + return r +} + +// Body sets the HTTP response body to be used. +func (r *Response) Body(body io.Reader) *Response { + r.BodyBuffer, r.Error = ioutil.ReadAll(body) + return r +} + +// BodyString defines the response body as string. +func (r *Response) BodyString(body string) *Response { + r.BodyBuffer = []byte(body) + return r +} + +// File defines the response body reading the data +// from disk based on the file path string. +func (r *Response) File(path string) *Response { + r.BodyBuffer, r.Error = ioutil.ReadFile(path) + return r +} + +// JSON defines the response body based on a JSON based input. +func (r *Response) JSON(data interface{}) *Response { + r.Header.Set("Content-Type", "application/json") + r.BodyBuffer, r.Error = readAndDecode(data, "json") + return r +} + +// XML defines the response body based on a XML based input. +func (r *Response) XML(data interface{}) *Response { + r.Header.Set("Content-Type", "application/xml") + r.BodyBuffer, r.Error = readAndDecode(data, "xml") + return r +} + +// SetError defines the response simulated error. +func (r *Response) SetError(err error) *Response { + r.Error = err + return r +} + +// Delay defines the response simulated delay. +// This feature is still experimental and will be improved in the future. +func (r *Response) Delay(delay time.Duration) *Response { + r.ResponseDelay = delay + return r +} + +// Map adds a new response mapper function to map http.Response before the matching process. +func (r *Response) Map(fn MapResponseFunc) *Response { + r.Mappers = append(r.Mappers, fn) + return r +} + +// Filter filters a new request filter function to filter http.Request before the matching process. +func (r *Response) Filter(fn FilterResponseFunc) *Response { + r.Filters = append(r.Filters, fn) + return r +} + +// EnableNetworking enables the use real networking for the current mock. +func (r *Response) EnableNetworking() *Response { + r.UseNetwork = true + return r +} + +// Done returns true if the mock was done and disabled. +func (r *Response) Done() bool { + return r.Mock.Done() +} + +func readAndDecode(data interface{}, kind string) ([]byte, error) { + buf := &bytes.Buffer{} + + switch data.(type) { + case string: + buf.WriteString(data.(string)) + case []byte: + buf.Write(data.([]byte)) + default: + var err error + if kind == "xml" { + err = xml.NewEncoder(buf).Encode(data) + } else { + err = json.NewEncoder(buf).Encode(data) + } + if err != nil { + return nil, err + } + } + + return ioutil.ReadAll(buf) +} diff --git a/vendor/gopkg.in/h2non/gock.v1/store.go b/vendor/gopkg.in/h2non/gock.v1/store.go new file mode 100644 index 0000000000..fc3207e5ba --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/store.go @@ -0,0 +1,100 @@ +package gock + +import ( + "sync" +) + +// storeMutex is used interally for store synchronization. +var storeMutex = sync.RWMutex{} + +// mocks is internally used to store registered mocks. +var mocks = []Mock{} + +// Register registers a new mock in the current mocks stack. +func Register(mock Mock) { + if Exists(mock) { + return + } + + // Make ops thread safe + storeMutex.Lock() + defer storeMutex.Unlock() + + // Expose mock in request/response for delegation + mock.Request().Mock = mock + mock.Response().Mock = mock + + // Registers the mock in the global store + mocks = append(mocks, mock) +} + +// GetAll returns the current stack of registed mocks. +func GetAll() []Mock { + storeMutex.RLock() + defer storeMutex.RUnlock() + return mocks +} + +// Exists checks if the given Mock is already registered. +func Exists(m Mock) bool { + storeMutex.RLock() + defer storeMutex.RUnlock() + for _, mock := range mocks { + if mock == m { + return true + } + } + return false +} + +// Remove removes a registered mock by reference. +func Remove(m Mock) { + for i, mock := range mocks { + if mock == m { + storeMutex.Lock() + mocks = append(mocks[:i], mocks[i+1:]...) + storeMutex.Unlock() + } + } +} + +// Flush flushes the current stack of registered mocks. +func Flush() { + storeMutex.Lock() + defer storeMutex.Unlock() + mocks = []Mock{} +} + +// Pending returns an slice of pending mocks. +func Pending() []Mock { + Clean() + storeMutex.RLock() + defer storeMutex.RUnlock() + return mocks +} + +// IsDone returns true if all the registered mocks has been triggered successfully. +func IsDone() bool { + return !IsPending() +} + +// IsPending returns true if there are pending mocks. +func IsPending() bool { + return len(Pending()) > 0 +} + +// Clean cleans the mocks store removing disabled or obsolete mocks. +func Clean() { + storeMutex.Lock() + defer storeMutex.Unlock() + + buf := []Mock{} + for _, mock := range mocks { + if mock.Done() { + continue + } + buf = append(buf, mock) + } + + mocks = buf +} diff --git a/vendor/gopkg.in/h2non/gock.v1/transport.go b/vendor/gopkg.in/h2non/gock.v1/transport.go new file mode 100644 index 0000000000..5b2bba2836 --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/transport.go @@ -0,0 +1,112 @@ +package gock + +import ( + "errors" + "net/http" + "sync" +) + +// var mutex *sync.Mutex = &sync.Mutex{} + +var ( + // DefaultTransport stores the default mock transport used by gock. + DefaultTransport = NewTransport() + + // NativeTransport stores the native net/http default transport + // in order to restore it when needed. + NativeTransport = http.DefaultTransport +) + +var ( + // ErrCannotMatch store the error returned in case of no matches. + ErrCannotMatch = errors.New("gock: cannot match any request") +) + +// Transport implements http.RoundTripper, which fulfills single http requests issued by +// an http.Client. +// +// gock's Transport encapsulates a given or default http.Transport for further +// delegation, if needed. +type Transport struct { + // mutex is used to make transport thread-safe of concurrent uses across goroutines. + mutex sync.Mutex + + // Transport encapsulates the original http.RoundTripper transport interface for delegation. + Transport http.RoundTripper +} + +// NewTransport creates a new *Transport with no responders. +func NewTransport() *Transport { + return &Transport{Transport: NativeTransport} +} + +// RoundTrip receives HTTP requests and routes them to the appropriate responder. It is required to +// implement the http.RoundTripper interface. You will not interact with this directly, instead +// the *http.Client you are using will call it for you. +func (m *Transport) RoundTrip(req *http.Request) (*http.Response, error) { + // Just act as a proxy if not intercepting + if !Intercepting() { + return m.Transport.RoundTrip(req) + } + + m.mutex.Lock() + defer Clean() + + var err error + var res *http.Response + + // Match mock for the incoming http.Request + mock, err := MatchMock(req) + if err != nil { + m.mutex.Unlock() + return nil, err + } + + // Invoke the observer with the intercepted http.Request and matched mock + if config.Observer != nil { + config.Observer(req, mock) + } + + // Verify if should use real networking + networking := shouldUseNetwork(req, mock) + if !networking && mock == nil { + m.mutex.Unlock() + trackUnmatchedRequest(req) + return nil, ErrCannotMatch + } + + // Ensure me unlock the mutex before building the response + m.mutex.Unlock() + + // Perform real networking via original transport + if networking { + res, err = m.Transport.RoundTrip(req) + // In no mock matched, continue with the response + if err != nil || mock == nil { + return res, err + } + } + + return Responder(req, mock.Response(), res) +} + +// CancelRequest is a no-op function. +func (m *Transport) CancelRequest(req *http.Request) {} + +func shouldUseNetwork(req *http.Request, mock Mock) bool { + if mock != nil && mock.Response().UseNetwork { + return true + } + if !config.Networking { + return false + } + if len(config.NetworkingFilters) == 0 { + return true + } + for _, filter := range config.NetworkingFilters { + if !filter(req) { + return false + } + } + return true +} diff --git a/vendor/gopkg.in/h2non/gock.v1/version.go b/vendor/gopkg.in/h2non/gock.v1/version.go new file mode 100644 index 0000000000..63378b011e --- /dev/null +++ b/vendor/gopkg.in/h2non/gock.v1/version.go @@ -0,0 +1,4 @@ +package gock + +// Version defines the current package semantic version. +const Version = "1.1.2" diff --git a/vendor/gopkg.in/yaml.v3/LICENSE b/vendor/gopkg.in/yaml.v3/LICENSE new file mode 100644 index 0000000000..2683e4bb1f --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/LICENSE @@ -0,0 +1,50 @@ + +This project is covered by two different licenses: MIT and Apache. + +#### MIT License #### + +The following files were ported to Go from C files of libyaml, and thus +are still covered by their original MIT license, with the additional +copyright staring in 2011 when the project was ported over: + + apic.go emitterc.go parserc.go readerc.go scannerc.go + writerc.go yamlh.go yamlprivateh.go + +Copyright (c) 2006-2010 Kirill Simonov +Copyright (c) 2006-2011 Kirill Simonov + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +### Apache License ### + +All the remaining project files are covered by the Apache license: + +Copyright (c) 2011-2019 Canonical Ltd + +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. diff --git a/vendor/gopkg.in/yaml.v3/NOTICE b/vendor/gopkg.in/yaml.v3/NOTICE new file mode 100644 index 0000000000..866d74a7ad --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/NOTICE @@ -0,0 +1,13 @@ +Copyright 2011-2016 Canonical Ltd. + +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. diff --git a/vendor/gopkg.in/yaml.v3/README.md b/vendor/gopkg.in/yaml.v3/README.md new file mode 100644 index 0000000000..08eb1babdd --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/README.md @@ -0,0 +1,150 @@ +# YAML support for the Go language + +Introduction +------------ + +The yaml package enables Go programs to comfortably encode and decode YAML +values. It was developed within [Canonical](https://www.canonical.com) as +part of the [juju](https://juju.ubuntu.com) project, and is based on a +pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML) +C library to parse and generate YAML data quickly and reliably. + +Compatibility +------------- + +The yaml package supports most of YAML 1.2, but preserves some behavior +from 1.1 for backwards compatibility. + +Specifically, as of v3 of the yaml package: + + - YAML 1.1 bools (_yes/no, on/off_) are supported as long as they are being + decoded into a typed bool value. Otherwise they behave as a string. Booleans + in YAML 1.2 are _true/false_ only. + - Octals encode and decode as _0777_ per YAML 1.1, rather than _0o777_ + as specified in YAML 1.2, because most parsers still use the old format. + Octals in the _0o777_ format are supported though, so new files work. + - Does not support base-60 floats. These are gone from YAML 1.2, and were + actually never supported by this package as it's clearly a poor choice. + +and offers backwards +compatibility with YAML 1.1 in some cases. +1.2, including support for +anchors, tags, map merging, etc. Multi-document unmarshalling is not yet +implemented, and base-60 floats from YAML 1.1 are purposefully not +supported since they're a poor design and are gone in YAML 1.2. + +Installation and usage +---------------------- + +The import path for the package is *gopkg.in/yaml.v3*. + +To install it, run: + + go get gopkg.in/yaml.v3 + +API documentation +----------------- + +If opened in a browser, the import path itself leads to the API documentation: + + - [https://gopkg.in/yaml.v3](https://gopkg.in/yaml.v3) + +API stability +------------- + +The package API for yaml v3 will remain stable as described in [gopkg.in](https://gopkg.in). + + +License +------- + +The yaml package is licensed under the MIT and Apache License 2.0 licenses. +Please see the LICENSE file for details. + + +Example +------- + +```Go +package main + +import ( + "fmt" + "log" + + "gopkg.in/yaml.v3" +) + +var data = ` +a: Easy! +b: + c: 2 + d: [3, 4] +` + +// Note: struct fields must be public in order for unmarshal to +// correctly populate the data. +type T struct { + A string + B struct { + RenamedC int `yaml:"c"` + D []int `yaml:",flow"` + } +} + +func main() { + t := T{} + + err := yaml.Unmarshal([]byte(data), &t) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- t:\n%v\n\n", t) + + d, err := yaml.Marshal(&t) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- t dump:\n%s\n\n", string(d)) + + m := make(map[interface{}]interface{}) + + err = yaml.Unmarshal([]byte(data), &m) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- m:\n%v\n\n", m) + + d, err = yaml.Marshal(&m) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- m dump:\n%s\n\n", string(d)) +} +``` + +This example will generate the following output: + +``` +--- t: +{Easy! {2 [3 4]}} + +--- t dump: +a: Easy! +b: + c: 2 + d: [3, 4] + + +--- m: +map[a:Easy! b:map[c:2 d:[3 4]]] + +--- m dump: +a: Easy! +b: + c: 2 + d: + - 3 + - 4 +``` + diff --git a/vendor/gopkg.in/yaml.v3/apic.go b/vendor/gopkg.in/yaml.v3/apic.go new file mode 100644 index 0000000000..ae7d049f18 --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/apic.go @@ -0,0 +1,747 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +import ( + "io" +) + +func yaml_insert_token(parser *yaml_parser_t, pos int, token *yaml_token_t) { + //fmt.Println("yaml_insert_token", "pos:", pos, "typ:", token.typ, "head:", parser.tokens_head, "len:", len(parser.tokens)) + + // Check if we can move the queue at the beginning of the buffer. + if parser.tokens_head > 0 && len(parser.tokens) == cap(parser.tokens) { + if parser.tokens_head != len(parser.tokens) { + copy(parser.tokens, parser.tokens[parser.tokens_head:]) + } + parser.tokens = parser.tokens[:len(parser.tokens)-parser.tokens_head] + parser.tokens_head = 0 + } + parser.tokens = append(parser.tokens, *token) + if pos < 0 { + return + } + copy(parser.tokens[parser.tokens_head+pos+1:], parser.tokens[parser.tokens_head+pos:]) + parser.tokens[parser.tokens_head+pos] = *token +} + +// Create a new parser object. +func yaml_parser_initialize(parser *yaml_parser_t) bool { + *parser = yaml_parser_t{ + raw_buffer: make([]byte, 0, input_raw_buffer_size), + buffer: make([]byte, 0, input_buffer_size), + } + return true +} + +// Destroy a parser object. +func yaml_parser_delete(parser *yaml_parser_t) { + *parser = yaml_parser_t{} +} + +// String read handler. +func yaml_string_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) { + if parser.input_pos == len(parser.input) { + return 0, io.EOF + } + n = copy(buffer, parser.input[parser.input_pos:]) + parser.input_pos += n + return n, nil +} + +// Reader read handler. +func yaml_reader_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) { + return parser.input_reader.Read(buffer) +} + +// Set a string input. +func yaml_parser_set_input_string(parser *yaml_parser_t, input []byte) { + if parser.read_handler != nil { + panic("must set the input source only once") + } + parser.read_handler = yaml_string_read_handler + parser.input = input + parser.input_pos = 0 +} + +// Set a file input. +func yaml_parser_set_input_reader(parser *yaml_parser_t, r io.Reader) { + if parser.read_handler != nil { + panic("must set the input source only once") + } + parser.read_handler = yaml_reader_read_handler + parser.input_reader = r +} + +// Set the source encoding. +func yaml_parser_set_encoding(parser *yaml_parser_t, encoding yaml_encoding_t) { + if parser.encoding != yaml_ANY_ENCODING { + panic("must set the encoding only once") + } + parser.encoding = encoding +} + +// Create a new emitter object. +func yaml_emitter_initialize(emitter *yaml_emitter_t) { + *emitter = yaml_emitter_t{ + buffer: make([]byte, output_buffer_size), + raw_buffer: make([]byte, 0, output_raw_buffer_size), + states: make([]yaml_emitter_state_t, 0, initial_stack_size), + events: make([]yaml_event_t, 0, initial_queue_size), + best_width: -1, + } +} + +// Destroy an emitter object. +func yaml_emitter_delete(emitter *yaml_emitter_t) { + *emitter = yaml_emitter_t{} +} + +// String write handler. +func yaml_string_write_handler(emitter *yaml_emitter_t, buffer []byte) error { + *emitter.output_buffer = append(*emitter.output_buffer, buffer...) + return nil +} + +// yaml_writer_write_handler uses emitter.output_writer to write the +// emitted text. +func yaml_writer_write_handler(emitter *yaml_emitter_t, buffer []byte) error { + _, err := emitter.output_writer.Write(buffer) + return err +} + +// Set a string output. +func yaml_emitter_set_output_string(emitter *yaml_emitter_t, output_buffer *[]byte) { + if emitter.write_handler != nil { + panic("must set the output target only once") + } + emitter.write_handler = yaml_string_write_handler + emitter.output_buffer = output_buffer +} + +// Set a file output. +func yaml_emitter_set_output_writer(emitter *yaml_emitter_t, w io.Writer) { + if emitter.write_handler != nil { + panic("must set the output target only once") + } + emitter.write_handler = yaml_writer_write_handler + emitter.output_writer = w +} + +// Set the output encoding. +func yaml_emitter_set_encoding(emitter *yaml_emitter_t, encoding yaml_encoding_t) { + if emitter.encoding != yaml_ANY_ENCODING { + panic("must set the output encoding only once") + } + emitter.encoding = encoding +} + +// Set the canonical output style. +func yaml_emitter_set_canonical(emitter *yaml_emitter_t, canonical bool) { + emitter.canonical = canonical +} + +// Set the indentation increment. +func yaml_emitter_set_indent(emitter *yaml_emitter_t, indent int) { + if indent < 2 || indent > 9 { + indent = 2 + } + emitter.best_indent = indent +} + +// Set the preferred line width. +func yaml_emitter_set_width(emitter *yaml_emitter_t, width int) { + if width < 0 { + width = -1 + } + emitter.best_width = width +} + +// Set if unescaped non-ASCII characters are allowed. +func yaml_emitter_set_unicode(emitter *yaml_emitter_t, unicode bool) { + emitter.unicode = unicode +} + +// Set the preferred line break character. +func yaml_emitter_set_break(emitter *yaml_emitter_t, line_break yaml_break_t) { + emitter.line_break = line_break +} + +///* +// * Destroy a token object. +// */ +// +//YAML_DECLARE(void) +//yaml_token_delete(yaml_token_t *token) +//{ +// assert(token); // Non-NULL token object expected. +// +// switch (token.type) +// { +// case YAML_TAG_DIRECTIVE_TOKEN: +// yaml_free(token.data.tag_directive.handle); +// yaml_free(token.data.tag_directive.prefix); +// break; +// +// case YAML_ALIAS_TOKEN: +// yaml_free(token.data.alias.value); +// break; +// +// case YAML_ANCHOR_TOKEN: +// yaml_free(token.data.anchor.value); +// break; +// +// case YAML_TAG_TOKEN: +// yaml_free(token.data.tag.handle); +// yaml_free(token.data.tag.suffix); +// break; +// +// case YAML_SCALAR_TOKEN: +// yaml_free(token.data.scalar.value); +// break; +// +// default: +// break; +// } +// +// memset(token, 0, sizeof(yaml_token_t)); +//} +// +///* +// * Check if a string is a valid UTF-8 sequence. +// * +// * Check 'reader.c' for more details on UTF-8 encoding. +// */ +// +//static int +//yaml_check_utf8(yaml_char_t *start, size_t length) +//{ +// yaml_char_t *end = start+length; +// yaml_char_t *pointer = start; +// +// while (pointer < end) { +// unsigned char octet; +// unsigned int width; +// unsigned int value; +// size_t k; +// +// octet = pointer[0]; +// width = (octet & 0x80) == 0x00 ? 1 : +// (octet & 0xE0) == 0xC0 ? 2 : +// (octet & 0xF0) == 0xE0 ? 3 : +// (octet & 0xF8) == 0xF0 ? 4 : 0; +// value = (octet & 0x80) == 0x00 ? octet & 0x7F : +// (octet & 0xE0) == 0xC0 ? octet & 0x1F : +// (octet & 0xF0) == 0xE0 ? octet & 0x0F : +// (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; +// if (!width) return 0; +// if (pointer+width > end) return 0; +// for (k = 1; k < width; k ++) { +// octet = pointer[k]; +// if ((octet & 0xC0) != 0x80) return 0; +// value = (value << 6) + (octet & 0x3F); +// } +// if (!((width == 1) || +// (width == 2 && value >= 0x80) || +// (width == 3 && value >= 0x800) || +// (width == 4 && value >= 0x10000))) return 0; +// +// pointer += width; +// } +// +// return 1; +//} +// + +// Create STREAM-START. +func yaml_stream_start_event_initialize(event *yaml_event_t, encoding yaml_encoding_t) { + *event = yaml_event_t{ + typ: yaml_STREAM_START_EVENT, + encoding: encoding, + } +} + +// Create STREAM-END. +func yaml_stream_end_event_initialize(event *yaml_event_t) { + *event = yaml_event_t{ + typ: yaml_STREAM_END_EVENT, + } +} + +// Create DOCUMENT-START. +func yaml_document_start_event_initialize( + event *yaml_event_t, + version_directive *yaml_version_directive_t, + tag_directives []yaml_tag_directive_t, + implicit bool, +) { + *event = yaml_event_t{ + typ: yaml_DOCUMENT_START_EVENT, + version_directive: version_directive, + tag_directives: tag_directives, + implicit: implicit, + } +} + +// Create DOCUMENT-END. +func yaml_document_end_event_initialize(event *yaml_event_t, implicit bool) { + *event = yaml_event_t{ + typ: yaml_DOCUMENT_END_EVENT, + implicit: implicit, + } +} + +// Create ALIAS. +func yaml_alias_event_initialize(event *yaml_event_t, anchor []byte) bool { + *event = yaml_event_t{ + typ: yaml_ALIAS_EVENT, + anchor: anchor, + } + return true +} + +// Create SCALAR. +func yaml_scalar_event_initialize(event *yaml_event_t, anchor, tag, value []byte, plain_implicit, quoted_implicit bool, style yaml_scalar_style_t) bool { + *event = yaml_event_t{ + typ: yaml_SCALAR_EVENT, + anchor: anchor, + tag: tag, + value: value, + implicit: plain_implicit, + quoted_implicit: quoted_implicit, + style: yaml_style_t(style), + } + return true +} + +// Create SEQUENCE-START. +func yaml_sequence_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_sequence_style_t) bool { + *event = yaml_event_t{ + typ: yaml_SEQUENCE_START_EVENT, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(style), + } + return true +} + +// Create SEQUENCE-END. +func yaml_sequence_end_event_initialize(event *yaml_event_t) bool { + *event = yaml_event_t{ + typ: yaml_SEQUENCE_END_EVENT, + } + return true +} + +// Create MAPPING-START. +func yaml_mapping_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_mapping_style_t) { + *event = yaml_event_t{ + typ: yaml_MAPPING_START_EVENT, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(style), + } +} + +// Create MAPPING-END. +func yaml_mapping_end_event_initialize(event *yaml_event_t) { + *event = yaml_event_t{ + typ: yaml_MAPPING_END_EVENT, + } +} + +// Destroy an event object. +func yaml_event_delete(event *yaml_event_t) { + *event = yaml_event_t{} +} + +///* +// * Create a document object. +// */ +// +//YAML_DECLARE(int) +//yaml_document_initialize(document *yaml_document_t, +// version_directive *yaml_version_directive_t, +// tag_directives_start *yaml_tag_directive_t, +// tag_directives_end *yaml_tag_directive_t, +// start_implicit int, end_implicit int) +//{ +// struct { +// error yaml_error_type_t +// } context +// struct { +// start *yaml_node_t +// end *yaml_node_t +// top *yaml_node_t +// } nodes = { NULL, NULL, NULL } +// version_directive_copy *yaml_version_directive_t = NULL +// struct { +// start *yaml_tag_directive_t +// end *yaml_tag_directive_t +// top *yaml_tag_directive_t +// } tag_directives_copy = { NULL, NULL, NULL } +// value yaml_tag_directive_t = { NULL, NULL } +// mark yaml_mark_t = { 0, 0, 0 } +// +// assert(document) // Non-NULL document object is expected. +// assert((tag_directives_start && tag_directives_end) || +// (tag_directives_start == tag_directives_end)) +// // Valid tag directives are expected. +// +// if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error +// +// if (version_directive) { +// version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)) +// if (!version_directive_copy) goto error +// version_directive_copy.major = version_directive.major +// version_directive_copy.minor = version_directive.minor +// } +// +// if (tag_directives_start != tag_directives_end) { +// tag_directive *yaml_tag_directive_t +// if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) +// goto error +// for (tag_directive = tag_directives_start +// tag_directive != tag_directives_end; tag_directive ++) { +// assert(tag_directive.handle) +// assert(tag_directive.prefix) +// if (!yaml_check_utf8(tag_directive.handle, +// strlen((char *)tag_directive.handle))) +// goto error +// if (!yaml_check_utf8(tag_directive.prefix, +// strlen((char *)tag_directive.prefix))) +// goto error +// value.handle = yaml_strdup(tag_directive.handle) +// value.prefix = yaml_strdup(tag_directive.prefix) +// if (!value.handle || !value.prefix) goto error +// if (!PUSH(&context, tag_directives_copy, value)) +// goto error +// value.handle = NULL +// value.prefix = NULL +// } +// } +// +// DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy, +// tag_directives_copy.start, tag_directives_copy.top, +// start_implicit, end_implicit, mark, mark) +// +// return 1 +// +//error: +// STACK_DEL(&context, nodes) +// yaml_free(version_directive_copy) +// while (!STACK_EMPTY(&context, tag_directives_copy)) { +// value yaml_tag_directive_t = POP(&context, tag_directives_copy) +// yaml_free(value.handle) +// yaml_free(value.prefix) +// } +// STACK_DEL(&context, tag_directives_copy) +// yaml_free(value.handle) +// yaml_free(value.prefix) +// +// return 0 +//} +// +///* +// * Destroy a document object. +// */ +// +//YAML_DECLARE(void) +//yaml_document_delete(document *yaml_document_t) +//{ +// struct { +// error yaml_error_type_t +// } context +// tag_directive *yaml_tag_directive_t +// +// context.error = YAML_NO_ERROR // Eliminate a compiler warning. +// +// assert(document) // Non-NULL document object is expected. +// +// while (!STACK_EMPTY(&context, document.nodes)) { +// node yaml_node_t = POP(&context, document.nodes) +// yaml_free(node.tag) +// switch (node.type) { +// case YAML_SCALAR_NODE: +// yaml_free(node.data.scalar.value) +// break +// case YAML_SEQUENCE_NODE: +// STACK_DEL(&context, node.data.sequence.items) +// break +// case YAML_MAPPING_NODE: +// STACK_DEL(&context, node.data.mapping.pairs) +// break +// default: +// assert(0) // Should not happen. +// } +// } +// STACK_DEL(&context, document.nodes) +// +// yaml_free(document.version_directive) +// for (tag_directive = document.tag_directives.start +// tag_directive != document.tag_directives.end +// tag_directive++) { +// yaml_free(tag_directive.handle) +// yaml_free(tag_directive.prefix) +// } +// yaml_free(document.tag_directives.start) +// +// memset(document, 0, sizeof(yaml_document_t)) +//} +// +///** +// * Get a document node. +// */ +// +//YAML_DECLARE(yaml_node_t *) +//yaml_document_get_node(document *yaml_document_t, index int) +//{ +// assert(document) // Non-NULL document object is expected. +// +// if (index > 0 && document.nodes.start + index <= document.nodes.top) { +// return document.nodes.start + index - 1 +// } +// return NULL +//} +// +///** +// * Get the root object. +// */ +// +//YAML_DECLARE(yaml_node_t *) +//yaml_document_get_root_node(document *yaml_document_t) +//{ +// assert(document) // Non-NULL document object is expected. +// +// if (document.nodes.top != document.nodes.start) { +// return document.nodes.start +// } +// return NULL +//} +// +///* +// * Add a scalar node to a document. +// */ +// +//YAML_DECLARE(int) +//yaml_document_add_scalar(document *yaml_document_t, +// tag *yaml_char_t, value *yaml_char_t, length int, +// style yaml_scalar_style_t) +//{ +// struct { +// error yaml_error_type_t +// } context +// mark yaml_mark_t = { 0, 0, 0 } +// tag_copy *yaml_char_t = NULL +// value_copy *yaml_char_t = NULL +// node yaml_node_t +// +// assert(document) // Non-NULL document object is expected. +// assert(value) // Non-NULL value is expected. +// +// if (!tag) { +// tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG +// } +// +// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error +// tag_copy = yaml_strdup(tag) +// if (!tag_copy) goto error +// +// if (length < 0) { +// length = strlen((char *)value) +// } +// +// if (!yaml_check_utf8(value, length)) goto error +// value_copy = yaml_malloc(length+1) +// if (!value_copy) goto error +// memcpy(value_copy, value, length) +// value_copy[length] = '\0' +// +// SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark) +// if (!PUSH(&context, document.nodes, node)) goto error +// +// return document.nodes.top - document.nodes.start +// +//error: +// yaml_free(tag_copy) +// yaml_free(value_copy) +// +// return 0 +//} +// +///* +// * Add a sequence node to a document. +// */ +// +//YAML_DECLARE(int) +//yaml_document_add_sequence(document *yaml_document_t, +// tag *yaml_char_t, style yaml_sequence_style_t) +//{ +// struct { +// error yaml_error_type_t +// } context +// mark yaml_mark_t = { 0, 0, 0 } +// tag_copy *yaml_char_t = NULL +// struct { +// start *yaml_node_item_t +// end *yaml_node_item_t +// top *yaml_node_item_t +// } items = { NULL, NULL, NULL } +// node yaml_node_t +// +// assert(document) // Non-NULL document object is expected. +// +// if (!tag) { +// tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG +// } +// +// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error +// tag_copy = yaml_strdup(tag) +// if (!tag_copy) goto error +// +// if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error +// +// SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end, +// style, mark, mark) +// if (!PUSH(&context, document.nodes, node)) goto error +// +// return document.nodes.top - document.nodes.start +// +//error: +// STACK_DEL(&context, items) +// yaml_free(tag_copy) +// +// return 0 +//} +// +///* +// * Add a mapping node to a document. +// */ +// +//YAML_DECLARE(int) +//yaml_document_add_mapping(document *yaml_document_t, +// tag *yaml_char_t, style yaml_mapping_style_t) +//{ +// struct { +// error yaml_error_type_t +// } context +// mark yaml_mark_t = { 0, 0, 0 } +// tag_copy *yaml_char_t = NULL +// struct { +// start *yaml_node_pair_t +// end *yaml_node_pair_t +// top *yaml_node_pair_t +// } pairs = { NULL, NULL, NULL } +// node yaml_node_t +// +// assert(document) // Non-NULL document object is expected. +// +// if (!tag) { +// tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG +// } +// +// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error +// tag_copy = yaml_strdup(tag) +// if (!tag_copy) goto error +// +// if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error +// +// MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, +// style, mark, mark) +// if (!PUSH(&context, document.nodes, node)) goto error +// +// return document.nodes.top - document.nodes.start +// +//error: +// STACK_DEL(&context, pairs) +// yaml_free(tag_copy) +// +// return 0 +//} +// +///* +// * Append an item to a sequence node. +// */ +// +//YAML_DECLARE(int) +//yaml_document_append_sequence_item(document *yaml_document_t, +// sequence int, item int) +//{ +// struct { +// error yaml_error_type_t +// } context +// +// assert(document) // Non-NULL document is required. +// assert(sequence > 0 +// && document.nodes.start + sequence <= document.nodes.top) +// // Valid sequence id is required. +// assert(document.nodes.start[sequence-1].type == YAML_SEQUENCE_NODE) +// // A sequence node is required. +// assert(item > 0 && document.nodes.start + item <= document.nodes.top) +// // Valid item id is required. +// +// if (!PUSH(&context, +// document.nodes.start[sequence-1].data.sequence.items, item)) +// return 0 +// +// return 1 +//} +// +///* +// * Append a pair of a key and a value to a mapping node. +// */ +// +//YAML_DECLARE(int) +//yaml_document_append_mapping_pair(document *yaml_document_t, +// mapping int, key int, value int) +//{ +// struct { +// error yaml_error_type_t +// } context +// +// pair yaml_node_pair_t +// +// assert(document) // Non-NULL document is required. +// assert(mapping > 0 +// && document.nodes.start + mapping <= document.nodes.top) +// // Valid mapping id is required. +// assert(document.nodes.start[mapping-1].type == YAML_MAPPING_NODE) +// // A mapping node is required. +// assert(key > 0 && document.nodes.start + key <= document.nodes.top) +// // Valid key id is required. +// assert(value > 0 && document.nodes.start + value <= document.nodes.top) +// // Valid value id is required. +// +// pair.key = key +// pair.value = value +// +// if (!PUSH(&context, +// document.nodes.start[mapping-1].data.mapping.pairs, pair)) +// return 0 +// +// return 1 +//} +// +// diff --git a/vendor/gopkg.in/yaml.v3/decode.go b/vendor/gopkg.in/yaml.v3/decode.go new file mode 100644 index 0000000000..0173b6982e --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/decode.go @@ -0,0 +1,1000 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// +// 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 yaml + +import ( + "encoding" + "encoding/base64" + "fmt" + "io" + "math" + "reflect" + "strconv" + "time" +) + +// ---------------------------------------------------------------------------- +// Parser, produces a node tree out of a libyaml event stream. + +type parser struct { + parser yaml_parser_t + event yaml_event_t + doc *Node + anchors map[string]*Node + doneInit bool + textless bool +} + +func newParser(b []byte) *parser { + p := parser{} + if !yaml_parser_initialize(&p.parser) { + panic("failed to initialize YAML emitter") + } + if len(b) == 0 { + b = []byte{'\n'} + } + yaml_parser_set_input_string(&p.parser, b) + return &p +} + +func newParserFromReader(r io.Reader) *parser { + p := parser{} + if !yaml_parser_initialize(&p.parser) { + panic("failed to initialize YAML emitter") + } + yaml_parser_set_input_reader(&p.parser, r) + return &p +} + +func (p *parser) init() { + if p.doneInit { + return + } + p.anchors = make(map[string]*Node) + p.expect(yaml_STREAM_START_EVENT) + p.doneInit = true +} + +func (p *parser) destroy() { + if p.event.typ != yaml_NO_EVENT { + yaml_event_delete(&p.event) + } + yaml_parser_delete(&p.parser) +} + +// expect consumes an event from the event stream and +// checks that it's of the expected type. +func (p *parser) expect(e yaml_event_type_t) { + if p.event.typ == yaml_NO_EVENT { + if !yaml_parser_parse(&p.parser, &p.event) { + p.fail() + } + } + if p.event.typ == yaml_STREAM_END_EVENT { + failf("attempted to go past the end of stream; corrupted value?") + } + if p.event.typ != e { + p.parser.problem = fmt.Sprintf("expected %s event but got %s", e, p.event.typ) + p.fail() + } + yaml_event_delete(&p.event) + p.event.typ = yaml_NO_EVENT +} + +// peek peeks at the next event in the event stream, +// puts the results into p.event and returns the event type. +func (p *parser) peek() yaml_event_type_t { + if p.event.typ != yaml_NO_EVENT { + return p.event.typ + } + // It's curious choice from the underlying API to generally return a + // positive result on success, but on this case return true in an error + // scenario. This was the source of bugs in the past (issue #666). + if !yaml_parser_parse(&p.parser, &p.event) || p.parser.error != yaml_NO_ERROR { + p.fail() + } + return p.event.typ +} + +func (p *parser) fail() { + var where string + var line int + if p.parser.context_mark.line != 0 { + line = p.parser.context_mark.line + // Scanner errors don't iterate line before returning error + if p.parser.error == yaml_SCANNER_ERROR { + line++ + } + } else if p.parser.problem_mark.line != 0 { + line = p.parser.problem_mark.line + // Scanner errors don't iterate line before returning error + if p.parser.error == yaml_SCANNER_ERROR { + line++ + } + } + if line != 0 { + where = "line " + strconv.Itoa(line) + ": " + } + var msg string + if len(p.parser.problem) > 0 { + msg = p.parser.problem + } else { + msg = "unknown problem parsing YAML content" + } + failf("%s%s", where, msg) +} + +func (p *parser) anchor(n *Node, anchor []byte) { + if anchor != nil { + n.Anchor = string(anchor) + p.anchors[n.Anchor] = n + } +} + +func (p *parser) parse() *Node { + p.init() + switch p.peek() { + case yaml_SCALAR_EVENT: + return p.scalar() + case yaml_ALIAS_EVENT: + return p.alias() + case yaml_MAPPING_START_EVENT: + return p.mapping() + case yaml_SEQUENCE_START_EVENT: + return p.sequence() + case yaml_DOCUMENT_START_EVENT: + return p.document() + case yaml_STREAM_END_EVENT: + // Happens when attempting to decode an empty buffer. + return nil + case yaml_TAIL_COMMENT_EVENT: + panic("internal error: unexpected tail comment event (please report)") + default: + panic("internal error: attempted to parse unknown event (please report): " + p.event.typ.String()) + } +} + +func (p *parser) node(kind Kind, defaultTag, tag, value string) *Node { + var style Style + if tag != "" && tag != "!" { + tag = shortTag(tag) + style = TaggedStyle + } else if defaultTag != "" { + tag = defaultTag + } else if kind == ScalarNode { + tag, _ = resolve("", value) + } + n := &Node{ + Kind: kind, + Tag: tag, + Value: value, + Style: style, + } + if !p.textless { + n.Line = p.event.start_mark.line + 1 + n.Column = p.event.start_mark.column + 1 + n.HeadComment = string(p.event.head_comment) + n.LineComment = string(p.event.line_comment) + n.FootComment = string(p.event.foot_comment) + } + return n +} + +func (p *parser) parseChild(parent *Node) *Node { + child := p.parse() + parent.Content = append(parent.Content, child) + return child +} + +func (p *parser) document() *Node { + n := p.node(DocumentNode, "", "", "") + p.doc = n + p.expect(yaml_DOCUMENT_START_EVENT) + p.parseChild(n) + if p.peek() == yaml_DOCUMENT_END_EVENT { + n.FootComment = string(p.event.foot_comment) + } + p.expect(yaml_DOCUMENT_END_EVENT) + return n +} + +func (p *parser) alias() *Node { + n := p.node(AliasNode, "", "", string(p.event.anchor)) + n.Alias = p.anchors[n.Value] + if n.Alias == nil { + failf("unknown anchor '%s' referenced", n.Value) + } + p.expect(yaml_ALIAS_EVENT) + return n +} + +func (p *parser) scalar() *Node { + var parsedStyle = p.event.scalar_style() + var nodeStyle Style + switch { + case parsedStyle&yaml_DOUBLE_QUOTED_SCALAR_STYLE != 0: + nodeStyle = DoubleQuotedStyle + case parsedStyle&yaml_SINGLE_QUOTED_SCALAR_STYLE != 0: + nodeStyle = SingleQuotedStyle + case parsedStyle&yaml_LITERAL_SCALAR_STYLE != 0: + nodeStyle = LiteralStyle + case parsedStyle&yaml_FOLDED_SCALAR_STYLE != 0: + nodeStyle = FoldedStyle + } + var nodeValue = string(p.event.value) + var nodeTag = string(p.event.tag) + var defaultTag string + if nodeStyle == 0 { + if nodeValue == "<<" { + defaultTag = mergeTag + } + } else { + defaultTag = strTag + } + n := p.node(ScalarNode, defaultTag, nodeTag, nodeValue) + n.Style |= nodeStyle + p.anchor(n, p.event.anchor) + p.expect(yaml_SCALAR_EVENT) + return n +} + +func (p *parser) sequence() *Node { + n := p.node(SequenceNode, seqTag, string(p.event.tag), "") + if p.event.sequence_style()&yaml_FLOW_SEQUENCE_STYLE != 0 { + n.Style |= FlowStyle + } + p.anchor(n, p.event.anchor) + p.expect(yaml_SEQUENCE_START_EVENT) + for p.peek() != yaml_SEQUENCE_END_EVENT { + p.parseChild(n) + } + n.LineComment = string(p.event.line_comment) + n.FootComment = string(p.event.foot_comment) + p.expect(yaml_SEQUENCE_END_EVENT) + return n +} + +func (p *parser) mapping() *Node { + n := p.node(MappingNode, mapTag, string(p.event.tag), "") + block := true + if p.event.mapping_style()&yaml_FLOW_MAPPING_STYLE != 0 { + block = false + n.Style |= FlowStyle + } + p.anchor(n, p.event.anchor) + p.expect(yaml_MAPPING_START_EVENT) + for p.peek() != yaml_MAPPING_END_EVENT { + k := p.parseChild(n) + if block && k.FootComment != "" { + // Must be a foot comment for the prior value when being dedented. + if len(n.Content) > 2 { + n.Content[len(n.Content)-3].FootComment = k.FootComment + k.FootComment = "" + } + } + v := p.parseChild(n) + if k.FootComment == "" && v.FootComment != "" { + k.FootComment = v.FootComment + v.FootComment = "" + } + if p.peek() == yaml_TAIL_COMMENT_EVENT { + if k.FootComment == "" { + k.FootComment = string(p.event.foot_comment) + } + p.expect(yaml_TAIL_COMMENT_EVENT) + } + } + n.LineComment = string(p.event.line_comment) + n.FootComment = string(p.event.foot_comment) + if n.Style&FlowStyle == 0 && n.FootComment != "" && len(n.Content) > 1 { + n.Content[len(n.Content)-2].FootComment = n.FootComment + n.FootComment = "" + } + p.expect(yaml_MAPPING_END_EVENT) + return n +} + +// ---------------------------------------------------------------------------- +// Decoder, unmarshals a node into a provided value. + +type decoder struct { + doc *Node + aliases map[*Node]bool + terrors []string + + stringMapType reflect.Type + generalMapType reflect.Type + + knownFields bool + uniqueKeys bool + decodeCount int + aliasCount int + aliasDepth int + + mergedFields map[interface{}]bool +} + +var ( + nodeType = reflect.TypeOf(Node{}) + durationType = reflect.TypeOf(time.Duration(0)) + stringMapType = reflect.TypeOf(map[string]interface{}{}) + generalMapType = reflect.TypeOf(map[interface{}]interface{}{}) + ifaceType = generalMapType.Elem() + timeType = reflect.TypeOf(time.Time{}) + ptrTimeType = reflect.TypeOf(&time.Time{}) +) + +func newDecoder() *decoder { + d := &decoder{ + stringMapType: stringMapType, + generalMapType: generalMapType, + uniqueKeys: true, + } + d.aliases = make(map[*Node]bool) + return d +} + +func (d *decoder) terror(n *Node, tag string, out reflect.Value) { + if n.Tag != "" { + tag = n.Tag + } + value := n.Value + if tag != seqTag && tag != mapTag { + if len(value) > 10 { + value = " `" + value[:7] + "...`" + } else { + value = " `" + value + "`" + } + } + d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.Line, shortTag(tag), value, out.Type())) +} + +func (d *decoder) callUnmarshaler(n *Node, u Unmarshaler) (good bool) { + err := u.UnmarshalYAML(n) + if e, ok := err.(*TypeError); ok { + d.terrors = append(d.terrors, e.Errors...) + return false + } + if err != nil { + fail(err) + } + return true +} + +func (d *decoder) callObsoleteUnmarshaler(n *Node, u obsoleteUnmarshaler) (good bool) { + terrlen := len(d.terrors) + err := u.UnmarshalYAML(func(v interface{}) (err error) { + defer handleErr(&err) + d.unmarshal(n, reflect.ValueOf(v)) + if len(d.terrors) > terrlen { + issues := d.terrors[terrlen:] + d.terrors = d.terrors[:terrlen] + return &TypeError{issues} + } + return nil + }) + if e, ok := err.(*TypeError); ok { + d.terrors = append(d.terrors, e.Errors...) + return false + } + if err != nil { + fail(err) + } + return true +} + +// d.prepare initializes and dereferences pointers and calls UnmarshalYAML +// if a value is found to implement it. +// It returns the initialized and dereferenced out value, whether +// unmarshalling was already done by UnmarshalYAML, and if so whether +// its types unmarshalled appropriately. +// +// If n holds a null value, prepare returns before doing anything. +func (d *decoder) prepare(n *Node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) { + if n.ShortTag() == nullTag { + return out, false, false + } + again := true + for again { + again = false + if out.Kind() == reflect.Ptr { + if out.IsNil() { + out.Set(reflect.New(out.Type().Elem())) + } + out = out.Elem() + again = true + } + if out.CanAddr() { + outi := out.Addr().Interface() + if u, ok := outi.(Unmarshaler); ok { + good = d.callUnmarshaler(n, u) + return out, true, good + } + if u, ok := outi.(obsoleteUnmarshaler); ok { + good = d.callObsoleteUnmarshaler(n, u) + return out, true, good + } + } + } + return out, false, false +} + +func (d *decoder) fieldByIndex(n *Node, v reflect.Value, index []int) (field reflect.Value) { + if n.ShortTag() == nullTag { + return reflect.Value{} + } + for _, num := range index { + for { + if v.Kind() == reflect.Ptr { + if v.IsNil() { + v.Set(reflect.New(v.Type().Elem())) + } + v = v.Elem() + continue + } + break + } + v = v.Field(num) + } + return v +} + +const ( + // 400,000 decode operations is ~500kb of dense object declarations, or + // ~5kb of dense object declarations with 10000% alias expansion + alias_ratio_range_low = 400000 + + // 4,000,000 decode operations is ~5MB of dense object declarations, or + // ~4.5MB of dense object declarations with 10% alias expansion + alias_ratio_range_high = 4000000 + + // alias_ratio_range is the range over which we scale allowed alias ratios + alias_ratio_range = float64(alias_ratio_range_high - alias_ratio_range_low) +) + +func allowedAliasRatio(decodeCount int) float64 { + switch { + case decodeCount <= alias_ratio_range_low: + // allow 99% to come from alias expansion for small-to-medium documents + return 0.99 + case decodeCount >= alias_ratio_range_high: + // allow 10% to come from alias expansion for very large documents + return 0.10 + default: + // scale smoothly from 99% down to 10% over the range. + // this maps to 396,000 - 400,000 allowed alias-driven decodes over the range. + // 400,000 decode operations is ~100MB of allocations in worst-case scenarios (single-item maps). + return 0.99 - 0.89*(float64(decodeCount-alias_ratio_range_low)/alias_ratio_range) + } +} + +func (d *decoder) unmarshal(n *Node, out reflect.Value) (good bool) { + d.decodeCount++ + if d.aliasDepth > 0 { + d.aliasCount++ + } + if d.aliasCount > 100 && d.decodeCount > 1000 && float64(d.aliasCount)/float64(d.decodeCount) > allowedAliasRatio(d.decodeCount) { + failf("document contains excessive aliasing") + } + if out.Type() == nodeType { + out.Set(reflect.ValueOf(n).Elem()) + return true + } + switch n.Kind { + case DocumentNode: + return d.document(n, out) + case AliasNode: + return d.alias(n, out) + } + out, unmarshaled, good := d.prepare(n, out) + if unmarshaled { + return good + } + switch n.Kind { + case ScalarNode: + good = d.scalar(n, out) + case MappingNode: + good = d.mapping(n, out) + case SequenceNode: + good = d.sequence(n, out) + case 0: + if n.IsZero() { + return d.null(out) + } + fallthrough + default: + failf("cannot decode node with unknown kind %d", n.Kind) + } + return good +} + +func (d *decoder) document(n *Node, out reflect.Value) (good bool) { + if len(n.Content) == 1 { + d.doc = n + d.unmarshal(n.Content[0], out) + return true + } + return false +} + +func (d *decoder) alias(n *Node, out reflect.Value) (good bool) { + if d.aliases[n] { + // TODO this could actually be allowed in some circumstances. + failf("anchor '%s' value contains itself", n.Value) + } + d.aliases[n] = true + d.aliasDepth++ + good = d.unmarshal(n.Alias, out) + d.aliasDepth-- + delete(d.aliases, n) + return good +} + +var zeroValue reflect.Value + +func resetMap(out reflect.Value) { + for _, k := range out.MapKeys() { + out.SetMapIndex(k, zeroValue) + } +} + +func (d *decoder) null(out reflect.Value) bool { + if out.CanAddr() { + switch out.Kind() { + case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice: + out.Set(reflect.Zero(out.Type())) + return true + } + } + return false +} + +func (d *decoder) scalar(n *Node, out reflect.Value) bool { + var tag string + var resolved interface{} + if n.indicatedString() { + tag = strTag + resolved = n.Value + } else { + tag, resolved = resolve(n.Tag, n.Value) + if tag == binaryTag { + data, err := base64.StdEncoding.DecodeString(resolved.(string)) + if err != nil { + failf("!!binary value contains invalid base64 data") + } + resolved = string(data) + } + } + if resolved == nil { + return d.null(out) + } + if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() { + // We've resolved to exactly the type we want, so use that. + out.Set(resolvedv) + return true + } + // Perhaps we can use the value as a TextUnmarshaler to + // set its value. + if out.CanAddr() { + u, ok := out.Addr().Interface().(encoding.TextUnmarshaler) + if ok { + var text []byte + if tag == binaryTag { + text = []byte(resolved.(string)) + } else { + // We let any value be unmarshaled into TextUnmarshaler. + // That might be more lax than we'd like, but the + // TextUnmarshaler itself should bowl out any dubious values. + text = []byte(n.Value) + } + err := u.UnmarshalText(text) + if err != nil { + fail(err) + } + return true + } + } + switch out.Kind() { + case reflect.String: + if tag == binaryTag { + out.SetString(resolved.(string)) + return true + } + out.SetString(n.Value) + return true + case reflect.Interface: + out.Set(reflect.ValueOf(resolved)) + return true + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + // This used to work in v2, but it's very unfriendly. + isDuration := out.Type() == durationType + + switch resolved := resolved.(type) { + case int: + if !isDuration && !out.OverflowInt(int64(resolved)) { + out.SetInt(int64(resolved)) + return true + } + case int64: + if !isDuration && !out.OverflowInt(resolved) { + out.SetInt(resolved) + return true + } + case uint64: + if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) { + out.SetInt(int64(resolved)) + return true + } + case float64: + if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) { + out.SetInt(int64(resolved)) + return true + } + case string: + if out.Type() == durationType { + d, err := time.ParseDuration(resolved) + if err == nil { + out.SetInt(int64(d)) + return true + } + } + } + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + switch resolved := resolved.(type) { + case int: + if resolved >= 0 && !out.OverflowUint(uint64(resolved)) { + out.SetUint(uint64(resolved)) + return true + } + case int64: + if resolved >= 0 && !out.OverflowUint(uint64(resolved)) { + out.SetUint(uint64(resolved)) + return true + } + case uint64: + if !out.OverflowUint(uint64(resolved)) { + out.SetUint(uint64(resolved)) + return true + } + case float64: + if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) { + out.SetUint(uint64(resolved)) + return true + } + } + case reflect.Bool: + switch resolved := resolved.(type) { + case bool: + out.SetBool(resolved) + return true + case string: + // This offers some compatibility with the 1.1 spec (https://yaml.org/type/bool.html). + // It only works if explicitly attempting to unmarshal into a typed bool value. + switch resolved { + case "y", "Y", "yes", "Yes", "YES", "on", "On", "ON": + out.SetBool(true) + return true + case "n", "N", "no", "No", "NO", "off", "Off", "OFF": + out.SetBool(false) + return true + } + } + case reflect.Float32, reflect.Float64: + switch resolved := resolved.(type) { + case int: + out.SetFloat(float64(resolved)) + return true + case int64: + out.SetFloat(float64(resolved)) + return true + case uint64: + out.SetFloat(float64(resolved)) + return true + case float64: + out.SetFloat(resolved) + return true + } + case reflect.Struct: + if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() { + out.Set(resolvedv) + return true + } + case reflect.Ptr: + panic("yaml internal error: please report the issue") + } + d.terror(n, tag, out) + return false +} + +func settableValueOf(i interface{}) reflect.Value { + v := reflect.ValueOf(i) + sv := reflect.New(v.Type()).Elem() + sv.Set(v) + return sv +} + +func (d *decoder) sequence(n *Node, out reflect.Value) (good bool) { + l := len(n.Content) + + var iface reflect.Value + switch out.Kind() { + case reflect.Slice: + out.Set(reflect.MakeSlice(out.Type(), l, l)) + case reflect.Array: + if l != out.Len() { + failf("invalid array: want %d elements but got %d", out.Len(), l) + } + case reflect.Interface: + // No type hints. Will have to use a generic sequence. + iface = out + out = settableValueOf(make([]interface{}, l)) + default: + d.terror(n, seqTag, out) + return false + } + et := out.Type().Elem() + + j := 0 + for i := 0; i < l; i++ { + e := reflect.New(et).Elem() + if ok := d.unmarshal(n.Content[i], e); ok { + out.Index(j).Set(e) + j++ + } + } + if out.Kind() != reflect.Array { + out.Set(out.Slice(0, j)) + } + if iface.IsValid() { + iface.Set(out) + } + return true +} + +func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) { + l := len(n.Content) + if d.uniqueKeys { + nerrs := len(d.terrors) + for i := 0; i < l; i += 2 { + ni := n.Content[i] + for j := i + 2; j < l; j += 2 { + nj := n.Content[j] + if ni.Kind == nj.Kind && ni.Value == nj.Value { + d.terrors = append(d.terrors, fmt.Sprintf("line %d: mapping key %#v already defined at line %d", nj.Line, nj.Value, ni.Line)) + } + } + } + if len(d.terrors) > nerrs { + return false + } + } + switch out.Kind() { + case reflect.Struct: + return d.mappingStruct(n, out) + case reflect.Map: + // okay + case reflect.Interface: + iface := out + if isStringMap(n) { + out = reflect.MakeMap(d.stringMapType) + } else { + out = reflect.MakeMap(d.generalMapType) + } + iface.Set(out) + default: + d.terror(n, mapTag, out) + return false + } + + outt := out.Type() + kt := outt.Key() + et := outt.Elem() + + stringMapType := d.stringMapType + generalMapType := d.generalMapType + if outt.Elem() == ifaceType { + if outt.Key().Kind() == reflect.String { + d.stringMapType = outt + } else if outt.Key() == ifaceType { + d.generalMapType = outt + } + } + + mergedFields := d.mergedFields + d.mergedFields = nil + + var mergeNode *Node + + mapIsNew := false + if out.IsNil() { + out.Set(reflect.MakeMap(outt)) + mapIsNew = true + } + for i := 0; i < l; i += 2 { + if isMerge(n.Content[i]) { + mergeNode = n.Content[i+1] + continue + } + k := reflect.New(kt).Elem() + if d.unmarshal(n.Content[i], k) { + if mergedFields != nil { + ki := k.Interface() + if mergedFields[ki] { + continue + } + mergedFields[ki] = true + } + kkind := k.Kind() + if kkind == reflect.Interface { + kkind = k.Elem().Kind() + } + if kkind == reflect.Map || kkind == reflect.Slice { + failf("invalid map key: %#v", k.Interface()) + } + e := reflect.New(et).Elem() + if d.unmarshal(n.Content[i+1], e) || n.Content[i+1].ShortTag() == nullTag && (mapIsNew || !out.MapIndex(k).IsValid()) { + out.SetMapIndex(k, e) + } + } + } + + d.mergedFields = mergedFields + if mergeNode != nil { + d.merge(n, mergeNode, out) + } + + d.stringMapType = stringMapType + d.generalMapType = generalMapType + return true +} + +func isStringMap(n *Node) bool { + if n.Kind != MappingNode { + return false + } + l := len(n.Content) + for i := 0; i < l; i += 2 { + shortTag := n.Content[i].ShortTag() + if shortTag != strTag && shortTag != mergeTag { + return false + } + } + return true +} + +func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) { + sinfo, err := getStructInfo(out.Type()) + if err != nil { + panic(err) + } + + var inlineMap reflect.Value + var elemType reflect.Type + if sinfo.InlineMap != -1 { + inlineMap = out.Field(sinfo.InlineMap) + elemType = inlineMap.Type().Elem() + } + + for _, index := range sinfo.InlineUnmarshalers { + field := d.fieldByIndex(n, out, index) + d.prepare(n, field) + } + + mergedFields := d.mergedFields + d.mergedFields = nil + var mergeNode *Node + var doneFields []bool + if d.uniqueKeys { + doneFields = make([]bool, len(sinfo.FieldsList)) + } + name := settableValueOf("") + l := len(n.Content) + for i := 0; i < l; i += 2 { + ni := n.Content[i] + if isMerge(ni) { + mergeNode = n.Content[i+1] + continue + } + if !d.unmarshal(ni, name) { + continue + } + sname := name.String() + if mergedFields != nil { + if mergedFields[sname] { + continue + } + mergedFields[sname] = true + } + if info, ok := sinfo.FieldsMap[sname]; ok { + if d.uniqueKeys { + if doneFields[info.Id] { + d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.Line, name.String(), out.Type())) + continue + } + doneFields[info.Id] = true + } + var field reflect.Value + if info.Inline == nil { + field = out.Field(info.Num) + } else { + field = d.fieldByIndex(n, out, info.Inline) + } + d.unmarshal(n.Content[i+1], field) + } else if sinfo.InlineMap != -1 { + if inlineMap.IsNil() { + inlineMap.Set(reflect.MakeMap(inlineMap.Type())) + } + value := reflect.New(elemType).Elem() + d.unmarshal(n.Content[i+1], value) + inlineMap.SetMapIndex(name, value) + } else if d.knownFields { + d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.Line, name.String(), out.Type())) + } + } + + d.mergedFields = mergedFields + if mergeNode != nil { + d.merge(n, mergeNode, out) + } + return true +} + +func failWantMap() { + failf("map merge requires map or sequence of maps as the value") +} + +func (d *decoder) merge(parent *Node, merge *Node, out reflect.Value) { + mergedFields := d.mergedFields + if mergedFields == nil { + d.mergedFields = make(map[interface{}]bool) + for i := 0; i < len(parent.Content); i += 2 { + k := reflect.New(ifaceType).Elem() + if d.unmarshal(parent.Content[i], k) { + d.mergedFields[k.Interface()] = true + } + } + } + + switch merge.Kind { + case MappingNode: + d.unmarshal(merge, out) + case AliasNode: + if merge.Alias != nil && merge.Alias.Kind != MappingNode { + failWantMap() + } + d.unmarshal(merge, out) + case SequenceNode: + for i := 0; i < len(merge.Content); i++ { + ni := merge.Content[i] + if ni.Kind == AliasNode { + if ni.Alias != nil && ni.Alias.Kind != MappingNode { + failWantMap() + } + } else if ni.Kind != MappingNode { + failWantMap() + } + d.unmarshal(ni, out) + } + default: + failWantMap() + } + + d.mergedFields = mergedFields +} + +func isMerge(n *Node) bool { + return n.Kind == ScalarNode && n.Value == "<<" && (n.Tag == "" || n.Tag == "!" || shortTag(n.Tag) == mergeTag) +} diff --git a/vendor/gopkg.in/yaml.v3/emitterc.go b/vendor/gopkg.in/yaml.v3/emitterc.go new file mode 100644 index 0000000000..0f47c9ca8a --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/emitterc.go @@ -0,0 +1,2020 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +import ( + "bytes" + "fmt" +) + +// Flush the buffer if needed. +func flush(emitter *yaml_emitter_t) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) { + return yaml_emitter_flush(emitter) + } + return true +} + +// Put a character to the output buffer. +func put(emitter *yaml_emitter_t, value byte) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) { + return false + } + emitter.buffer[emitter.buffer_pos] = value + emitter.buffer_pos++ + emitter.column++ + return true +} + +// Put a line break to the output buffer. +func put_break(emitter *yaml_emitter_t) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) { + return false + } + switch emitter.line_break { + case yaml_CR_BREAK: + emitter.buffer[emitter.buffer_pos] = '\r' + emitter.buffer_pos += 1 + case yaml_LN_BREAK: + emitter.buffer[emitter.buffer_pos] = '\n' + emitter.buffer_pos += 1 + case yaml_CRLN_BREAK: + emitter.buffer[emitter.buffer_pos+0] = '\r' + emitter.buffer[emitter.buffer_pos+1] = '\n' + emitter.buffer_pos += 2 + default: + panic("unknown line break setting") + } + if emitter.column == 0 { + emitter.space_above = true + } + emitter.column = 0 + emitter.line++ + // [Go] Do this here and below and drop from everywhere else (see commented lines). + emitter.indention = true + return true +} + +// Copy a character from a string into buffer. +func write(emitter *yaml_emitter_t, s []byte, i *int) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) { + return false + } + p := emitter.buffer_pos + w := width(s[*i]) + switch w { + case 4: + emitter.buffer[p+3] = s[*i+3] + fallthrough + case 3: + emitter.buffer[p+2] = s[*i+2] + fallthrough + case 2: + emitter.buffer[p+1] = s[*i+1] + fallthrough + case 1: + emitter.buffer[p+0] = s[*i+0] + default: + panic("unknown character width") + } + emitter.column++ + emitter.buffer_pos += w + *i += w + return true +} + +// Write a whole string into buffer. +func write_all(emitter *yaml_emitter_t, s []byte) bool { + for i := 0; i < len(s); { + if !write(emitter, s, &i) { + return false + } + } + return true +} + +// Copy a line break character from a string into buffer. +func write_break(emitter *yaml_emitter_t, s []byte, i *int) bool { + if s[*i] == '\n' { + if !put_break(emitter) { + return false + } + *i++ + } else { + if !write(emitter, s, i) { + return false + } + if emitter.column == 0 { + emitter.space_above = true + } + emitter.column = 0 + emitter.line++ + // [Go] Do this here and above and drop from everywhere else (see commented lines). + emitter.indention = true + } + return true +} + +// Set an emitter error and return false. +func yaml_emitter_set_emitter_error(emitter *yaml_emitter_t, problem string) bool { + emitter.error = yaml_EMITTER_ERROR + emitter.problem = problem + return false +} + +// Emit an event. +func yaml_emitter_emit(emitter *yaml_emitter_t, event *yaml_event_t) bool { + emitter.events = append(emitter.events, *event) + for !yaml_emitter_need_more_events(emitter) { + event := &emitter.events[emitter.events_head] + if !yaml_emitter_analyze_event(emitter, event) { + return false + } + if !yaml_emitter_state_machine(emitter, event) { + return false + } + yaml_event_delete(event) + emitter.events_head++ + } + return true +} + +// Check if we need to accumulate more events before emitting. +// +// We accumulate extra +// - 1 event for DOCUMENT-START +// - 2 events for SEQUENCE-START +// - 3 events for MAPPING-START +// +func yaml_emitter_need_more_events(emitter *yaml_emitter_t) bool { + if emitter.events_head == len(emitter.events) { + return true + } + var accumulate int + switch emitter.events[emitter.events_head].typ { + case yaml_DOCUMENT_START_EVENT: + accumulate = 1 + break + case yaml_SEQUENCE_START_EVENT: + accumulate = 2 + break + case yaml_MAPPING_START_EVENT: + accumulate = 3 + break + default: + return false + } + if len(emitter.events)-emitter.events_head > accumulate { + return false + } + var level int + for i := emitter.events_head; i < len(emitter.events); i++ { + switch emitter.events[i].typ { + case yaml_STREAM_START_EVENT, yaml_DOCUMENT_START_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT: + level++ + case yaml_STREAM_END_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_END_EVENT, yaml_MAPPING_END_EVENT: + level-- + } + if level == 0 { + return false + } + } + return true +} + +// Append a directive to the directives stack. +func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_directive_t, allow_duplicates bool) bool { + for i := 0; i < len(emitter.tag_directives); i++ { + if bytes.Equal(value.handle, emitter.tag_directives[i].handle) { + if allow_duplicates { + return true + } + return yaml_emitter_set_emitter_error(emitter, "duplicate %TAG directive") + } + } + + // [Go] Do we actually need to copy this given garbage collection + // and the lack of deallocating destructors? + tag_copy := yaml_tag_directive_t{ + handle: make([]byte, len(value.handle)), + prefix: make([]byte, len(value.prefix)), + } + copy(tag_copy.handle, value.handle) + copy(tag_copy.prefix, value.prefix) + emitter.tag_directives = append(emitter.tag_directives, tag_copy) + return true +} + +// Increase the indentation level. +func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool { + emitter.indents = append(emitter.indents, emitter.indent) + if emitter.indent < 0 { + if flow { + emitter.indent = emitter.best_indent + } else { + emitter.indent = 0 + } + } else if !indentless { + // [Go] This was changed so that indentations are more regular. + if emitter.states[len(emitter.states)-1] == yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE { + // The first indent inside a sequence will just skip the "- " indicator. + emitter.indent += 2 + } else { + // Everything else aligns to the chosen indentation. + emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent) + } + } + return true +} + +// State dispatcher. +func yaml_emitter_state_machine(emitter *yaml_emitter_t, event *yaml_event_t) bool { + switch emitter.state { + default: + case yaml_EMIT_STREAM_START_STATE: + return yaml_emitter_emit_stream_start(emitter, event) + + case yaml_EMIT_FIRST_DOCUMENT_START_STATE: + return yaml_emitter_emit_document_start(emitter, event, true) + + case yaml_EMIT_DOCUMENT_START_STATE: + return yaml_emitter_emit_document_start(emitter, event, false) + + case yaml_EMIT_DOCUMENT_CONTENT_STATE: + return yaml_emitter_emit_document_content(emitter, event) + + case yaml_EMIT_DOCUMENT_END_STATE: + return yaml_emitter_emit_document_end(emitter, event) + + case yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE: + return yaml_emitter_emit_flow_sequence_item(emitter, event, true, false) + + case yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE: + return yaml_emitter_emit_flow_sequence_item(emitter, event, false, true) + + case yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE: + return yaml_emitter_emit_flow_sequence_item(emitter, event, false, false) + + case yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE: + return yaml_emitter_emit_flow_mapping_key(emitter, event, true, false) + + case yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE: + return yaml_emitter_emit_flow_mapping_key(emitter, event, false, true) + + case yaml_EMIT_FLOW_MAPPING_KEY_STATE: + return yaml_emitter_emit_flow_mapping_key(emitter, event, false, false) + + case yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE: + return yaml_emitter_emit_flow_mapping_value(emitter, event, true) + + case yaml_EMIT_FLOW_MAPPING_VALUE_STATE: + return yaml_emitter_emit_flow_mapping_value(emitter, event, false) + + case yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE: + return yaml_emitter_emit_block_sequence_item(emitter, event, true) + + case yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE: + return yaml_emitter_emit_block_sequence_item(emitter, event, false) + + case yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE: + return yaml_emitter_emit_block_mapping_key(emitter, event, true) + + case yaml_EMIT_BLOCK_MAPPING_KEY_STATE: + return yaml_emitter_emit_block_mapping_key(emitter, event, false) + + case yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE: + return yaml_emitter_emit_block_mapping_value(emitter, event, true) + + case yaml_EMIT_BLOCK_MAPPING_VALUE_STATE: + return yaml_emitter_emit_block_mapping_value(emitter, event, false) + + case yaml_EMIT_END_STATE: + return yaml_emitter_set_emitter_error(emitter, "expected nothing after STREAM-END") + } + panic("invalid emitter state") +} + +// Expect STREAM-START. +func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if event.typ != yaml_STREAM_START_EVENT { + return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START") + } + if emitter.encoding == yaml_ANY_ENCODING { + emitter.encoding = event.encoding + if emitter.encoding == yaml_ANY_ENCODING { + emitter.encoding = yaml_UTF8_ENCODING + } + } + if emitter.best_indent < 2 || emitter.best_indent > 9 { + emitter.best_indent = 2 + } + if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent*2 { + emitter.best_width = 80 + } + if emitter.best_width < 0 { + emitter.best_width = 1<<31 - 1 + } + if emitter.line_break == yaml_ANY_BREAK { + emitter.line_break = yaml_LN_BREAK + } + + emitter.indent = -1 + emitter.line = 0 + emitter.column = 0 + emitter.whitespace = true + emitter.indention = true + emitter.space_above = true + emitter.foot_indent = -1 + + if emitter.encoding != yaml_UTF8_ENCODING { + if !yaml_emitter_write_bom(emitter) { + return false + } + } + emitter.state = yaml_EMIT_FIRST_DOCUMENT_START_STATE + return true +} + +// Expect DOCUMENT-START or STREAM-END. +func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + + if event.typ == yaml_DOCUMENT_START_EVENT { + + if event.version_directive != nil { + if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) { + return false + } + } + + for i := 0; i < len(event.tag_directives); i++ { + tag_directive := &event.tag_directives[i] + if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) { + return false + } + if !yaml_emitter_append_tag_directive(emitter, tag_directive, false) { + return false + } + } + + for i := 0; i < len(default_tag_directives); i++ { + tag_directive := &default_tag_directives[i] + if !yaml_emitter_append_tag_directive(emitter, tag_directive, true) { + return false + } + } + + implicit := event.implicit + if !first || emitter.canonical { + implicit = false + } + + if emitter.open_ended && (event.version_directive != nil || len(event.tag_directives) > 0) { + if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if event.version_directive != nil { + implicit = false + if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte("1.1"), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if len(event.tag_directives) > 0 { + implicit = false + for i := 0; i < len(event.tag_directives); i++ { + tag_directive := &event.tag_directives[i] + if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) { + return false + } + if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) { + return false + } + if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + } + + if yaml_emitter_check_empty_document(emitter) { + implicit = false + } + if !implicit { + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte("---"), true, false, false) { + return false + } + if emitter.canonical || true { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + } + + if len(emitter.head_comment) > 0 { + if !yaml_emitter_process_head_comment(emitter) { + return false + } + if !put_break(emitter) { + return false + } + } + + emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE + return true + } + + if event.typ == yaml_STREAM_END_EVENT { + if emitter.open_ended { + if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_flush(emitter) { + return false + } + emitter.state = yaml_EMIT_END_STATE + return true + } + + return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-START or STREAM-END") +} + +// Expect the root node. +func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_event_t) bool { + emitter.states = append(emitter.states, yaml_EMIT_DOCUMENT_END_STATE) + + if !yaml_emitter_process_head_comment(emitter) { + return false + } + if !yaml_emitter_emit_node(emitter, event, true, false, false, false) { + return false + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + return true +} + +// Expect DOCUMENT-END. +func yaml_emitter_emit_document_end(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if event.typ != yaml_DOCUMENT_END_EVENT { + return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-END") + } + // [Go] Force document foot separation. + emitter.foot_indent = 0 + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + emitter.foot_indent = -1 + if !yaml_emitter_write_indent(emitter) { + return false + } + if !event.implicit { + // [Go] Allocate the slice elsewhere. + if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_flush(emitter) { + return false + } + emitter.state = yaml_EMIT_DOCUMENT_START_STATE + emitter.tag_directives = emitter.tag_directives[:0] + return true +} + +// Expect a flow item node. +func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool { + if first { + if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) { + return false + } + if !yaml_emitter_increase_indent(emitter, true, false) { + return false + } + emitter.flow_level++ + } + + if event.typ == yaml_SEQUENCE_END_EVENT { + if emitter.canonical && !first && !trail { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + emitter.flow_level-- + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + if emitter.column == 0 || emitter.canonical && !first { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) { + return false + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + + return true + } + + if !first && !trail { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + + if !yaml_emitter_process_head_comment(emitter) { + return false + } + if emitter.column == 0 { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if emitter.canonical || emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 { + emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE) + } else { + emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE) + } + if !yaml_emitter_emit_node(emitter, event, false, true, false, false) { + return false + } + if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + return true +} + +// Expect a flow key node. +func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool { + if first { + if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) { + return false + } + if !yaml_emitter_increase_indent(emitter, true, false) { + return false + } + emitter.flow_level++ + } + + if event.typ == yaml_MAPPING_END_EVENT { + if (emitter.canonical || len(emitter.head_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0) && !first && !trail { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + if !yaml_emitter_process_head_comment(emitter) { + return false + } + emitter.flow_level-- + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + if emitter.canonical && !first { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) { + return false + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true + } + + if !first && !trail { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + + if !yaml_emitter_process_head_comment(emitter) { + return false + } + + if emitter.column == 0 { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if emitter.canonical || emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if !emitter.canonical && yaml_emitter_check_simple_key(emitter) { + emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, true) + } + if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, false) { + return false + } + emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, false) +} + +// Expect a flow value node. +func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool { + if simple { + if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) { + return false + } + } else { + if emitter.canonical || emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, false) { + return false + } + } + if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 { + emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE) + } else { + emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_KEY_STATE) + } + if !yaml_emitter_emit_node(emitter, event, false, false, true, false) { + return false + } + if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + return true +} + +// Expect a block item node. +func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + if first { + if !yaml_emitter_increase_indent(emitter, false, false) { + return false + } + } + if event.typ == yaml_SEQUENCE_END_EVENT { + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true + } + if !yaml_emitter_process_head_comment(emitter) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte{'-'}, true, false, true) { + return false + } + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE) + if !yaml_emitter_emit_node(emitter, event, false, true, false, false) { + return false + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + return true +} + +// Expect a block key node. +func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + if first { + if !yaml_emitter_increase_indent(emitter, false, false) { + return false + } + } + if !yaml_emitter_process_head_comment(emitter) { + return false + } + if event.typ == yaml_MAPPING_END_EVENT { + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if len(emitter.line_comment) > 0 { + // [Go] A line comment was provided for the key. That's unusual as the + // scanner associates line comments with the value. Either way, + // save the line comment and render it appropriately later. + emitter.key_line_comment = emitter.line_comment + emitter.line_comment = nil + } + if yaml_emitter_check_simple_key(emitter) { + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, true) + } + if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, true) { + return false + } + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, false) +} + +// Expect a block value node. +func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool { + if simple { + if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) { + return false + } + } else { + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, true) { + return false + } + } + if len(emitter.key_line_comment) > 0 { + // [Go] Line comments are generally associated with the value, but when there's + // no value on the same line as a mapping key they end up attached to the + // key itself. + if event.typ == yaml_SCALAR_EVENT { + if len(emitter.line_comment) == 0 { + // A scalar is coming and it has no line comments by itself yet, + // so just let it handle the line comment as usual. If it has a + // line comment, we can't have both so the one from the key is lost. + emitter.line_comment = emitter.key_line_comment + emitter.key_line_comment = nil + } + } else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) { + // An indented block follows, so write the comment right now. + emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment + if !yaml_emitter_process_line_comment(emitter) { + return false + } + emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment + } + } + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE) + if !yaml_emitter_emit_node(emitter, event, false, false, true, false) { + return false + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + if !yaml_emitter_process_foot_comment(emitter) { + return false + } + return true +} + +func yaml_emitter_silent_nil_event(emitter *yaml_emitter_t, event *yaml_event_t) bool { + return event.typ == yaml_SCALAR_EVENT && event.implicit && !emitter.canonical && len(emitter.scalar_data.value) == 0 +} + +// Expect a node. +func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t, + root bool, sequence bool, mapping bool, simple_key bool) bool { + + emitter.root_context = root + emitter.sequence_context = sequence + emitter.mapping_context = mapping + emitter.simple_key_context = simple_key + + switch event.typ { + case yaml_ALIAS_EVENT: + return yaml_emitter_emit_alias(emitter, event) + case yaml_SCALAR_EVENT: + return yaml_emitter_emit_scalar(emitter, event) + case yaml_SEQUENCE_START_EVENT: + return yaml_emitter_emit_sequence_start(emitter, event) + case yaml_MAPPING_START_EVENT: + return yaml_emitter_emit_mapping_start(emitter, event) + default: + return yaml_emitter_set_emitter_error(emitter, + fmt.Sprintf("expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS, but got %v", event.typ)) + } +} + +// Expect ALIAS. +func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_process_anchor(emitter) { + return false + } + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true +} + +// Expect SCALAR. +func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_select_scalar_style(emitter, event) { + return false + } + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } + if !yaml_emitter_increase_indent(emitter, true, false) { + return false + } + if !yaml_emitter_process_scalar(emitter) { + return false + } + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true +} + +// Expect SEQUENCE-START. +func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } + if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE || + yaml_emitter_check_empty_sequence(emitter) { + emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE + } else { + emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE + } + return true +} + +// Expect MAPPING-START. +func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } + if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE || + yaml_emitter_check_empty_mapping(emitter) { + emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE + } else { + emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE + } + return true +} + +// Check if the document content is an empty scalar. +func yaml_emitter_check_empty_document(emitter *yaml_emitter_t) bool { + return false // [Go] Huh? +} + +// Check if the next events represent an empty sequence. +func yaml_emitter_check_empty_sequence(emitter *yaml_emitter_t) bool { + if len(emitter.events)-emitter.events_head < 2 { + return false + } + return emitter.events[emitter.events_head].typ == yaml_SEQUENCE_START_EVENT && + emitter.events[emitter.events_head+1].typ == yaml_SEQUENCE_END_EVENT +} + +// Check if the next events represent an empty mapping. +func yaml_emitter_check_empty_mapping(emitter *yaml_emitter_t) bool { + if len(emitter.events)-emitter.events_head < 2 { + return false + } + return emitter.events[emitter.events_head].typ == yaml_MAPPING_START_EVENT && + emitter.events[emitter.events_head+1].typ == yaml_MAPPING_END_EVENT +} + +// Check if the next node can be expressed as a simple key. +func yaml_emitter_check_simple_key(emitter *yaml_emitter_t) bool { + length := 0 + switch emitter.events[emitter.events_head].typ { + case yaml_ALIAS_EVENT: + length += len(emitter.anchor_data.anchor) + case yaml_SCALAR_EVENT: + if emitter.scalar_data.multiline { + return false + } + length += len(emitter.anchor_data.anchor) + + len(emitter.tag_data.handle) + + len(emitter.tag_data.suffix) + + len(emitter.scalar_data.value) + case yaml_SEQUENCE_START_EVENT: + if !yaml_emitter_check_empty_sequence(emitter) { + return false + } + length += len(emitter.anchor_data.anchor) + + len(emitter.tag_data.handle) + + len(emitter.tag_data.suffix) + case yaml_MAPPING_START_EVENT: + if !yaml_emitter_check_empty_mapping(emitter) { + return false + } + length += len(emitter.anchor_data.anchor) + + len(emitter.tag_data.handle) + + len(emitter.tag_data.suffix) + default: + return false + } + return length <= 128 +} + +// Determine an acceptable scalar style. +func yaml_emitter_select_scalar_style(emitter *yaml_emitter_t, event *yaml_event_t) bool { + + no_tag := len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 + if no_tag && !event.implicit && !event.quoted_implicit { + return yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified") + } + + style := event.scalar_style() + if style == yaml_ANY_SCALAR_STYLE { + style = yaml_PLAIN_SCALAR_STYLE + } + if emitter.canonical { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + if emitter.simple_key_context && emitter.scalar_data.multiline { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + + if style == yaml_PLAIN_SCALAR_STYLE { + if emitter.flow_level > 0 && !emitter.scalar_data.flow_plain_allowed || + emitter.flow_level == 0 && !emitter.scalar_data.block_plain_allowed { + style = yaml_SINGLE_QUOTED_SCALAR_STYLE + } + if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) { + style = yaml_SINGLE_QUOTED_SCALAR_STYLE + } + if no_tag && !event.implicit { + style = yaml_SINGLE_QUOTED_SCALAR_STYLE + } + } + if style == yaml_SINGLE_QUOTED_SCALAR_STYLE { + if !emitter.scalar_data.single_quoted_allowed { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + } + if style == yaml_LITERAL_SCALAR_STYLE || style == yaml_FOLDED_SCALAR_STYLE { + if !emitter.scalar_data.block_allowed || emitter.flow_level > 0 || emitter.simple_key_context { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + } + + if no_tag && !event.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE { + emitter.tag_data.handle = []byte{'!'} + } + emitter.scalar_data.style = style + return true +} + +// Write an anchor. +func yaml_emitter_process_anchor(emitter *yaml_emitter_t) bool { + if emitter.anchor_data.anchor == nil { + return true + } + c := []byte{'&'} + if emitter.anchor_data.alias { + c[0] = '*' + } + if !yaml_emitter_write_indicator(emitter, c, true, false, false) { + return false + } + return yaml_emitter_write_anchor(emitter, emitter.anchor_data.anchor) +} + +// Write a tag. +func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool { + if len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 { + return true + } + if len(emitter.tag_data.handle) > 0 { + if !yaml_emitter_write_tag_handle(emitter, emitter.tag_data.handle) { + return false + } + if len(emitter.tag_data.suffix) > 0 { + if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) { + return false + } + } + } else { + // [Go] Allocate these slices elsewhere. + if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) { + return false + } + if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) { + return false + } + } + return true +} + +// Write a scalar. +func yaml_emitter_process_scalar(emitter *yaml_emitter_t) bool { + switch emitter.scalar_data.style { + case yaml_PLAIN_SCALAR_STYLE: + return yaml_emitter_write_plain_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context) + + case yaml_SINGLE_QUOTED_SCALAR_STYLE: + return yaml_emitter_write_single_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context) + + case yaml_DOUBLE_QUOTED_SCALAR_STYLE: + return yaml_emitter_write_double_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context) + + case yaml_LITERAL_SCALAR_STYLE: + return yaml_emitter_write_literal_scalar(emitter, emitter.scalar_data.value) + + case yaml_FOLDED_SCALAR_STYLE: + return yaml_emitter_write_folded_scalar(emitter, emitter.scalar_data.value) + } + panic("unknown scalar style") +} + +// Write a head comment. +func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool { + if len(emitter.tail_comment) > 0 { + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_comment(emitter, emitter.tail_comment) { + return false + } + emitter.tail_comment = emitter.tail_comment[:0] + emitter.foot_indent = emitter.indent + if emitter.foot_indent < 0 { + emitter.foot_indent = 0 + } + } + + if len(emitter.head_comment) == 0 { + return true + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_comment(emitter, emitter.head_comment) { + return false + } + emitter.head_comment = emitter.head_comment[:0] + return true +} + +// Write an line comment. +func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool { + if len(emitter.line_comment) == 0 { + return true + } + if !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + if !yaml_emitter_write_comment(emitter, emitter.line_comment) { + return false + } + emitter.line_comment = emitter.line_comment[:0] + return true +} + +// Write a foot comment. +func yaml_emitter_process_foot_comment(emitter *yaml_emitter_t) bool { + if len(emitter.foot_comment) == 0 { + return true + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_comment(emitter, emitter.foot_comment) { + return false + } + emitter.foot_comment = emitter.foot_comment[:0] + emitter.foot_indent = emitter.indent + if emitter.foot_indent < 0 { + emitter.foot_indent = 0 + } + return true +} + +// Check if a %YAML directive is valid. +func yaml_emitter_analyze_version_directive(emitter *yaml_emitter_t, version_directive *yaml_version_directive_t) bool { + if version_directive.major != 1 || version_directive.minor != 1 { + return yaml_emitter_set_emitter_error(emitter, "incompatible %YAML directive") + } + return true +} + +// Check if a %TAG directive is valid. +func yaml_emitter_analyze_tag_directive(emitter *yaml_emitter_t, tag_directive *yaml_tag_directive_t) bool { + handle := tag_directive.handle + prefix := tag_directive.prefix + if len(handle) == 0 { + return yaml_emitter_set_emitter_error(emitter, "tag handle must not be empty") + } + if handle[0] != '!' { + return yaml_emitter_set_emitter_error(emitter, "tag handle must start with '!'") + } + if handle[len(handle)-1] != '!' { + return yaml_emitter_set_emitter_error(emitter, "tag handle must end with '!'") + } + for i := 1; i < len(handle)-1; i += width(handle[i]) { + if !is_alpha(handle, i) { + return yaml_emitter_set_emitter_error(emitter, "tag handle must contain alphanumerical characters only") + } + } + if len(prefix) == 0 { + return yaml_emitter_set_emitter_error(emitter, "tag prefix must not be empty") + } + return true +} + +// Check if an anchor is valid. +func yaml_emitter_analyze_anchor(emitter *yaml_emitter_t, anchor []byte, alias bool) bool { + if len(anchor) == 0 { + problem := "anchor value must not be empty" + if alias { + problem = "alias value must not be empty" + } + return yaml_emitter_set_emitter_error(emitter, problem) + } + for i := 0; i < len(anchor); i += width(anchor[i]) { + if !is_alpha(anchor, i) { + problem := "anchor value must contain alphanumerical characters only" + if alias { + problem = "alias value must contain alphanumerical characters only" + } + return yaml_emitter_set_emitter_error(emitter, problem) + } + } + emitter.anchor_data.anchor = anchor + emitter.anchor_data.alias = alias + return true +} + +// Check if a tag is valid. +func yaml_emitter_analyze_tag(emitter *yaml_emitter_t, tag []byte) bool { + if len(tag) == 0 { + return yaml_emitter_set_emitter_error(emitter, "tag value must not be empty") + } + for i := 0; i < len(emitter.tag_directives); i++ { + tag_directive := &emitter.tag_directives[i] + if bytes.HasPrefix(tag, tag_directive.prefix) { + emitter.tag_data.handle = tag_directive.handle + emitter.tag_data.suffix = tag[len(tag_directive.prefix):] + return true + } + } + emitter.tag_data.suffix = tag + return true +} + +// Check if a scalar is valid. +func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool { + var ( + block_indicators = false + flow_indicators = false + line_breaks = false + special_characters = false + tab_characters = false + + leading_space = false + leading_break = false + trailing_space = false + trailing_break = false + break_space = false + space_break = false + + preceded_by_whitespace = false + followed_by_whitespace = false + previous_space = false + previous_break = false + ) + + emitter.scalar_data.value = value + + if len(value) == 0 { + emitter.scalar_data.multiline = false + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = true + emitter.scalar_data.single_quoted_allowed = true + emitter.scalar_data.block_allowed = false + return true + } + + if len(value) >= 3 && ((value[0] == '-' && value[1] == '-' && value[2] == '-') || (value[0] == '.' && value[1] == '.' && value[2] == '.')) { + block_indicators = true + flow_indicators = true + } + + preceded_by_whitespace = true + for i, w := 0, 0; i < len(value); i += w { + w = width(value[i]) + followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w) + + if i == 0 { + switch value[i] { + case '#', ',', '[', ']', '{', '}', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`': + flow_indicators = true + block_indicators = true + case '?', ':': + flow_indicators = true + if followed_by_whitespace { + block_indicators = true + } + case '-': + if followed_by_whitespace { + flow_indicators = true + block_indicators = true + } + } + } else { + switch value[i] { + case ',', '?', '[', ']', '{', '}': + flow_indicators = true + case ':': + flow_indicators = true + if followed_by_whitespace { + block_indicators = true + } + case '#': + if preceded_by_whitespace { + flow_indicators = true + block_indicators = true + } + } + } + + if value[i] == '\t' { + tab_characters = true + } else if !is_printable(value, i) || !is_ascii(value, i) && !emitter.unicode { + special_characters = true + } + if is_space(value, i) { + if i == 0 { + leading_space = true + } + if i+width(value[i]) == len(value) { + trailing_space = true + } + if previous_break { + break_space = true + } + previous_space = true + previous_break = false + } else if is_break(value, i) { + line_breaks = true + if i == 0 { + leading_break = true + } + if i+width(value[i]) == len(value) { + trailing_break = true + } + if previous_space { + space_break = true + } + previous_space = false + previous_break = true + } else { + previous_space = false + previous_break = false + } + + // [Go]: Why 'z'? Couldn't be the end of the string as that's the loop condition. + preceded_by_whitespace = is_blankz(value, i) + } + + emitter.scalar_data.multiline = line_breaks + emitter.scalar_data.flow_plain_allowed = true + emitter.scalar_data.block_plain_allowed = true + emitter.scalar_data.single_quoted_allowed = true + emitter.scalar_data.block_allowed = true + + if leading_space || leading_break || trailing_space || trailing_break { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + } + if trailing_space { + emitter.scalar_data.block_allowed = false + } + if break_space { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + emitter.scalar_data.single_quoted_allowed = false + } + if space_break || tab_characters || special_characters { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + emitter.scalar_data.single_quoted_allowed = false + } + if space_break || special_characters { + emitter.scalar_data.block_allowed = false + } + if line_breaks { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + } + if flow_indicators { + emitter.scalar_data.flow_plain_allowed = false + } + if block_indicators { + emitter.scalar_data.block_plain_allowed = false + } + return true +} + +// Check if the event data is valid. +func yaml_emitter_analyze_event(emitter *yaml_emitter_t, event *yaml_event_t) bool { + + emitter.anchor_data.anchor = nil + emitter.tag_data.handle = nil + emitter.tag_data.suffix = nil + emitter.scalar_data.value = nil + + if len(event.head_comment) > 0 { + emitter.head_comment = event.head_comment + } + if len(event.line_comment) > 0 { + emitter.line_comment = event.line_comment + } + if len(event.foot_comment) > 0 { + emitter.foot_comment = event.foot_comment + } + if len(event.tail_comment) > 0 { + emitter.tail_comment = event.tail_comment + } + + switch event.typ { + case yaml_ALIAS_EVENT: + if !yaml_emitter_analyze_anchor(emitter, event.anchor, true) { + return false + } + + case yaml_SCALAR_EVENT: + if len(event.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) { + return false + } + } + if len(event.tag) > 0 && (emitter.canonical || (!event.implicit && !event.quoted_implicit)) { + if !yaml_emitter_analyze_tag(emitter, event.tag) { + return false + } + } + if !yaml_emitter_analyze_scalar(emitter, event.value) { + return false + } + + case yaml_SEQUENCE_START_EVENT: + if len(event.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) { + return false + } + } + if len(event.tag) > 0 && (emitter.canonical || !event.implicit) { + if !yaml_emitter_analyze_tag(emitter, event.tag) { + return false + } + } + + case yaml_MAPPING_START_EVENT: + if len(event.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) { + return false + } + } + if len(event.tag) > 0 && (emitter.canonical || !event.implicit) { + if !yaml_emitter_analyze_tag(emitter, event.tag) { + return false + } + } + } + return true +} + +// Write the BOM character. +func yaml_emitter_write_bom(emitter *yaml_emitter_t) bool { + if !flush(emitter) { + return false + } + pos := emitter.buffer_pos + emitter.buffer[pos+0] = '\xEF' + emitter.buffer[pos+1] = '\xBB' + emitter.buffer[pos+2] = '\xBF' + emitter.buffer_pos += 3 + return true +} + +func yaml_emitter_write_indent(emitter *yaml_emitter_t) bool { + indent := emitter.indent + if indent < 0 { + indent = 0 + } + if !emitter.indention || emitter.column > indent || (emitter.column == indent && !emitter.whitespace) { + if !put_break(emitter) { + return false + } + } + if emitter.foot_indent == indent { + if !put_break(emitter) { + return false + } + } + for emitter.column < indent { + if !put(emitter, ' ') { + return false + } + } + emitter.whitespace = true + //emitter.indention = true + emitter.space_above = false + emitter.foot_indent = -1 + return true +} + +func yaml_emitter_write_indicator(emitter *yaml_emitter_t, indicator []byte, need_whitespace, is_whitespace, is_indention bool) bool { + if need_whitespace && !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + if !write_all(emitter, indicator) { + return false + } + emitter.whitespace = is_whitespace + emitter.indention = (emitter.indention && is_indention) + emitter.open_ended = false + return true +} + +func yaml_emitter_write_anchor(emitter *yaml_emitter_t, value []byte) bool { + if !write_all(emitter, value) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool { + if !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + if !write_all(emitter, value) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool { + if need_whitespace && !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + for i := 0; i < len(value); { + var must_write bool + switch value[i] { + case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']': + must_write = true + default: + must_write = is_alpha(value, i) + } + if must_write { + if !write(emitter, value, &i) { + return false + } + } else { + w := width(value[i]) + for k := 0; k < w; k++ { + octet := value[i] + i++ + if !put(emitter, '%') { + return false + } + + c := octet >> 4 + if c < 10 { + c += '0' + } else { + c += 'A' - 10 + } + if !put(emitter, c) { + return false + } + + c = octet & 0x0f + if c < 10 { + c += '0' + } else { + c += 'A' - 10 + } + if !put(emitter, c) { + return false + } + } + } + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_plain_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool { + if len(value) > 0 && !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + + spaces := false + breaks := false + for i := 0; i < len(value); { + if is_space(value, i) { + if allow_breaks && !spaces && emitter.column > emitter.best_width && !is_space(value, i+1) { + if !yaml_emitter_write_indent(emitter) { + return false + } + i += width(value[i]) + } else { + if !write(emitter, value, &i) { + return false + } + } + spaces = true + } else if is_break(value, i) { + if !breaks && value[i] == '\n' { + if !put_break(emitter) { + return false + } + } + if !write_break(emitter, value, &i) { + return false + } + //emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !write(emitter, value, &i) { + return false + } + emitter.indention = false + spaces = false + breaks = false + } + } + + if len(value) > 0 { + emitter.whitespace = false + } + emitter.indention = false + if emitter.root_context { + emitter.open_ended = true + } + + return true +} + +func yaml_emitter_write_single_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool { + + if !yaml_emitter_write_indicator(emitter, []byte{'\''}, true, false, false) { + return false + } + + spaces := false + breaks := false + for i := 0; i < len(value); { + if is_space(value, i) { + if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 && !is_space(value, i+1) { + if !yaml_emitter_write_indent(emitter) { + return false + } + i += width(value[i]) + } else { + if !write(emitter, value, &i) { + return false + } + } + spaces = true + } else if is_break(value, i) { + if !breaks && value[i] == '\n' { + if !put_break(emitter) { + return false + } + } + if !write_break(emitter, value, &i) { + return false + } + //emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if value[i] == '\'' { + if !put(emitter, '\'') { + return false + } + } + if !write(emitter, value, &i) { + return false + } + emitter.indention = false + spaces = false + breaks = false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{'\''}, false, false, false) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_double_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool { + spaces := false + if !yaml_emitter_write_indicator(emitter, []byte{'"'}, true, false, false) { + return false + } + + for i := 0; i < len(value); { + if !is_printable(value, i) || (!emitter.unicode && !is_ascii(value, i)) || + is_bom(value, i) || is_break(value, i) || + value[i] == '"' || value[i] == '\\' { + + octet := value[i] + + var w int + var v rune + switch { + case octet&0x80 == 0x00: + w, v = 1, rune(octet&0x7F) + case octet&0xE0 == 0xC0: + w, v = 2, rune(octet&0x1F) + case octet&0xF0 == 0xE0: + w, v = 3, rune(octet&0x0F) + case octet&0xF8 == 0xF0: + w, v = 4, rune(octet&0x07) + } + for k := 1; k < w; k++ { + octet = value[i+k] + v = (v << 6) + (rune(octet) & 0x3F) + } + i += w + + if !put(emitter, '\\') { + return false + } + + var ok bool + switch v { + case 0x00: + ok = put(emitter, '0') + case 0x07: + ok = put(emitter, 'a') + case 0x08: + ok = put(emitter, 'b') + case 0x09: + ok = put(emitter, 't') + case 0x0A: + ok = put(emitter, 'n') + case 0x0b: + ok = put(emitter, 'v') + case 0x0c: + ok = put(emitter, 'f') + case 0x0d: + ok = put(emitter, 'r') + case 0x1b: + ok = put(emitter, 'e') + case 0x22: + ok = put(emitter, '"') + case 0x5c: + ok = put(emitter, '\\') + case 0x85: + ok = put(emitter, 'N') + case 0xA0: + ok = put(emitter, '_') + case 0x2028: + ok = put(emitter, 'L') + case 0x2029: + ok = put(emitter, 'P') + default: + if v <= 0xFF { + ok = put(emitter, 'x') + w = 2 + } else if v <= 0xFFFF { + ok = put(emitter, 'u') + w = 4 + } else { + ok = put(emitter, 'U') + w = 8 + } + for k := (w - 1) * 4; ok && k >= 0; k -= 4 { + digit := byte((v >> uint(k)) & 0x0F) + if digit < 10 { + ok = put(emitter, digit+'0') + } else { + ok = put(emitter, digit+'A'-10) + } + } + } + if !ok { + return false + } + spaces = false + } else if is_space(value, i) { + if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 { + if !yaml_emitter_write_indent(emitter) { + return false + } + if is_space(value, i+1) { + if !put(emitter, '\\') { + return false + } + } + i += width(value[i]) + } else if !write(emitter, value, &i) { + return false + } + spaces = true + } else { + if !write(emitter, value, &i) { + return false + } + spaces = false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{'"'}, false, false, false) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_block_scalar_hints(emitter *yaml_emitter_t, value []byte) bool { + if is_space(value, 0) || is_break(value, 0) { + indent_hint := []byte{'0' + byte(emitter.best_indent)} + if !yaml_emitter_write_indicator(emitter, indent_hint, false, false, false) { + return false + } + } + + emitter.open_ended = false + + var chomp_hint [1]byte + if len(value) == 0 { + chomp_hint[0] = '-' + } else { + i := len(value) - 1 + for value[i]&0xC0 == 0x80 { + i-- + } + if !is_break(value, i) { + chomp_hint[0] = '-' + } else if i == 0 { + chomp_hint[0] = '+' + emitter.open_ended = true + } else { + i-- + for value[i]&0xC0 == 0x80 { + i-- + } + if is_break(value, i) { + chomp_hint[0] = '+' + emitter.open_ended = true + } + } + } + if chomp_hint[0] != 0 { + if !yaml_emitter_write_indicator(emitter, chomp_hint[:], false, false, false) { + return false + } + } + return true +} + +func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bool { + if !yaml_emitter_write_indicator(emitter, []byte{'|'}, true, false, false) { + return false + } + if !yaml_emitter_write_block_scalar_hints(emitter, value) { + return false + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + //emitter.indention = true + emitter.whitespace = true + breaks := true + for i := 0; i < len(value); { + if is_break(value, i) { + if !write_break(emitter, value, &i) { + return false + } + //emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !write(emitter, value, &i) { + return false + } + emitter.indention = false + breaks = false + } + } + + return true +} + +func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) bool { + if !yaml_emitter_write_indicator(emitter, []byte{'>'}, true, false, false) { + return false + } + if !yaml_emitter_write_block_scalar_hints(emitter, value) { + return false + } + if !yaml_emitter_process_line_comment(emitter) { + return false + } + + //emitter.indention = true + emitter.whitespace = true + + breaks := true + leading_spaces := true + for i := 0; i < len(value); { + if is_break(value, i) { + if !breaks && !leading_spaces && value[i] == '\n' { + k := 0 + for is_break(value, k) { + k += width(value[k]) + } + if !is_blankz(value, k) { + if !put_break(emitter) { + return false + } + } + } + if !write_break(emitter, value, &i) { + return false + } + //emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + leading_spaces = is_blank(value, i) + } + if !breaks && is_space(value, i) && !is_space(value, i+1) && emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + i += width(value[i]) + } else { + if !write(emitter, value, &i) { + return false + } + } + emitter.indention = false + breaks = false + } + } + return true +} + +func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte) bool { + breaks := false + pound := false + for i := 0; i < len(comment); { + if is_break(comment, i) { + if !write_break(emitter, comment, &i) { + return false + } + //emitter.indention = true + breaks = true + pound = false + } else { + if breaks && !yaml_emitter_write_indent(emitter) { + return false + } + if !pound { + if comment[i] != '#' && (!put(emitter, '#') || !put(emitter, ' ')) { + return false + } + pound = true + } + if !write(emitter, comment, &i) { + return false + } + emitter.indention = false + breaks = false + } + } + if !breaks && !put_break(emitter) { + return false + } + + emitter.whitespace = true + //emitter.indention = true + return true +} diff --git a/vendor/gopkg.in/yaml.v3/encode.go b/vendor/gopkg.in/yaml.v3/encode.go new file mode 100644 index 0000000000..de9e72a3e6 --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/encode.go @@ -0,0 +1,577 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// +// 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 yaml + +import ( + "encoding" + "fmt" + "io" + "reflect" + "regexp" + "sort" + "strconv" + "strings" + "time" + "unicode/utf8" +) + +type encoder struct { + emitter yaml_emitter_t + event yaml_event_t + out []byte + flow bool + indent int + doneInit bool +} + +func newEncoder() *encoder { + e := &encoder{} + yaml_emitter_initialize(&e.emitter) + yaml_emitter_set_output_string(&e.emitter, &e.out) + yaml_emitter_set_unicode(&e.emitter, true) + return e +} + +func newEncoderWithWriter(w io.Writer) *encoder { + e := &encoder{} + yaml_emitter_initialize(&e.emitter) + yaml_emitter_set_output_writer(&e.emitter, w) + yaml_emitter_set_unicode(&e.emitter, true) + return e +} + +func (e *encoder) init() { + if e.doneInit { + return + } + if e.indent == 0 { + e.indent = 4 + } + e.emitter.best_indent = e.indent + yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING) + e.emit() + e.doneInit = true +} + +func (e *encoder) finish() { + e.emitter.open_ended = false + yaml_stream_end_event_initialize(&e.event) + e.emit() +} + +func (e *encoder) destroy() { + yaml_emitter_delete(&e.emitter) +} + +func (e *encoder) emit() { + // This will internally delete the e.event value. + e.must(yaml_emitter_emit(&e.emitter, &e.event)) +} + +func (e *encoder) must(ok bool) { + if !ok { + msg := e.emitter.problem + if msg == "" { + msg = "unknown problem generating YAML content" + } + failf("%s", msg) + } +} + +func (e *encoder) marshalDoc(tag string, in reflect.Value) { + e.init() + var node *Node + if in.IsValid() { + node, _ = in.Interface().(*Node) + } + if node != nil && node.Kind == DocumentNode { + e.nodev(in) + } else { + yaml_document_start_event_initialize(&e.event, nil, nil, true) + e.emit() + e.marshal(tag, in) + yaml_document_end_event_initialize(&e.event, true) + e.emit() + } +} + +func (e *encoder) marshal(tag string, in reflect.Value) { + tag = shortTag(tag) + if !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() { + e.nilv() + return + } + iface := in.Interface() + switch value := iface.(type) { + case *Node: + e.nodev(in) + return + case Node: + if !in.CanAddr() { + var n = reflect.New(in.Type()).Elem() + n.Set(in) + in = n + } + e.nodev(in.Addr()) + return + case time.Time: + e.timev(tag, in) + return + case *time.Time: + e.timev(tag, in.Elem()) + return + case time.Duration: + e.stringv(tag, reflect.ValueOf(value.String())) + return + case Marshaler: + v, err := value.MarshalYAML() + if err != nil { + fail(err) + } + if v == nil { + e.nilv() + return + } + e.marshal(tag, reflect.ValueOf(v)) + return + case encoding.TextMarshaler: + text, err := value.MarshalText() + if err != nil { + fail(err) + } + in = reflect.ValueOf(string(text)) + case nil: + e.nilv() + return + } + switch in.Kind() { + case reflect.Interface: + e.marshal(tag, in.Elem()) + case reflect.Map: + e.mapv(tag, in) + case reflect.Ptr: + e.marshal(tag, in.Elem()) + case reflect.Struct: + e.structv(tag, in) + case reflect.Slice, reflect.Array: + e.slicev(tag, in) + case reflect.String: + e.stringv(tag, in) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + e.intv(tag, in) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + e.uintv(tag, in) + case reflect.Float32, reflect.Float64: + e.floatv(tag, in) + case reflect.Bool: + e.boolv(tag, in) + default: + panic("cannot marshal type: " + in.Type().String()) + } +} + +func (e *encoder) mapv(tag string, in reflect.Value) { + e.mappingv(tag, func() { + keys := keyList(in.MapKeys()) + sort.Sort(keys) + for _, k := range keys { + e.marshal("", k) + e.marshal("", in.MapIndex(k)) + } + }) +} + +func (e *encoder) fieldByIndex(v reflect.Value, index []int) (field reflect.Value) { + for _, num := range index { + for { + if v.Kind() == reflect.Ptr { + if v.IsNil() { + return reflect.Value{} + } + v = v.Elem() + continue + } + break + } + v = v.Field(num) + } + return v +} + +func (e *encoder) structv(tag string, in reflect.Value) { + sinfo, err := getStructInfo(in.Type()) + if err != nil { + panic(err) + } + e.mappingv(tag, func() { + for _, info := range sinfo.FieldsList { + var value reflect.Value + if info.Inline == nil { + value = in.Field(info.Num) + } else { + value = e.fieldByIndex(in, info.Inline) + if !value.IsValid() { + continue + } + } + if info.OmitEmpty && isZero(value) { + continue + } + e.marshal("", reflect.ValueOf(info.Key)) + e.flow = info.Flow + e.marshal("", value) + } + if sinfo.InlineMap >= 0 { + m := in.Field(sinfo.InlineMap) + if m.Len() > 0 { + e.flow = false + keys := keyList(m.MapKeys()) + sort.Sort(keys) + for _, k := range keys { + if _, found := sinfo.FieldsMap[k.String()]; found { + panic(fmt.Sprintf("cannot have key %q in inlined map: conflicts with struct field", k.String())) + } + e.marshal("", k) + e.flow = false + e.marshal("", m.MapIndex(k)) + } + } + } + }) +} + +func (e *encoder) mappingv(tag string, f func()) { + implicit := tag == "" + style := yaml_BLOCK_MAPPING_STYLE + if e.flow { + e.flow = false + style = yaml_FLOW_MAPPING_STYLE + } + yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style) + e.emit() + f() + yaml_mapping_end_event_initialize(&e.event) + e.emit() +} + +func (e *encoder) slicev(tag string, in reflect.Value) { + implicit := tag == "" + style := yaml_BLOCK_SEQUENCE_STYLE + if e.flow { + e.flow = false + style = yaml_FLOW_SEQUENCE_STYLE + } + e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)) + e.emit() + n := in.Len() + for i := 0; i < n; i++ { + e.marshal("", in.Index(i)) + } + e.must(yaml_sequence_end_event_initialize(&e.event)) + e.emit() +} + +// isBase60 returns whether s is in base 60 notation as defined in YAML 1.1. +// +// The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported +// in YAML 1.2 and by this package, but these should be marshalled quoted for +// the time being for compatibility with other parsers. +func isBase60Float(s string) (result bool) { + // Fast path. + if s == "" { + return false + } + c := s[0] + if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 { + return false + } + // Do the full match. + return base60float.MatchString(s) +} + +// From http://yaml.org/type/float.html, except the regular expression there +// is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix. +var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`) + +// isOldBool returns whether s is bool notation as defined in YAML 1.1. +// +// We continue to force strings that YAML 1.1 would interpret as booleans to be +// rendered as quotes strings so that the marshalled output valid for YAML 1.1 +// parsing. +func isOldBool(s string) (result bool) { + switch s { + case "y", "Y", "yes", "Yes", "YES", "on", "On", "ON", + "n", "N", "no", "No", "NO", "off", "Off", "OFF": + return true + default: + return false + } +} + +func (e *encoder) stringv(tag string, in reflect.Value) { + var style yaml_scalar_style_t + s := in.String() + canUsePlain := true + switch { + case !utf8.ValidString(s): + if tag == binaryTag { + failf("explicitly tagged !!binary data must be base64-encoded") + } + if tag != "" { + failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag)) + } + // It can't be encoded directly as YAML so use a binary tag + // and encode it as base64. + tag = binaryTag + s = encodeBase64(s) + case tag == "": + // Check to see if it would resolve to a specific + // tag when encoded unquoted. If it doesn't, + // there's no need to quote it. + rtag, _ := resolve("", s) + canUsePlain = rtag == strTag && !(isBase60Float(s) || isOldBool(s)) + } + // Note: it's possible for user code to emit invalid YAML + // if they explicitly specify a tag and a string containing + // text that's incompatible with that tag. + switch { + case strings.Contains(s, "\n"): + if e.flow { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } else { + style = yaml_LITERAL_SCALAR_STYLE + } + case canUsePlain: + style = yaml_PLAIN_SCALAR_STYLE + default: + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + e.emitScalar(s, "", tag, style, nil, nil, nil, nil) +} + +func (e *encoder) boolv(tag string, in reflect.Value) { + var s string + if in.Bool() { + s = "true" + } else { + s = "false" + } + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil) +} + +func (e *encoder) intv(tag string, in reflect.Value) { + s := strconv.FormatInt(in.Int(), 10) + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil) +} + +func (e *encoder) uintv(tag string, in reflect.Value) { + s := strconv.FormatUint(in.Uint(), 10) + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil) +} + +func (e *encoder) timev(tag string, in reflect.Value) { + t := in.Interface().(time.Time) + s := t.Format(time.RFC3339Nano) + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil) +} + +func (e *encoder) floatv(tag string, in reflect.Value) { + // Issue #352: When formatting, use the precision of the underlying value + precision := 64 + if in.Kind() == reflect.Float32 { + precision = 32 + } + + s := strconv.FormatFloat(in.Float(), 'g', -1, precision) + switch s { + case "+Inf": + s = ".inf" + case "-Inf": + s = "-.inf" + case "NaN": + s = ".nan" + } + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil) +} + +func (e *encoder) nilv() { + e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil) +} + +func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t, head, line, foot, tail []byte) { + // TODO Kill this function. Replace all initialize calls by their underlining Go literals. + implicit := tag == "" + if !implicit { + tag = longTag(tag) + } + e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style)) + e.event.head_comment = head + e.event.line_comment = line + e.event.foot_comment = foot + e.event.tail_comment = tail + e.emit() +} + +func (e *encoder) nodev(in reflect.Value) { + e.node(in.Interface().(*Node), "") +} + +func (e *encoder) node(node *Node, tail string) { + // Zero nodes behave as nil. + if node.Kind == 0 && node.IsZero() { + e.nilv() + return + } + + // If the tag was not explicitly requested, and dropping it won't change the + // implicit tag of the value, don't include it in the presentation. + var tag = node.Tag + var stag = shortTag(tag) + var forceQuoting bool + if tag != "" && node.Style&TaggedStyle == 0 { + if node.Kind == ScalarNode { + if stag == strTag && node.Style&(SingleQuotedStyle|DoubleQuotedStyle|LiteralStyle|FoldedStyle) != 0 { + tag = "" + } else { + rtag, _ := resolve("", node.Value) + if rtag == stag { + tag = "" + } else if stag == strTag { + tag = "" + forceQuoting = true + } + } + } else { + var rtag string + switch node.Kind { + case MappingNode: + rtag = mapTag + case SequenceNode: + rtag = seqTag + } + if rtag == stag { + tag = "" + } + } + } + + switch node.Kind { + case DocumentNode: + yaml_document_start_event_initialize(&e.event, nil, nil, true) + e.event.head_comment = []byte(node.HeadComment) + e.emit() + for _, node := range node.Content { + e.node(node, "") + } + yaml_document_end_event_initialize(&e.event, true) + e.event.foot_comment = []byte(node.FootComment) + e.emit() + + case SequenceNode: + style := yaml_BLOCK_SEQUENCE_STYLE + if node.Style&FlowStyle != 0 { + style = yaml_FLOW_SEQUENCE_STYLE + } + e.must(yaml_sequence_start_event_initialize(&e.event, []byte(node.Anchor), []byte(longTag(tag)), tag == "", style)) + e.event.head_comment = []byte(node.HeadComment) + e.emit() + for _, node := range node.Content { + e.node(node, "") + } + e.must(yaml_sequence_end_event_initialize(&e.event)) + e.event.line_comment = []byte(node.LineComment) + e.event.foot_comment = []byte(node.FootComment) + e.emit() + + case MappingNode: + style := yaml_BLOCK_MAPPING_STYLE + if node.Style&FlowStyle != 0 { + style = yaml_FLOW_MAPPING_STYLE + } + yaml_mapping_start_event_initialize(&e.event, []byte(node.Anchor), []byte(longTag(tag)), tag == "", style) + e.event.tail_comment = []byte(tail) + e.event.head_comment = []byte(node.HeadComment) + e.emit() + + // The tail logic below moves the foot comment of prior keys to the following key, + // since the value for each key may be a nested structure and the foot needs to be + // processed only the entirety of the value is streamed. The last tail is processed + // with the mapping end event. + var tail string + for i := 0; i+1 < len(node.Content); i += 2 { + k := node.Content[i] + foot := k.FootComment + if foot != "" { + kopy := *k + kopy.FootComment = "" + k = &kopy + } + e.node(k, tail) + tail = foot + + v := node.Content[i+1] + e.node(v, "") + } + + yaml_mapping_end_event_initialize(&e.event) + e.event.tail_comment = []byte(tail) + e.event.line_comment = []byte(node.LineComment) + e.event.foot_comment = []byte(node.FootComment) + e.emit() + + case AliasNode: + yaml_alias_event_initialize(&e.event, []byte(node.Value)) + e.event.head_comment = []byte(node.HeadComment) + e.event.line_comment = []byte(node.LineComment) + e.event.foot_comment = []byte(node.FootComment) + e.emit() + + case ScalarNode: + value := node.Value + if !utf8.ValidString(value) { + if stag == binaryTag { + failf("explicitly tagged !!binary data must be base64-encoded") + } + if stag != "" { + failf("cannot marshal invalid UTF-8 data as %s", stag) + } + // It can't be encoded directly as YAML so use a binary tag + // and encode it as base64. + tag = binaryTag + value = encodeBase64(value) + } + + style := yaml_PLAIN_SCALAR_STYLE + switch { + case node.Style&DoubleQuotedStyle != 0: + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + case node.Style&SingleQuotedStyle != 0: + style = yaml_SINGLE_QUOTED_SCALAR_STYLE + case node.Style&LiteralStyle != 0: + style = yaml_LITERAL_SCALAR_STYLE + case node.Style&FoldedStyle != 0: + style = yaml_FOLDED_SCALAR_STYLE + case strings.Contains(value, "\n"): + style = yaml_LITERAL_SCALAR_STYLE + case forceQuoting: + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + + e.emitScalar(value, node.Anchor, tag, style, []byte(node.HeadComment), []byte(node.LineComment), []byte(node.FootComment), []byte(tail)) + default: + failf("cannot encode node with unknown kind %d", node.Kind) + } +} diff --git a/vendor/gopkg.in/yaml.v3/parserc.go b/vendor/gopkg.in/yaml.v3/parserc.go new file mode 100644 index 0000000000..268558a0d6 --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/parserc.go @@ -0,0 +1,1258 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +import ( + "bytes" +) + +// The parser implements the following grammar: +// +// stream ::= STREAM-START implicit_document? explicit_document* STREAM-END +// implicit_document ::= block_node DOCUMENT-END* +// explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* +// block_node_or_indentless_sequence ::= +// ALIAS +// | properties (block_content | indentless_block_sequence)? +// | block_content +// | indentless_block_sequence +// block_node ::= ALIAS +// | properties block_content? +// | block_content +// flow_node ::= ALIAS +// | properties flow_content? +// | flow_content +// properties ::= TAG ANCHOR? | ANCHOR TAG? +// block_content ::= block_collection | flow_collection | SCALAR +// flow_content ::= flow_collection | SCALAR +// block_collection ::= block_sequence | block_mapping +// flow_collection ::= flow_sequence | flow_mapping +// block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END +// indentless_sequence ::= (BLOCK-ENTRY block_node?)+ +// block_mapping ::= BLOCK-MAPPING_START +// ((KEY block_node_or_indentless_sequence?)? +// (VALUE block_node_or_indentless_sequence?)?)* +// BLOCK-END +// flow_sequence ::= FLOW-SEQUENCE-START +// (flow_sequence_entry FLOW-ENTRY)* +// flow_sequence_entry? +// FLOW-SEQUENCE-END +// flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? +// flow_mapping ::= FLOW-MAPPING-START +// (flow_mapping_entry FLOW-ENTRY)* +// flow_mapping_entry? +// FLOW-MAPPING-END +// flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? + +// Peek the next token in the token queue. +func peek_token(parser *yaml_parser_t) *yaml_token_t { + if parser.token_available || yaml_parser_fetch_more_tokens(parser) { + token := &parser.tokens[parser.tokens_head] + yaml_parser_unfold_comments(parser, token) + return token + } + return nil +} + +// yaml_parser_unfold_comments walks through the comments queue and joins all +// comments behind the position of the provided token into the respective +// top-level comment slices in the parser. +func yaml_parser_unfold_comments(parser *yaml_parser_t, token *yaml_token_t) { + for parser.comments_head < len(parser.comments) && token.start_mark.index >= parser.comments[parser.comments_head].token_mark.index { + comment := &parser.comments[parser.comments_head] + if len(comment.head) > 0 { + if token.typ == yaml_BLOCK_END_TOKEN { + // No heads on ends, so keep comment.head for a follow up token. + break + } + if len(parser.head_comment) > 0 { + parser.head_comment = append(parser.head_comment, '\n') + } + parser.head_comment = append(parser.head_comment, comment.head...) + } + if len(comment.foot) > 0 { + if len(parser.foot_comment) > 0 { + parser.foot_comment = append(parser.foot_comment, '\n') + } + parser.foot_comment = append(parser.foot_comment, comment.foot...) + } + if len(comment.line) > 0 { + if len(parser.line_comment) > 0 { + parser.line_comment = append(parser.line_comment, '\n') + } + parser.line_comment = append(parser.line_comment, comment.line...) + } + *comment = yaml_comment_t{} + parser.comments_head++ + } +} + +// Remove the next token from the queue (must be called after peek_token). +func skip_token(parser *yaml_parser_t) { + parser.token_available = false + parser.tokens_parsed++ + parser.stream_end_produced = parser.tokens[parser.tokens_head].typ == yaml_STREAM_END_TOKEN + parser.tokens_head++ +} + +// Get the next event. +func yaml_parser_parse(parser *yaml_parser_t, event *yaml_event_t) bool { + // Erase the event object. + *event = yaml_event_t{} + + // No events after the end of the stream or error. + if parser.stream_end_produced || parser.error != yaml_NO_ERROR || parser.state == yaml_PARSE_END_STATE { + return true + } + + // Generate the next event. + return yaml_parser_state_machine(parser, event) +} + +// Set parser error. +func yaml_parser_set_parser_error(parser *yaml_parser_t, problem string, problem_mark yaml_mark_t) bool { + parser.error = yaml_PARSER_ERROR + parser.problem = problem + parser.problem_mark = problem_mark + return false +} + +func yaml_parser_set_parser_error_context(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string, problem_mark yaml_mark_t) bool { + parser.error = yaml_PARSER_ERROR + parser.context = context + parser.context_mark = context_mark + parser.problem = problem + parser.problem_mark = problem_mark + return false +} + +// State dispatcher. +func yaml_parser_state_machine(parser *yaml_parser_t, event *yaml_event_t) bool { + //trace("yaml_parser_state_machine", "state:", parser.state.String()) + + switch parser.state { + case yaml_PARSE_STREAM_START_STATE: + return yaml_parser_parse_stream_start(parser, event) + + case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE: + return yaml_parser_parse_document_start(parser, event, true) + + case yaml_PARSE_DOCUMENT_START_STATE: + return yaml_parser_parse_document_start(parser, event, false) + + case yaml_PARSE_DOCUMENT_CONTENT_STATE: + return yaml_parser_parse_document_content(parser, event) + + case yaml_PARSE_DOCUMENT_END_STATE: + return yaml_parser_parse_document_end(parser, event) + + case yaml_PARSE_BLOCK_NODE_STATE: + return yaml_parser_parse_node(parser, event, true, false) + + case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE: + return yaml_parser_parse_node(parser, event, true, true) + + case yaml_PARSE_FLOW_NODE_STATE: + return yaml_parser_parse_node(parser, event, false, false) + + case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE: + return yaml_parser_parse_block_sequence_entry(parser, event, true) + + case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE: + return yaml_parser_parse_block_sequence_entry(parser, event, false) + + case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE: + return yaml_parser_parse_indentless_sequence_entry(parser, event) + + case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE: + return yaml_parser_parse_block_mapping_key(parser, event, true) + + case yaml_PARSE_BLOCK_MAPPING_KEY_STATE: + return yaml_parser_parse_block_mapping_key(parser, event, false) + + case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE: + return yaml_parser_parse_block_mapping_value(parser, event) + + case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE: + return yaml_parser_parse_flow_sequence_entry(parser, event, true) + + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE: + return yaml_parser_parse_flow_sequence_entry(parser, event, false) + + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE: + return yaml_parser_parse_flow_sequence_entry_mapping_key(parser, event) + + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE: + return yaml_parser_parse_flow_sequence_entry_mapping_value(parser, event) + + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE: + return yaml_parser_parse_flow_sequence_entry_mapping_end(parser, event) + + case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE: + return yaml_parser_parse_flow_mapping_key(parser, event, true) + + case yaml_PARSE_FLOW_MAPPING_KEY_STATE: + return yaml_parser_parse_flow_mapping_key(parser, event, false) + + case yaml_PARSE_FLOW_MAPPING_VALUE_STATE: + return yaml_parser_parse_flow_mapping_value(parser, event, false) + + case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE: + return yaml_parser_parse_flow_mapping_value(parser, event, true) + + default: + panic("invalid parser state") + } +} + +// Parse the production: +// stream ::= STREAM-START implicit_document? explicit_document* STREAM-END +// ************ +func yaml_parser_parse_stream_start(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_STREAM_START_TOKEN { + return yaml_parser_set_parser_error(parser, "did not find expected ", token.start_mark) + } + parser.state = yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE + *event = yaml_event_t{ + typ: yaml_STREAM_START_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + encoding: token.encoding, + } + skip_token(parser) + return true +} + +// Parse the productions: +// implicit_document ::= block_node DOCUMENT-END* +// * +// explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* +// ************************* +func yaml_parser_parse_document_start(parser *yaml_parser_t, event *yaml_event_t, implicit bool) bool { + + token := peek_token(parser) + if token == nil { + return false + } + + // Parse extra document end indicators. + if !implicit { + for token.typ == yaml_DOCUMENT_END_TOKEN { + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + } + } + + if implicit && token.typ != yaml_VERSION_DIRECTIVE_TOKEN && + token.typ != yaml_TAG_DIRECTIVE_TOKEN && + token.typ != yaml_DOCUMENT_START_TOKEN && + token.typ != yaml_STREAM_END_TOKEN { + // Parse an implicit document. + if !yaml_parser_process_directives(parser, nil, nil) { + return false + } + parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE) + parser.state = yaml_PARSE_BLOCK_NODE_STATE + + var head_comment []byte + if len(parser.head_comment) > 0 { + // [Go] Scan the header comment backwards, and if an empty line is found, break + // the header so the part before the last empty line goes into the + // document header, while the bottom of it goes into a follow up event. + for i := len(parser.head_comment) - 1; i > 0; i-- { + if parser.head_comment[i] == '\n' { + if i == len(parser.head_comment)-1 { + head_comment = parser.head_comment[:i] + parser.head_comment = parser.head_comment[i+1:] + break + } else if parser.head_comment[i-1] == '\n' { + head_comment = parser.head_comment[:i-1] + parser.head_comment = parser.head_comment[i+1:] + break + } + } + } + } + + *event = yaml_event_t{ + typ: yaml_DOCUMENT_START_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + + head_comment: head_comment, + } + + } else if token.typ != yaml_STREAM_END_TOKEN { + // Parse an explicit document. + var version_directive *yaml_version_directive_t + var tag_directives []yaml_tag_directive_t + start_mark := token.start_mark + if !yaml_parser_process_directives(parser, &version_directive, &tag_directives) { + return false + } + token = peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_DOCUMENT_START_TOKEN { + yaml_parser_set_parser_error(parser, + "did not find expected ", token.start_mark) + return false + } + parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE) + parser.state = yaml_PARSE_DOCUMENT_CONTENT_STATE + end_mark := token.end_mark + + *event = yaml_event_t{ + typ: yaml_DOCUMENT_START_EVENT, + start_mark: start_mark, + end_mark: end_mark, + version_directive: version_directive, + tag_directives: tag_directives, + implicit: false, + } + skip_token(parser) + + } else { + // Parse the stream end. + parser.state = yaml_PARSE_END_STATE + *event = yaml_event_t{ + typ: yaml_STREAM_END_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + } + skip_token(parser) + } + + return true +} + +// Parse the productions: +// explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* +// *********** +// +func yaml_parser_parse_document_content(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + + if token.typ == yaml_VERSION_DIRECTIVE_TOKEN || + token.typ == yaml_TAG_DIRECTIVE_TOKEN || + token.typ == yaml_DOCUMENT_START_TOKEN || + token.typ == yaml_DOCUMENT_END_TOKEN || + token.typ == yaml_STREAM_END_TOKEN { + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + return yaml_parser_process_empty_scalar(parser, event, + token.start_mark) + } + return yaml_parser_parse_node(parser, event, true, false) +} + +// Parse the productions: +// implicit_document ::= block_node DOCUMENT-END* +// ************* +// explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* +// +func yaml_parser_parse_document_end(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + + start_mark := token.start_mark + end_mark := token.start_mark + + implicit := true + if token.typ == yaml_DOCUMENT_END_TOKEN { + end_mark = token.end_mark + skip_token(parser) + implicit = false + } + + parser.tag_directives = parser.tag_directives[:0] + + parser.state = yaml_PARSE_DOCUMENT_START_STATE + *event = yaml_event_t{ + typ: yaml_DOCUMENT_END_EVENT, + start_mark: start_mark, + end_mark: end_mark, + implicit: implicit, + } + yaml_parser_set_event_comments(parser, event) + if len(event.head_comment) > 0 && len(event.foot_comment) == 0 { + event.foot_comment = event.head_comment + event.head_comment = nil + } + return true +} + +func yaml_parser_set_event_comments(parser *yaml_parser_t, event *yaml_event_t) { + event.head_comment = parser.head_comment + event.line_comment = parser.line_comment + event.foot_comment = parser.foot_comment + parser.head_comment = nil + parser.line_comment = nil + parser.foot_comment = nil + parser.tail_comment = nil + parser.stem_comment = nil +} + +// Parse the productions: +// block_node_or_indentless_sequence ::= +// ALIAS +// ***** +// | properties (block_content | indentless_block_sequence)? +// ********** * +// | block_content | indentless_block_sequence +// * +// block_node ::= ALIAS +// ***** +// | properties block_content? +// ********** * +// | block_content +// * +// flow_node ::= ALIAS +// ***** +// | properties flow_content? +// ********** * +// | flow_content +// * +// properties ::= TAG ANCHOR? | ANCHOR TAG? +// ************************* +// block_content ::= block_collection | flow_collection | SCALAR +// ****** +// flow_content ::= flow_collection | SCALAR +// ****** +func yaml_parser_parse_node(parser *yaml_parser_t, event *yaml_event_t, block, indentless_sequence bool) bool { + //defer trace("yaml_parser_parse_node", "block:", block, "indentless_sequence:", indentless_sequence)() + + token := peek_token(parser) + if token == nil { + return false + } + + if token.typ == yaml_ALIAS_TOKEN { + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + *event = yaml_event_t{ + typ: yaml_ALIAS_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + anchor: token.value, + } + yaml_parser_set_event_comments(parser, event) + skip_token(parser) + return true + } + + start_mark := token.start_mark + end_mark := token.start_mark + + var tag_token bool + var tag_handle, tag_suffix, anchor []byte + var tag_mark yaml_mark_t + if token.typ == yaml_ANCHOR_TOKEN { + anchor = token.value + start_mark = token.start_mark + end_mark = token.end_mark + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ == yaml_TAG_TOKEN { + tag_token = true + tag_handle = token.value + tag_suffix = token.suffix + tag_mark = token.start_mark + end_mark = token.end_mark + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + } + } else if token.typ == yaml_TAG_TOKEN { + tag_token = true + tag_handle = token.value + tag_suffix = token.suffix + start_mark = token.start_mark + tag_mark = token.start_mark + end_mark = token.end_mark + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ == yaml_ANCHOR_TOKEN { + anchor = token.value + end_mark = token.end_mark + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + } + } + + var tag []byte + if tag_token { + if len(tag_handle) == 0 { + tag = tag_suffix + tag_suffix = nil + } else { + for i := range parser.tag_directives { + if bytes.Equal(parser.tag_directives[i].handle, tag_handle) { + tag = append([]byte(nil), parser.tag_directives[i].prefix...) + tag = append(tag, tag_suffix...) + break + } + } + if len(tag) == 0 { + yaml_parser_set_parser_error_context(parser, + "while parsing a node", start_mark, + "found undefined tag handle", tag_mark) + return false + } + } + } + + implicit := len(tag) == 0 + if indentless_sequence && token.typ == yaml_BLOCK_ENTRY_TOKEN { + end_mark = token.end_mark + parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE + *event = yaml_event_t{ + typ: yaml_SEQUENCE_START_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE), + } + return true + } + if token.typ == yaml_SCALAR_TOKEN { + var plain_implicit, quoted_implicit bool + end_mark = token.end_mark + if (len(tag) == 0 && token.style == yaml_PLAIN_SCALAR_STYLE) || (len(tag) == 1 && tag[0] == '!') { + plain_implicit = true + } else if len(tag) == 0 { + quoted_implicit = true + } + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + + *event = yaml_event_t{ + typ: yaml_SCALAR_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + value: token.value, + implicit: plain_implicit, + quoted_implicit: quoted_implicit, + style: yaml_style_t(token.style), + } + yaml_parser_set_event_comments(parser, event) + skip_token(parser) + return true + } + if token.typ == yaml_FLOW_SEQUENCE_START_TOKEN { + // [Go] Some of the events below can be merged as they differ only on style. + end_mark = token.end_mark + parser.state = yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE + *event = yaml_event_t{ + typ: yaml_SEQUENCE_START_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_FLOW_SEQUENCE_STYLE), + } + yaml_parser_set_event_comments(parser, event) + return true + } + if token.typ == yaml_FLOW_MAPPING_START_TOKEN { + end_mark = token.end_mark + parser.state = yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE + *event = yaml_event_t{ + typ: yaml_MAPPING_START_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_FLOW_MAPPING_STYLE), + } + yaml_parser_set_event_comments(parser, event) + return true + } + if block && token.typ == yaml_BLOCK_SEQUENCE_START_TOKEN { + end_mark = token.end_mark + parser.state = yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE + *event = yaml_event_t{ + typ: yaml_SEQUENCE_START_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE), + } + if parser.stem_comment != nil { + event.head_comment = parser.stem_comment + parser.stem_comment = nil + } + return true + } + if block && token.typ == yaml_BLOCK_MAPPING_START_TOKEN { + end_mark = token.end_mark + parser.state = yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE + *event = yaml_event_t{ + typ: yaml_MAPPING_START_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_BLOCK_MAPPING_STYLE), + } + if parser.stem_comment != nil { + event.head_comment = parser.stem_comment + parser.stem_comment = nil + } + return true + } + if len(anchor) > 0 || len(tag) > 0 { + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + + *event = yaml_event_t{ + typ: yaml_SCALAR_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + quoted_implicit: false, + style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE), + } + return true + } + + context := "while parsing a flow node" + if block { + context = "while parsing a block node" + } + yaml_parser_set_parser_error_context(parser, context, start_mark, + "did not find expected node content", token.start_mark) + return false +} + +// Parse the productions: +// block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END +// ******************** *********** * ********* +// +func yaml_parser_parse_block_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { + if first { + token := peek_token(parser) + if token == nil { + return false + } + parser.marks = append(parser.marks, token.start_mark) + skip_token(parser) + } + + token := peek_token(parser) + if token == nil { + return false + } + + if token.typ == yaml_BLOCK_ENTRY_TOKEN { + mark := token.end_mark + prior_head_len := len(parser.head_comment) + skip_token(parser) + yaml_parser_split_stem_comment(parser, prior_head_len) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_BLOCK_ENTRY_TOKEN && token.typ != yaml_BLOCK_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE) + return yaml_parser_parse_node(parser, event, true, false) + } else { + parser.state = yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE + return yaml_parser_process_empty_scalar(parser, event, mark) + } + } + if token.typ == yaml_BLOCK_END_TOKEN { + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + + *event = yaml_event_t{ + typ: yaml_SEQUENCE_END_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + } + + skip_token(parser) + return true + } + + context_mark := parser.marks[len(parser.marks)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + return yaml_parser_set_parser_error_context(parser, + "while parsing a block collection", context_mark, + "did not find expected '-' indicator", token.start_mark) +} + +// Parse the productions: +// indentless_sequence ::= (BLOCK-ENTRY block_node?)+ +// *********** * +func yaml_parser_parse_indentless_sequence_entry(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + + if token.typ == yaml_BLOCK_ENTRY_TOKEN { + mark := token.end_mark + prior_head_len := len(parser.head_comment) + skip_token(parser) + yaml_parser_split_stem_comment(parser, prior_head_len) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_BLOCK_ENTRY_TOKEN && + token.typ != yaml_KEY_TOKEN && + token.typ != yaml_VALUE_TOKEN && + token.typ != yaml_BLOCK_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE) + return yaml_parser_parse_node(parser, event, true, false) + } + parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE + return yaml_parser_process_empty_scalar(parser, event, mark) + } + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + + *event = yaml_event_t{ + typ: yaml_SEQUENCE_END_EVENT, + start_mark: token.start_mark, + end_mark: token.start_mark, // [Go] Shouldn't this be token.end_mark? + } + return true +} + +// Split stem comment from head comment. +// +// When a sequence or map is found under a sequence entry, the former head comment +// is assigned to the underlying sequence or map as a whole, not the individual +// sequence or map entry as would be expected otherwise. To handle this case the +// previous head comment is moved aside as the stem comment. +func yaml_parser_split_stem_comment(parser *yaml_parser_t, stem_len int) { + if stem_len == 0 { + return + } + + token := peek_token(parser) + if token == nil || token.typ != yaml_BLOCK_SEQUENCE_START_TOKEN && token.typ != yaml_BLOCK_MAPPING_START_TOKEN { + return + } + + parser.stem_comment = parser.head_comment[:stem_len] + if len(parser.head_comment) == stem_len { + parser.head_comment = nil + } else { + // Copy suffix to prevent very strange bugs if someone ever appends + // further bytes to the prefix in the stem_comment slice above. + parser.head_comment = append([]byte(nil), parser.head_comment[stem_len+1:]...) + } +} + +// Parse the productions: +// block_mapping ::= BLOCK-MAPPING_START +// ******************* +// ((KEY block_node_or_indentless_sequence?)? +// *** * +// (VALUE block_node_or_indentless_sequence?)?)* +// +// BLOCK-END +// ********* +// +func yaml_parser_parse_block_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { + if first { + token := peek_token(parser) + if token == nil { + return false + } + parser.marks = append(parser.marks, token.start_mark) + skip_token(parser) + } + + token := peek_token(parser) + if token == nil { + return false + } + + // [Go] A tail comment was left from the prior mapping value processed. Emit an event + // as it needs to be processed with that value and not the following key. + if len(parser.tail_comment) > 0 { + *event = yaml_event_t{ + typ: yaml_TAIL_COMMENT_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + foot_comment: parser.tail_comment, + } + parser.tail_comment = nil + return true + } + + if token.typ == yaml_KEY_TOKEN { + mark := token.end_mark + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_KEY_TOKEN && + token.typ != yaml_VALUE_TOKEN && + token.typ != yaml_BLOCK_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE) + return yaml_parser_parse_node(parser, event, true, true) + } else { + parser.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE + return yaml_parser_process_empty_scalar(parser, event, mark) + } + } else if token.typ == yaml_BLOCK_END_TOKEN { + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + *event = yaml_event_t{ + typ: yaml_MAPPING_END_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + } + yaml_parser_set_event_comments(parser, event) + skip_token(parser) + return true + } + + context_mark := parser.marks[len(parser.marks)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + return yaml_parser_set_parser_error_context(parser, + "while parsing a block mapping", context_mark, + "did not find expected key", token.start_mark) +} + +// Parse the productions: +// block_mapping ::= BLOCK-MAPPING_START +// +// ((KEY block_node_or_indentless_sequence?)? +// +// (VALUE block_node_or_indentless_sequence?)?)* +// ***** * +// BLOCK-END +// +// +func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + if token.typ == yaml_VALUE_TOKEN { + mark := token.end_mark + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_KEY_TOKEN && + token.typ != yaml_VALUE_TOKEN && + token.typ != yaml_BLOCK_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE) + return yaml_parser_parse_node(parser, event, true, true) + } + parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE + return yaml_parser_process_empty_scalar(parser, event, mark) + } + parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE + return yaml_parser_process_empty_scalar(parser, event, token.start_mark) +} + +// Parse the productions: +// flow_sequence ::= FLOW-SEQUENCE-START +// ******************* +// (flow_sequence_entry FLOW-ENTRY)* +// * ********** +// flow_sequence_entry? +// * +// FLOW-SEQUENCE-END +// ***************** +// flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? +// * +// +func yaml_parser_parse_flow_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { + if first { + token := peek_token(parser) + if token == nil { + return false + } + parser.marks = append(parser.marks, token.start_mark) + skip_token(parser) + } + token := peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { + if !first { + if token.typ == yaml_FLOW_ENTRY_TOKEN { + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + } else { + context_mark := parser.marks[len(parser.marks)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + return yaml_parser_set_parser_error_context(parser, + "while parsing a flow sequence", context_mark, + "did not find expected ',' or ']'", token.start_mark) + } + } + + if token.typ == yaml_KEY_TOKEN { + parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE + *event = yaml_event_t{ + typ: yaml_MAPPING_START_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + implicit: true, + style: yaml_style_t(yaml_FLOW_MAPPING_STYLE), + } + skip_token(parser) + return true + } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE) + return yaml_parser_parse_node(parser, event, false, false) + } + } + + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + + *event = yaml_event_t{ + typ: yaml_SEQUENCE_END_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + } + yaml_parser_set_event_comments(parser, event) + + skip_token(parser) + return true +} + +// +// Parse the productions: +// flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? +// *** * +// +func yaml_parser_parse_flow_sequence_entry_mapping_key(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_VALUE_TOKEN && + token.typ != yaml_FLOW_ENTRY_TOKEN && + token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE) + return yaml_parser_parse_node(parser, event, false, false) + } + mark := token.end_mark + skip_token(parser) + parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE + return yaml_parser_process_empty_scalar(parser, event, mark) +} + +// Parse the productions: +// flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? +// ***** * +// +func yaml_parser_parse_flow_sequence_entry_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + if token.typ == yaml_VALUE_TOKEN { + skip_token(parser) + token := peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE) + return yaml_parser_parse_node(parser, event, false, false) + } + } + parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE + return yaml_parser_process_empty_scalar(parser, event, token.start_mark) +} + +// Parse the productions: +// flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? +// * +// +func yaml_parser_parse_flow_sequence_entry_mapping_end(parser *yaml_parser_t, event *yaml_event_t) bool { + token := peek_token(parser) + if token == nil { + return false + } + parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE + *event = yaml_event_t{ + typ: yaml_MAPPING_END_EVENT, + start_mark: token.start_mark, + end_mark: token.start_mark, // [Go] Shouldn't this be end_mark? + } + return true +} + +// Parse the productions: +// flow_mapping ::= FLOW-MAPPING-START +// ****************** +// (flow_mapping_entry FLOW-ENTRY)* +// * ********** +// flow_mapping_entry? +// ****************** +// FLOW-MAPPING-END +// **************** +// flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? +// * *** * +// +func yaml_parser_parse_flow_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { + if first { + token := peek_token(parser) + parser.marks = append(parser.marks, token.start_mark) + skip_token(parser) + } + + token := peek_token(parser) + if token == nil { + return false + } + + if token.typ != yaml_FLOW_MAPPING_END_TOKEN { + if !first { + if token.typ == yaml_FLOW_ENTRY_TOKEN { + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + } else { + context_mark := parser.marks[len(parser.marks)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + return yaml_parser_set_parser_error_context(parser, + "while parsing a flow mapping", context_mark, + "did not find expected ',' or '}'", token.start_mark) + } + } + + if token.typ == yaml_KEY_TOKEN { + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_VALUE_TOKEN && + token.typ != yaml_FLOW_ENTRY_TOKEN && + token.typ != yaml_FLOW_MAPPING_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE) + return yaml_parser_parse_node(parser, event, false, false) + } else { + parser.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE + return yaml_parser_process_empty_scalar(parser, event, token.start_mark) + } + } else if token.typ != yaml_FLOW_MAPPING_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE) + return yaml_parser_parse_node(parser, event, false, false) + } + } + + parser.state = parser.states[len(parser.states)-1] + parser.states = parser.states[:len(parser.states)-1] + parser.marks = parser.marks[:len(parser.marks)-1] + *event = yaml_event_t{ + typ: yaml_MAPPING_END_EVENT, + start_mark: token.start_mark, + end_mark: token.end_mark, + } + yaml_parser_set_event_comments(parser, event) + skip_token(parser) + return true +} + +// Parse the productions: +// flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? +// * ***** * +// +func yaml_parser_parse_flow_mapping_value(parser *yaml_parser_t, event *yaml_event_t, empty bool) bool { + token := peek_token(parser) + if token == nil { + return false + } + if empty { + parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE + return yaml_parser_process_empty_scalar(parser, event, token.start_mark) + } + if token.typ == yaml_VALUE_TOKEN { + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_MAPPING_END_TOKEN { + parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE) + return yaml_parser_parse_node(parser, event, false, false) + } + } + parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE + return yaml_parser_process_empty_scalar(parser, event, token.start_mark) +} + +// Generate an empty scalar event. +func yaml_parser_process_empty_scalar(parser *yaml_parser_t, event *yaml_event_t, mark yaml_mark_t) bool { + *event = yaml_event_t{ + typ: yaml_SCALAR_EVENT, + start_mark: mark, + end_mark: mark, + value: nil, // Empty + implicit: true, + style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE), + } + return true +} + +var default_tag_directives = []yaml_tag_directive_t{ + {[]byte("!"), []byte("!")}, + {[]byte("!!"), []byte("tag:yaml.org,2002:")}, +} + +// Parse directives. +func yaml_parser_process_directives(parser *yaml_parser_t, + version_directive_ref **yaml_version_directive_t, + tag_directives_ref *[]yaml_tag_directive_t) bool { + + var version_directive *yaml_version_directive_t + var tag_directives []yaml_tag_directive_t + + token := peek_token(parser) + if token == nil { + return false + } + + for token.typ == yaml_VERSION_DIRECTIVE_TOKEN || token.typ == yaml_TAG_DIRECTIVE_TOKEN { + if token.typ == yaml_VERSION_DIRECTIVE_TOKEN { + if version_directive != nil { + yaml_parser_set_parser_error(parser, + "found duplicate %YAML directive", token.start_mark) + return false + } + if token.major != 1 || token.minor != 1 { + yaml_parser_set_parser_error(parser, + "found incompatible YAML document", token.start_mark) + return false + } + version_directive = &yaml_version_directive_t{ + major: token.major, + minor: token.minor, + } + } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN { + value := yaml_tag_directive_t{ + handle: token.value, + prefix: token.prefix, + } + if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) { + return false + } + tag_directives = append(tag_directives, value) + } + + skip_token(parser) + token = peek_token(parser) + if token == nil { + return false + } + } + + for i := range default_tag_directives { + if !yaml_parser_append_tag_directive(parser, default_tag_directives[i], true, token.start_mark) { + return false + } + } + + if version_directive_ref != nil { + *version_directive_ref = version_directive + } + if tag_directives_ref != nil { + *tag_directives_ref = tag_directives + } + return true +} + +// Append a tag directive to the directives stack. +func yaml_parser_append_tag_directive(parser *yaml_parser_t, value yaml_tag_directive_t, allow_duplicates bool, mark yaml_mark_t) bool { + for i := range parser.tag_directives { + if bytes.Equal(value.handle, parser.tag_directives[i].handle) { + if allow_duplicates { + return true + } + return yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark) + } + } + + // [Go] I suspect the copy is unnecessary. This was likely done + // because there was no way to track ownership of the data. + value_copy := yaml_tag_directive_t{ + handle: make([]byte, len(value.handle)), + prefix: make([]byte, len(value.prefix)), + } + copy(value_copy.handle, value.handle) + copy(value_copy.prefix, value.prefix) + parser.tag_directives = append(parser.tag_directives, value_copy) + return true +} diff --git a/vendor/gopkg.in/yaml.v3/readerc.go b/vendor/gopkg.in/yaml.v3/readerc.go new file mode 100644 index 0000000000..b7de0a89c4 --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/readerc.go @@ -0,0 +1,434 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +import ( + "io" +) + +// Set the reader error and return 0. +func yaml_parser_set_reader_error(parser *yaml_parser_t, problem string, offset int, value int) bool { + parser.error = yaml_READER_ERROR + parser.problem = problem + parser.problem_offset = offset + parser.problem_value = value + return false +} + +// Byte order marks. +const ( + bom_UTF8 = "\xef\xbb\xbf" + bom_UTF16LE = "\xff\xfe" + bom_UTF16BE = "\xfe\xff" +) + +// Determine the input stream encoding by checking the BOM symbol. If no BOM is +// found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure. +func yaml_parser_determine_encoding(parser *yaml_parser_t) bool { + // Ensure that we had enough bytes in the raw buffer. + for !parser.eof && len(parser.raw_buffer)-parser.raw_buffer_pos < 3 { + if !yaml_parser_update_raw_buffer(parser) { + return false + } + } + + // Determine the encoding. + buf := parser.raw_buffer + pos := parser.raw_buffer_pos + avail := len(buf) - pos + if avail >= 2 && buf[pos] == bom_UTF16LE[0] && buf[pos+1] == bom_UTF16LE[1] { + parser.encoding = yaml_UTF16LE_ENCODING + parser.raw_buffer_pos += 2 + parser.offset += 2 + } else if avail >= 2 && buf[pos] == bom_UTF16BE[0] && buf[pos+1] == bom_UTF16BE[1] { + parser.encoding = yaml_UTF16BE_ENCODING + parser.raw_buffer_pos += 2 + parser.offset += 2 + } else if avail >= 3 && buf[pos] == bom_UTF8[0] && buf[pos+1] == bom_UTF8[1] && buf[pos+2] == bom_UTF8[2] { + parser.encoding = yaml_UTF8_ENCODING + parser.raw_buffer_pos += 3 + parser.offset += 3 + } else { + parser.encoding = yaml_UTF8_ENCODING + } + return true +} + +// Update the raw buffer. +func yaml_parser_update_raw_buffer(parser *yaml_parser_t) bool { + size_read := 0 + + // Return if the raw buffer is full. + if parser.raw_buffer_pos == 0 && len(parser.raw_buffer) == cap(parser.raw_buffer) { + return true + } + + // Return on EOF. + if parser.eof { + return true + } + + // Move the remaining bytes in the raw buffer to the beginning. + if parser.raw_buffer_pos > 0 && parser.raw_buffer_pos < len(parser.raw_buffer) { + copy(parser.raw_buffer, parser.raw_buffer[parser.raw_buffer_pos:]) + } + parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)-parser.raw_buffer_pos] + parser.raw_buffer_pos = 0 + + // Call the read handler to fill the buffer. + size_read, err := parser.read_handler(parser, parser.raw_buffer[len(parser.raw_buffer):cap(parser.raw_buffer)]) + parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)+size_read] + if err == io.EOF { + parser.eof = true + } else if err != nil { + return yaml_parser_set_reader_error(parser, "input error: "+err.Error(), parser.offset, -1) + } + return true +} + +// Ensure that the buffer contains at least `length` characters. +// Return true on success, false on failure. +// +// The length is supposed to be significantly less that the buffer size. +func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool { + if parser.read_handler == nil { + panic("read handler must be set") + } + + // [Go] This function was changed to guarantee the requested length size at EOF. + // The fact we need to do this is pretty awful, but the description above implies + // for that to be the case, and there are tests + + // If the EOF flag is set and the raw buffer is empty, do nothing. + if parser.eof && parser.raw_buffer_pos == len(parser.raw_buffer) { + // [Go] ACTUALLY! Read the documentation of this function above. + // This is just broken. To return true, we need to have the + // given length in the buffer. Not doing that means every single + // check that calls this function to make sure the buffer has a + // given length is Go) panicking; or C) accessing invalid memory. + //return true + } + + // Return if the buffer contains enough characters. + if parser.unread >= length { + return true + } + + // Determine the input encoding if it is not known yet. + if parser.encoding == yaml_ANY_ENCODING { + if !yaml_parser_determine_encoding(parser) { + return false + } + } + + // Move the unread characters to the beginning of the buffer. + buffer_len := len(parser.buffer) + if parser.buffer_pos > 0 && parser.buffer_pos < buffer_len { + copy(parser.buffer, parser.buffer[parser.buffer_pos:]) + buffer_len -= parser.buffer_pos + parser.buffer_pos = 0 + } else if parser.buffer_pos == buffer_len { + buffer_len = 0 + parser.buffer_pos = 0 + } + + // Open the whole buffer for writing, and cut it before returning. + parser.buffer = parser.buffer[:cap(parser.buffer)] + + // Fill the buffer until it has enough characters. + first := true + for parser.unread < length { + + // Fill the raw buffer if necessary. + if !first || parser.raw_buffer_pos == len(parser.raw_buffer) { + if !yaml_parser_update_raw_buffer(parser) { + parser.buffer = parser.buffer[:buffer_len] + return false + } + } + first = false + + // Decode the raw buffer. + inner: + for parser.raw_buffer_pos != len(parser.raw_buffer) { + var value rune + var width int + + raw_unread := len(parser.raw_buffer) - parser.raw_buffer_pos + + // Decode the next character. + switch parser.encoding { + case yaml_UTF8_ENCODING: + // Decode a UTF-8 character. Check RFC 3629 + // (http://www.ietf.org/rfc/rfc3629.txt) for more details. + // + // The following table (taken from the RFC) is used for + // decoding. + // + // Char. number range | UTF-8 octet sequence + // (hexadecimal) | (binary) + // --------------------+------------------------------------ + // 0000 0000-0000 007F | 0xxxxxxx + // 0000 0080-0000 07FF | 110xxxxx 10xxxxxx + // 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx + // 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + // + // Additionally, the characters in the range 0xD800-0xDFFF + // are prohibited as they are reserved for use with UTF-16 + // surrogate pairs. + + // Determine the length of the UTF-8 sequence. + octet := parser.raw_buffer[parser.raw_buffer_pos] + switch { + case octet&0x80 == 0x00: + width = 1 + case octet&0xE0 == 0xC0: + width = 2 + case octet&0xF0 == 0xE0: + width = 3 + case octet&0xF8 == 0xF0: + width = 4 + default: + // The leading octet is invalid. + return yaml_parser_set_reader_error(parser, + "invalid leading UTF-8 octet", + parser.offset, int(octet)) + } + + // Check if the raw buffer contains an incomplete character. + if width > raw_unread { + if parser.eof { + return yaml_parser_set_reader_error(parser, + "incomplete UTF-8 octet sequence", + parser.offset, -1) + } + break inner + } + + // Decode the leading octet. + switch { + case octet&0x80 == 0x00: + value = rune(octet & 0x7F) + case octet&0xE0 == 0xC0: + value = rune(octet & 0x1F) + case octet&0xF0 == 0xE0: + value = rune(octet & 0x0F) + case octet&0xF8 == 0xF0: + value = rune(octet & 0x07) + default: + value = 0 + } + + // Check and decode the trailing octets. + for k := 1; k < width; k++ { + octet = parser.raw_buffer[parser.raw_buffer_pos+k] + + // Check if the octet is valid. + if (octet & 0xC0) != 0x80 { + return yaml_parser_set_reader_error(parser, + "invalid trailing UTF-8 octet", + parser.offset+k, int(octet)) + } + + // Decode the octet. + value = (value << 6) + rune(octet&0x3F) + } + + // Check the length of the sequence against the value. + switch { + case width == 1: + case width == 2 && value >= 0x80: + case width == 3 && value >= 0x800: + case width == 4 && value >= 0x10000: + default: + return yaml_parser_set_reader_error(parser, + "invalid length of a UTF-8 sequence", + parser.offset, -1) + } + + // Check the range of the value. + if value >= 0xD800 && value <= 0xDFFF || value > 0x10FFFF { + return yaml_parser_set_reader_error(parser, + "invalid Unicode character", + parser.offset, int(value)) + } + + case yaml_UTF16LE_ENCODING, yaml_UTF16BE_ENCODING: + var low, high int + if parser.encoding == yaml_UTF16LE_ENCODING { + low, high = 0, 1 + } else { + low, high = 1, 0 + } + + // The UTF-16 encoding is not as simple as one might + // naively think. Check RFC 2781 + // (http://www.ietf.org/rfc/rfc2781.txt). + // + // Normally, two subsequent bytes describe a Unicode + // character. However a special technique (called a + // surrogate pair) is used for specifying character + // values larger than 0xFFFF. + // + // A surrogate pair consists of two pseudo-characters: + // high surrogate area (0xD800-0xDBFF) + // low surrogate area (0xDC00-0xDFFF) + // + // The following formulas are used for decoding + // and encoding characters using surrogate pairs: + // + // U = U' + 0x10000 (0x01 00 00 <= U <= 0x10 FF FF) + // U' = yyyyyyyyyyxxxxxxxxxx (0 <= U' <= 0x0F FF FF) + // W1 = 110110yyyyyyyyyy + // W2 = 110111xxxxxxxxxx + // + // where U is the character value, W1 is the high surrogate + // area, W2 is the low surrogate area. + + // Check for incomplete UTF-16 character. + if raw_unread < 2 { + if parser.eof { + return yaml_parser_set_reader_error(parser, + "incomplete UTF-16 character", + parser.offset, -1) + } + break inner + } + + // Get the character. + value = rune(parser.raw_buffer[parser.raw_buffer_pos+low]) + + (rune(parser.raw_buffer[parser.raw_buffer_pos+high]) << 8) + + // Check for unexpected low surrogate area. + if value&0xFC00 == 0xDC00 { + return yaml_parser_set_reader_error(parser, + "unexpected low surrogate area", + parser.offset, int(value)) + } + + // Check for a high surrogate area. + if value&0xFC00 == 0xD800 { + width = 4 + + // Check for incomplete surrogate pair. + if raw_unread < 4 { + if parser.eof { + return yaml_parser_set_reader_error(parser, + "incomplete UTF-16 surrogate pair", + parser.offset, -1) + } + break inner + } + + // Get the next character. + value2 := rune(parser.raw_buffer[parser.raw_buffer_pos+low+2]) + + (rune(parser.raw_buffer[parser.raw_buffer_pos+high+2]) << 8) + + // Check for a low surrogate area. + if value2&0xFC00 != 0xDC00 { + return yaml_parser_set_reader_error(parser, + "expected low surrogate area", + parser.offset+2, int(value2)) + } + + // Generate the value of the surrogate pair. + value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF) + } else { + width = 2 + } + + default: + panic("impossible") + } + + // Check if the character is in the allowed range: + // #x9 | #xA | #xD | [#x20-#x7E] (8 bit) + // | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] (16 bit) + // | [#x10000-#x10FFFF] (32 bit) + switch { + case value == 0x09: + case value == 0x0A: + case value == 0x0D: + case value >= 0x20 && value <= 0x7E: + case value == 0x85: + case value >= 0xA0 && value <= 0xD7FF: + case value >= 0xE000 && value <= 0xFFFD: + case value >= 0x10000 && value <= 0x10FFFF: + default: + return yaml_parser_set_reader_error(parser, + "control characters are not allowed", + parser.offset, int(value)) + } + + // Move the raw pointers. + parser.raw_buffer_pos += width + parser.offset += width + + // Finally put the character into the buffer. + if value <= 0x7F { + // 0000 0000-0000 007F . 0xxxxxxx + parser.buffer[buffer_len+0] = byte(value) + buffer_len += 1 + } else if value <= 0x7FF { + // 0000 0080-0000 07FF . 110xxxxx 10xxxxxx + parser.buffer[buffer_len+0] = byte(0xC0 + (value >> 6)) + parser.buffer[buffer_len+1] = byte(0x80 + (value & 0x3F)) + buffer_len += 2 + } else if value <= 0xFFFF { + // 0000 0800-0000 FFFF . 1110xxxx 10xxxxxx 10xxxxxx + parser.buffer[buffer_len+0] = byte(0xE0 + (value >> 12)) + parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 6) & 0x3F)) + parser.buffer[buffer_len+2] = byte(0x80 + (value & 0x3F)) + buffer_len += 3 + } else { + // 0001 0000-0010 FFFF . 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + parser.buffer[buffer_len+0] = byte(0xF0 + (value >> 18)) + parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 12) & 0x3F)) + parser.buffer[buffer_len+2] = byte(0x80 + ((value >> 6) & 0x3F)) + parser.buffer[buffer_len+3] = byte(0x80 + (value & 0x3F)) + buffer_len += 4 + } + + parser.unread++ + } + + // On EOF, put NUL into the buffer and return. + if parser.eof { + parser.buffer[buffer_len] = 0 + buffer_len++ + parser.unread++ + break + } + } + // [Go] Read the documentation of this function above. To return true, + // we need to have the given length in the buffer. Not doing that means + // every single check that calls this function to make sure the buffer + // has a given length is Go) panicking; or C) accessing invalid memory. + // This happens here due to the EOF above breaking early. + for buffer_len < length { + parser.buffer[buffer_len] = 0 + buffer_len++ + } + parser.buffer = parser.buffer[:buffer_len] + return true +} diff --git a/vendor/gopkg.in/yaml.v3/resolve.go b/vendor/gopkg.in/yaml.v3/resolve.go new file mode 100644 index 0000000000..64ae888057 --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/resolve.go @@ -0,0 +1,326 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// +// 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 yaml + +import ( + "encoding/base64" + "math" + "regexp" + "strconv" + "strings" + "time" +) + +type resolveMapItem struct { + value interface{} + tag string +} + +var resolveTable = make([]byte, 256) +var resolveMap = make(map[string]resolveMapItem) + +func init() { + t := resolveTable + t[int('+')] = 'S' // Sign + t[int('-')] = 'S' + for _, c := range "0123456789" { + t[int(c)] = 'D' // Digit + } + for _, c := range "yYnNtTfFoO~" { + t[int(c)] = 'M' // In map + } + t[int('.')] = '.' // Float (potentially in map) + + var resolveMapList = []struct { + v interface{} + tag string + l []string + }{ + {true, boolTag, []string{"true", "True", "TRUE"}}, + {false, boolTag, []string{"false", "False", "FALSE"}}, + {nil, nullTag, []string{"", "~", "null", "Null", "NULL"}}, + {math.NaN(), floatTag, []string{".nan", ".NaN", ".NAN"}}, + {math.Inf(+1), floatTag, []string{".inf", ".Inf", ".INF"}}, + {math.Inf(+1), floatTag, []string{"+.inf", "+.Inf", "+.INF"}}, + {math.Inf(-1), floatTag, []string{"-.inf", "-.Inf", "-.INF"}}, + {"<<", mergeTag, []string{"<<"}}, + } + + m := resolveMap + for _, item := range resolveMapList { + for _, s := range item.l { + m[s] = resolveMapItem{item.v, item.tag} + } + } +} + +const ( + nullTag = "!!null" + boolTag = "!!bool" + strTag = "!!str" + intTag = "!!int" + floatTag = "!!float" + timestampTag = "!!timestamp" + seqTag = "!!seq" + mapTag = "!!map" + binaryTag = "!!binary" + mergeTag = "!!merge" +) + +var longTags = make(map[string]string) +var shortTags = make(map[string]string) + +func init() { + for _, stag := range []string{nullTag, boolTag, strTag, intTag, floatTag, timestampTag, seqTag, mapTag, binaryTag, mergeTag} { + ltag := longTag(stag) + longTags[stag] = ltag + shortTags[ltag] = stag + } +} + +const longTagPrefix = "tag:yaml.org,2002:" + +func shortTag(tag string) string { + if strings.HasPrefix(tag, longTagPrefix) { + if stag, ok := shortTags[tag]; ok { + return stag + } + return "!!" + tag[len(longTagPrefix):] + } + return tag +} + +func longTag(tag string) string { + if strings.HasPrefix(tag, "!!") { + if ltag, ok := longTags[tag]; ok { + return ltag + } + return longTagPrefix + tag[2:] + } + return tag +} + +func resolvableTag(tag string) bool { + switch tag { + case "", strTag, boolTag, intTag, floatTag, nullTag, timestampTag: + return true + } + return false +} + +var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`) + +func resolve(tag string, in string) (rtag string, out interface{}) { + tag = shortTag(tag) + if !resolvableTag(tag) { + return tag, in + } + + defer func() { + switch tag { + case "", rtag, strTag, binaryTag: + return + case floatTag: + if rtag == intTag { + switch v := out.(type) { + case int64: + rtag = floatTag + out = float64(v) + return + case int: + rtag = floatTag + out = float64(v) + return + } + } + } + failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag)) + }() + + // Any data is accepted as a !!str or !!binary. + // Otherwise, the prefix is enough of a hint about what it might be. + hint := byte('N') + if in != "" { + hint = resolveTable[in[0]] + } + if hint != 0 && tag != strTag && tag != binaryTag { + // Handle things we can lookup in a map. + if item, ok := resolveMap[in]; ok { + return item.tag, item.value + } + + // Base 60 floats are a bad idea, were dropped in YAML 1.2, and + // are purposefully unsupported here. They're still quoted on + // the way out for compatibility with other parser, though. + + switch hint { + case 'M': + // We've already checked the map above. + + case '.': + // Not in the map, so maybe a normal float. + floatv, err := strconv.ParseFloat(in, 64) + if err == nil { + return floatTag, floatv + } + + case 'D', 'S': + // Int, float, or timestamp. + // Only try values as a timestamp if the value is unquoted or there's an explicit + // !!timestamp tag. + if tag == "" || tag == timestampTag { + t, ok := parseTimestamp(in) + if ok { + return timestampTag, t + } + } + + plain := strings.Replace(in, "_", "", -1) + intv, err := strconv.ParseInt(plain, 0, 64) + if err == nil { + if intv == int64(int(intv)) { + return intTag, int(intv) + } else { + return intTag, intv + } + } + uintv, err := strconv.ParseUint(plain, 0, 64) + if err == nil { + return intTag, uintv + } + if yamlStyleFloat.MatchString(plain) { + floatv, err := strconv.ParseFloat(plain, 64) + if err == nil { + return floatTag, floatv + } + } + if strings.HasPrefix(plain, "0b") { + intv, err := strconv.ParseInt(plain[2:], 2, 64) + if err == nil { + if intv == int64(int(intv)) { + return intTag, int(intv) + } else { + return intTag, intv + } + } + uintv, err := strconv.ParseUint(plain[2:], 2, 64) + if err == nil { + return intTag, uintv + } + } else if strings.HasPrefix(plain, "-0b") { + intv, err := strconv.ParseInt("-"+plain[3:], 2, 64) + if err == nil { + if true || intv == int64(int(intv)) { + return intTag, int(intv) + } else { + return intTag, intv + } + } + } + // Octals as introduced in version 1.2 of the spec. + // Octals from the 1.1 spec, spelled as 0777, are still + // decoded by default in v3 as well for compatibility. + // May be dropped in v4 depending on how usage evolves. + if strings.HasPrefix(plain, "0o") { + intv, err := strconv.ParseInt(plain[2:], 8, 64) + if err == nil { + if intv == int64(int(intv)) { + return intTag, int(intv) + } else { + return intTag, intv + } + } + uintv, err := strconv.ParseUint(plain[2:], 8, 64) + if err == nil { + return intTag, uintv + } + } else if strings.HasPrefix(plain, "-0o") { + intv, err := strconv.ParseInt("-"+plain[3:], 8, 64) + if err == nil { + if true || intv == int64(int(intv)) { + return intTag, int(intv) + } else { + return intTag, intv + } + } + } + default: + panic("internal error: missing handler for resolver table: " + string(rune(hint)) + " (with " + in + ")") + } + } + return strTag, in +} + +// encodeBase64 encodes s as base64 that is broken up into multiple lines +// as appropriate for the resulting length. +func encodeBase64(s string) string { + const lineLen = 70 + encLen := base64.StdEncoding.EncodedLen(len(s)) + lines := encLen/lineLen + 1 + buf := make([]byte, encLen*2+lines) + in := buf[0:encLen] + out := buf[encLen:] + base64.StdEncoding.Encode(in, []byte(s)) + k := 0 + for i := 0; i < len(in); i += lineLen { + j := i + lineLen + if j > len(in) { + j = len(in) + } + k += copy(out[k:], in[i:j]) + if lines > 1 { + out[k] = '\n' + k++ + } + } + return string(out[:k]) +} + +// This is a subset of the formats allowed by the regular expression +// defined at http://yaml.org/type/timestamp.html. +var allowedTimestampFormats = []string{ + "2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields. + "2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t". + "2006-1-2 15:4:5.999999999", // space separated with no time zone + "2006-1-2", // date only + // Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5" + // from the set of examples. +} + +// parseTimestamp parses s as a timestamp string and +// returns the timestamp and reports whether it succeeded. +// Timestamp formats are defined at http://yaml.org/type/timestamp.html +func parseTimestamp(s string) (time.Time, bool) { + // TODO write code to check all the formats supported by + // http://yaml.org/type/timestamp.html instead of using time.Parse. + + // Quick check: all date formats start with YYYY-. + i := 0 + for ; i < len(s); i++ { + if c := s[i]; c < '0' || c > '9' { + break + } + } + if i != 4 || i == len(s) || s[i] != '-' { + return time.Time{}, false + } + for _, format := range allowedTimestampFormats { + if t, err := time.Parse(format, s); err == nil { + return t, true + } + } + return time.Time{}, false +} diff --git a/vendor/gopkg.in/yaml.v3/scannerc.go b/vendor/gopkg.in/yaml.v3/scannerc.go new file mode 100644 index 0000000000..ca0070108f --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/scannerc.go @@ -0,0 +1,3038 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +import ( + "bytes" + "fmt" +) + +// Introduction +// ************ +// +// The following notes assume that you are familiar with the YAML specification +// (http://yaml.org/spec/1.2/spec.html). We mostly follow it, although in +// some cases we are less restrictive that it requires. +// +// The process of transforming a YAML stream into a sequence of events is +// divided on two steps: Scanning and Parsing. +// +// The Scanner transforms the input stream into a sequence of tokens, while the +// parser transform the sequence of tokens produced by the Scanner into a +// sequence of parsing events. +// +// The Scanner is rather clever and complicated. The Parser, on the contrary, +// is a straightforward implementation of a recursive-descendant parser (or, +// LL(1) parser, as it is usually called). +// +// Actually there are two issues of Scanning that might be called "clever", the +// rest is quite straightforward. The issues are "block collection start" and +// "simple keys". Both issues are explained below in details. +// +// Here the Scanning step is explained and implemented. We start with the list +// of all the tokens produced by the Scanner together with short descriptions. +// +// Now, tokens: +// +// STREAM-START(encoding) # The stream start. +// STREAM-END # The stream end. +// VERSION-DIRECTIVE(major,minor) # The '%YAML' directive. +// TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive. +// DOCUMENT-START # '---' +// DOCUMENT-END # '...' +// BLOCK-SEQUENCE-START # Indentation increase denoting a block +// BLOCK-MAPPING-START # sequence or a block mapping. +// BLOCK-END # Indentation decrease. +// FLOW-SEQUENCE-START # '[' +// FLOW-SEQUENCE-END # ']' +// BLOCK-SEQUENCE-START # '{' +// BLOCK-SEQUENCE-END # '}' +// BLOCK-ENTRY # '-' +// FLOW-ENTRY # ',' +// KEY # '?' or nothing (simple keys). +// VALUE # ':' +// ALIAS(anchor) # '*anchor' +// ANCHOR(anchor) # '&anchor' +// TAG(handle,suffix) # '!handle!suffix' +// SCALAR(value,style) # A scalar. +// +// The following two tokens are "virtual" tokens denoting the beginning and the +// end of the stream: +// +// STREAM-START(encoding) +// STREAM-END +// +// We pass the information about the input stream encoding with the +// STREAM-START token. +// +// The next two tokens are responsible for tags: +// +// VERSION-DIRECTIVE(major,minor) +// TAG-DIRECTIVE(handle,prefix) +// +// Example: +// +// %YAML 1.1 +// %TAG ! !foo +// %TAG !yaml! tag:yaml.org,2002: +// --- +// +// The correspoding sequence of tokens: +// +// STREAM-START(utf-8) +// VERSION-DIRECTIVE(1,1) +// TAG-DIRECTIVE("!","!foo") +// TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:") +// DOCUMENT-START +// STREAM-END +// +// Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole +// line. +// +// The document start and end indicators are represented by: +// +// DOCUMENT-START +// DOCUMENT-END +// +// Note that if a YAML stream contains an implicit document (without '---' +// and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be +// produced. +// +// In the following examples, we present whole documents together with the +// produced tokens. +// +// 1. An implicit document: +// +// 'a scalar' +// +// Tokens: +// +// STREAM-START(utf-8) +// SCALAR("a scalar",single-quoted) +// STREAM-END +// +// 2. An explicit document: +// +// --- +// 'a scalar' +// ... +// +// Tokens: +// +// STREAM-START(utf-8) +// DOCUMENT-START +// SCALAR("a scalar",single-quoted) +// DOCUMENT-END +// STREAM-END +// +// 3. Several documents in a stream: +// +// 'a scalar' +// --- +// 'another scalar' +// --- +// 'yet another scalar' +// +// Tokens: +// +// STREAM-START(utf-8) +// SCALAR("a scalar",single-quoted) +// DOCUMENT-START +// SCALAR("another scalar",single-quoted) +// DOCUMENT-START +// SCALAR("yet another scalar",single-quoted) +// STREAM-END +// +// We have already introduced the SCALAR token above. The following tokens are +// used to describe aliases, anchors, tag, and scalars: +// +// ALIAS(anchor) +// ANCHOR(anchor) +// TAG(handle,suffix) +// SCALAR(value,style) +// +// The following series of examples illustrate the usage of these tokens: +// +// 1. A recursive sequence: +// +// &A [ *A ] +// +// Tokens: +// +// STREAM-START(utf-8) +// ANCHOR("A") +// FLOW-SEQUENCE-START +// ALIAS("A") +// FLOW-SEQUENCE-END +// STREAM-END +// +// 2. A tagged scalar: +// +// !!float "3.14" # A good approximation. +// +// Tokens: +// +// STREAM-START(utf-8) +// TAG("!!","float") +// SCALAR("3.14",double-quoted) +// STREAM-END +// +// 3. Various scalar styles: +// +// --- # Implicit empty plain scalars do not produce tokens. +// --- a plain scalar +// --- 'a single-quoted scalar' +// --- "a double-quoted scalar" +// --- |- +// a literal scalar +// --- >- +// a folded +// scalar +// +// Tokens: +// +// STREAM-START(utf-8) +// DOCUMENT-START +// DOCUMENT-START +// SCALAR("a plain scalar",plain) +// DOCUMENT-START +// SCALAR("a single-quoted scalar",single-quoted) +// DOCUMENT-START +// SCALAR("a double-quoted scalar",double-quoted) +// DOCUMENT-START +// SCALAR("a literal scalar",literal) +// DOCUMENT-START +// SCALAR("a folded scalar",folded) +// STREAM-END +// +// Now it's time to review collection-related tokens. We will start with +// flow collections: +// +// FLOW-SEQUENCE-START +// FLOW-SEQUENCE-END +// FLOW-MAPPING-START +// FLOW-MAPPING-END +// FLOW-ENTRY +// KEY +// VALUE +// +// The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and +// FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}' +// correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the +// indicators '?' and ':', which are used for denoting mapping keys and values, +// are represented by the KEY and VALUE tokens. +// +// The following examples show flow collections: +// +// 1. A flow sequence: +// +// [item 1, item 2, item 3] +// +// Tokens: +// +// STREAM-START(utf-8) +// FLOW-SEQUENCE-START +// SCALAR("item 1",plain) +// FLOW-ENTRY +// SCALAR("item 2",plain) +// FLOW-ENTRY +// SCALAR("item 3",plain) +// FLOW-SEQUENCE-END +// STREAM-END +// +// 2. A flow mapping: +// +// { +// a simple key: a value, # Note that the KEY token is produced. +// ? a complex key: another value, +// } +// +// Tokens: +// +// STREAM-START(utf-8) +// FLOW-MAPPING-START +// KEY +// SCALAR("a simple key",plain) +// VALUE +// SCALAR("a value",plain) +// FLOW-ENTRY +// KEY +// SCALAR("a complex key",plain) +// VALUE +// SCALAR("another value",plain) +// FLOW-ENTRY +// FLOW-MAPPING-END +// STREAM-END +// +// A simple key is a key which is not denoted by the '?' indicator. Note that +// the Scanner still produce the KEY token whenever it encounters a simple key. +// +// For scanning block collections, the following tokens are used (note that we +// repeat KEY and VALUE here): +// +// BLOCK-SEQUENCE-START +// BLOCK-MAPPING-START +// BLOCK-END +// BLOCK-ENTRY +// KEY +// VALUE +// +// The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation +// increase that precedes a block collection (cf. the INDENT token in Python). +// The token BLOCK-END denote indentation decrease that ends a block collection +// (cf. the DEDENT token in Python). However YAML has some syntax pecularities +// that makes detections of these tokens more complex. +// +// The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators +// '-', '?', and ':' correspondingly. +// +// The following examples show how the tokens BLOCK-SEQUENCE-START, +// BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner: +// +// 1. Block sequences: +// +// - item 1 +// - item 2 +// - +// - item 3.1 +// - item 3.2 +// - +// key 1: value 1 +// key 2: value 2 +// +// Tokens: +// +// STREAM-START(utf-8) +// BLOCK-SEQUENCE-START +// BLOCK-ENTRY +// SCALAR("item 1",plain) +// BLOCK-ENTRY +// SCALAR("item 2",plain) +// BLOCK-ENTRY +// BLOCK-SEQUENCE-START +// BLOCK-ENTRY +// SCALAR("item 3.1",plain) +// BLOCK-ENTRY +// SCALAR("item 3.2",plain) +// BLOCK-END +// BLOCK-ENTRY +// BLOCK-MAPPING-START +// KEY +// SCALAR("key 1",plain) +// VALUE +// SCALAR("value 1",plain) +// KEY +// SCALAR("key 2",plain) +// VALUE +// SCALAR("value 2",plain) +// BLOCK-END +// BLOCK-END +// STREAM-END +// +// 2. Block mappings: +// +// a simple key: a value # The KEY token is produced here. +// ? a complex key +// : another value +// a mapping: +// key 1: value 1 +// key 2: value 2 +// a sequence: +// - item 1 +// - item 2 +// +// Tokens: +// +// STREAM-START(utf-8) +// BLOCK-MAPPING-START +// KEY +// SCALAR("a simple key",plain) +// VALUE +// SCALAR("a value",plain) +// KEY +// SCALAR("a complex key",plain) +// VALUE +// SCALAR("another value",plain) +// KEY +// SCALAR("a mapping",plain) +// BLOCK-MAPPING-START +// KEY +// SCALAR("key 1",plain) +// VALUE +// SCALAR("value 1",plain) +// KEY +// SCALAR("key 2",plain) +// VALUE +// SCALAR("value 2",plain) +// BLOCK-END +// KEY +// SCALAR("a sequence",plain) +// VALUE +// BLOCK-SEQUENCE-START +// BLOCK-ENTRY +// SCALAR("item 1",plain) +// BLOCK-ENTRY +// SCALAR("item 2",plain) +// BLOCK-END +// BLOCK-END +// STREAM-END +// +// YAML does not always require to start a new block collection from a new +// line. If the current line contains only '-', '?', and ':' indicators, a new +// block collection may start at the current line. The following examples +// illustrate this case: +// +// 1. Collections in a sequence: +// +// - - item 1 +// - item 2 +// - key 1: value 1 +// key 2: value 2 +// - ? complex key +// : complex value +// +// Tokens: +// +// STREAM-START(utf-8) +// BLOCK-SEQUENCE-START +// BLOCK-ENTRY +// BLOCK-SEQUENCE-START +// BLOCK-ENTRY +// SCALAR("item 1",plain) +// BLOCK-ENTRY +// SCALAR("item 2",plain) +// BLOCK-END +// BLOCK-ENTRY +// BLOCK-MAPPING-START +// KEY +// SCALAR("key 1",plain) +// VALUE +// SCALAR("value 1",plain) +// KEY +// SCALAR("key 2",plain) +// VALUE +// SCALAR("value 2",plain) +// BLOCK-END +// BLOCK-ENTRY +// BLOCK-MAPPING-START +// KEY +// SCALAR("complex key") +// VALUE +// SCALAR("complex value") +// BLOCK-END +// BLOCK-END +// STREAM-END +// +// 2. Collections in a mapping: +// +// ? a sequence +// : - item 1 +// - item 2 +// ? a mapping +// : key 1: value 1 +// key 2: value 2 +// +// Tokens: +// +// STREAM-START(utf-8) +// BLOCK-MAPPING-START +// KEY +// SCALAR("a sequence",plain) +// VALUE +// BLOCK-SEQUENCE-START +// BLOCK-ENTRY +// SCALAR("item 1",plain) +// BLOCK-ENTRY +// SCALAR("item 2",plain) +// BLOCK-END +// KEY +// SCALAR("a mapping",plain) +// VALUE +// BLOCK-MAPPING-START +// KEY +// SCALAR("key 1",plain) +// VALUE +// SCALAR("value 1",plain) +// KEY +// SCALAR("key 2",plain) +// VALUE +// SCALAR("value 2",plain) +// BLOCK-END +// BLOCK-END +// STREAM-END +// +// YAML also permits non-indented sequences if they are included into a block +// mapping. In this case, the token BLOCK-SEQUENCE-START is not produced: +// +// key: +// - item 1 # BLOCK-SEQUENCE-START is NOT produced here. +// - item 2 +// +// Tokens: +// +// STREAM-START(utf-8) +// BLOCK-MAPPING-START +// KEY +// SCALAR("key",plain) +// VALUE +// BLOCK-ENTRY +// SCALAR("item 1",plain) +// BLOCK-ENTRY +// SCALAR("item 2",plain) +// BLOCK-END +// + +// Ensure that the buffer contains the required number of characters. +// Return true on success, false on failure (reader error or memory error). +func cache(parser *yaml_parser_t, length int) bool { + // [Go] This was inlined: !cache(A, B) -> unread < B && !update(A, B) + return parser.unread >= length || yaml_parser_update_buffer(parser, length) +} + +// Advance the buffer pointer. +func skip(parser *yaml_parser_t) { + if !is_blank(parser.buffer, parser.buffer_pos) { + parser.newlines = 0 + } + parser.mark.index++ + parser.mark.column++ + parser.unread-- + parser.buffer_pos += width(parser.buffer[parser.buffer_pos]) +} + +func skip_line(parser *yaml_parser_t) { + if is_crlf(parser.buffer, parser.buffer_pos) { + parser.mark.index += 2 + parser.mark.column = 0 + parser.mark.line++ + parser.unread -= 2 + parser.buffer_pos += 2 + parser.newlines++ + } else if is_break(parser.buffer, parser.buffer_pos) { + parser.mark.index++ + parser.mark.column = 0 + parser.mark.line++ + parser.unread-- + parser.buffer_pos += width(parser.buffer[parser.buffer_pos]) + parser.newlines++ + } +} + +// Copy a character to a string buffer and advance pointers. +func read(parser *yaml_parser_t, s []byte) []byte { + if !is_blank(parser.buffer, parser.buffer_pos) { + parser.newlines = 0 + } + w := width(parser.buffer[parser.buffer_pos]) + if w == 0 { + panic("invalid character sequence") + } + if len(s) == 0 { + s = make([]byte, 0, 32) + } + if w == 1 && len(s)+w <= cap(s) { + s = s[:len(s)+1] + s[len(s)-1] = parser.buffer[parser.buffer_pos] + parser.buffer_pos++ + } else { + s = append(s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...) + parser.buffer_pos += w + } + parser.mark.index++ + parser.mark.column++ + parser.unread-- + return s +} + +// Copy a line break character to a string buffer and advance pointers. +func read_line(parser *yaml_parser_t, s []byte) []byte { + buf := parser.buffer + pos := parser.buffer_pos + switch { + case buf[pos] == '\r' && buf[pos+1] == '\n': + // CR LF . LF + s = append(s, '\n') + parser.buffer_pos += 2 + parser.mark.index++ + parser.unread-- + case buf[pos] == '\r' || buf[pos] == '\n': + // CR|LF . LF + s = append(s, '\n') + parser.buffer_pos += 1 + case buf[pos] == '\xC2' && buf[pos+1] == '\x85': + // NEL . LF + s = append(s, '\n') + parser.buffer_pos += 2 + case buf[pos] == '\xE2' && buf[pos+1] == '\x80' && (buf[pos+2] == '\xA8' || buf[pos+2] == '\xA9'): + // LS|PS . LS|PS + s = append(s, buf[parser.buffer_pos:pos+3]...) + parser.buffer_pos += 3 + default: + return s + } + parser.mark.index++ + parser.mark.column = 0 + parser.mark.line++ + parser.unread-- + parser.newlines++ + return s +} + +// Get the next token. +func yaml_parser_scan(parser *yaml_parser_t, token *yaml_token_t) bool { + // Erase the token object. + *token = yaml_token_t{} // [Go] Is this necessary? + + // No tokens after STREAM-END or error. + if parser.stream_end_produced || parser.error != yaml_NO_ERROR { + return true + } + + // Ensure that the tokens queue contains enough tokens. + if !parser.token_available { + if !yaml_parser_fetch_more_tokens(parser) { + return false + } + } + + // Fetch the next token from the queue. + *token = parser.tokens[parser.tokens_head] + parser.tokens_head++ + parser.tokens_parsed++ + parser.token_available = false + + if token.typ == yaml_STREAM_END_TOKEN { + parser.stream_end_produced = true + } + return true +} + +// Set the scanner error and return false. +func yaml_parser_set_scanner_error(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string) bool { + parser.error = yaml_SCANNER_ERROR + parser.context = context + parser.context_mark = context_mark + parser.problem = problem + parser.problem_mark = parser.mark + return false +} + +func yaml_parser_set_scanner_tag_error(parser *yaml_parser_t, directive bool, context_mark yaml_mark_t, problem string) bool { + context := "while parsing a tag" + if directive { + context = "while parsing a %TAG directive" + } + return yaml_parser_set_scanner_error(parser, context, context_mark, problem) +} + +func trace(args ...interface{}) func() { + pargs := append([]interface{}{"+++"}, args...) + fmt.Println(pargs...) + pargs = append([]interface{}{"---"}, args...) + return func() { fmt.Println(pargs...) } +} + +// Ensure that the tokens queue contains at least one token which can be +// returned to the Parser. +func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool { + // While we need more tokens to fetch, do it. + for { + // [Go] The comment parsing logic requires a lookahead of two tokens + // so that foot comments may be parsed in time of associating them + // with the tokens that are parsed before them, and also for line + // comments to be transformed into head comments in some edge cases. + if parser.tokens_head < len(parser.tokens)-2 { + // If a potential simple key is at the head position, we need to fetch + // the next token to disambiguate it. + head_tok_idx, ok := parser.simple_keys_by_tok[parser.tokens_parsed] + if !ok { + break + } else if valid, ok := yaml_simple_key_is_valid(parser, &parser.simple_keys[head_tok_idx]); !ok { + return false + } else if !valid { + break + } + } + // Fetch the next token. + if !yaml_parser_fetch_next_token(parser) { + return false + } + } + + parser.token_available = true + return true +} + +// The dispatcher for token fetchers. +func yaml_parser_fetch_next_token(parser *yaml_parser_t) (ok bool) { + // Ensure that the buffer is initialized. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + // Check if we just started scanning. Fetch STREAM-START then. + if !parser.stream_start_produced { + return yaml_parser_fetch_stream_start(parser) + } + + scan_mark := parser.mark + + // Eat whitespaces and comments until we reach the next token. + if !yaml_parser_scan_to_next_token(parser) { + return false + } + + // [Go] While unrolling indents, transform the head comments of prior + // indentation levels observed after scan_start into foot comments at + // the respective indexes. + + // Check the indentation level against the current column. + if !yaml_parser_unroll_indent(parser, parser.mark.column, scan_mark) { + return false + } + + // Ensure that the buffer contains at least 4 characters. 4 is the length + // of the longest indicators ('--- ' and '... '). + if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) { + return false + } + + // Is it the end of the stream? + if is_z(parser.buffer, parser.buffer_pos) { + return yaml_parser_fetch_stream_end(parser) + } + + // Is it a directive? + if parser.mark.column == 0 && parser.buffer[parser.buffer_pos] == '%' { + return yaml_parser_fetch_directive(parser) + } + + buf := parser.buffer + pos := parser.buffer_pos + + // Is it the document start indicator? + if parser.mark.column == 0 && buf[pos] == '-' && buf[pos+1] == '-' && buf[pos+2] == '-' && is_blankz(buf, pos+3) { + return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_START_TOKEN) + } + + // Is it the document end indicator? + if parser.mark.column == 0 && buf[pos] == '.' && buf[pos+1] == '.' && buf[pos+2] == '.' && is_blankz(buf, pos+3) { + return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_END_TOKEN) + } + + comment_mark := parser.mark + if len(parser.tokens) > 0 && (parser.flow_level == 0 && buf[pos] == ':' || parser.flow_level > 0 && buf[pos] == ',') { + // Associate any following comments with the prior token. + comment_mark = parser.tokens[len(parser.tokens)-1].start_mark + } + defer func() { + if !ok { + return + } + if len(parser.tokens) > 0 && parser.tokens[len(parser.tokens)-1].typ == yaml_BLOCK_ENTRY_TOKEN { + // Sequence indicators alone have no line comments. It becomes + // a head comment for whatever follows. + return + } + if !yaml_parser_scan_line_comment(parser, comment_mark) { + ok = false + return + } + }() + + // Is it the flow sequence start indicator? + if buf[pos] == '[' { + return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_SEQUENCE_START_TOKEN) + } + + // Is it the flow mapping start indicator? + if parser.buffer[parser.buffer_pos] == '{' { + return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_MAPPING_START_TOKEN) + } + + // Is it the flow sequence end indicator? + if parser.buffer[parser.buffer_pos] == ']' { + return yaml_parser_fetch_flow_collection_end(parser, + yaml_FLOW_SEQUENCE_END_TOKEN) + } + + // Is it the flow mapping end indicator? + if parser.buffer[parser.buffer_pos] == '}' { + return yaml_parser_fetch_flow_collection_end(parser, + yaml_FLOW_MAPPING_END_TOKEN) + } + + // Is it the flow entry indicator? + if parser.buffer[parser.buffer_pos] == ',' { + return yaml_parser_fetch_flow_entry(parser) + } + + // Is it the block entry indicator? + if parser.buffer[parser.buffer_pos] == '-' && is_blankz(parser.buffer, parser.buffer_pos+1) { + return yaml_parser_fetch_block_entry(parser) + } + + // Is it the key indicator? + if parser.buffer[parser.buffer_pos] == '?' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) { + return yaml_parser_fetch_key(parser) + } + + // Is it the value indicator? + if parser.buffer[parser.buffer_pos] == ':' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) { + return yaml_parser_fetch_value(parser) + } + + // Is it an alias? + if parser.buffer[parser.buffer_pos] == '*' { + return yaml_parser_fetch_anchor(parser, yaml_ALIAS_TOKEN) + } + + // Is it an anchor? + if parser.buffer[parser.buffer_pos] == '&' { + return yaml_parser_fetch_anchor(parser, yaml_ANCHOR_TOKEN) + } + + // Is it a tag? + if parser.buffer[parser.buffer_pos] == '!' { + return yaml_parser_fetch_tag(parser) + } + + // Is it a literal scalar? + if parser.buffer[parser.buffer_pos] == '|' && parser.flow_level == 0 { + return yaml_parser_fetch_block_scalar(parser, true) + } + + // Is it a folded scalar? + if parser.buffer[parser.buffer_pos] == '>' && parser.flow_level == 0 { + return yaml_parser_fetch_block_scalar(parser, false) + } + + // Is it a single-quoted scalar? + if parser.buffer[parser.buffer_pos] == '\'' { + return yaml_parser_fetch_flow_scalar(parser, true) + } + + // Is it a double-quoted scalar? + if parser.buffer[parser.buffer_pos] == '"' { + return yaml_parser_fetch_flow_scalar(parser, false) + } + + // Is it a plain scalar? + // + // A plain scalar may start with any non-blank characters except + // + // '-', '?', ':', ',', '[', ']', '{', '}', + // '#', '&', '*', '!', '|', '>', '\'', '\"', + // '%', '@', '`'. + // + // In the block context (and, for the '-' indicator, in the flow context + // too), it may also start with the characters + // + // '-', '?', ':' + // + // if it is followed by a non-space character. + // + // The last rule is more restrictive than the specification requires. + // [Go] TODO Make this logic more reasonable. + //switch parser.buffer[parser.buffer_pos] { + //case '-', '?', ':', ',', '?', '-', ',', ':', ']', '[', '}', '{', '&', '#', '!', '*', '>', '|', '"', '\'', '@', '%', '-', '`': + //} + if !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '-' || + parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':' || + parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '[' || + parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' || + parser.buffer[parser.buffer_pos] == '}' || parser.buffer[parser.buffer_pos] == '#' || + parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '*' || + parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '|' || + parser.buffer[parser.buffer_pos] == '>' || parser.buffer[parser.buffer_pos] == '\'' || + parser.buffer[parser.buffer_pos] == '"' || parser.buffer[parser.buffer_pos] == '%' || + parser.buffer[parser.buffer_pos] == '@' || parser.buffer[parser.buffer_pos] == '`') || + (parser.buffer[parser.buffer_pos] == '-' && !is_blank(parser.buffer, parser.buffer_pos+1)) || + (parser.flow_level == 0 && + (parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':') && + !is_blankz(parser.buffer, parser.buffer_pos+1)) { + return yaml_parser_fetch_plain_scalar(parser) + } + + // If we don't determine the token type so far, it is an error. + return yaml_parser_set_scanner_error(parser, + "while scanning for the next token", parser.mark, + "found character that cannot start any token") +} + +func yaml_simple_key_is_valid(parser *yaml_parser_t, simple_key *yaml_simple_key_t) (valid, ok bool) { + if !simple_key.possible { + return false, true + } + + // The 1.2 specification says: + // + // "If the ? indicator is omitted, parsing needs to see past the + // implicit key to recognize it as such. To limit the amount of + // lookahead required, the “:” indicator must appear at most 1024 + // Unicode characters beyond the start of the key. In addition, the key + // is restricted to a single line." + // + if simple_key.mark.line < parser.mark.line || simple_key.mark.index+1024 < parser.mark.index { + // Check if the potential simple key to be removed is required. + if simple_key.required { + return false, yaml_parser_set_scanner_error(parser, + "while scanning a simple key", simple_key.mark, + "could not find expected ':'") + } + simple_key.possible = false + return false, true + } + return true, true +} + +// Check if a simple key may start at the current position and add it if +// needed. +func yaml_parser_save_simple_key(parser *yaml_parser_t) bool { + // A simple key is required at the current position if the scanner is in + // the block context and the current column coincides with the indentation + // level. + + required := parser.flow_level == 0 && parser.indent == parser.mark.column + + // + // If the current position may start a simple key, save it. + // + if parser.simple_key_allowed { + simple_key := yaml_simple_key_t{ + possible: true, + required: required, + token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head), + mark: parser.mark, + } + + if !yaml_parser_remove_simple_key(parser) { + return false + } + parser.simple_keys[len(parser.simple_keys)-1] = simple_key + parser.simple_keys_by_tok[simple_key.token_number] = len(parser.simple_keys) - 1 + } + return true +} + +// Remove a potential simple key at the current flow level. +func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool { + i := len(parser.simple_keys) - 1 + if parser.simple_keys[i].possible { + // If the key is required, it is an error. + if parser.simple_keys[i].required { + return yaml_parser_set_scanner_error(parser, + "while scanning a simple key", parser.simple_keys[i].mark, + "could not find expected ':'") + } + // Remove the key from the stack. + parser.simple_keys[i].possible = false + delete(parser.simple_keys_by_tok, parser.simple_keys[i].token_number) + } + return true +} + +// max_flow_level limits the flow_level +const max_flow_level = 10000 + +// Increase the flow level and resize the simple key list if needed. +func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool { + // Reset the simple key on the next level. + parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{ + possible: false, + required: false, + token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head), + mark: parser.mark, + }) + + // Increase the flow level. + parser.flow_level++ + if parser.flow_level > max_flow_level { + return yaml_parser_set_scanner_error(parser, + "while increasing flow level", parser.simple_keys[len(parser.simple_keys)-1].mark, + fmt.Sprintf("exceeded max depth of %d", max_flow_level)) + } + return true +} + +// Decrease the flow level. +func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool { + if parser.flow_level > 0 { + parser.flow_level-- + last := len(parser.simple_keys) - 1 + delete(parser.simple_keys_by_tok, parser.simple_keys[last].token_number) + parser.simple_keys = parser.simple_keys[:last] + } + return true +} + +// max_indents limits the indents stack size +const max_indents = 10000 + +// Push the current indentation level to the stack and set the new level +// the current column is greater than the indentation level. In this case, +// append or insert the specified token into the token queue. +func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml_token_type_t, mark yaml_mark_t) bool { + // In the flow context, do nothing. + if parser.flow_level > 0 { + return true + } + + if parser.indent < column { + // Push the current indentation level to the stack and set the new + // indentation level. + parser.indents = append(parser.indents, parser.indent) + parser.indent = column + if len(parser.indents) > max_indents { + return yaml_parser_set_scanner_error(parser, + "while increasing indent level", parser.simple_keys[len(parser.simple_keys)-1].mark, + fmt.Sprintf("exceeded max depth of %d", max_indents)) + } + + // Create a token and insert it into the queue. + token := yaml_token_t{ + typ: typ, + start_mark: mark, + end_mark: mark, + } + if number > -1 { + number -= parser.tokens_parsed + } + yaml_insert_token(parser, number, &token) + } + return true +} + +// Pop indentation levels from the indents stack until the current level +// becomes less or equal to the column. For each indentation level, append +// the BLOCK-END token. +func yaml_parser_unroll_indent(parser *yaml_parser_t, column int, scan_mark yaml_mark_t) bool { + // In the flow context, do nothing. + if parser.flow_level > 0 { + return true + } + + block_mark := scan_mark + block_mark.index-- + + // Loop through the indentation levels in the stack. + for parser.indent > column { + + // [Go] Reposition the end token before potential following + // foot comments of parent blocks. For that, search + // backwards for recent comments that were at the same + // indent as the block that is ending now. + stop_index := block_mark.index + for i := len(parser.comments) - 1; i >= 0; i-- { + comment := &parser.comments[i] + + if comment.end_mark.index < stop_index { + // Don't go back beyond the start of the comment/whitespace scan, unless column < 0. + // If requested indent column is < 0, then the document is over and everything else + // is a foot anyway. + break + } + if comment.start_mark.column == parser.indent+1 { + // This is a good match. But maybe there's a former comment + // at that same indent level, so keep searching. + block_mark = comment.start_mark + } + + // While the end of the former comment matches with + // the start of the following one, we know there's + // nothing in between and scanning is still safe. + stop_index = comment.scan_mark.index + } + + // Create a token and append it to the queue. + token := yaml_token_t{ + typ: yaml_BLOCK_END_TOKEN, + start_mark: block_mark, + end_mark: block_mark, + } + yaml_insert_token(parser, -1, &token) + + // Pop the indentation level. + parser.indent = parser.indents[len(parser.indents)-1] + parser.indents = parser.indents[:len(parser.indents)-1] + } + return true +} + +// Initialize the scanner and produce the STREAM-START token. +func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool { + + // Set the initial indentation. + parser.indent = -1 + + // Initialize the simple key stack. + parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{}) + + parser.simple_keys_by_tok = make(map[int]int) + + // A simple key is allowed at the beginning of the stream. + parser.simple_key_allowed = true + + // We have started. + parser.stream_start_produced = true + + // Create the STREAM-START token and append it to the queue. + token := yaml_token_t{ + typ: yaml_STREAM_START_TOKEN, + start_mark: parser.mark, + end_mark: parser.mark, + encoding: parser.encoding, + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the STREAM-END token and shut down the scanner. +func yaml_parser_fetch_stream_end(parser *yaml_parser_t) bool { + + // Force new line. + if parser.mark.column != 0 { + parser.mark.column = 0 + parser.mark.line++ + } + + // Reset the indentation level. + if !yaml_parser_unroll_indent(parser, -1, parser.mark) { + return false + } + + // Reset simple keys. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + parser.simple_key_allowed = false + + // Create the STREAM-END token and append it to the queue. + token := yaml_token_t{ + typ: yaml_STREAM_END_TOKEN, + start_mark: parser.mark, + end_mark: parser.mark, + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token. +func yaml_parser_fetch_directive(parser *yaml_parser_t) bool { + // Reset the indentation level. + if !yaml_parser_unroll_indent(parser, -1, parser.mark) { + return false + } + + // Reset simple keys. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + parser.simple_key_allowed = false + + // Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. + token := yaml_token_t{} + if !yaml_parser_scan_directive(parser, &token) { + return false + } + // Append the token to the queue. + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the DOCUMENT-START or DOCUMENT-END token. +func yaml_parser_fetch_document_indicator(parser *yaml_parser_t, typ yaml_token_type_t) bool { + // Reset the indentation level. + if !yaml_parser_unroll_indent(parser, -1, parser.mark) { + return false + } + + // Reset simple keys. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + parser.simple_key_allowed = false + + // Consume the token. + start_mark := parser.mark + + skip(parser) + skip(parser) + skip(parser) + + end_mark := parser.mark + + // Create the DOCUMENT-START or DOCUMENT-END token. + token := yaml_token_t{ + typ: typ, + start_mark: start_mark, + end_mark: end_mark, + } + // Append the token to the queue. + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token. +func yaml_parser_fetch_flow_collection_start(parser *yaml_parser_t, typ yaml_token_type_t) bool { + + // The indicators '[' and '{' may start a simple key. + if !yaml_parser_save_simple_key(parser) { + return false + } + + // Increase the flow level. + if !yaml_parser_increase_flow_level(parser) { + return false + } + + // A simple key may follow the indicators '[' and '{'. + parser.simple_key_allowed = true + + // Consume the token. + start_mark := parser.mark + skip(parser) + end_mark := parser.mark + + // Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. + token := yaml_token_t{ + typ: typ, + start_mark: start_mark, + end_mark: end_mark, + } + // Append the token to the queue. + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token. +func yaml_parser_fetch_flow_collection_end(parser *yaml_parser_t, typ yaml_token_type_t) bool { + // Reset any potential simple key on the current flow level. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + // Decrease the flow level. + if !yaml_parser_decrease_flow_level(parser) { + return false + } + + // No simple keys after the indicators ']' and '}'. + parser.simple_key_allowed = false + + // Consume the token. + + start_mark := parser.mark + skip(parser) + end_mark := parser.mark + + // Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. + token := yaml_token_t{ + typ: typ, + start_mark: start_mark, + end_mark: end_mark, + } + // Append the token to the queue. + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the FLOW-ENTRY token. +func yaml_parser_fetch_flow_entry(parser *yaml_parser_t) bool { + // Reset any potential simple keys on the current flow level. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + // Simple keys are allowed after ','. + parser.simple_key_allowed = true + + // Consume the token. + start_mark := parser.mark + skip(parser) + end_mark := parser.mark + + // Create the FLOW-ENTRY token and append it to the queue. + token := yaml_token_t{ + typ: yaml_FLOW_ENTRY_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the BLOCK-ENTRY token. +func yaml_parser_fetch_block_entry(parser *yaml_parser_t) bool { + // Check if the scanner is in the block context. + if parser.flow_level == 0 { + // Check if we are allowed to start a new entry. + if !parser.simple_key_allowed { + return yaml_parser_set_scanner_error(parser, "", parser.mark, + "block sequence entries are not allowed in this context") + } + // Add the BLOCK-SEQUENCE-START token if needed. + if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_SEQUENCE_START_TOKEN, parser.mark) { + return false + } + } else { + // It is an error for the '-' indicator to occur in the flow context, + // but we let the Parser detect and report about it because the Parser + // is able to point to the context. + } + + // Reset any potential simple keys on the current flow level. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + // Simple keys are allowed after '-'. + parser.simple_key_allowed = true + + // Consume the token. + start_mark := parser.mark + skip(parser) + end_mark := parser.mark + + // Create the BLOCK-ENTRY token and append it to the queue. + token := yaml_token_t{ + typ: yaml_BLOCK_ENTRY_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the KEY token. +func yaml_parser_fetch_key(parser *yaml_parser_t) bool { + + // In the block context, additional checks are required. + if parser.flow_level == 0 { + // Check if we are allowed to start a new key (not nessesary simple). + if !parser.simple_key_allowed { + return yaml_parser_set_scanner_error(parser, "", parser.mark, + "mapping keys are not allowed in this context") + } + // Add the BLOCK-MAPPING-START token if needed. + if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) { + return false + } + } + + // Reset any potential simple keys on the current flow level. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + // Simple keys are allowed after '?' in the block context. + parser.simple_key_allowed = parser.flow_level == 0 + + // Consume the token. + start_mark := parser.mark + skip(parser) + end_mark := parser.mark + + // Create the KEY token and append it to the queue. + token := yaml_token_t{ + typ: yaml_KEY_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the VALUE token. +func yaml_parser_fetch_value(parser *yaml_parser_t) bool { + + simple_key := &parser.simple_keys[len(parser.simple_keys)-1] + + // Have we found a simple key? + if valid, ok := yaml_simple_key_is_valid(parser, simple_key); !ok { + return false + + } else if valid { + + // Create the KEY token and insert it into the queue. + token := yaml_token_t{ + typ: yaml_KEY_TOKEN, + start_mark: simple_key.mark, + end_mark: simple_key.mark, + } + yaml_insert_token(parser, simple_key.token_number-parser.tokens_parsed, &token) + + // In the block context, we may need to add the BLOCK-MAPPING-START token. + if !yaml_parser_roll_indent(parser, simple_key.mark.column, + simple_key.token_number, + yaml_BLOCK_MAPPING_START_TOKEN, simple_key.mark) { + return false + } + + // Remove the simple key. + simple_key.possible = false + delete(parser.simple_keys_by_tok, simple_key.token_number) + + // A simple key cannot follow another simple key. + parser.simple_key_allowed = false + + } else { + // The ':' indicator follows a complex key. + + // In the block context, extra checks are required. + if parser.flow_level == 0 { + + // Check if we are allowed to start a complex value. + if !parser.simple_key_allowed { + return yaml_parser_set_scanner_error(parser, "", parser.mark, + "mapping values are not allowed in this context") + } + + // Add the BLOCK-MAPPING-START token if needed. + if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) { + return false + } + } + + // Simple keys after ':' are allowed in the block context. + parser.simple_key_allowed = parser.flow_level == 0 + } + + // Consume the token. + start_mark := parser.mark + skip(parser) + end_mark := parser.mark + + // Create the VALUE token and append it to the queue. + token := yaml_token_t{ + typ: yaml_VALUE_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the ALIAS or ANCHOR token. +func yaml_parser_fetch_anchor(parser *yaml_parser_t, typ yaml_token_type_t) bool { + // An anchor or an alias could be a simple key. + if !yaml_parser_save_simple_key(parser) { + return false + } + + // A simple key cannot follow an anchor or an alias. + parser.simple_key_allowed = false + + // Create the ALIAS or ANCHOR token and append it to the queue. + var token yaml_token_t + if !yaml_parser_scan_anchor(parser, &token, typ) { + return false + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the TAG token. +func yaml_parser_fetch_tag(parser *yaml_parser_t) bool { + // A tag could be a simple key. + if !yaml_parser_save_simple_key(parser) { + return false + } + + // A simple key cannot follow a tag. + parser.simple_key_allowed = false + + // Create the TAG token and append it to the queue. + var token yaml_token_t + if !yaml_parser_scan_tag(parser, &token) { + return false + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens. +func yaml_parser_fetch_block_scalar(parser *yaml_parser_t, literal bool) bool { + // Remove any potential simple keys. + if !yaml_parser_remove_simple_key(parser) { + return false + } + + // A simple key may follow a block scalar. + parser.simple_key_allowed = true + + // Create the SCALAR token and append it to the queue. + var token yaml_token_t + if !yaml_parser_scan_block_scalar(parser, &token, literal) { + return false + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens. +func yaml_parser_fetch_flow_scalar(parser *yaml_parser_t, single bool) bool { + // A plain scalar could be a simple key. + if !yaml_parser_save_simple_key(parser) { + return false + } + + // A simple key cannot follow a flow scalar. + parser.simple_key_allowed = false + + // Create the SCALAR token and append it to the queue. + var token yaml_token_t + if !yaml_parser_scan_flow_scalar(parser, &token, single) { + return false + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Produce the SCALAR(...,plain) token. +func yaml_parser_fetch_plain_scalar(parser *yaml_parser_t) bool { + // A plain scalar could be a simple key. + if !yaml_parser_save_simple_key(parser) { + return false + } + + // A simple key cannot follow a flow scalar. + parser.simple_key_allowed = false + + // Create the SCALAR token and append it to the queue. + var token yaml_token_t + if !yaml_parser_scan_plain_scalar(parser, &token) { + return false + } + yaml_insert_token(parser, -1, &token) + return true +} + +// Eat whitespaces and comments until the next token is found. +func yaml_parser_scan_to_next_token(parser *yaml_parser_t) bool { + + scan_mark := parser.mark + + // Until the next token is not found. + for { + // Allow the BOM mark to start a line. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if parser.mark.column == 0 && is_bom(parser.buffer, parser.buffer_pos) { + skip(parser) + } + + // Eat whitespaces. + // Tabs are allowed: + // - in the flow context + // - in the block context, but not at the beginning of the line or + // after '-', '?', or ':' (complex value). + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + for parser.buffer[parser.buffer_pos] == ' ' || ((parser.flow_level > 0 || !parser.simple_key_allowed) && parser.buffer[parser.buffer_pos] == '\t') { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Check if we just had a line comment under a sequence entry that + // looks more like a header to the following content. Similar to this: + // + // - # The comment + // - Some data + // + // If so, transform the line comment to a head comment and reposition. + if len(parser.comments) > 0 && len(parser.tokens) > 1 { + tokenA := parser.tokens[len(parser.tokens)-2] + tokenB := parser.tokens[len(parser.tokens)-1] + comment := &parser.comments[len(parser.comments)-1] + if tokenA.typ == yaml_BLOCK_SEQUENCE_START_TOKEN && tokenB.typ == yaml_BLOCK_ENTRY_TOKEN && len(comment.line) > 0 && !is_break(parser.buffer, parser.buffer_pos) { + // If it was in the prior line, reposition so it becomes a + // header of the follow up token. Otherwise, keep it in place + // so it becomes a header of the former. + comment.head = comment.line + comment.line = nil + if comment.start_mark.line == parser.mark.line-1 { + comment.token_mark = parser.mark + } + } + } + + // Eat a comment until a line break. + if parser.buffer[parser.buffer_pos] == '#' { + if !yaml_parser_scan_comments(parser, scan_mark) { + return false + } + } + + // If it is a line break, eat it. + if is_break(parser.buffer, parser.buffer_pos) { + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + skip_line(parser) + + // In the block context, a new line may start a simple key. + if parser.flow_level == 0 { + parser.simple_key_allowed = true + } + } else { + break // We have found a token. + } + } + + return true +} + +// Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token. +// +// Scope: +// %YAML 1.1 # a comment \n +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// %TAG !yaml! tag:yaml.org,2002: \n +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// +func yaml_parser_scan_directive(parser *yaml_parser_t, token *yaml_token_t) bool { + // Eat '%'. + start_mark := parser.mark + skip(parser) + + // Scan the directive name. + var name []byte + if !yaml_parser_scan_directive_name(parser, start_mark, &name) { + return false + } + + // Is it a YAML directive? + if bytes.Equal(name, []byte("YAML")) { + // Scan the VERSION directive value. + var major, minor int8 + if !yaml_parser_scan_version_directive_value(parser, start_mark, &major, &minor) { + return false + } + end_mark := parser.mark + + // Create a VERSION-DIRECTIVE token. + *token = yaml_token_t{ + typ: yaml_VERSION_DIRECTIVE_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + major: major, + minor: minor, + } + + // Is it a TAG directive? + } else if bytes.Equal(name, []byte("TAG")) { + // Scan the TAG directive value. + var handle, prefix []byte + if !yaml_parser_scan_tag_directive_value(parser, start_mark, &handle, &prefix) { + return false + } + end_mark := parser.mark + + // Create a TAG-DIRECTIVE token. + *token = yaml_token_t{ + typ: yaml_TAG_DIRECTIVE_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + value: handle, + prefix: prefix, + } + + // Unknown directive. + } else { + yaml_parser_set_scanner_error(parser, "while scanning a directive", + start_mark, "found unknown directive name") + return false + } + + // Eat the rest of the line including any comments. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + for is_blank(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + if parser.buffer[parser.buffer_pos] == '#' { + // [Go] Discard this inline comment for the time being. + //if !yaml_parser_scan_line_comment(parser, start_mark) { + // return false + //} + for !is_breakz(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + } + + // Check if we are at the end of the line. + if !is_breakz(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a directive", + start_mark, "did not find expected comment or line break") + return false + } + + // Eat a line break. + if is_break(parser.buffer, parser.buffer_pos) { + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + skip_line(parser) + } + + return true +} + +// Scan the directive name. +// +// Scope: +// %YAML 1.1 # a comment \n +// ^^^^ +// %TAG !yaml! tag:yaml.org,2002: \n +// ^^^ +// +func yaml_parser_scan_directive_name(parser *yaml_parser_t, start_mark yaml_mark_t, name *[]byte) bool { + // Consume the directive name. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + var s []byte + for is_alpha(parser.buffer, parser.buffer_pos) { + s = read(parser, s) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Check if the name is empty. + if len(s) == 0 { + yaml_parser_set_scanner_error(parser, "while scanning a directive", + start_mark, "could not find expected directive name") + return false + } + + // Check for an blank character after the name. + if !is_blankz(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a directive", + start_mark, "found unexpected non-alphabetical character") + return false + } + *name = s + return true +} + +// Scan the value of VERSION-DIRECTIVE. +// +// Scope: +// %YAML 1.1 # a comment \n +// ^^^^^^ +func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int8) bool { + // Eat whitespaces. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + for is_blank(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Consume the major version number. + if !yaml_parser_scan_version_directive_number(parser, start_mark, major) { + return false + } + + // Eat '.'. + if parser.buffer[parser.buffer_pos] != '.' { + return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", + start_mark, "did not find expected digit or '.' character") + } + + skip(parser) + + // Consume the minor version number. + if !yaml_parser_scan_version_directive_number(parser, start_mark, minor) { + return false + } + return true +} + +const max_number_length = 2 + +// Scan the version number of VERSION-DIRECTIVE. +// +// Scope: +// %YAML 1.1 # a comment \n +// ^ +// %YAML 1.1 # a comment \n +// ^ +func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int8) bool { + + // Repeat while the next character is digit. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + var value, length int8 + for is_digit(parser.buffer, parser.buffer_pos) { + // Check if the number is too long. + length++ + if length > max_number_length { + return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", + start_mark, "found extremely long version number") + } + value = value*10 + int8(as_digit(parser.buffer, parser.buffer_pos)) + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Check if the number was present. + if length == 0 { + return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", + start_mark, "did not find expected version number") + } + *number = value + return true +} + +// Scan the value of a TAG-DIRECTIVE token. +// +// Scope: +// %TAG !yaml! tag:yaml.org,2002: \n +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// +func yaml_parser_scan_tag_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, handle, prefix *[]byte) bool { + var handle_value, prefix_value []byte + + // Eat whitespaces. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + for is_blank(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Scan a handle. + if !yaml_parser_scan_tag_handle(parser, true, start_mark, &handle_value) { + return false + } + + // Expect a whitespace. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if !is_blank(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive", + start_mark, "did not find expected whitespace") + return false + } + + // Eat whitespaces. + for is_blank(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Scan a prefix. + if !yaml_parser_scan_tag_uri(parser, true, nil, start_mark, &prefix_value) { + return false + } + + // Expect a whitespace or line break. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if !is_blankz(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive", + start_mark, "did not find expected whitespace or line break") + return false + } + + *handle = handle_value + *prefix = prefix_value + return true +} + +func yaml_parser_scan_anchor(parser *yaml_parser_t, token *yaml_token_t, typ yaml_token_type_t) bool { + var s []byte + + // Eat the indicator character. + start_mark := parser.mark + skip(parser) + + // Consume the value. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + for is_alpha(parser.buffer, parser.buffer_pos) { + s = read(parser, s) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + end_mark := parser.mark + + /* + * Check if length of the anchor is greater than 0 and it is followed by + * a whitespace character or one of the indicators: + * + * '?', ':', ',', ']', '}', '%', '@', '`'. + */ + + if len(s) == 0 || + !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '?' || + parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == ',' || + parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '}' || + parser.buffer[parser.buffer_pos] == '%' || parser.buffer[parser.buffer_pos] == '@' || + parser.buffer[parser.buffer_pos] == '`') { + context := "while scanning an alias" + if typ == yaml_ANCHOR_TOKEN { + context = "while scanning an anchor" + } + yaml_parser_set_scanner_error(parser, context, start_mark, + "did not find expected alphabetic or numeric character") + return false + } + + // Create a token. + *token = yaml_token_t{ + typ: typ, + start_mark: start_mark, + end_mark: end_mark, + value: s, + } + + return true +} + +/* + * Scan a TAG token. + */ + +func yaml_parser_scan_tag(parser *yaml_parser_t, token *yaml_token_t) bool { + var handle, suffix []byte + + start_mark := parser.mark + + // Check if the tag is in the canonical form. + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + + if parser.buffer[parser.buffer_pos+1] == '<' { + // Keep the handle as '' + + // Eat '!<' + skip(parser) + skip(parser) + + // Consume the tag value. + if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) { + return false + } + + // Check for '>' and eat it. + if parser.buffer[parser.buffer_pos] != '>' { + yaml_parser_set_scanner_error(parser, "while scanning a tag", + start_mark, "did not find the expected '>'") + return false + } + + skip(parser) + } else { + // The tag has either the '!suffix' or the '!handle!suffix' form. + + // First, try to scan a handle. + if !yaml_parser_scan_tag_handle(parser, false, start_mark, &handle) { + return false + } + + // Check if it is, indeed, handle. + if handle[0] == '!' && len(handle) > 1 && handle[len(handle)-1] == '!' { + // Scan the suffix now. + if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) { + return false + } + } else { + // It wasn't a handle after all. Scan the rest of the tag. + if !yaml_parser_scan_tag_uri(parser, false, handle, start_mark, &suffix) { + return false + } + + // Set the handle to '!'. + handle = []byte{'!'} + + // A special case: the '!' tag. Set the handle to '' and the + // suffix to '!'. + if len(suffix) == 0 { + handle, suffix = suffix, handle + } + } + } + + // Check the character which ends the tag. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if !is_blankz(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a tag", + start_mark, "did not find expected whitespace or line break") + return false + } + + end_mark := parser.mark + + // Create a token. + *token = yaml_token_t{ + typ: yaml_TAG_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + value: handle, + suffix: suffix, + } + return true +} + +// Scan a tag handle. +func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, handle *[]byte) bool { + // Check the initial '!' character. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if parser.buffer[parser.buffer_pos] != '!' { + yaml_parser_set_scanner_tag_error(parser, directive, + start_mark, "did not find expected '!'") + return false + } + + var s []byte + + // Copy the '!' character. + s = read(parser, s) + + // Copy all subsequent alphabetical and numerical characters. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + for is_alpha(parser.buffer, parser.buffer_pos) { + s = read(parser, s) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Check if the trailing character is '!' and copy it. + if parser.buffer[parser.buffer_pos] == '!' { + s = read(parser, s) + } else { + // It's either the '!' tag or not really a tag handle. If it's a %TAG + // directive, it's an error. If it's a tag token, it must be a part of URI. + if directive && string(s) != "!" { + yaml_parser_set_scanner_tag_error(parser, directive, + start_mark, "did not find expected '!'") + return false + } + } + + *handle = s + return true +} + +// Scan a tag. +func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte, start_mark yaml_mark_t, uri *[]byte) bool { + //size_t length = head ? strlen((char *)head) : 0 + var s []byte + hasTag := len(head) > 0 + + // Copy the head if needed. + // + // Note that we don't copy the leading '!' character. + if len(head) > 1 { + s = append(s, head[1:]...) + } + + // Scan the tag. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + // The set of characters that may appear in URI is as follows: + // + // '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&', + // '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']', + // '%'. + // [Go] TODO Convert this into more reasonable logic. + for is_alpha(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == ';' || + parser.buffer[parser.buffer_pos] == '/' || parser.buffer[parser.buffer_pos] == '?' || + parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == '@' || + parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '=' || + parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '$' || + parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '.' || + parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '~' || + parser.buffer[parser.buffer_pos] == '*' || parser.buffer[parser.buffer_pos] == '\'' || + parser.buffer[parser.buffer_pos] == '(' || parser.buffer[parser.buffer_pos] == ')' || + parser.buffer[parser.buffer_pos] == '[' || parser.buffer[parser.buffer_pos] == ']' || + parser.buffer[parser.buffer_pos] == '%' { + // Check if it is a URI-escape sequence. + if parser.buffer[parser.buffer_pos] == '%' { + if !yaml_parser_scan_uri_escapes(parser, directive, start_mark, &s) { + return false + } + } else { + s = read(parser, s) + } + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + hasTag = true + } + + if !hasTag { + yaml_parser_set_scanner_tag_error(parser, directive, + start_mark, "did not find expected tag URI") + return false + } + *uri = s + return true +} + +// Decode an URI-escape sequence corresponding to a single UTF-8 character. +func yaml_parser_scan_uri_escapes(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, s *[]byte) bool { + + // Decode the required number of characters. + w := 1024 + for w > 0 { + // Check for a URI-escaped octet. + if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) { + return false + } + + if !(parser.buffer[parser.buffer_pos] == '%' && + is_hex(parser.buffer, parser.buffer_pos+1) && + is_hex(parser.buffer, parser.buffer_pos+2)) { + return yaml_parser_set_scanner_tag_error(parser, directive, + start_mark, "did not find URI escaped octet") + } + + // Get the octet. + octet := byte((as_hex(parser.buffer, parser.buffer_pos+1) << 4) + as_hex(parser.buffer, parser.buffer_pos+2)) + + // If it is the leading octet, determine the length of the UTF-8 sequence. + if w == 1024 { + w = width(octet) + if w == 0 { + return yaml_parser_set_scanner_tag_error(parser, directive, + start_mark, "found an incorrect leading UTF-8 octet") + } + } else { + // Check if the trailing octet is correct. + if octet&0xC0 != 0x80 { + return yaml_parser_set_scanner_tag_error(parser, directive, + start_mark, "found an incorrect trailing UTF-8 octet") + } + } + + // Copy the octet and move the pointers. + *s = append(*s, octet) + skip(parser) + skip(parser) + skip(parser) + w-- + } + return true +} + +// Scan a block scalar. +func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, literal bool) bool { + // Eat the indicator '|' or '>'. + start_mark := parser.mark + skip(parser) + + // Scan the additional block scalar indicators. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + // Check for a chomping indicator. + var chomping, increment int + if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' { + // Set the chomping method and eat the indicator. + if parser.buffer[parser.buffer_pos] == '+' { + chomping = +1 + } else { + chomping = -1 + } + skip(parser) + + // Check for an indentation indicator. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if is_digit(parser.buffer, parser.buffer_pos) { + // Check that the indentation is greater than 0. + if parser.buffer[parser.buffer_pos] == '0' { + yaml_parser_set_scanner_error(parser, "while scanning a block scalar", + start_mark, "found an indentation indicator equal to 0") + return false + } + + // Get the indentation level and eat the indicator. + increment = as_digit(parser.buffer, parser.buffer_pos) + skip(parser) + } + + } else if is_digit(parser.buffer, parser.buffer_pos) { + // Do the same as above, but in the opposite order. + + if parser.buffer[parser.buffer_pos] == '0' { + yaml_parser_set_scanner_error(parser, "while scanning a block scalar", + start_mark, "found an indentation indicator equal to 0") + return false + } + increment = as_digit(parser.buffer, parser.buffer_pos) + skip(parser) + + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' { + if parser.buffer[parser.buffer_pos] == '+' { + chomping = +1 + } else { + chomping = -1 + } + skip(parser) + } + } + + // Eat whitespaces and comments to the end of the line. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + for is_blank(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + if parser.buffer[parser.buffer_pos] == '#' { + if !yaml_parser_scan_line_comment(parser, start_mark) { + return false + } + for !is_breakz(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + } + + // Check if we are at the end of the line. + if !is_breakz(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a block scalar", + start_mark, "did not find expected comment or line break") + return false + } + + // Eat a line break. + if is_break(parser.buffer, parser.buffer_pos) { + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + skip_line(parser) + } + + end_mark := parser.mark + + // Set the indentation level if it was specified. + var indent int + if increment > 0 { + if parser.indent >= 0 { + indent = parser.indent + increment + } else { + indent = increment + } + } + + // Scan the leading line breaks and determine the indentation level if needed. + var s, leading_break, trailing_breaks []byte + if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) { + return false + } + + // Scan the block scalar content. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + var leading_blank, trailing_blank bool + for parser.mark.column == indent && !is_z(parser.buffer, parser.buffer_pos) { + // We are at the beginning of a non-empty line. + + // Is it a trailing whitespace? + trailing_blank = is_blank(parser.buffer, parser.buffer_pos) + + // Check if we need to fold the leading line break. + if !literal && !leading_blank && !trailing_blank && len(leading_break) > 0 && leading_break[0] == '\n' { + // Do we need to join the lines by space? + if len(trailing_breaks) == 0 { + s = append(s, ' ') + } + } else { + s = append(s, leading_break...) + } + leading_break = leading_break[:0] + + // Append the remaining line breaks. + s = append(s, trailing_breaks...) + trailing_breaks = trailing_breaks[:0] + + // Is it a leading whitespace? + leading_blank = is_blank(parser.buffer, parser.buffer_pos) + + // Consume the current line. + for !is_breakz(parser.buffer, parser.buffer_pos) { + s = read(parser, s) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Consume the line break. + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + + leading_break = read_line(parser, leading_break) + + // Eat the following indentation spaces and line breaks. + if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) { + return false + } + } + + // Chomp the tail. + if chomping != -1 { + s = append(s, leading_break...) + } + if chomping == 1 { + s = append(s, trailing_breaks...) + } + + // Create a token. + *token = yaml_token_t{ + typ: yaml_SCALAR_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + value: s, + style: yaml_LITERAL_SCALAR_STYLE, + } + if !literal { + token.style = yaml_FOLDED_SCALAR_STYLE + } + return true +} + +// Scan indentation spaces and line breaks for a block scalar. Determine the +// indentation level if needed. +func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, breaks *[]byte, start_mark yaml_mark_t, end_mark *yaml_mark_t) bool { + *end_mark = parser.mark + + // Eat the indentation spaces and line breaks. + max_indent := 0 + for { + // Eat the indentation spaces. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + for (*indent == 0 || parser.mark.column < *indent) && is_space(parser.buffer, parser.buffer_pos) { + skip(parser) + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + if parser.mark.column > max_indent { + max_indent = parser.mark.column + } + + // Check for a tab character messing the indentation. + if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) { + return yaml_parser_set_scanner_error(parser, "while scanning a block scalar", + start_mark, "found a tab character where an indentation space is expected") + } + + // Have we found a non-empty line? + if !is_break(parser.buffer, parser.buffer_pos) { + break + } + + // Consume the line break. + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + // [Go] Should really be returning breaks instead. + *breaks = read_line(parser, *breaks) + *end_mark = parser.mark + } + + // Determine the indentation level if needed. + if *indent == 0 { + *indent = max_indent + if *indent < parser.indent+1 { + *indent = parser.indent + 1 + } + if *indent < 1 { + *indent = 1 + } + } + return true +} + +// Scan a quoted scalar. +func yaml_parser_scan_flow_scalar(parser *yaml_parser_t, token *yaml_token_t, single bool) bool { + // Eat the left quote. + start_mark := parser.mark + skip(parser) + + // Consume the content of the quoted scalar. + var s, leading_break, trailing_breaks, whitespaces []byte + for { + // Check that there are no document indicators at the beginning of the line. + if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) { + return false + } + + if parser.mark.column == 0 && + ((parser.buffer[parser.buffer_pos+0] == '-' && + parser.buffer[parser.buffer_pos+1] == '-' && + parser.buffer[parser.buffer_pos+2] == '-') || + (parser.buffer[parser.buffer_pos+0] == '.' && + parser.buffer[parser.buffer_pos+1] == '.' && + parser.buffer[parser.buffer_pos+2] == '.')) && + is_blankz(parser.buffer, parser.buffer_pos+3) { + yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar", + start_mark, "found unexpected document indicator") + return false + } + + // Check for EOF. + if is_z(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar", + start_mark, "found unexpected end of stream") + return false + } + + // Consume non-blank characters. + leading_blanks := false + for !is_blankz(parser.buffer, parser.buffer_pos) { + if single && parser.buffer[parser.buffer_pos] == '\'' && parser.buffer[parser.buffer_pos+1] == '\'' { + // Is is an escaped single quote. + s = append(s, '\'') + skip(parser) + skip(parser) + + } else if single && parser.buffer[parser.buffer_pos] == '\'' { + // It is a right single quote. + break + } else if !single && parser.buffer[parser.buffer_pos] == '"' { + // It is a right double quote. + break + + } else if !single && parser.buffer[parser.buffer_pos] == '\\' && is_break(parser.buffer, parser.buffer_pos+1) { + // It is an escaped line break. + if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) { + return false + } + skip(parser) + skip_line(parser) + leading_blanks = true + break + + } else if !single && parser.buffer[parser.buffer_pos] == '\\' { + // It is an escape sequence. + code_length := 0 + + // Check the escape character. + switch parser.buffer[parser.buffer_pos+1] { + case '0': + s = append(s, 0) + case 'a': + s = append(s, '\x07') + case 'b': + s = append(s, '\x08') + case 't', '\t': + s = append(s, '\x09') + case 'n': + s = append(s, '\x0A') + case 'v': + s = append(s, '\x0B') + case 'f': + s = append(s, '\x0C') + case 'r': + s = append(s, '\x0D') + case 'e': + s = append(s, '\x1B') + case ' ': + s = append(s, '\x20') + case '"': + s = append(s, '"') + case '\'': + s = append(s, '\'') + case '\\': + s = append(s, '\\') + case 'N': // NEL (#x85) + s = append(s, '\xC2') + s = append(s, '\x85') + case '_': // #xA0 + s = append(s, '\xC2') + s = append(s, '\xA0') + case 'L': // LS (#x2028) + s = append(s, '\xE2') + s = append(s, '\x80') + s = append(s, '\xA8') + case 'P': // PS (#x2029) + s = append(s, '\xE2') + s = append(s, '\x80') + s = append(s, '\xA9') + case 'x': + code_length = 2 + case 'u': + code_length = 4 + case 'U': + code_length = 8 + default: + yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", + start_mark, "found unknown escape character") + return false + } + + skip(parser) + skip(parser) + + // Consume an arbitrary escape code. + if code_length > 0 { + var value int + + // Scan the character value. + if parser.unread < code_length && !yaml_parser_update_buffer(parser, code_length) { + return false + } + for k := 0; k < code_length; k++ { + if !is_hex(parser.buffer, parser.buffer_pos+k) { + yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", + start_mark, "did not find expected hexdecimal number") + return false + } + value = (value << 4) + as_hex(parser.buffer, parser.buffer_pos+k) + } + + // Check the value and write the character. + if (value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF { + yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", + start_mark, "found invalid Unicode character escape code") + return false + } + if value <= 0x7F { + s = append(s, byte(value)) + } else if value <= 0x7FF { + s = append(s, byte(0xC0+(value>>6))) + s = append(s, byte(0x80+(value&0x3F))) + } else if value <= 0xFFFF { + s = append(s, byte(0xE0+(value>>12))) + s = append(s, byte(0x80+((value>>6)&0x3F))) + s = append(s, byte(0x80+(value&0x3F))) + } else { + s = append(s, byte(0xF0+(value>>18))) + s = append(s, byte(0x80+((value>>12)&0x3F))) + s = append(s, byte(0x80+((value>>6)&0x3F))) + s = append(s, byte(0x80+(value&0x3F))) + } + + // Advance the pointer. + for k := 0; k < code_length; k++ { + skip(parser) + } + } + } else { + // It is a non-escaped non-blank character. + s = read(parser, s) + } + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + } + + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + // Check if we are at the end of the scalar. + if single { + if parser.buffer[parser.buffer_pos] == '\'' { + break + } + } else { + if parser.buffer[parser.buffer_pos] == '"' { + break + } + } + + // Consume blank characters. + for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) { + if is_blank(parser.buffer, parser.buffer_pos) { + // Consume a space or a tab character. + if !leading_blanks { + whitespaces = read(parser, whitespaces) + } else { + skip(parser) + } + } else { + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + + // Check if it is a first line break. + if !leading_blanks { + whitespaces = whitespaces[:0] + leading_break = read_line(parser, leading_break) + leading_blanks = true + } else { + trailing_breaks = read_line(parser, trailing_breaks) + } + } + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Join the whitespaces or fold line breaks. + if leading_blanks { + // Do we need to fold line breaks? + if len(leading_break) > 0 && leading_break[0] == '\n' { + if len(trailing_breaks) == 0 { + s = append(s, ' ') + } else { + s = append(s, trailing_breaks...) + } + } else { + s = append(s, leading_break...) + s = append(s, trailing_breaks...) + } + trailing_breaks = trailing_breaks[:0] + leading_break = leading_break[:0] + } else { + s = append(s, whitespaces...) + whitespaces = whitespaces[:0] + } + } + + // Eat the right quote. + skip(parser) + end_mark := parser.mark + + // Create a token. + *token = yaml_token_t{ + typ: yaml_SCALAR_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + value: s, + style: yaml_SINGLE_QUOTED_SCALAR_STYLE, + } + if !single { + token.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + return true +} + +// Scan a plain scalar. +func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) bool { + + var s, leading_break, trailing_breaks, whitespaces []byte + var leading_blanks bool + var indent = parser.indent + 1 + + start_mark := parser.mark + end_mark := parser.mark + + // Consume the content of the plain scalar. + for { + // Check for a document indicator. + if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) { + return false + } + if parser.mark.column == 0 && + ((parser.buffer[parser.buffer_pos+0] == '-' && + parser.buffer[parser.buffer_pos+1] == '-' && + parser.buffer[parser.buffer_pos+2] == '-') || + (parser.buffer[parser.buffer_pos+0] == '.' && + parser.buffer[parser.buffer_pos+1] == '.' && + parser.buffer[parser.buffer_pos+2] == '.')) && + is_blankz(parser.buffer, parser.buffer_pos+3) { + break + } + + // Check for a comment. + if parser.buffer[parser.buffer_pos] == '#' { + break + } + + // Consume non-blank characters. + for !is_blankz(parser.buffer, parser.buffer_pos) { + + // Check for indicators that may end a plain scalar. + if (parser.buffer[parser.buffer_pos] == ':' && is_blankz(parser.buffer, parser.buffer_pos+1)) || + (parser.flow_level > 0 && + (parser.buffer[parser.buffer_pos] == ',' || + parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == '[' || + parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' || + parser.buffer[parser.buffer_pos] == '}')) { + break + } + + // Check if we need to join whitespaces and breaks. + if leading_blanks || len(whitespaces) > 0 { + if leading_blanks { + // Do we need to fold line breaks? + if leading_break[0] == '\n' { + if len(trailing_breaks) == 0 { + s = append(s, ' ') + } else { + s = append(s, trailing_breaks...) + } + } else { + s = append(s, leading_break...) + s = append(s, trailing_breaks...) + } + trailing_breaks = trailing_breaks[:0] + leading_break = leading_break[:0] + leading_blanks = false + } else { + s = append(s, whitespaces...) + whitespaces = whitespaces[:0] + } + } + + // Copy the character. + s = read(parser, s) + + end_mark = parser.mark + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + } + + // Is it the end? + if !(is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos)) { + break + } + + // Consume blank characters. + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + + for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) { + if is_blank(parser.buffer, parser.buffer_pos) { + + // Check for tab characters that abuse indentation. + if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) { + yaml_parser_set_scanner_error(parser, "while scanning a plain scalar", + start_mark, "found a tab character that violates indentation") + return false + } + + // Consume a space or a tab character. + if !leading_blanks { + whitespaces = read(parser, whitespaces) + } else { + skip(parser) + } + } else { + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + + // Check if it is a first line break. + if !leading_blanks { + whitespaces = whitespaces[:0] + leading_break = read_line(parser, leading_break) + leading_blanks = true + } else { + trailing_breaks = read_line(parser, trailing_breaks) + } + } + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + } + + // Check indentation level. + if parser.flow_level == 0 && parser.mark.column < indent { + break + } + } + + // Create a token. + *token = yaml_token_t{ + typ: yaml_SCALAR_TOKEN, + start_mark: start_mark, + end_mark: end_mark, + value: s, + style: yaml_PLAIN_SCALAR_STYLE, + } + + // Note that we change the 'simple_key_allowed' flag. + if leading_blanks { + parser.simple_key_allowed = true + } + return true +} + +func yaml_parser_scan_line_comment(parser *yaml_parser_t, token_mark yaml_mark_t) bool { + if parser.newlines > 0 { + return true + } + + var start_mark yaml_mark_t + var text []byte + + for peek := 0; peek < 512; peek++ { + if parser.unread < peek+1 && !yaml_parser_update_buffer(parser, peek+1) { + break + } + if is_blank(parser.buffer, parser.buffer_pos+peek) { + continue + } + if parser.buffer[parser.buffer_pos+peek] == '#' { + seen := parser.mark.index+peek + for { + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if is_breakz(parser.buffer, parser.buffer_pos) { + if parser.mark.index >= seen { + break + } + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + skip_line(parser) + } else if parser.mark.index >= seen { + if len(text) == 0 { + start_mark = parser.mark + } + text = read(parser, text) + } else { + skip(parser) + } + } + } + break + } + if len(text) > 0 { + parser.comments = append(parser.comments, yaml_comment_t{ + token_mark: token_mark, + start_mark: start_mark, + line: text, + }) + } + return true +} + +func yaml_parser_scan_comments(parser *yaml_parser_t, scan_mark yaml_mark_t) bool { + token := parser.tokens[len(parser.tokens)-1] + + if token.typ == yaml_FLOW_ENTRY_TOKEN && len(parser.tokens) > 1 { + token = parser.tokens[len(parser.tokens)-2] + } + + var token_mark = token.start_mark + var start_mark yaml_mark_t + var next_indent = parser.indent + if next_indent < 0 { + next_indent = 0 + } + + var recent_empty = false + var first_empty = parser.newlines <= 1 + + var line = parser.mark.line + var column = parser.mark.column + + var text []byte + + // The foot line is the place where a comment must start to + // still be considered as a foot of the prior content. + // If there's some content in the currently parsed line, then + // the foot is the line below it. + var foot_line = -1 + if scan_mark.line > 0 { + foot_line = parser.mark.line-parser.newlines+1 + if parser.newlines == 0 && parser.mark.column > 1 { + foot_line++ + } + } + + var peek = 0 + for ; peek < 512; peek++ { + if parser.unread < peek+1 && !yaml_parser_update_buffer(parser, peek+1) { + break + } + column++ + if is_blank(parser.buffer, parser.buffer_pos+peek) { + continue + } + c := parser.buffer[parser.buffer_pos+peek] + var close_flow = parser.flow_level > 0 && (c == ']' || c == '}') + if close_flow || is_breakz(parser.buffer, parser.buffer_pos+peek) { + // Got line break or terminator. + if close_flow || !recent_empty { + if close_flow || first_empty && (start_mark.line == foot_line && token.typ != yaml_VALUE_TOKEN || start_mark.column-1 < next_indent) { + // This is the first empty line and there were no empty lines before, + // so this initial part of the comment is a foot of the prior token + // instead of being a head for the following one. Split it up. + // Alternatively, this might also be the last comment inside a flow + // scope, so it must be a footer. + if len(text) > 0 { + if start_mark.column-1 < next_indent { + // If dedented it's unrelated to the prior token. + token_mark = start_mark + } + parser.comments = append(parser.comments, yaml_comment_t{ + scan_mark: scan_mark, + token_mark: token_mark, + start_mark: start_mark, + end_mark: yaml_mark_t{parser.mark.index + peek, line, column}, + foot: text, + }) + scan_mark = yaml_mark_t{parser.mark.index + peek, line, column} + token_mark = scan_mark + text = nil + } + } else { + if len(text) > 0 && parser.buffer[parser.buffer_pos+peek] != 0 { + text = append(text, '\n') + } + } + } + if !is_break(parser.buffer, parser.buffer_pos+peek) { + break + } + first_empty = false + recent_empty = true + column = 0 + line++ + continue + } + + if len(text) > 0 && (close_flow || column-1 < next_indent && column != start_mark.column) { + // The comment at the different indentation is a foot of the + // preceding data rather than a head of the upcoming one. + parser.comments = append(parser.comments, yaml_comment_t{ + scan_mark: scan_mark, + token_mark: token_mark, + start_mark: start_mark, + end_mark: yaml_mark_t{parser.mark.index + peek, line, column}, + foot: text, + }) + scan_mark = yaml_mark_t{parser.mark.index + peek, line, column} + token_mark = scan_mark + text = nil + } + + if parser.buffer[parser.buffer_pos+peek] != '#' { + break + } + + if len(text) == 0 { + start_mark = yaml_mark_t{parser.mark.index + peek, line, column} + } else { + text = append(text, '\n') + } + + recent_empty = false + + // Consume until after the consumed comment line. + seen := parser.mark.index+peek + for { + if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { + return false + } + if is_breakz(parser.buffer, parser.buffer_pos) { + if parser.mark.index >= seen { + break + } + if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) { + return false + } + skip_line(parser) + } else if parser.mark.index >= seen { + text = read(parser, text) + } else { + skip(parser) + } + } + + peek = 0 + column = 0 + line = parser.mark.line + next_indent = parser.indent + if next_indent < 0 { + next_indent = 0 + } + } + + if len(text) > 0 { + parser.comments = append(parser.comments, yaml_comment_t{ + scan_mark: scan_mark, + token_mark: start_mark, + start_mark: start_mark, + end_mark: yaml_mark_t{parser.mark.index + peek - 1, line, column}, + head: text, + }) + } + return true +} diff --git a/vendor/gopkg.in/yaml.v3/sorter.go b/vendor/gopkg.in/yaml.v3/sorter.go new file mode 100644 index 0000000000..9210ece7e9 --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/sorter.go @@ -0,0 +1,134 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// +// 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 yaml + +import ( + "reflect" + "unicode" +) + +type keyList []reflect.Value + +func (l keyList) Len() int { return len(l) } +func (l keyList) Swap(i, j int) { l[i], l[j] = l[j], l[i] } +func (l keyList) Less(i, j int) bool { + a := l[i] + b := l[j] + ak := a.Kind() + bk := b.Kind() + for (ak == reflect.Interface || ak == reflect.Ptr) && !a.IsNil() { + a = a.Elem() + ak = a.Kind() + } + for (bk == reflect.Interface || bk == reflect.Ptr) && !b.IsNil() { + b = b.Elem() + bk = b.Kind() + } + af, aok := keyFloat(a) + bf, bok := keyFloat(b) + if aok && bok { + if af != bf { + return af < bf + } + if ak != bk { + return ak < bk + } + return numLess(a, b) + } + if ak != reflect.String || bk != reflect.String { + return ak < bk + } + ar, br := []rune(a.String()), []rune(b.String()) + digits := false + for i := 0; i < len(ar) && i < len(br); i++ { + if ar[i] == br[i] { + digits = unicode.IsDigit(ar[i]) + continue + } + al := unicode.IsLetter(ar[i]) + bl := unicode.IsLetter(br[i]) + if al && bl { + return ar[i] < br[i] + } + if al || bl { + if digits { + return al + } else { + return bl + } + } + var ai, bi int + var an, bn int64 + if ar[i] == '0' || br[i] == '0' { + for j := i - 1; j >= 0 && unicode.IsDigit(ar[j]); j-- { + if ar[j] != '0' { + an = 1 + bn = 1 + break + } + } + } + for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ { + an = an*10 + int64(ar[ai]-'0') + } + for bi = i; bi < len(br) && unicode.IsDigit(br[bi]); bi++ { + bn = bn*10 + int64(br[bi]-'0') + } + if an != bn { + return an < bn + } + if ai != bi { + return ai < bi + } + return ar[i] < br[i] + } + return len(ar) < len(br) +} + +// keyFloat returns a float value for v if it is a number/bool +// and whether it is a number/bool or not. +func keyFloat(v reflect.Value) (f float64, ok bool) { + switch v.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return float64(v.Int()), true + case reflect.Float32, reflect.Float64: + return v.Float(), true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return float64(v.Uint()), true + case reflect.Bool: + if v.Bool() { + return 1, true + } + return 0, true + } + return 0, false +} + +// numLess returns whether a < b. +// a and b must necessarily have the same kind. +func numLess(a, b reflect.Value) bool { + switch a.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return a.Int() < b.Int() + case reflect.Float32, reflect.Float64: + return a.Float() < b.Float() + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return a.Uint() < b.Uint() + case reflect.Bool: + return !a.Bool() && b.Bool() + } + panic("not a number") +} diff --git a/vendor/gopkg.in/yaml.v3/writerc.go b/vendor/gopkg.in/yaml.v3/writerc.go new file mode 100644 index 0000000000..b8a116bf9a --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/writerc.go @@ -0,0 +1,48 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +// Set the writer error and return false. +func yaml_emitter_set_writer_error(emitter *yaml_emitter_t, problem string) bool { + emitter.error = yaml_WRITER_ERROR + emitter.problem = problem + return false +} + +// Flush the output buffer. +func yaml_emitter_flush(emitter *yaml_emitter_t) bool { + if emitter.write_handler == nil { + panic("write handler not set") + } + + // Check if the buffer is empty. + if emitter.buffer_pos == 0 { + return true + } + + if err := emitter.write_handler(emitter, emitter.buffer[:emitter.buffer_pos]); err != nil { + return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error()) + } + emitter.buffer_pos = 0 + return true +} diff --git a/vendor/gopkg.in/yaml.v3/yaml.go b/vendor/gopkg.in/yaml.v3/yaml.go new file mode 100644 index 0000000000..8cec6da48d --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/yaml.go @@ -0,0 +1,698 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// +// 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 yaml implements YAML support for the Go language. +// +// Source code and other details for the project are available at GitHub: +// +// https://github.com/go-yaml/yaml +// +package yaml + +import ( + "errors" + "fmt" + "io" + "reflect" + "strings" + "sync" + "unicode/utf8" +) + +// The Unmarshaler interface may be implemented by types to customize their +// behavior when being unmarshaled from a YAML document. +type Unmarshaler interface { + UnmarshalYAML(value *Node) error +} + +type obsoleteUnmarshaler interface { + UnmarshalYAML(unmarshal func(interface{}) error) error +} + +// The Marshaler interface may be implemented by types to customize their +// behavior when being marshaled into a YAML document. The returned value +// is marshaled in place of the original value implementing Marshaler. +// +// If an error is returned by MarshalYAML, the marshaling procedure stops +// and returns with the provided error. +type Marshaler interface { + MarshalYAML() (interface{}, error) +} + +// Unmarshal decodes the first document found within the in byte slice +// and assigns decoded values into the out value. +// +// Maps and pointers (to a struct, string, int, etc) are accepted as out +// values. If an internal pointer within a struct is not initialized, +// the yaml package will initialize it if necessary for unmarshalling +// the provided data. The out parameter must not be nil. +// +// The type of the decoded values should be compatible with the respective +// values in out. If one or more values cannot be decoded due to a type +// mismatches, decoding continues partially until the end of the YAML +// content, and a *yaml.TypeError is returned with details for all +// missed values. +// +// Struct fields are only unmarshalled if they are exported (have an +// upper case first letter), and are unmarshalled using the field name +// lowercased as the default key. Custom keys may be defined via the +// "yaml" name in the field tag: the content preceding the first comma +// is used as the key, and the following comma-separated options are +// used to tweak the marshalling process (see Marshal). +// Conflicting names result in a runtime error. +// +// For example: +// +// type T struct { +// F int `yaml:"a,omitempty"` +// B int +// } +// var t T +// yaml.Unmarshal([]byte("a: 1\nb: 2"), &t) +// +// See the documentation of Marshal for the format of tags and a list of +// supported tag options. +// +func Unmarshal(in []byte, out interface{}) (err error) { + return unmarshal(in, out, false) +} + +// A Decoder reads and decodes YAML values from an input stream. +type Decoder struct { + parser *parser + knownFields bool +} + +// NewDecoder returns a new decoder that reads from r. +// +// The decoder introduces its own buffering and may read +// data from r beyond the YAML values requested. +func NewDecoder(r io.Reader) *Decoder { + return &Decoder{ + parser: newParserFromReader(r), + } +} + +// KnownFields ensures that the keys in decoded mappings to +// exist as fields in the struct being decoded into. +func (dec *Decoder) KnownFields(enable bool) { + dec.knownFields = enable +} + +// Decode reads the next YAML-encoded value from its input +// and stores it in the value pointed to by v. +// +// See the documentation for Unmarshal for details about the +// conversion of YAML into a Go value. +func (dec *Decoder) Decode(v interface{}) (err error) { + d := newDecoder() + d.knownFields = dec.knownFields + defer handleErr(&err) + node := dec.parser.parse() + if node == nil { + return io.EOF + } + out := reflect.ValueOf(v) + if out.Kind() == reflect.Ptr && !out.IsNil() { + out = out.Elem() + } + d.unmarshal(node, out) + if len(d.terrors) > 0 { + return &TypeError{d.terrors} + } + return nil +} + +// Decode decodes the node and stores its data into the value pointed to by v. +// +// See the documentation for Unmarshal for details about the +// conversion of YAML into a Go value. +func (n *Node) Decode(v interface{}) (err error) { + d := newDecoder() + defer handleErr(&err) + out := reflect.ValueOf(v) + if out.Kind() == reflect.Ptr && !out.IsNil() { + out = out.Elem() + } + d.unmarshal(n, out) + if len(d.terrors) > 0 { + return &TypeError{d.terrors} + } + return nil +} + +func unmarshal(in []byte, out interface{}, strict bool) (err error) { + defer handleErr(&err) + d := newDecoder() + p := newParser(in) + defer p.destroy() + node := p.parse() + if node != nil { + v := reflect.ValueOf(out) + if v.Kind() == reflect.Ptr && !v.IsNil() { + v = v.Elem() + } + d.unmarshal(node, v) + } + if len(d.terrors) > 0 { + return &TypeError{d.terrors} + } + return nil +} + +// Marshal serializes the value provided into a YAML document. The structure +// of the generated document will reflect the structure of the value itself. +// Maps and pointers (to struct, string, int, etc) are accepted as the in value. +// +// Struct fields are only marshalled if they are exported (have an upper case +// first letter), and are marshalled using the field name lowercased as the +// default key. Custom keys may be defined via the "yaml" name in the field +// tag: the content preceding the first comma is used as the key, and the +// following comma-separated options are used to tweak the marshalling process. +// Conflicting names result in a runtime error. +// +// The field tag format accepted is: +// +// `(...) yaml:"[][,[,]]" (...)` +// +// The following flags are currently supported: +// +// omitempty Only include the field if it's not set to the zero +// value for the type or to empty slices or maps. +// Zero valued structs will be omitted if all their public +// fields are zero, unless they implement an IsZero +// method (see the IsZeroer interface type), in which +// case the field will be excluded if IsZero returns true. +// +// flow Marshal using a flow style (useful for structs, +// sequences and maps). +// +// inline Inline the field, which must be a struct or a map, +// causing all of its fields or keys to be processed as if +// they were part of the outer struct. For maps, keys must +// not conflict with the yaml keys of other struct fields. +// +// In addition, if the key is "-", the field is ignored. +// +// For example: +// +// type T struct { +// F int `yaml:"a,omitempty"` +// B int +// } +// yaml.Marshal(&T{B: 2}) // Returns "b: 2\n" +// yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n" +// +func Marshal(in interface{}) (out []byte, err error) { + defer handleErr(&err) + e := newEncoder() + defer e.destroy() + e.marshalDoc("", reflect.ValueOf(in)) + e.finish() + out = e.out + return +} + +// An Encoder writes YAML values to an output stream. +type Encoder struct { + encoder *encoder +} + +// NewEncoder returns a new encoder that writes to w. +// The Encoder should be closed after use to flush all data +// to w. +func NewEncoder(w io.Writer) *Encoder { + return &Encoder{ + encoder: newEncoderWithWriter(w), + } +} + +// Encode writes the YAML encoding of v to the stream. +// If multiple items are encoded to the stream, the +// second and subsequent document will be preceded +// with a "---" document separator, but the first will not. +// +// See the documentation for Marshal for details about the conversion of Go +// values to YAML. +func (e *Encoder) Encode(v interface{}) (err error) { + defer handleErr(&err) + e.encoder.marshalDoc("", reflect.ValueOf(v)) + return nil +} + +// Encode encodes value v and stores its representation in n. +// +// See the documentation for Marshal for details about the +// conversion of Go values into YAML. +func (n *Node) Encode(v interface{}) (err error) { + defer handleErr(&err) + e := newEncoder() + defer e.destroy() + e.marshalDoc("", reflect.ValueOf(v)) + e.finish() + p := newParser(e.out) + p.textless = true + defer p.destroy() + doc := p.parse() + *n = *doc.Content[0] + return nil +} + +// SetIndent changes the used indentation used when encoding. +func (e *Encoder) SetIndent(spaces int) { + if spaces < 0 { + panic("yaml: cannot indent to a negative number of spaces") + } + e.encoder.indent = spaces +} + +// Close closes the encoder by writing any remaining data. +// It does not write a stream terminating string "...". +func (e *Encoder) Close() (err error) { + defer handleErr(&err) + e.encoder.finish() + return nil +} + +func handleErr(err *error) { + if v := recover(); v != nil { + if e, ok := v.(yamlError); ok { + *err = e.err + } else { + panic(v) + } + } +} + +type yamlError struct { + err error +} + +func fail(err error) { + panic(yamlError{err}) +} + +func failf(format string, args ...interface{}) { + panic(yamlError{fmt.Errorf("yaml: "+format, args...)}) +} + +// A TypeError is returned by Unmarshal when one or more fields in +// the YAML document cannot be properly decoded into the requested +// types. When this error is returned, the value is still +// unmarshaled partially. +type TypeError struct { + Errors []string +} + +func (e *TypeError) Error() string { + return fmt.Sprintf("yaml: unmarshal errors:\n %s", strings.Join(e.Errors, "\n ")) +} + +type Kind uint32 + +const ( + DocumentNode Kind = 1 << iota + SequenceNode + MappingNode + ScalarNode + AliasNode +) + +type Style uint32 + +const ( + TaggedStyle Style = 1 << iota + DoubleQuotedStyle + SingleQuotedStyle + LiteralStyle + FoldedStyle + FlowStyle +) + +// Node represents an element in the YAML document hierarchy. While documents +// are typically encoded and decoded into higher level types, such as structs +// and maps, Node is an intermediate representation that allows detailed +// control over the content being decoded or encoded. +// +// It's worth noting that although Node offers access into details such as +// line numbers, colums, and comments, the content when re-encoded will not +// have its original textual representation preserved. An effort is made to +// render the data plesantly, and to preserve comments near the data they +// describe, though. +// +// Values that make use of the Node type interact with the yaml package in the +// same way any other type would do, by encoding and decoding yaml data +// directly or indirectly into them. +// +// For example: +// +// var person struct { +// Name string +// Address yaml.Node +// } +// err := yaml.Unmarshal(data, &person) +// +// Or by itself: +// +// var person Node +// err := yaml.Unmarshal(data, &person) +// +type Node struct { + // Kind defines whether the node is a document, a mapping, a sequence, + // a scalar value, or an alias to another node. The specific data type of + // scalar nodes may be obtained via the ShortTag and LongTag methods. + Kind Kind + + // Style allows customizing the apperance of the node in the tree. + Style Style + + // Tag holds the YAML tag defining the data type for the value. + // When decoding, this field will always be set to the resolved tag, + // even when it wasn't explicitly provided in the YAML content. + // When encoding, if this field is unset the value type will be + // implied from the node properties, and if it is set, it will only + // be serialized into the representation if TaggedStyle is used or + // the implicit tag diverges from the provided one. + Tag string + + // Value holds the unescaped and unquoted represenation of the value. + Value string + + // Anchor holds the anchor name for this node, which allows aliases to point to it. + Anchor string + + // Alias holds the node that this alias points to. Only valid when Kind is AliasNode. + Alias *Node + + // Content holds contained nodes for documents, mappings, and sequences. + Content []*Node + + // HeadComment holds any comments in the lines preceding the node and + // not separated by an empty line. + HeadComment string + + // LineComment holds any comments at the end of the line where the node is in. + LineComment string + + // FootComment holds any comments following the node and before empty lines. + FootComment string + + // Line and Column hold the node position in the decoded YAML text. + // These fields are not respected when encoding the node. + Line int + Column int +} + +// IsZero returns whether the node has all of its fields unset. +func (n *Node) IsZero() bool { + return n.Kind == 0 && n.Style == 0 && n.Tag == "" && n.Value == "" && n.Anchor == "" && n.Alias == nil && n.Content == nil && + n.HeadComment == "" && n.LineComment == "" && n.FootComment == "" && n.Line == 0 && n.Column == 0 +} + + +// LongTag returns the long form of the tag that indicates the data type for +// the node. If the Tag field isn't explicitly defined, one will be computed +// based on the node properties. +func (n *Node) LongTag() string { + return longTag(n.ShortTag()) +} + +// ShortTag returns the short form of the YAML tag that indicates data type for +// the node. If the Tag field isn't explicitly defined, one will be computed +// based on the node properties. +func (n *Node) ShortTag() string { + if n.indicatedString() { + return strTag + } + if n.Tag == "" || n.Tag == "!" { + switch n.Kind { + case MappingNode: + return mapTag + case SequenceNode: + return seqTag + case AliasNode: + if n.Alias != nil { + return n.Alias.ShortTag() + } + case ScalarNode: + tag, _ := resolve("", n.Value) + return tag + case 0: + // Special case to make the zero value convenient. + if n.IsZero() { + return nullTag + } + } + return "" + } + return shortTag(n.Tag) +} + +func (n *Node) indicatedString() bool { + return n.Kind == ScalarNode && + (shortTag(n.Tag) == strTag || + (n.Tag == "" || n.Tag == "!") && n.Style&(SingleQuotedStyle|DoubleQuotedStyle|LiteralStyle|FoldedStyle) != 0) +} + +// SetString is a convenience function that sets the node to a string value +// and defines its style in a pleasant way depending on its content. +func (n *Node) SetString(s string) { + n.Kind = ScalarNode + if utf8.ValidString(s) { + n.Value = s + n.Tag = strTag + } else { + n.Value = encodeBase64(s) + n.Tag = binaryTag + } + if strings.Contains(n.Value, "\n") { + n.Style = LiteralStyle + } +} + +// -------------------------------------------------------------------------- +// Maintain a mapping of keys to structure field indexes + +// The code in this section was copied from mgo/bson. + +// structInfo holds details for the serialization of fields of +// a given struct. +type structInfo struct { + FieldsMap map[string]fieldInfo + FieldsList []fieldInfo + + // InlineMap is the number of the field in the struct that + // contains an ,inline map, or -1 if there's none. + InlineMap int + + // InlineUnmarshalers holds indexes to inlined fields that + // contain unmarshaler values. + InlineUnmarshalers [][]int +} + +type fieldInfo struct { + Key string + Num int + OmitEmpty bool + Flow bool + // Id holds the unique field identifier, so we can cheaply + // check for field duplicates without maintaining an extra map. + Id int + + // Inline holds the field index if the field is part of an inlined struct. + Inline []int +} + +var structMap = make(map[reflect.Type]*structInfo) +var fieldMapMutex sync.RWMutex +var unmarshalerType reflect.Type + +func init() { + var v Unmarshaler + unmarshalerType = reflect.ValueOf(&v).Elem().Type() +} + +func getStructInfo(st reflect.Type) (*structInfo, error) { + fieldMapMutex.RLock() + sinfo, found := structMap[st] + fieldMapMutex.RUnlock() + if found { + return sinfo, nil + } + + n := st.NumField() + fieldsMap := make(map[string]fieldInfo) + fieldsList := make([]fieldInfo, 0, n) + inlineMap := -1 + inlineUnmarshalers := [][]int(nil) + for i := 0; i != n; i++ { + field := st.Field(i) + if field.PkgPath != "" && !field.Anonymous { + continue // Private field + } + + info := fieldInfo{Num: i} + + tag := field.Tag.Get("yaml") + if tag == "" && strings.Index(string(field.Tag), ":") < 0 { + tag = string(field.Tag) + } + if tag == "-" { + continue + } + + inline := false + fields := strings.Split(tag, ",") + if len(fields) > 1 { + for _, flag := range fields[1:] { + switch flag { + case "omitempty": + info.OmitEmpty = true + case "flow": + info.Flow = true + case "inline": + inline = true + default: + return nil, errors.New(fmt.Sprintf("unsupported flag %q in tag %q of type %s", flag, tag, st)) + } + } + tag = fields[0] + } + + if inline { + switch field.Type.Kind() { + case reflect.Map: + if inlineMap >= 0 { + return nil, errors.New("multiple ,inline maps in struct " + st.String()) + } + if field.Type.Key() != reflect.TypeOf("") { + return nil, errors.New("option ,inline needs a map with string keys in struct " + st.String()) + } + inlineMap = info.Num + case reflect.Struct, reflect.Ptr: + ftype := field.Type + for ftype.Kind() == reflect.Ptr { + ftype = ftype.Elem() + } + if ftype.Kind() != reflect.Struct { + return nil, errors.New("option ,inline may only be used on a struct or map field") + } + if reflect.PtrTo(ftype).Implements(unmarshalerType) { + inlineUnmarshalers = append(inlineUnmarshalers, []int{i}) + } else { + sinfo, err := getStructInfo(ftype) + if err != nil { + return nil, err + } + for _, index := range sinfo.InlineUnmarshalers { + inlineUnmarshalers = append(inlineUnmarshalers, append([]int{i}, index...)) + } + for _, finfo := range sinfo.FieldsList { + if _, found := fieldsMap[finfo.Key]; found { + msg := "duplicated key '" + finfo.Key + "' in struct " + st.String() + return nil, errors.New(msg) + } + if finfo.Inline == nil { + finfo.Inline = []int{i, finfo.Num} + } else { + finfo.Inline = append([]int{i}, finfo.Inline...) + } + finfo.Id = len(fieldsList) + fieldsMap[finfo.Key] = finfo + fieldsList = append(fieldsList, finfo) + } + } + default: + return nil, errors.New("option ,inline may only be used on a struct or map field") + } + continue + } + + if tag != "" { + info.Key = tag + } else { + info.Key = strings.ToLower(field.Name) + } + + if _, found = fieldsMap[info.Key]; found { + msg := "duplicated key '" + info.Key + "' in struct " + st.String() + return nil, errors.New(msg) + } + + info.Id = len(fieldsList) + fieldsList = append(fieldsList, info) + fieldsMap[info.Key] = info + } + + sinfo = &structInfo{ + FieldsMap: fieldsMap, + FieldsList: fieldsList, + InlineMap: inlineMap, + InlineUnmarshalers: inlineUnmarshalers, + } + + fieldMapMutex.Lock() + structMap[st] = sinfo + fieldMapMutex.Unlock() + return sinfo, nil +} + +// IsZeroer is used to check whether an object is zero to +// determine whether it should be omitted when marshaling +// with the omitempty flag. One notable implementation +// is time.Time. +type IsZeroer interface { + IsZero() bool +} + +func isZero(v reflect.Value) bool { + kind := v.Kind() + if z, ok := v.Interface().(IsZeroer); ok { + if (kind == reflect.Ptr || kind == reflect.Interface) && v.IsNil() { + return true + } + return z.IsZero() + } + switch kind { + case reflect.String: + return len(v.String()) == 0 + case reflect.Interface, reflect.Ptr: + return v.IsNil() + case reflect.Slice: + return v.Len() == 0 + case reflect.Map: + return v.Len() == 0 + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Bool: + return !v.Bool() + case reflect.Struct: + vt := v.Type() + for i := v.NumField() - 1; i >= 0; i-- { + if vt.Field(i).PkgPath != "" { + continue // Private field + } + if !isZero(v.Field(i)) { + return false + } + } + return true + } + return false +} diff --git a/vendor/gopkg.in/yaml.v3/yamlh.go b/vendor/gopkg.in/yaml.v3/yamlh.go new file mode 100644 index 0000000000..7c6d007706 --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/yamlh.go @@ -0,0 +1,807 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +import ( + "fmt" + "io" +) + +// The version directive data. +type yaml_version_directive_t struct { + major int8 // The major version number. + minor int8 // The minor version number. +} + +// The tag directive data. +type yaml_tag_directive_t struct { + handle []byte // The tag handle. + prefix []byte // The tag prefix. +} + +type yaml_encoding_t int + +// The stream encoding. +const ( + // Let the parser choose the encoding. + yaml_ANY_ENCODING yaml_encoding_t = iota + + yaml_UTF8_ENCODING // The default UTF-8 encoding. + yaml_UTF16LE_ENCODING // The UTF-16-LE encoding with BOM. + yaml_UTF16BE_ENCODING // The UTF-16-BE encoding with BOM. +) + +type yaml_break_t int + +// Line break types. +const ( + // Let the parser choose the break type. + yaml_ANY_BREAK yaml_break_t = iota + + yaml_CR_BREAK // Use CR for line breaks (Mac style). + yaml_LN_BREAK // Use LN for line breaks (Unix style). + yaml_CRLN_BREAK // Use CR LN for line breaks (DOS style). +) + +type yaml_error_type_t int + +// Many bad things could happen with the parser and emitter. +const ( + // No error is produced. + yaml_NO_ERROR yaml_error_type_t = iota + + yaml_MEMORY_ERROR // Cannot allocate or reallocate a block of memory. + yaml_READER_ERROR // Cannot read or decode the input stream. + yaml_SCANNER_ERROR // Cannot scan the input stream. + yaml_PARSER_ERROR // Cannot parse the input stream. + yaml_COMPOSER_ERROR // Cannot compose a YAML document. + yaml_WRITER_ERROR // Cannot write to the output stream. + yaml_EMITTER_ERROR // Cannot emit a YAML stream. +) + +// The pointer position. +type yaml_mark_t struct { + index int // The position index. + line int // The position line. + column int // The position column. +} + +// Node Styles + +type yaml_style_t int8 + +type yaml_scalar_style_t yaml_style_t + +// Scalar styles. +const ( + // Let the emitter choose the style. + yaml_ANY_SCALAR_STYLE yaml_scalar_style_t = 0 + + yaml_PLAIN_SCALAR_STYLE yaml_scalar_style_t = 1 << iota // The plain scalar style. + yaml_SINGLE_QUOTED_SCALAR_STYLE // The single-quoted scalar style. + yaml_DOUBLE_QUOTED_SCALAR_STYLE // The double-quoted scalar style. + yaml_LITERAL_SCALAR_STYLE // The literal scalar style. + yaml_FOLDED_SCALAR_STYLE // The folded scalar style. +) + +type yaml_sequence_style_t yaml_style_t + +// Sequence styles. +const ( + // Let the emitter choose the style. + yaml_ANY_SEQUENCE_STYLE yaml_sequence_style_t = iota + + yaml_BLOCK_SEQUENCE_STYLE // The block sequence style. + yaml_FLOW_SEQUENCE_STYLE // The flow sequence style. +) + +type yaml_mapping_style_t yaml_style_t + +// Mapping styles. +const ( + // Let the emitter choose the style. + yaml_ANY_MAPPING_STYLE yaml_mapping_style_t = iota + + yaml_BLOCK_MAPPING_STYLE // The block mapping style. + yaml_FLOW_MAPPING_STYLE // The flow mapping style. +) + +// Tokens + +type yaml_token_type_t int + +// Token types. +const ( + // An empty token. + yaml_NO_TOKEN yaml_token_type_t = iota + + yaml_STREAM_START_TOKEN // A STREAM-START token. + yaml_STREAM_END_TOKEN // A STREAM-END token. + + yaml_VERSION_DIRECTIVE_TOKEN // A VERSION-DIRECTIVE token. + yaml_TAG_DIRECTIVE_TOKEN // A TAG-DIRECTIVE token. + yaml_DOCUMENT_START_TOKEN // A DOCUMENT-START token. + yaml_DOCUMENT_END_TOKEN // A DOCUMENT-END token. + + yaml_BLOCK_SEQUENCE_START_TOKEN // A BLOCK-SEQUENCE-START token. + yaml_BLOCK_MAPPING_START_TOKEN // A BLOCK-SEQUENCE-END token. + yaml_BLOCK_END_TOKEN // A BLOCK-END token. + + yaml_FLOW_SEQUENCE_START_TOKEN // A FLOW-SEQUENCE-START token. + yaml_FLOW_SEQUENCE_END_TOKEN // A FLOW-SEQUENCE-END token. + yaml_FLOW_MAPPING_START_TOKEN // A FLOW-MAPPING-START token. + yaml_FLOW_MAPPING_END_TOKEN // A FLOW-MAPPING-END token. + + yaml_BLOCK_ENTRY_TOKEN // A BLOCK-ENTRY token. + yaml_FLOW_ENTRY_TOKEN // A FLOW-ENTRY token. + yaml_KEY_TOKEN // A KEY token. + yaml_VALUE_TOKEN // A VALUE token. + + yaml_ALIAS_TOKEN // An ALIAS token. + yaml_ANCHOR_TOKEN // An ANCHOR token. + yaml_TAG_TOKEN // A TAG token. + yaml_SCALAR_TOKEN // A SCALAR token. +) + +func (tt yaml_token_type_t) String() string { + switch tt { + case yaml_NO_TOKEN: + return "yaml_NO_TOKEN" + case yaml_STREAM_START_TOKEN: + return "yaml_STREAM_START_TOKEN" + case yaml_STREAM_END_TOKEN: + return "yaml_STREAM_END_TOKEN" + case yaml_VERSION_DIRECTIVE_TOKEN: + return "yaml_VERSION_DIRECTIVE_TOKEN" + case yaml_TAG_DIRECTIVE_TOKEN: + return "yaml_TAG_DIRECTIVE_TOKEN" + case yaml_DOCUMENT_START_TOKEN: + return "yaml_DOCUMENT_START_TOKEN" + case yaml_DOCUMENT_END_TOKEN: + return "yaml_DOCUMENT_END_TOKEN" + case yaml_BLOCK_SEQUENCE_START_TOKEN: + return "yaml_BLOCK_SEQUENCE_START_TOKEN" + case yaml_BLOCK_MAPPING_START_TOKEN: + return "yaml_BLOCK_MAPPING_START_TOKEN" + case yaml_BLOCK_END_TOKEN: + return "yaml_BLOCK_END_TOKEN" + case yaml_FLOW_SEQUENCE_START_TOKEN: + return "yaml_FLOW_SEQUENCE_START_TOKEN" + case yaml_FLOW_SEQUENCE_END_TOKEN: + return "yaml_FLOW_SEQUENCE_END_TOKEN" + case yaml_FLOW_MAPPING_START_TOKEN: + return "yaml_FLOW_MAPPING_START_TOKEN" + case yaml_FLOW_MAPPING_END_TOKEN: + return "yaml_FLOW_MAPPING_END_TOKEN" + case yaml_BLOCK_ENTRY_TOKEN: + return "yaml_BLOCK_ENTRY_TOKEN" + case yaml_FLOW_ENTRY_TOKEN: + return "yaml_FLOW_ENTRY_TOKEN" + case yaml_KEY_TOKEN: + return "yaml_KEY_TOKEN" + case yaml_VALUE_TOKEN: + return "yaml_VALUE_TOKEN" + case yaml_ALIAS_TOKEN: + return "yaml_ALIAS_TOKEN" + case yaml_ANCHOR_TOKEN: + return "yaml_ANCHOR_TOKEN" + case yaml_TAG_TOKEN: + return "yaml_TAG_TOKEN" + case yaml_SCALAR_TOKEN: + return "yaml_SCALAR_TOKEN" + } + return "" +} + +// The token structure. +type yaml_token_t struct { + // The token type. + typ yaml_token_type_t + + // The start/end of the token. + start_mark, end_mark yaml_mark_t + + // The stream encoding (for yaml_STREAM_START_TOKEN). + encoding yaml_encoding_t + + // The alias/anchor/scalar value or tag/tag directive handle + // (for yaml_ALIAS_TOKEN, yaml_ANCHOR_TOKEN, yaml_SCALAR_TOKEN, yaml_TAG_TOKEN, yaml_TAG_DIRECTIVE_TOKEN). + value []byte + + // The tag suffix (for yaml_TAG_TOKEN). + suffix []byte + + // The tag directive prefix (for yaml_TAG_DIRECTIVE_TOKEN). + prefix []byte + + // The scalar style (for yaml_SCALAR_TOKEN). + style yaml_scalar_style_t + + // The version directive major/minor (for yaml_VERSION_DIRECTIVE_TOKEN). + major, minor int8 +} + +// Events + +type yaml_event_type_t int8 + +// Event types. +const ( + // An empty event. + yaml_NO_EVENT yaml_event_type_t = iota + + yaml_STREAM_START_EVENT // A STREAM-START event. + yaml_STREAM_END_EVENT // A STREAM-END event. + yaml_DOCUMENT_START_EVENT // A DOCUMENT-START event. + yaml_DOCUMENT_END_EVENT // A DOCUMENT-END event. + yaml_ALIAS_EVENT // An ALIAS event. + yaml_SCALAR_EVENT // A SCALAR event. + yaml_SEQUENCE_START_EVENT // A SEQUENCE-START event. + yaml_SEQUENCE_END_EVENT // A SEQUENCE-END event. + yaml_MAPPING_START_EVENT // A MAPPING-START event. + yaml_MAPPING_END_EVENT // A MAPPING-END event. + yaml_TAIL_COMMENT_EVENT +) + +var eventStrings = []string{ + yaml_NO_EVENT: "none", + yaml_STREAM_START_EVENT: "stream start", + yaml_STREAM_END_EVENT: "stream end", + yaml_DOCUMENT_START_EVENT: "document start", + yaml_DOCUMENT_END_EVENT: "document end", + yaml_ALIAS_EVENT: "alias", + yaml_SCALAR_EVENT: "scalar", + yaml_SEQUENCE_START_EVENT: "sequence start", + yaml_SEQUENCE_END_EVENT: "sequence end", + yaml_MAPPING_START_EVENT: "mapping start", + yaml_MAPPING_END_EVENT: "mapping end", + yaml_TAIL_COMMENT_EVENT: "tail comment", +} + +func (e yaml_event_type_t) String() string { + if e < 0 || int(e) >= len(eventStrings) { + return fmt.Sprintf("unknown event %d", e) + } + return eventStrings[e] +} + +// The event structure. +type yaml_event_t struct { + + // The event type. + typ yaml_event_type_t + + // The start and end of the event. + start_mark, end_mark yaml_mark_t + + // The document encoding (for yaml_STREAM_START_EVENT). + encoding yaml_encoding_t + + // The version directive (for yaml_DOCUMENT_START_EVENT). + version_directive *yaml_version_directive_t + + // The list of tag directives (for yaml_DOCUMENT_START_EVENT). + tag_directives []yaml_tag_directive_t + + // The comments + head_comment []byte + line_comment []byte + foot_comment []byte + tail_comment []byte + + // The anchor (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_ALIAS_EVENT). + anchor []byte + + // The tag (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT). + tag []byte + + // The scalar value (for yaml_SCALAR_EVENT). + value []byte + + // Is the document start/end indicator implicit, or the tag optional? + // (for yaml_DOCUMENT_START_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_SCALAR_EVENT). + implicit bool + + // Is the tag optional for any non-plain style? (for yaml_SCALAR_EVENT). + quoted_implicit bool + + // The style (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT). + style yaml_style_t +} + +func (e *yaml_event_t) scalar_style() yaml_scalar_style_t { return yaml_scalar_style_t(e.style) } +func (e *yaml_event_t) sequence_style() yaml_sequence_style_t { return yaml_sequence_style_t(e.style) } +func (e *yaml_event_t) mapping_style() yaml_mapping_style_t { return yaml_mapping_style_t(e.style) } + +// Nodes + +const ( + yaml_NULL_TAG = "tag:yaml.org,2002:null" // The tag !!null with the only possible value: null. + yaml_BOOL_TAG = "tag:yaml.org,2002:bool" // The tag !!bool with the values: true and false. + yaml_STR_TAG = "tag:yaml.org,2002:str" // The tag !!str for string values. + yaml_INT_TAG = "tag:yaml.org,2002:int" // The tag !!int for integer values. + yaml_FLOAT_TAG = "tag:yaml.org,2002:float" // The tag !!float for float values. + yaml_TIMESTAMP_TAG = "tag:yaml.org,2002:timestamp" // The tag !!timestamp for date and time values. + + yaml_SEQ_TAG = "tag:yaml.org,2002:seq" // The tag !!seq is used to denote sequences. + yaml_MAP_TAG = "tag:yaml.org,2002:map" // The tag !!map is used to denote mapping. + + // Not in original libyaml. + yaml_BINARY_TAG = "tag:yaml.org,2002:binary" + yaml_MERGE_TAG = "tag:yaml.org,2002:merge" + + yaml_DEFAULT_SCALAR_TAG = yaml_STR_TAG // The default scalar tag is !!str. + yaml_DEFAULT_SEQUENCE_TAG = yaml_SEQ_TAG // The default sequence tag is !!seq. + yaml_DEFAULT_MAPPING_TAG = yaml_MAP_TAG // The default mapping tag is !!map. +) + +type yaml_node_type_t int + +// Node types. +const ( + // An empty node. + yaml_NO_NODE yaml_node_type_t = iota + + yaml_SCALAR_NODE // A scalar node. + yaml_SEQUENCE_NODE // A sequence node. + yaml_MAPPING_NODE // A mapping node. +) + +// An element of a sequence node. +type yaml_node_item_t int + +// An element of a mapping node. +type yaml_node_pair_t struct { + key int // The key of the element. + value int // The value of the element. +} + +// The node structure. +type yaml_node_t struct { + typ yaml_node_type_t // The node type. + tag []byte // The node tag. + + // The node data. + + // The scalar parameters (for yaml_SCALAR_NODE). + scalar struct { + value []byte // The scalar value. + length int // The length of the scalar value. + style yaml_scalar_style_t // The scalar style. + } + + // The sequence parameters (for YAML_SEQUENCE_NODE). + sequence struct { + items_data []yaml_node_item_t // The stack of sequence items. + style yaml_sequence_style_t // The sequence style. + } + + // The mapping parameters (for yaml_MAPPING_NODE). + mapping struct { + pairs_data []yaml_node_pair_t // The stack of mapping pairs (key, value). + pairs_start *yaml_node_pair_t // The beginning of the stack. + pairs_end *yaml_node_pair_t // The end of the stack. + pairs_top *yaml_node_pair_t // The top of the stack. + style yaml_mapping_style_t // The mapping style. + } + + start_mark yaml_mark_t // The beginning of the node. + end_mark yaml_mark_t // The end of the node. + +} + +// The document structure. +type yaml_document_t struct { + + // The document nodes. + nodes []yaml_node_t + + // The version directive. + version_directive *yaml_version_directive_t + + // The list of tag directives. + tag_directives_data []yaml_tag_directive_t + tag_directives_start int // The beginning of the tag directives list. + tag_directives_end int // The end of the tag directives list. + + start_implicit int // Is the document start indicator implicit? + end_implicit int // Is the document end indicator implicit? + + // The start/end of the document. + start_mark, end_mark yaml_mark_t +} + +// The prototype of a read handler. +// +// The read handler is called when the parser needs to read more bytes from the +// source. The handler should write not more than size bytes to the buffer. +// The number of written bytes should be set to the size_read variable. +// +// [in,out] data A pointer to an application data specified by +// yaml_parser_set_input(). +// [out] buffer The buffer to write the data from the source. +// [in] size The size of the buffer. +// [out] size_read The actual number of bytes read from the source. +// +// On success, the handler should return 1. If the handler failed, +// the returned value should be 0. On EOF, the handler should set the +// size_read to 0 and return 1. +type yaml_read_handler_t func(parser *yaml_parser_t, buffer []byte) (n int, err error) + +// This structure holds information about a potential simple key. +type yaml_simple_key_t struct { + possible bool // Is a simple key possible? + required bool // Is a simple key required? + token_number int // The number of the token. + mark yaml_mark_t // The position mark. +} + +// The states of the parser. +type yaml_parser_state_t int + +const ( + yaml_PARSE_STREAM_START_STATE yaml_parser_state_t = iota + + yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE // Expect the beginning of an implicit document. + yaml_PARSE_DOCUMENT_START_STATE // Expect DOCUMENT-START. + yaml_PARSE_DOCUMENT_CONTENT_STATE // Expect the content of a document. + yaml_PARSE_DOCUMENT_END_STATE // Expect DOCUMENT-END. + yaml_PARSE_BLOCK_NODE_STATE // Expect a block node. + yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE // Expect a block node or indentless sequence. + yaml_PARSE_FLOW_NODE_STATE // Expect a flow node. + yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE // Expect the first entry of a block sequence. + yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE // Expect an entry of a block sequence. + yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE // Expect an entry of an indentless sequence. + yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE // Expect the first key of a block mapping. + yaml_PARSE_BLOCK_MAPPING_KEY_STATE // Expect a block mapping key. + yaml_PARSE_BLOCK_MAPPING_VALUE_STATE // Expect a block mapping value. + yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE // Expect the first entry of a flow sequence. + yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE // Expect an entry of a flow sequence. + yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE // Expect a key of an ordered mapping. + yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE // Expect a value of an ordered mapping. + yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE // Expect the and of an ordered mapping entry. + yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE // Expect the first key of a flow mapping. + yaml_PARSE_FLOW_MAPPING_KEY_STATE // Expect a key of a flow mapping. + yaml_PARSE_FLOW_MAPPING_VALUE_STATE // Expect a value of a flow mapping. + yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE // Expect an empty value of a flow mapping. + yaml_PARSE_END_STATE // Expect nothing. +) + +func (ps yaml_parser_state_t) String() string { + switch ps { + case yaml_PARSE_STREAM_START_STATE: + return "yaml_PARSE_STREAM_START_STATE" + case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE: + return "yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE" + case yaml_PARSE_DOCUMENT_START_STATE: + return "yaml_PARSE_DOCUMENT_START_STATE" + case yaml_PARSE_DOCUMENT_CONTENT_STATE: + return "yaml_PARSE_DOCUMENT_CONTENT_STATE" + case yaml_PARSE_DOCUMENT_END_STATE: + return "yaml_PARSE_DOCUMENT_END_STATE" + case yaml_PARSE_BLOCK_NODE_STATE: + return "yaml_PARSE_BLOCK_NODE_STATE" + case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE: + return "yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE" + case yaml_PARSE_FLOW_NODE_STATE: + return "yaml_PARSE_FLOW_NODE_STATE" + case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE: + return "yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE" + case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE: + return "yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE" + case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE: + return "yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE" + case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE: + return "yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE" + case yaml_PARSE_BLOCK_MAPPING_KEY_STATE: + return "yaml_PARSE_BLOCK_MAPPING_KEY_STATE" + case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE: + return "yaml_PARSE_BLOCK_MAPPING_VALUE_STATE" + case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE: + return "yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE" + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE: + return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE" + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE: + return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE" + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE: + return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE" + case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE: + return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE" + case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE: + return "yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE" + case yaml_PARSE_FLOW_MAPPING_KEY_STATE: + return "yaml_PARSE_FLOW_MAPPING_KEY_STATE" + case yaml_PARSE_FLOW_MAPPING_VALUE_STATE: + return "yaml_PARSE_FLOW_MAPPING_VALUE_STATE" + case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE: + return "yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE" + case yaml_PARSE_END_STATE: + return "yaml_PARSE_END_STATE" + } + return "" +} + +// This structure holds aliases data. +type yaml_alias_data_t struct { + anchor []byte // The anchor. + index int // The node id. + mark yaml_mark_t // The anchor mark. +} + +// The parser structure. +// +// All members are internal. Manage the structure using the +// yaml_parser_ family of functions. +type yaml_parser_t struct { + + // Error handling + + error yaml_error_type_t // Error type. + + problem string // Error description. + + // The byte about which the problem occurred. + problem_offset int + problem_value int + problem_mark yaml_mark_t + + // The error context. + context string + context_mark yaml_mark_t + + // Reader stuff + + read_handler yaml_read_handler_t // Read handler. + + input_reader io.Reader // File input data. + input []byte // String input data. + input_pos int + + eof bool // EOF flag + + buffer []byte // The working buffer. + buffer_pos int // The current position of the buffer. + + unread int // The number of unread characters in the buffer. + + newlines int // The number of line breaks since last non-break/non-blank character + + raw_buffer []byte // The raw buffer. + raw_buffer_pos int // The current position of the buffer. + + encoding yaml_encoding_t // The input encoding. + + offset int // The offset of the current position (in bytes). + mark yaml_mark_t // The mark of the current position. + + // Comments + + head_comment []byte // The current head comments + line_comment []byte // The current line comments + foot_comment []byte // The current foot comments + tail_comment []byte // Foot comment that happens at the end of a block. + stem_comment []byte // Comment in item preceding a nested structure (list inside list item, etc) + + comments []yaml_comment_t // The folded comments for all parsed tokens + comments_head int + + // Scanner stuff + + stream_start_produced bool // Have we started to scan the input stream? + stream_end_produced bool // Have we reached the end of the input stream? + + flow_level int // The number of unclosed '[' and '{' indicators. + + tokens []yaml_token_t // The tokens queue. + tokens_head int // The head of the tokens queue. + tokens_parsed int // The number of tokens fetched from the queue. + token_available bool // Does the tokens queue contain a token ready for dequeueing. + + indent int // The current indentation level. + indents []int // The indentation levels stack. + + simple_key_allowed bool // May a simple key occur at the current position? + simple_keys []yaml_simple_key_t // The stack of simple keys. + simple_keys_by_tok map[int]int // possible simple_key indexes indexed by token_number + + // Parser stuff + + state yaml_parser_state_t // The current parser state. + states []yaml_parser_state_t // The parser states stack. + marks []yaml_mark_t // The stack of marks. + tag_directives []yaml_tag_directive_t // The list of TAG directives. + + // Dumper stuff + + aliases []yaml_alias_data_t // The alias data. + + document *yaml_document_t // The currently parsed document. +} + +type yaml_comment_t struct { + + scan_mark yaml_mark_t // Position where scanning for comments started + token_mark yaml_mark_t // Position after which tokens will be associated with this comment + start_mark yaml_mark_t // Position of '#' comment mark + end_mark yaml_mark_t // Position where comment terminated + + head []byte + line []byte + foot []byte +} + +// Emitter Definitions + +// The prototype of a write handler. +// +// The write handler is called when the emitter needs to flush the accumulated +// characters to the output. The handler should write @a size bytes of the +// @a buffer to the output. +// +// @param[in,out] data A pointer to an application data specified by +// yaml_emitter_set_output(). +// @param[in] buffer The buffer with bytes to be written. +// @param[in] size The size of the buffer. +// +// @returns On success, the handler should return @c 1. If the handler failed, +// the returned value should be @c 0. +// +type yaml_write_handler_t func(emitter *yaml_emitter_t, buffer []byte) error + +type yaml_emitter_state_t int + +// The emitter states. +const ( + // Expect STREAM-START. + yaml_EMIT_STREAM_START_STATE yaml_emitter_state_t = iota + + yaml_EMIT_FIRST_DOCUMENT_START_STATE // Expect the first DOCUMENT-START or STREAM-END. + yaml_EMIT_DOCUMENT_START_STATE // Expect DOCUMENT-START or STREAM-END. + yaml_EMIT_DOCUMENT_CONTENT_STATE // Expect the content of a document. + yaml_EMIT_DOCUMENT_END_STATE // Expect DOCUMENT-END. + yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE // Expect the first item of a flow sequence. + yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE // Expect the next item of a flow sequence, with the comma already written out + yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE // Expect an item of a flow sequence. + yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE // Expect the first key of a flow mapping. + yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE // Expect the next key of a flow mapping, with the comma already written out + yaml_EMIT_FLOW_MAPPING_KEY_STATE // Expect a key of a flow mapping. + yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE // Expect a value for a simple key of a flow mapping. + yaml_EMIT_FLOW_MAPPING_VALUE_STATE // Expect a value of a flow mapping. + yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE // Expect the first item of a block sequence. + yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE // Expect an item of a block sequence. + yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE // Expect the first key of a block mapping. + yaml_EMIT_BLOCK_MAPPING_KEY_STATE // Expect the key of a block mapping. + yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE // Expect a value for a simple key of a block mapping. + yaml_EMIT_BLOCK_MAPPING_VALUE_STATE // Expect a value of a block mapping. + yaml_EMIT_END_STATE // Expect nothing. +) + +// The emitter structure. +// +// All members are internal. Manage the structure using the @c yaml_emitter_ +// family of functions. +type yaml_emitter_t struct { + + // Error handling + + error yaml_error_type_t // Error type. + problem string // Error description. + + // Writer stuff + + write_handler yaml_write_handler_t // Write handler. + + output_buffer *[]byte // String output data. + output_writer io.Writer // File output data. + + buffer []byte // The working buffer. + buffer_pos int // The current position of the buffer. + + raw_buffer []byte // The raw buffer. + raw_buffer_pos int // The current position of the buffer. + + encoding yaml_encoding_t // The stream encoding. + + // Emitter stuff + + canonical bool // If the output is in the canonical style? + best_indent int // The number of indentation spaces. + best_width int // The preferred width of the output lines. + unicode bool // Allow unescaped non-ASCII characters? + line_break yaml_break_t // The preferred line break. + + state yaml_emitter_state_t // The current emitter state. + states []yaml_emitter_state_t // The stack of states. + + events []yaml_event_t // The event queue. + events_head int // The head of the event queue. + + indents []int // The stack of indentation levels. + + tag_directives []yaml_tag_directive_t // The list of tag directives. + + indent int // The current indentation level. + + flow_level int // The current flow level. + + root_context bool // Is it the document root context? + sequence_context bool // Is it a sequence context? + mapping_context bool // Is it a mapping context? + simple_key_context bool // Is it a simple mapping key context? + + line int // The current line. + column int // The current column. + whitespace bool // If the last character was a whitespace? + indention bool // If the last character was an indentation character (' ', '-', '?', ':')? + open_ended bool // If an explicit document end is required? + + space_above bool // Is there's an empty line above? + foot_indent int // The indent used to write the foot comment above, or -1 if none. + + // Anchor analysis. + anchor_data struct { + anchor []byte // The anchor value. + alias bool // Is it an alias? + } + + // Tag analysis. + tag_data struct { + handle []byte // The tag handle. + suffix []byte // The tag suffix. + } + + // Scalar analysis. + scalar_data struct { + value []byte // The scalar value. + multiline bool // Does the scalar contain line breaks? + flow_plain_allowed bool // Can the scalar be expessed in the flow plain style? + block_plain_allowed bool // Can the scalar be expressed in the block plain style? + single_quoted_allowed bool // Can the scalar be expressed in the single quoted style? + block_allowed bool // Can the scalar be expressed in the literal or folded styles? + style yaml_scalar_style_t // The output style. + } + + // Comments + head_comment []byte + line_comment []byte + foot_comment []byte + tail_comment []byte + + key_line_comment []byte + + // Dumper stuff + + opened bool // If the stream was already opened? + closed bool // If the stream was already closed? + + // The information associated with the document nodes. + anchors *struct { + references int // The number of references. + anchor int // The anchor id. + serialized bool // If the node has been emitted? + } + + last_anchor_id int // The last assigned anchor id. + + document *yaml_document_t // The currently emitted document. +} diff --git a/vendor/gopkg.in/yaml.v3/yamlprivateh.go b/vendor/gopkg.in/yaml.v3/yamlprivateh.go new file mode 100644 index 0000000000..e88f9c54ae --- /dev/null +++ b/vendor/gopkg.in/yaml.v3/yamlprivateh.go @@ -0,0 +1,198 @@ +// +// Copyright (c) 2011-2019 Canonical Ltd +// Copyright (c) 2006-2010 Kirill Simonov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaml + +const ( + // The size of the input raw buffer. + input_raw_buffer_size = 512 + + // The size of the input buffer. + // It should be possible to decode the whole raw buffer. + input_buffer_size = input_raw_buffer_size * 3 + + // The size of the output buffer. + output_buffer_size = 128 + + // The size of the output raw buffer. + // It should be possible to encode the whole output buffer. + output_raw_buffer_size = (output_buffer_size*2 + 2) + + // The size of other stacks and queues. + initial_stack_size = 16 + initial_queue_size = 16 + initial_string_size = 16 +) + +// Check if the character at the specified position is an alphabetical +// character, a digit, '_', or '-'. +func is_alpha(b []byte, i int) bool { + return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'Z' || b[i] >= 'a' && b[i] <= 'z' || b[i] == '_' || b[i] == '-' +} + +// Check if the character at the specified position is a digit. +func is_digit(b []byte, i int) bool { + return b[i] >= '0' && b[i] <= '9' +} + +// Get the value of a digit. +func as_digit(b []byte, i int) int { + return int(b[i]) - '0' +} + +// Check if the character at the specified position is a hex-digit. +func is_hex(b []byte, i int) bool { + return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'F' || b[i] >= 'a' && b[i] <= 'f' +} + +// Get the value of a hex-digit. +func as_hex(b []byte, i int) int { + bi := b[i] + if bi >= 'A' && bi <= 'F' { + return int(bi) - 'A' + 10 + } + if bi >= 'a' && bi <= 'f' { + return int(bi) - 'a' + 10 + } + return int(bi) - '0' +} + +// Check if the character is ASCII. +func is_ascii(b []byte, i int) bool { + return b[i] <= 0x7F +} + +// Check if the character at the start of the buffer can be printed unescaped. +func is_printable(b []byte, i int) bool { + return ((b[i] == 0x0A) || // . == #x0A + (b[i] >= 0x20 && b[i] <= 0x7E) || // #x20 <= . <= #x7E + (b[i] == 0xC2 && b[i+1] >= 0xA0) || // #0xA0 <= . <= #xD7FF + (b[i] > 0xC2 && b[i] < 0xED) || + (b[i] == 0xED && b[i+1] < 0xA0) || + (b[i] == 0xEE) || + (b[i] == 0xEF && // #xE000 <= . <= #xFFFD + !(b[i+1] == 0xBB && b[i+2] == 0xBF) && // && . != #xFEFF + !(b[i+1] == 0xBF && (b[i+2] == 0xBE || b[i+2] == 0xBF)))) +} + +// Check if the character at the specified position is NUL. +func is_z(b []byte, i int) bool { + return b[i] == 0x00 +} + +// Check if the beginning of the buffer is a BOM. +func is_bom(b []byte, i int) bool { + return b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF +} + +// Check if the character at the specified position is space. +func is_space(b []byte, i int) bool { + return b[i] == ' ' +} + +// Check if the character at the specified position is tab. +func is_tab(b []byte, i int) bool { + return b[i] == '\t' +} + +// Check if the character at the specified position is blank (space or tab). +func is_blank(b []byte, i int) bool { + //return is_space(b, i) || is_tab(b, i) + return b[i] == ' ' || b[i] == '\t' +} + +// Check if the character at the specified position is a line break. +func is_break(b []byte, i int) bool { + return (b[i] == '\r' || // CR (#xD) + b[i] == '\n' || // LF (#xA) + b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9) // PS (#x2029) +} + +func is_crlf(b []byte, i int) bool { + return b[i] == '\r' && b[i+1] == '\n' +} + +// Check if the character is a line break or NUL. +func is_breakz(b []byte, i int) bool { + //return is_break(b, i) || is_z(b, i) + return ( + // is_break: + b[i] == '\r' || // CR (#xD) + b[i] == '\n' || // LF (#xA) + b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) + // is_z: + b[i] == 0) +} + +// Check if the character is a line break, space, or NUL. +func is_spacez(b []byte, i int) bool { + //return is_space(b, i) || is_breakz(b, i) + return ( + // is_space: + b[i] == ' ' || + // is_breakz: + b[i] == '\r' || // CR (#xD) + b[i] == '\n' || // LF (#xA) + b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) + b[i] == 0) +} + +// Check if the character is a line break, space, tab, or NUL. +func is_blankz(b []byte, i int) bool { + //return is_blank(b, i) || is_breakz(b, i) + return ( + // is_blank: + b[i] == ' ' || b[i] == '\t' || + // is_breakz: + b[i] == '\r' || // CR (#xD) + b[i] == '\n' || // LF (#xA) + b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) + b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) + b[i] == 0) +} + +// Determine the width of the character. +func width(b byte) int { + // Don't replace these by a switch without first + // confirming that it is being inlined. + if b&0x80 == 0x00 { + return 1 + } + if b&0xE0 == 0xC0 { + return 2 + } + if b&0xF0 == 0xE0 { + return 3 + } + if b&0xF8 == 0xF0 { + return 4 + } + return 0 + +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 6c8b6839f7..0c50d1371b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -61,6 +61,9 @@ cloud.google.com/go/storage/internal/apiv2/storagepb ## explicit; go 1.24.0 filippo.io/edwards25519 filippo.io/edwards25519/field +# github.com/ActiveState/vt10x v1.3.1 +## explicit; go 1.13 +github.com/ActiveState/vt10x # github.com/AlecAivazis/survey/v2 v2.3.7 ## explicit; go 1.13 github.com/AlecAivazis/survey/v2 @@ -827,6 +830,9 @@ github.com/grpc-ecosystem/grpc-gateway/v2/internal/httprule github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options github.com/grpc-ecosystem/grpc-gateway/v2/runtime github.com/grpc-ecosystem/grpc-gateway/v2/utilities +# github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 +## explicit +github.com/h2non/parth # github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b ## explicit; go 1.11 github.com/hako/durafmt @@ -957,6 +963,9 @@ github.com/klauspost/compress/internal/le github.com/klauspost/compress/internal/snapref github.com/klauspost/compress/zstd github.com/klauspost/compress/zstd/internal/xxhash +# github.com/kr/pty v1.1.1 +## explicit +github.com/kr/pty # github.com/ktr0731/go-ansisgr v0.1.0 ## explicit; go 1.19 github.com/ktr0731/go-ansisgr @@ -1262,6 +1271,10 @@ github.com/spiffe/go-spiffe/v2/internal/jwtutil github.com/spiffe/go-spiffe/v2/internal/pemutil github.com/spiffe/go-spiffe/v2/internal/x509util github.com/spiffe/go-spiffe/v2/spiffeid +# github.com/stretchr/testify v1.11.1 +## explicit; go 1.17 +github.com/stretchr/testify/assert +github.com/stretchr/testify/assert/yaml # github.com/subosito/gotenv v1.6.0 ## explicit; go 1.18 github.com/subosito/gotenv @@ -1300,35 +1313,6 @@ github.com/tektoncd/chains/pkg/chains/storage/pubsub github.com/tektoncd/chains/pkg/chains/storage/tekton github.com/tektoncd/chains/pkg/config github.com/tektoncd/chains/pkg/patch -# github.com/tektoncd/hub v1.24.0 => github.com/openshift-pipelines/hub v1.24.0 -## explicit; go 1.25.8 -github.com/tektoncd/hub/api/pkg/cli/app -github.com/tektoncd/hub/api/pkg/cli/cmd -github.com/tektoncd/hub/api/pkg/cli/cmd/check_upgrade -github.com/tektoncd/hub/api/pkg/cli/cmd/downgrade -github.com/tektoncd/hub/api/pkg/cli/cmd/get -github.com/tektoncd/hub/api/pkg/cli/cmd/info -github.com/tektoncd/hub/api/pkg/cli/cmd/install -github.com/tektoncd/hub/api/pkg/cli/cmd/reinstall -github.com/tektoncd/hub/api/pkg/cli/cmd/search -github.com/tektoncd/hub/api/pkg/cli/cmd/upgrade -github.com/tektoncd/hub/api/pkg/cli/flag -github.com/tektoncd/hub/api/pkg/cli/formatter -github.com/tektoncd/hub/api/pkg/cli/gvr -github.com/tektoncd/hub/api/pkg/cli/hub -github.com/tektoncd/hub/api/pkg/cli/installer -github.com/tektoncd/hub/api/pkg/cli/kube -github.com/tektoncd/hub/api/pkg/cli/options -github.com/tektoncd/hub/api/pkg/cli/printer -github.com/tektoncd/hub/api/pkg/cli/version -github.com/tektoncd/hub/api/pkg/git -github.com/tektoncd/hub/api/pkg/parser -github.com/tektoncd/hub/api/v1/gen/catalog -github.com/tektoncd/hub/api/v1/gen/catalog/views -github.com/tektoncd/hub/api/v1/gen/http/catalog/client -github.com/tektoncd/hub/api/v1/gen/http/resource/client -github.com/tektoncd/hub/api/v1/gen/resource -github.com/tektoncd/hub/api/v1/gen/resource/views # github.com/tektoncd/pipeline v1.12.0 ## explicit; go 1.25.7 github.com/tektoncd/pipeline/internal/artifactref @@ -1764,7 +1748,7 @@ go.yaml.in/yaml/v2 # go.yaml.in/yaml/v3 v3.0.4 ## explicit; go 1.16 go.yaml.in/yaml/v3 -# goa.design/goa/v3 v3.23.4 +# goa.design/goa/v3 v3.26.0 ## explicit; go 1.24.0 goa.design/goa/v3/http goa.design/goa/v3/pkg @@ -2158,12 +2142,18 @@ google.golang.org/protobuf/types/known/wrapperspb # gopkg.in/evanphx/json-patch.v4 v4.13.0 ## explicit gopkg.in/evanphx/json-patch.v4 +# gopkg.in/h2non/gock.v1 v1.1.2 +## explicit; go 1.13 +gopkg.in/h2non/gock.v1 # gopkg.in/inf.v0 v0.9.1 ## explicit gopkg.in/inf.v0 # gopkg.in/yaml.v2 v2.4.0 ## explicit; go 1.15 gopkg.in/yaml.v2 +# gopkg.in/yaml.v3 v3.0.1 +## explicit +gopkg.in/yaml.v3 # gotest.tools v2.2.0+incompatible ## explicit gotest.tools/assert @@ -2902,4 +2892,3 @@ sigs.k8s.io/structured-merge-diff/v6/value ## explicit; go 1.22 sigs.k8s.io/yaml # github.com/alibabacloud-go/cr-20160607 => github.com/vdemeester/cr-20160607 v1.0.1 -# github.com/tektoncd/hub => github.com/openshift-pipelines/hub v1.24.0