From f4dae2b44a0ece07a36e07fc6199ca35cc9ebd95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Fri, 28 Jun 2024 18:54:42 +0200 Subject: [PATCH 1/2] Using scripts without relying on vendor dir --- .codecov.yaml | 2 +- .github/workflows/kind-e2e.yaml | 4 +- go.mod | 2 + go.sum | 4 +- hack/release.sh | 5 +- hack/tools.go | 2 +- hack/update-checksums.sh | 7 +- hack/update-codegen.sh | 30 ++-- hack/update-deps.sh | 5 +- hack/update-reference-docs.sh | 7 +- hack/verify-codegen.sh | 5 +- test/e2e-common.sh | 3 +- test/presubmit-tests.sh | 4 +- .../knative.dev/hack}/boilerplate.go.txt | 2 +- vendor/knative.dev/hack/cmd/script/README.md | 49 +++++ vendor/knative.dev/hack/cmd/script/main.go | 28 +++ vendor/knative.dev/hack/codegen-library.sh | 154 ++++++++++++++-- vendor/knative.dev/hack/embed.go | 2 +- vendor/knative.dev/hack/go.work | 2 +- vendor/knative.dev/hack/go.work.sum | 143 +++++++++------ vendor/knative.dev/hack/library.sh | 167 ++++++++++++++---- .../knative.dev/hack/pkg/inflator/cli/app.go | 40 +++++ .../knative.dev/hack/pkg/inflator/cli/exec.go | 51 ++++++ .../hack/pkg/inflator/cli/flags.go | 46 +++++ .../hack/pkg/inflator/cli/print.go | 33 ++++ .../hack/pkg/inflator/cli/usage.go | 25 +++ .../hack/pkg/inflator/extract/errors.go | 24 +++ .../hack/pkg/inflator/extract/extract.go | 136 ++++++++++++++ .../hack/pkg/inflator/extract/logger.go | 12 ++ .../knative.dev/hack/pkg/retcode/retcode.go | 28 +++ vendor/knative.dev/hack/presubmit-tests.sh | 8 +- vendor/knative.dev/hack/release.sh | 6 +- vendor/modules.txt | 9 +- 33 files changed, 905 insertions(+), 140 deletions(-) rename {hack/boilerplate => vendor/knative.dev/hack}/boilerplate.go.txt (93%) create mode 100644 vendor/knative.dev/hack/cmd/script/README.md create mode 100644 vendor/knative.dev/hack/cmd/script/main.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/cli/app.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/cli/exec.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/cli/flags.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/cli/print.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/cli/usage.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/extract/errors.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/extract/extract.go create mode 100644 vendor/knative.dev/hack/pkg/inflator/extract/logger.go create mode 100644 vendor/knative.dev/hack/pkg/retcode/retcode.go diff --git a/.codecov.yaml b/.codecov.yaml index c922a17ba6a..71186579a91 100644 --- a/.codecov.yaml +++ b/.codecov.yaml @@ -4,7 +4,7 @@ coverage: status: project: default: - target: 80 + target: auto # Reject the coverage drop of more than 1% threshold: 1% patch: # Disable the coverage threshold of the patch, so that PRs are diff --git a/.github/workflows/kind-e2e.yaml b/.github/workflows/kind-e2e.yaml index 26129223850..d57058bbbd4 100644 --- a/.github/workflows/kind-e2e.yaml +++ b/.github/workflows/kind-e2e.yaml @@ -66,7 +66,7 @@ jobs: uses: ko-build/setup-ko@v0.6 - name: Check out code onto GOPATH - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install KinD run: | @@ -110,7 +110,7 @@ jobs: - name: Wait for things to be up run: | set -e - source ./vendor/knative.dev/hack/infra-library.sh + source "$(go run knative.dev/hack/cmd/script infra-library.sh)" wait_until_pods_running ${SYSTEM_NAMESPACE} - name: Run e2e Tests diff --git a/go.mod b/go.mod index 55a5c979746..e1dcdf32021 100644 --- a/go.mod +++ b/go.mod @@ -50,6 +50,8 @@ require ( sigs.k8s.io/yaml v1.4.0 ) +replace knative.dev/hack => github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 + require ( contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect contrib.go.opencensus.io/exporter/prometheus v0.4.2 // indirect diff --git a/go.sum b/go.sum index 5046b4dbd16..856b1a06c19 100644 --- a/go.sum +++ b/go.sum @@ -61,6 +61,8 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= github.com/blendle/zapdriver v1.3.1 h1:C3dydBOWYRiOk+B8X9IVZ5IOe+7cl+tGOexN4QqHfpE= github.com/blendle/zapdriver v1.3.1/go.mod h1:mdXfREi6u5MArG4j9fewC+FGnXaBR+T4Ox4J2u4eHCc= +github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 h1:VsOr85OYA4TrKh+GrGrmWlOx2nz/VzsWhzQQLO1T8UA= +github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g= github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw= @@ -838,8 +840,6 @@ k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/A k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA= k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ= k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= -knative.dev/hack v0.0.0-20240607132042-09143140a254 h1:1YFnu3U6dWZg0oxm6GU8kEdA9A+BvSWKJO7sg3N0kq8= -knative.dev/hack v0.0.0-20240607132042-09143140a254/go.mod h1:yk2OjGDsbEnQjfxdm0/HJKS2WqTLEFg/N6nUs6Rqx3Q= knative.dev/hack/schema v0.0.0-20240607132042-09143140a254 h1:b9hFHGtxx0Kpm4EEjSD72lL0jms91To3OEVBTbqfOYI= knative.dev/hack/schema v0.0.0-20240607132042-09143140a254/go.mod h1:3pWwBLnTZSM9psSgCAvhKOHIPTzqfEMlWRpDu6IYhK0= knative.dev/pkg v0.0.0-20240626134149-3f6a546ac3a4 h1:slPKf3UKdBFZlz+hFy+KXzTgY9yOePLzRuEhKzgc5a4= diff --git a/hack/release.sh b/hack/release.sh index ebfc50cf0e1..e21ee98212c 100755 --- a/hack/release.sh +++ b/hack/release.sh @@ -17,7 +17,8 @@ # Documentation about this script and how to use it can be found # at https://github.com/knative/test-infra/tree/main/ci -source $(dirname $0)/../vendor/knative.dev/hack/release.sh +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script release.sh)" function build_release() { # Run `generate-yamls.sh`, which should be versioned with the @@ -32,4 +33,4 @@ function build_release() { fi } -main $@ +main "$@" diff --git a/hack/tools.go b/hack/tools.go index 622e5c32556..3e8e8109244 100644 --- a/hack/tools.go +++ b/hack/tools.go @@ -20,7 +20,7 @@ limitations under the License. package tools import ( - _ "knative.dev/hack" + _ "knative.dev/hack/cmd/script" _ "knative.dev/pkg/configmap/hash-gen" _ "knative.dev/pkg/hack" diff --git a/hack/update-checksums.sh b/hack/update-checksums.sh index b047f13dfa8..a5cd53af1ac 100755 --- a/hack/update-checksums.sh +++ b/hack/update-checksums.sh @@ -24,7 +24,8 @@ if [ -z "${GOPATH:-}" ]; then export GOPATH=$(go env GOPATH) fi -source $(dirname $0)/../vendor/knative.dev/hack/library.sh +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script library.sh)" -go run "${REPO_ROOT_DIR}/vendor/knative.dev/pkg/configmap/hash-gen" "${REPO_ROOT_DIR}"/config/core/configmaps/*.yaml -go run "${REPO_ROOT_DIR}/vendor/knative.dev/pkg/configmap/hash-gen" "${REPO_ROOT_DIR}"/config/channels/in-memory-channel/configmaps/*.yaml +go run knative.dev/pkg/configmap/hash-gen "${REPO_ROOT_DIR}"/config/core/configmaps/*.yaml +go run knative.dev/pkg/configmap/hash-gen "${REPO_ROOT_DIR}"/config/channels/in-memory-channel/configmaps/*.yaml diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index fdeb803f9ca..8dce900a229 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -18,17 +18,18 @@ set -o errexit set -o nounset set -o pipefail -source $(dirname $0)/../vendor/knative.dev/hack/codegen-library.sh +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script codegen-library.sh)" # If we run with -mod=vendor here, then generate-groups.sh looks for vendor files in the wrong place. export GOFLAGS=-mod= -echo "=== Update Codegen for $MODULE_NAME" +log.step "Update Codegen for $MODULE_NAME" # Compute _example hash for all configmaps. group "Generating checksums for configmap _example keys" -${REPO_ROOT_DIR}/hack/update-checksums.sh +"${REPO_ROOT_DIR}/hack/update-checksums.sh" group "Kubernetes Codegen" @@ -36,37 +37,34 @@ group "Kubernetes Codegen" # --output-base because this script should also be able to run inside the vendor dir of # k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir # instead of the $GOPATH directly. For normal projects this can be dropped. -${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \ +generate-groups "deepcopy,client,informer,lister" \ knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \ - "sinks:v1alpha1 eventing:v1alpha1 eventing:v1beta1 eventing:v1beta2 eventing:v1beta3 eventing:v1 messaging:v1 flows:v1 sources:v1beta2 sources:v1" \ - --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt + "sinks:v1alpha1 eventing:v1alpha1 eventing:v1beta1 eventing:v1beta2 eventing:v1beta3 eventing:v1 messaging:v1 flows:v1 sources:v1beta2 sources:v1" # Deep copy config -${GOPATH}/bin/deepcopy-gen \ +"${GOBIN}/deepcopy-gen" \ -O zz_generated.deepcopy \ - --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \ + --go-header-file "$(boilerplate)" \ -i knative.dev/eventing/pkg/apis/config \ -i knative.dev/eventing/pkg/apis/messaging/config \ # Only deepcopy the Duck types, as they are not real resources. -${CODEGEN_PKG}/generate-groups.sh "deepcopy" \ +generate-groups "deepcopy" \ knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \ - "duck:v1beta1 duck:v1alpha1 duck:v1" \ - --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt + "duck:v1beta1 duck:v1alpha1 duck:v1" group "Knative Codegen" # Knative Injection -${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \ +generate-knative "injection" \ knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \ - "sinks:v1alpha1 eventing:v1alpha1 eventing:v1beta1 eventing:v1beta2 eventing:v1beta3 eventing:v1 messaging:v1 flows:v1 sources:v1beta2 sources:v1 duck:v1beta1 duck:v1" \ - --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt + "sinks:v1alpha1 eventing:v1alpha1 eventing:v1beta1 eventing:v1beta2 eventing:v1beta3 eventing:v1 messaging:v1 flows:v1 sources:v1beta2 sources:v1 duck:v1beta1 duck:v1" group "Generating API reference docs" -${REPO_ROOT_DIR}/hack/update-reference-docs.sh +"${REPO_ROOT_DIR}/hack/update-reference-docs.sh" group "Update deps post-codegen" # Make sure our dependencies are up-to-date -${REPO_ROOT_DIR}/hack/update-deps.sh +"${REPO_ROOT_DIR}/hack/update-deps.sh" diff --git a/hack/update-deps.sh b/hack/update-deps.sh index 2abc69bd9c0..ab18be727a7 100755 --- a/hack/update-deps.sh +++ b/hack/update-deps.sh @@ -18,8 +18,9 @@ set -o errexit set -o nounset set -o pipefail -source $(dirname "$0")/../vendor/knative.dev/hack/library.sh +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script library.sh)" -$(dirname $0)/update-cert-manager.sh +"$(dirname "$0")/update-cert-manager.sh" go_update_deps "$@" diff --git a/hack/update-reference-docs.sh b/hack/update-reference-docs.sh index cd6e3209062..ef85773999e 100755 --- a/hack/update-reference-docs.sh +++ b/hack/update-reference-docs.sh @@ -24,11 +24,12 @@ if [ -z "${GOPATH:-}" ]; then export GOPATH=$(go env GOPATH) fi -source $(dirname $0)/../vendor/knative.dev/hack/library.sh +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script library.sh)" ( - cd ${REPO_ROOT_DIR} - go run "${REPO_ROOT_DIR}/vendor/github.com/ahmetb/gen-crd-api-reference-docs" \ + cd "${REPO_ROOT_DIR}" + go run github.com/ahmetb/gen-crd-api-reference-docs \ -out-file "docs/eventing-api.md" \ -api-dir "knative.dev/eventing/pkg/apis" \ -template-dir "${REPO_ROOT_DIR}/vendor/github.com/ahmetb/gen-crd-api-reference-docs/template" \ diff --git a/hack/verify-codegen.sh b/hack/verify-codegen.sh index 6b7f1dcc71a..0628c626993 100755 --- a/hack/verify-codegen.sh +++ b/hack/verify-codegen.sh @@ -20,9 +20,10 @@ set -o pipefail export GO111MODULE=on -source $(dirname $0)/../vendor/knative.dev/hack/library.sh +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script library.sh)" -readonly TMP_DIFFROOT="$(mktemp -d ${REPO_ROOT_DIR}/tmpdiffroot.XXXXXX)" +readonly TMP_DIFFROOT="$(mktemp -d "${REPO_ROOT_DIR}/tmpdiffroot.XXXXXX")" cleanup() { rm -rf "${TMP_DIFFROOT}" diff --git a/test/e2e-common.sh b/test/e2e-common.sh index 20e052c9335..9ed9a974672 100755 --- a/test/e2e-common.sh +++ b/test/e2e-common.sh @@ -20,7 +20,8 @@ export GO111MODULE=on -source "$(dirname "${BASH_SOURCE[0]}")/../vendor/knative.dev/hack/e2e-tests.sh" +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script e2e-tests.sh)" # If gcloud is not available make it a no-op, not an error. which gcloud &>/dev/null || gcloud() { echo "[ignore-gcloud $*]" 1>&2; } diff --git a/test/presubmit-tests.sh b/test/presubmit-tests.sh index 42a2933f6ae..8435002ce98 100755 --- a/test/presubmit-tests.sh +++ b/test/presubmit-tests.sh @@ -29,7 +29,9 @@ export GO111MODULE=on export KO_FLAGS="--platform=linux/amd64" -source "$(dirname "${BASH_SOURCE[0]}")/../vendor/knative.dev/hack/presubmit-tests.sh" + +# shellcheck disable=SC1090 +source "$(go run knative.dev/hack/cmd/script presubmit-tests.sh)" # We use the default build, unit and integration test runners. diff --git a/hack/boilerplate/boilerplate.go.txt b/vendor/knative.dev/hack/boilerplate.go.txt similarity index 93% rename from hack/boilerplate/boilerplate.go.txt rename to vendor/knative.dev/hack/boilerplate.go.txt index eaf9f8fc549..8fb5f9cdf5c 100644 --- a/hack/boilerplate/boilerplate.go.txt +++ b/vendor/knative.dev/hack/boilerplate.go.txt @@ -1,5 +1,5 @@ /* -Copyright 2021 The Knative Authors +Copyright #{YEAR} The Knative Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/vendor/knative.dev/hack/cmd/script/README.md b/vendor/knative.dev/hack/cmd/script/README.md new file mode 100644 index 00000000000..66579b2493a --- /dev/null +++ b/vendor/knative.dev/hack/cmd/script/README.md @@ -0,0 +1,49 @@ +# Vendorless Knative + +The Knative projects can be built without a vendor directory. This is a +convenience for developers, and brings a number of benefits: + +* It is easier to see the changes to the code and review them. +* It is easier to maintain the build and CI scripts, as they don't need to + filter out the vendor directory. +* The project doesn't look dated (the proper dependency management tools + are available for Go since 1.13+). +* No vendor directory means less possibility of accidentally traversing + into it by symlinks or scripts. + +For more details and reasons for avoiding the vendor directory, see +[knative/infra#134](https://github.com/knative/infra/issues/134). + +## Status + +The [knative/infra#134](https://github.com/knative/infra/issues/134) is +ongoing effort. Currently, it is possible to use make projects vendorless, +only if they don't use Knative nor Kubernetes code-generation tools. See the +epic issue for current status. + +## Migration to a vendorless project + +The following steps are required to migrate a project to be vendorless: + +1. Update the `knative.dev/hack` dependency to the latest version. +1. Update the project scripts to use the scripts inflation: + ```patch + -source $(dirname $0)/../vendor/knative.dev/hack/release.sh + +source "$(go run knative.dev/hack/cmd/script release.sh)" + ``` +1. Update the `hack/tools.go` file to refer to the `knative.dev/hack/cmd/script` + tool: + ```go + package hack + + import ( + _ "knative.dev/hack/cmd/script" + ) + ``` +1. Remove the `vendor` directory. +1. Run `hack/update-deps.sh` to update the `go.mod` file(s). + +### Examples of migrated projects + +* [knative/func#1966](https://github.com/knative/func/pull/1966) +* [knative-extensions/kn-plugin-event#307](https://github.com/knative-extensions/kn-plugin-event/pull/307) diff --git a/vendor/knative.dev/hack/cmd/script/main.go b/vendor/knative.dev/hack/cmd/script/main.go new file mode 100644 index 00000000000..288e10710f7 --- /dev/null +++ b/vendor/knative.dev/hack/cmd/script/main.go @@ -0,0 +1,28 @@ +/* +Copyright 2022 The Knative 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 + + https://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 main + +import "knative.dev/hack/pkg/inflator/cli" + +func main() { + cli.ExecuteOrDie(cli.Options...) +} + +// RunMain is used by tests to run the main function. +func RunMain() { // nolint:deadcode + main() +} diff --git a/vendor/knative.dev/hack/codegen-library.sh b/vendor/knative.dev/hack/codegen-library.sh index 5f713dd4364..bc37859fd07 100644 --- a/vendor/knative.dev/hack/codegen-library.sh +++ b/vendor/knative.dev/hack/codegen-library.sh @@ -16,21 +16,151 @@ # Setup the env for doing Knative style codegen. -kn_hack_library=${kn_hack_library:-"$(dirname $0)/../vendor/knative.dev/hack/library.sh"} +# Store Bash options +oldstate="$(set +o)" + +set -Eeuo pipefail + +export repodir kn_hack_dir kn_hack_library \ + MODULE_NAME CODEGEN_TMP_GOPATH CODEGEN_ORIGINAL_GOPATH GOPATH GOBIN \ + CODEGEN_PKG KNATIVE_CODEGEN_PKG + +kn_hack_dir="$(realpath "$(dirname "${BASH_SOURCE[0]:-$0}")")" +kn_hack_library=${kn_hack_library:-"${kn_hack_dir}/library.sh"} if [[ -f "$kn_hack_library" ]]; then - source $kn_hack_library + # shellcheck disable=SC1090 + source "$kn_hack_library" else - echo "this file is intended to be imported from a golang project that vendors knative.dev/hack" - exit + echo "The \$kn_hack_library points to a non-existent file: $kn_hack_library" >&2 + exit 42 fi -export MODULE_NAME=$(go_mod_module_name) -export GOPATH=$(go_mod_gopath_hack) -export GOBIN=${GOPATH}/bin # Set GOBIN explicitly as deepcopy-gen is installed by go install. -export CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${REPO_ROOT_DIR}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)} -export KNATIVE_CODEGEN_PKG=${KNATIVE_CODEGEN_PKG:-$(cd ${REPO_ROOT_DIR}; ls -d -1 ./vendor/knative.dev/pkg 2>/dev/null || echo "${REPO_ROOT_DIR}")} +repodir="$(go_run knative.dev/toolbox/modscope@latest current --path)" + +function go-resolve-pkg-dir() { + local pkg="${1:?Pass the package name}" + local pkgdir + if [ -d "${repodir}/vendor" ]; then + pkgdir="${repodir}/vendor/${pkg}" + if [ -d "${pkgdir}" ]; then + echo "${pkgdir}" + return 0 + else + return 1 + fi + else + go mod download -x > /dev/stderr + go list -m -f '{{.Dir}}' "${pkg}" 2>/dev/null + return $? + fi +} + +# Change dir to the original executing script's directory, not the current source! +pushd "$(dirname "$(realpath "$0")")" > /dev/null + +if ! CODEGEN_PKG="${CODEGEN_PKG:-"$(go-resolve-pkg-dir k8s.io/code-generator)"}"; then + warning "Failed to determine the k8s.io/code-generator package" +fi +if ! KNATIVE_CODEGEN_PKG="${KNATIVE_CODEGEN_PKG:-"$(go-resolve-pkg-dir knative.dev/pkg)"}"; then + warning "Failed to determine the knative.dev/pkg package" +fi + +popd > /dev/null + +CODEGEN_ORIGINAL_GOPATH="$(go env GOPATH)" +CODEGEN_TMP_GOPATH=$(go_mod_gopath_hack) +GOPATH="${CODEGEN_TMP_GOPATH}" +GOBIN="${GOPATH}/bin" # Set GOBIN explicitly as k8s-gen' are installed by go install. + +if [[ -n "${CODEGEN_PKG}" ]] && ! [ -x "${CODEGEN_PKG}/generate-groups.sh" ]; then + chmod +x "${CODEGEN_PKG}/generate-groups.sh" + chmod +x "${CODEGEN_PKG}/generate-internal-groups.sh" +fi +if [[ -n "${KNATIVE_CODEGEN_PKG}" ]] && ! [ -x "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" ]; then + chmod +x "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" +fi + +# Generate boilerplate file with the current year +function boilerplate() { + local go_header_file="${kn_hack_dir}/boilerplate.go.txt" + local current_boilerplate_file="${TMPDIR}/boilerplate.go.txt" + # Replace #{YEAR} with the current year + sed "s/#{YEAR}/$(date +%Y)/" \ + < "${go_header_file}" \ + > "${current_boilerplate_file}" + echo "${current_boilerplate_file}" +} + +# Generate K8s' groups codegen +function generate-groups() { + if [[ -z "${CODEGEN_PKG}" ]]; then + abort "CODEGEN_PKG is not set" + fi + "${CODEGEN_PKG}"/generate-groups.sh \ + "$@" \ + --go-header-file "$(boilerplate)" +} + +# Generate K8s' internal groups codegen +function generate-internal-groups() { + if [[ -z "${CODEGEN_PKG}" ]]; then + abort "CODEGEN_PKG is not set" + fi + "${CODEGEN_PKG}"/generate-internal-groups.sh \ + "$@" \ + --go-header-file "$(boilerplate)" +} + +# Generate Knative style codegen +function generate-knative() { + if [[ -z "${KNATIVE_CODEGEN_PKG}" ]]; then + abort "KNATIVE_CODEGEN_PKG is not set" + fi + "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" \ + "$@" \ + --go-header-file "$(boilerplate)" +} + +# Cleanup after generating code +function cleanup-codegen() { + restore-changes-if-its-copyright-year-only + restore-gopath +} + +# Restore changes if the file contains only the change in the copyright year +function restore-changes-if-its-copyright-year-only() { + local difflist + log "Cleaning up generated code" + difflist="$(mktemp)" + git diff --exit-code --name-only > "$difflist" + # list git changes and skip those which differ only in the boilerplate year + while read -r file; do + # check if the file contains just the change in the boilerplate year + if [ "$(LANG=C git diff --exit-code --shortstat -- "$file")" = ' 1 file changed, 1 insertion(+), 1 deletion(-)' ] && \ + [[ "$(git diff --exit-code -U1 -- "$file" | grep -Ec '^[+-]\s*[*#]?\s*Copyright 2[0-9]{3}')" -eq 2 ]]; then + # restore changes to that file + git checkout -- "$file" + fi + done < "$difflist" + rm -f "$difflist" +} + +# Restore the GOPATH and clean up the temporary directory +function restore-gopath() { + # Skip this if the directory is already checked out onto the GOPATH. + if __is_checkout_onto_gopath; then + return + fi + if [ -n "$CODEGEN_TMP_GOPATH" ] && [ -d "$CODEGEN_TMP_GOPATH" ]; then + chmod -R u+w "${CODEGEN_TMP_GOPATH}" + rm -rf "${CODEGEN_TMP_GOPATH}" + unset CODEGEN_TMP_GOPATH + fi + unset CODEGEN_ORIGINAL_GOPATH GOPATH GOBIN +} + +add_trap cleanup-codegen EXIT -[ -x ${CODEGEN_PKG}/generate-groups.sh ] || chmod +x ${CODEGEN_PKG}/generate-groups.sh -[ -x ${CODEGEN_PKG}/generate-internal-groups.sh ] || chmod +x ${CODEGEN_PKG}/generate-internal-groups.sh -[ -x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh ] || chmod +x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh +# Restore Bash options +eval "$oldstate" diff --git a/vendor/knative.dev/hack/embed.go b/vendor/knative.dev/hack/embed.go index 62f856ec68c..aedb546df13 100644 --- a/vendor/knative.dev/hack/embed.go +++ b/vendor/knative.dev/hack/embed.go @@ -20,5 +20,5 @@ import ( "embed" ) -//go:embed *.sh +//go:embed *.sh *.go.txt var Scripts embed.FS diff --git a/vendor/knative.dev/hack/go.work b/vendor/knative.dev/hack/go.work index b6265ad9e26..0c69c9e28d6 100644 --- a/vendor/knative.dev/hack/go.work +++ b/vendor/knative.dev/hack/go.work @@ -1,4 +1,4 @@ -go 1.18 +go 1.21 use ( . diff --git a/vendor/knative.dev/hack/go.work.sum b/vendor/knative.dev/hack/go.work.sum index 1cc0cde9370..f8a39b236e2 100644 --- a/vendor/knative.dev/hack/go.work.sum +++ b/vendor/knative.dev/hack/go.work.sum @@ -1,61 +1,102 @@ -cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ= -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46 h1:lsxEuwrXEAokXB9qhlbKWPpo3KMLZQ5WB5WLQRW1uq0= -github.com/PuerkitoBio/purell v1.0.0 h1:0GoNN3taZV6QI81IXgCbxMyEaJDXMSIjArYBCYzVVvs= -github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2 h1:JCHLVE3B+kJde7bIEo5N4J+ZbLhp0J1Fs+ulyRws4gE= -github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk= -github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI= +github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= +github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46aU4V9E= +github.com/alecthomas/chroma/v2 v2.14.0/go.mod h1:QolEbTfmUHIMVpBqxeDnNBj2uoeI4EbYP4i6n68SG4I= +github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA= +github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os= +github.com/alecthomas/mango-kong v0.1.0 h1:iFVfP1k1K4qpml3JUQmD5I8MCQYfIvsD9mRdrw7jJC4= +github.com/alecthomas/mango-kong v0.1.0/go.mod h1:t+TYVdsONUolf/BwVcm+15eqcdAj15h4Qe9MMFAwwT4= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA= +github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= +github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= +github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA= +github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc= +github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0= +github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw= +github.com/charmbracelet/bubbletea v0.26.3 h1:iXyGvI+FfOWqkB2V07m1DF3xxQijxjY2j8PqiXYqasg= +github.com/charmbracelet/bubbletea v0.26.3/go.mod h1:bpZHfDHTYJC5g+FBK+ptJRCQotRC+Dhh3AoMxa/2+3Q= +github.com/charmbracelet/glamour v0.7.0 h1:2BtKGZ4iVJCDfMF229EzbeR1QRKLWztO9dMtjmqZSng= +github.com/charmbracelet/glamour v0.7.0/go.mod h1:jUMh5MeihljJPQbJ/wf4ldw2+yBP59+ctV36jASy7ps= +github.com/charmbracelet/huh v0.4.2 h1:5wLkwrA58XDAfEZsJzNQlfJ+K8N9+wYwvR5FOM7jXFM= +github.com/charmbracelet/huh v0.4.2/go.mod h1:g9OXBgtY3zRV4ahnVih9bZE+1yGYN+y2C9Q6L2P+WM0= +github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= +github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM= +github.com/charmbracelet/x/exp/strings v0.0.0-20240525152034-77596eb8760e h1:DhvN6ye3nHLhRtNHtlrQ0Zk+vmeN7YtEnyIRfcl7e0E= +github.com/charmbracelet/x/exp/strings v0.0.0-20240525152034-77596eb8760e/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ= +github.com/charmbracelet/x/input v0.1.1 h1:YDOJaTUKCqtGnq9PHzx3pkkl4pXDOANUHmhH3DqMtM4= +github.com/charmbracelet/x/input v0.1.1/go.mod h1:jvdTVUnNWj/RD6hjC4FsoB0SeZCJ2ZBkiuFP9zXvZI0= +github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI= +github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw= +github.com/charmbracelet/x/windows v0.1.2 h1:Iumiwq2G+BRmgoayww/qfcvof7W/3uLoelhxojXlRWg= +github.com/charmbracelet/x/windows v0.1.2/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w= -github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 h1:cenwrSVm+Z7QLSV/BsnenAOcDXdX4cMv4wP0B/5QbPg= -github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= -github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc= -github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633 h1:H2pdYOb3KQ1/YsqVWoWNLQO+fusocsw354rqGTZtAgw= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473 h1:4cmBvAEBNJaGARUEs3/suWRyfyBfhf7I60WBZq+bv2w= -github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A= -github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses= -github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= -github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680 h1:ZktWZesgun21uEDrwW7iEV1zPCGQldM2atlJZ3TdvVM= -github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1 h1:wSt/4CYxs70xbATrGXhokKF1i0tZjENLOo1ioIO13zk= -github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9 h1:tF+augKRWlWx0J0B7ZyyKSiTyV6E1zZe+7b3qQlcEf8= -github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501 h1:C1JKChikHGpXwT5UQDFaryIpDtyyGL/CR6C2kB7F1oc= -github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87 h1:zP3nY8Tk2E6RTkqGYrarZXuzh+ffyLDljLxCy1iJw80= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= -github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 h1:5ZkaAPbicIKTF2I64qf5Fh8Aa83Q/dnOafMYV0OMwjA= -github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8= -github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= -github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= -github.com/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyycI+I= -github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= -github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= -github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E= +github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= +github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84= +github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8= +github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0= +github.com/kisielk/errcheck v1.5.0 h1:e8esj/e4R+SAOwFwN+n3zr0nYeCyeweozKfO23MvHzY= github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg= -github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= -github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a h1:TpvdAwDAt1K4ANVOfcihouRdvP+MgAfDWwBuct4l6ZY= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58= +github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs= +github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= +github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/mango v0.2.0 h1:iNNc0c5VLQ6fsMgAqGQofByNUBH2Q2nEbD6TaI+5yyQ= +github.com/muesli/mango v0.2.0/go.mod h1:5XFpbC8jY5UUv89YQciiXNlbi+iJgt29VDC5xbzrLL4= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8= +github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d h1:7PxY7LVfSZm7PEeBTyK1rj1gABdCO2mbri6GKO1cMDs= +github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus= -github.com/onsi/ginkgo v1.11.0 h1:JAKSXpt1YjtLA7YpPiqO9ss6sNXEsPfSGdwN0UHqzrw= -github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME= +github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA= +github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4 h1:c2HOrn5iMezYjSlGPncknSEr/8x5LELb/ilJbXi9DEA= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 h1:XQyxROzUlZH+WIQwySDgnISgOivlhjIEwaQaJEJrrN0= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs= -golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= -golang.org/x/sys v0.0.0-20201112073958-5cba982894dd h1:5CtCZbICpIOFdgO940moixOPjc0178IU44m4EjOO5IY= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= -google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= -google.golang.org/protobuf v1.24.0 h1:UhZDfRO8JRQru4/+LlLE0BRKGF8L+PICnvYZmx/fEGA= -gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs= -k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac h1:sAvhNk5RRuc6FNYGqe7Ygz3PSo/2wGWbulskmzRX8Vs= -k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6 h1:+WnxoVtG8TMiudHBSEtrVL1egv36TkkJm+bA8AxicmQ= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U= +github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= +github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GAsO5s= +github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4= +golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= diff --git a/vendor/knative.dev/hack/library.sh b/vendor/knative.dev/hack/library.sh index e5d21b70e67..f4ef09c6a58 100644 --- a/vendor/knative.dev/hack/library.sh +++ b/vendor/knative.dev/hack/library.sh @@ -138,18 +138,72 @@ function calcRetcode() { echo "$rc" } +# Print error message. +# Parameters: $* - error message to be displayed +function error() { + local first="$1" + shift + local args=("ERROR: $first" "$@") + gum_banner \ + --color '#D00' \ + --padding '1 3' \ + --border double \ + "${args[@]}" > /dev/stderr +} + # Print error message and call exit(n) where n calculated from the error message. # Parameters: $1..$n - error message to be displayed # Globals: abort_retcode will change the default retcode to be returned function abort() { - make_banner '*' "ERROR: $*" >&2 + error "$@" readonly abort_retcode="${abort_retcode:-$(calcRetcode "$*")}" exit "$abort_retcode" } +# Simple header for logging purposes. +function header() { + local upper + upper="$(echo "$*" | tr '[:lower:]' '[:upper:]')" + gum_banner \ + --border double \ + --color 44 \ + --padding '1 3' \ + "$upper" +} + +# Simple subheader for logging purposes. +function subheader() { + gum_banner \ + --color 45 \ + "$@" +} + +# Simple log step for logging purposes. +function log.step() { + echo "=== $*" | gum_style --foreground 44 +} + +# Simple log for logging purposes. +function log() { + echo "--- $*" | gum_style --foreground 45 +} + +# Simple warning banner for logging purposes. +function warning() { + local first="$1" + shift + local args=("WARN: $first" "$@") + gum_banner \ + --color '#DD0' \ + --padding '1 3' \ + --border rounded \ + "${args[@]}" > /dev/stderr +} + # Display a box banner. # Parameters: $1 - character to use for the box. # $2 - banner message. +# Deprecated: Use `gum_banner` instead. function make_banner() { local msg="$1$1$1$1 $2 $1$1$1$1" local border="${msg//[^$1]/$1}" @@ -161,25 +215,62 @@ function make_banner() { fi } -# Simple header for logging purposes. -function header() { - local upper="$(echo $1 | tr a-z A-Z)" - make_banner "=" "${upper}" -} - -# Simple subheader for logging purposes. -function subheader() { - make_banner "-" "$1" +# Display a fancy box banner. +# Parameters: +# [--border ] - a gum border type for the box, defaults to 'rounded' +# [--color ] - a gum color for the box, defaults to '0'' +# [--padding ] - a gum padding for the box, defaults to '0 1' +# $* - banner message. +function gum_banner() { + local border='rounded' + local color='0' + local padding='0 1' + while [[ $# -gt 0 ]]; do + case "$1" in + --border) + border="$2" + shift 2 + ;; + --color) + color="$2" + shift 2 + ;; + --padding) + padding="$2" + shift 2 + ;; + *) + break + ;; + esac + done + local args=( + --align center + --border "$border" + --foreground "$color" + --border-foreground "$color" + --padding "$padding" + "$@" + ) + # TODO: Remove once logs have timestamps on Prow, details see: + # https://github.com/kubernetes/test-infra/issues/10100 + if (( IS_PROW )); then + local dt + # RFC3339Nano format with 3 digits of ns without timezone offset + dt="$(TZ='UTC' date --rfc-3339=ns | sed -E 's/\.([0-9]{3})[0-9]+.+$/.\1/')" + args+=('' "at $dt") + fi + gum_style "${args[@]}" } -# Simple warning banner for logging purposes. -function warning() { - make_banner '!' "WARN: $*" >&2 +# Simple info banner for logging purposes. +function gum_style() { + go_run github.com/charmbracelet/gum@v0.14.1 style "$@" } # Checks whether the given function exists. function function_exists() { - [[ "$(type -t $1)" == "function" ]] + [[ "$(type -t "$1")" == "function" ]] } # GitHub Actions aware output grouping. @@ -187,7 +278,7 @@ function group() { # End the group is there is already a group. if [ -z ${__GROUP_TRACKER+x} ]; then export __GROUP_TRACKER="grouping" - trap end_group EXIT + add_trap end_group EXIT else end_group fi @@ -198,10 +289,10 @@ function group() { # GitHub Actions aware output grouping. function start_group() { if [[ -n ${GITHUB_WORKFLOW:-} ]]; then - echo "::group::$@" - trap end_group EXIT + echo "::group::$*" + add_trap end_group EXIT else - echo "--- $@" + log "$@" fi } @@ -517,7 +608,7 @@ function report_go_test() { echo "Test log (ANSI) written to ${ansilog}" htmllog="${logfile/.jsonl/.html}" - go_run github.com/buildkite/terminal-to-html/v3/cmd/terminal-to-html@v3.11.0 \ + go_run github.com/buildkite/terminal-to-html/v3/cmd/terminal-to-html@v3.10.0 \ --preview < "$ansilog" > "$htmllog" echo "Test log (HTML) written to ${htmllog}" @@ -682,18 +773,18 @@ function go_update_deps() { function __clean_goworksum_if_exists() { if [ -f "$REPO_ROOT_DIR/go.work.sum" ]; then - echo "=== Cleaning the go.work.sum file" + log.step 'Cleaning the go.work.sum file' true > "$REPO_ROOT_DIR/go.work.sum" fi } function __remove_goworksum_if_empty() { if [ -f "$REPO_ROOT_DIR/go.work" ]; then - echo "=== Syncing the go workspace" + log.step 'Syncing the go workspace' go work sync fi if ! [ -s "$REPO_ROOT_DIR/go.work.sum" ]; then - echo "=== Removing empty go.work.sum" + log.step 'Removing empty go.work.sum' rm -f "$REPO_ROOT_DIR/go.work.sum" fi } @@ -706,7 +797,7 @@ function __go_update_deps_for_module() { export GONOSUMDB="${GONOSUMDB:-},knative.dev/*" export GONOPROXY="${GONOPROXY:-},knative.dev/*" - echo "=== Update Deps for Golang module: $(go_mod_module_name)" + log.step "Update Deps for Golang module: $(go_mod_module_name)" local UPGRADE=0 local RELEASE="v9000.1" # release v9000 is so far in the future, it will always pick the default branch. @@ -752,7 +843,12 @@ function __go_update_deps_for_module() { if [[ "${FORCE_VENDOR:-false}" == "true" ]] || [ -d vendor ]; then group "Go mod vendor" - go mod vendor 2>&1 | grep -v "ignoring symlink" || true + # Call go work vendor for Go 1.22+ and go.work file exists. + if [ -f "$REPO_ROOT_DIR/go.work" ] && go help work vendor >/dev/null 2>&1; then + go work vendor + else + go mod vendor + fi else go mod download -x fi @@ -785,22 +881,32 @@ function go_mod_module_name() { go_run knative.dev/toolbox/modscope@latest current } +function __is_checkout_onto_gopath() { + ! [ "${REPO_ROOT_DIR##"$(go env GOPATH)"}" = "$REPO_ROOT_DIR" ] +} + # Return a GOPATH to a temp directory. Works around the out-of-GOPATH issues # for k8s client gen mixed with go mod. # Intended to be used like: # export GOPATH=$(go_mod_gopath_hack) function go_mod_gopath_hack() { - # Skip this if the directory is already checked out onto the GOPATH. - if [[ "${REPO_ROOT_DIR##$(go env GOPATH)}" != "$REPO_ROOT_DIR" ]]; then + # Skip this if the directory is already checked out onto the GOPATH. + if __is_checkout_onto_gopath; then go env GOPATH return fi - local TMP_DIR="$(mktemp -d)" - local TMP_REPO_PATH="${TMP_DIR}/src/$(go_mod_module_name)" - mkdir -p "$(dirname "${TMP_REPO_PATH}")" && ln -s "${REPO_ROOT_DIR}" "${TMP_REPO_PATH}" + local TMP_GOPATH TMP_REPO_PATH + TMP_GOPATH="$TMPDIR/go" + TMP_REPO_PATH="${TMP_GOPATH}/src/$(go_mod_module_name)" + if [ -d "${TMP_REPO_PATH}" ]; then + echo "${TMP_GOPATH}" + return + fi + mkdir -p "$(dirname "${TMP_REPO_PATH}")" + ln -s "${REPO_ROOT_DIR}" "${TMP_REPO_PATH}" - echo "${TMP_DIR}" + echo "${TMP_GOPATH}" } # Run kntest tool @@ -1014,6 +1120,7 @@ function latest_version() { # Initializations that depend on previous functions. # These MUST come last. +MODULE_NAME="$(go_mod_module_name)" readonly _TEST_INFRA_SCRIPTS_DIR="$(dirname $(get_canonical_path "${BASH_SOURCE[0]}"))" readonly REPO_NAME_FORMATTED="Knative $(capitalize "${REPO_NAME//-/ }")" diff --git a/vendor/knative.dev/hack/pkg/inflator/cli/app.go b/vendor/knative.dev/hack/pkg/inflator/cli/app.go new file mode 100644 index 00000000000..445adca2df8 --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/cli/app.go @@ -0,0 +1,40 @@ +package cli + +import ( + "fmt" + + "knative.dev/hack/pkg/inflator/extract" + "knative.dev/hack/pkg/retcode" +) + +// Execute will execute the application. +func Execute(opts []Option) Result { + ex := Execution{}.Default().Configure(opts) + fl, err := parseArgs(&ex) + if err != nil { + return Result{ + Execution: ex, + Err: err, + } + } + op := createOperation(fl, ex.Args) + return Result{ + Execution: ex, + Err: op.Extract(ex), + } +} + +// ExecuteOrDie will execute the application or perform os.Exit in case of error. +func ExecuteOrDie(opts ...Option) { + if r := Execute(opts); r.Err != nil { + r.PrintErrln(fmt.Sprintf("%v", r.Err)) + r.Exit(retcode.Calc(r.Err)) + } +} + +func createOperation(fl *flags, argv []string) extract.Operation { + return extract.Operation{ + ScriptName: argv[0], + Verbose: fl.verbose, + } +} diff --git a/vendor/knative.dev/hack/pkg/inflator/cli/exec.go b/vendor/knative.dev/hack/pkg/inflator/cli/exec.go new file mode 100644 index 00000000000..3f3617b2d82 --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/cli/exec.go @@ -0,0 +1,51 @@ +package cli + +import ( + "io" + "os" +) + +// Execution is used to execute a command. +type Execution struct { + Args []string + Stdout io.Writer + Stderr io.Writer + Exit func(code int) +} + +// Default will set default values for the execution. +func (e Execution) Default() Execution { + if e.Stdout == nil { + e.Stdout = os.Stdout + } + if e.Stderr == nil { + e.Stderr = os.Stderr + } + if e.Exit == nil { + e.Exit = os.Exit + } + if len(e.Args) == 0 { + e.Args = os.Args[1:] + } + return e +} + +// Configure will configure the execution. +func (e Execution) Configure(opts []Option) Execution { + for _, opt := range opts { + opt(&e) + } + return e +} + +// Option is used to configure an App. +type Option func(*Execution) + +// Options to override the commandline for testing purposes. +var Options []Option //nolint:gochecknoglobals + +// Result is a result of execution. +type Result struct { + Execution + Err error +} diff --git a/vendor/knative.dev/hack/pkg/inflator/cli/flags.go b/vendor/knative.dev/hack/pkg/inflator/cli/flags.go new file mode 100644 index 00000000000..3e9331819b3 --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/cli/flags.go @@ -0,0 +1,46 @@ +package cli + +import ( + "os" + "strings" +) + +const ( + // ManualVerboseEnvVar is the environment variable that can be set to disable + // automatic verbose mode on CI servers. + ManualVerboseEnvVar = "KNATIVE_HACK_SCRIPT_MANUAL_VERBOSE" +) + +type flags struct { + verbose bool +} + +func parseArgs(ex *Execution) (*flags, error) { + f := flags{ + verbose: isCiServer(), + } + if len(ex.Args) == 0 { + return nil, usageErr{} + } + for i := 0; i < len(ex.Args); i++ { + if ex.Args[i] == "-v" || ex.Args[i] == "--verbose" { + f.verbose = true + ex.Args = append(ex.Args[:i], ex.Args[i+1:]...) + i-- + } + + if ex.Args[i] == "-h" || ex.Args[i] == "--help" { + return nil, usageErr{} + } + } + return &f, nil +} + +func isCiServer() bool { + if strings.HasPrefix(strings.ToLower(os.Getenv(ManualVerboseEnvVar)), "t") { + return false + } + return os.Getenv("CI") != "" || + os.Getenv("BUILD_ID") != "" || + os.Getenv("PROW_JOB_ID") != "" +} diff --git a/vendor/knative.dev/hack/pkg/inflator/cli/print.go b/vendor/knative.dev/hack/pkg/inflator/cli/print.go new file mode 100644 index 00000000000..ca734572227 --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/cli/print.go @@ -0,0 +1,33 @@ +package cli + +import "fmt" + +// Print is a convenience method to Print to the defined output, fallback to Stderr if not set. +func (e Execution) Print(i ...interface{}) { + fmt.Fprint(e.Stdout, i...) +} + +// Println is a convenience method to Println to the defined output, fallback to Stderr if not set. +func (e Execution) Println(i ...interface{}) { + e.Print(fmt.Sprintln(i...)) +} + +// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set. +func (e Execution) Printf(format string, i ...interface{}) { + e.Print(fmt.Sprintf(format, i...)) +} + +// PrintErr is a convenience method to Print to the defined Err output, fallback to Stderr if not set. +func (e Execution) PrintErr(i ...interface{}) { + fmt.Fprint(e.Stderr, i...) +} + +// PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set. +func (e Execution) PrintErrln(i ...interface{}) { + e.PrintErr(fmt.Sprintln(i...)) +} + +// PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set. +func (e Execution) PrintErrf(format string, i ...interface{}) { + e.PrintErr(fmt.Sprintf(format, i...)) +} diff --git a/vendor/knative.dev/hack/pkg/inflator/cli/usage.go b/vendor/knative.dev/hack/pkg/inflator/cli/usage.go new file mode 100644 index 00000000000..e338e536a9a --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/cli/usage.go @@ -0,0 +1,25 @@ +package cli + +type usageErr struct{} + +func (u usageErr) Retcode() int { + return 0 +} + +func (u usageErr) Error() string { + return `Hacks as Go self-extracting binary + +Will extract Hack scripts to a temporary directory, and provide a source +file path to requested shell script. + +# In Bash script +source "$(go run knative.dev/hack/cmd/script@latest library.sh)" + +Usage: + script [flags] library.sh + +Flags: + -h, --help help + -v, --verbose verbose output +` +} diff --git a/vendor/knative.dev/hack/pkg/inflator/extract/errors.go b/vendor/knative.dev/hack/pkg/inflator/extract/errors.go new file mode 100644 index 00000000000..f6583458bbc --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/extract/errors.go @@ -0,0 +1,24 @@ +package extract + +import ( + "errors" + "fmt" +) + +var ( + // ErrBug is an error that indicates a bug in the code. + ErrBug = errors.New("probably a bug in the code") + + // ErrUnexpected is an error that indicates an unexpected situation. + ErrUnexpected = errors.New("unexpected situation") +) + +func wrapErr(err error, target error) error { + if err == nil { + return nil + } + if errors.Is(err, target) { + return err + } + return fmt.Errorf("%w: %v", target, err) +} diff --git a/vendor/knative.dev/hack/pkg/inflator/extract/extract.go b/vendor/knative.dev/hack/pkg/inflator/extract/extract.go new file mode 100644 index 00000000000..b48ac5687b8 --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/extract/extract.go @@ -0,0 +1,136 @@ +package extract + +import ( + "io/fs" + "math" + "os" + "path" + "strings" + + "knative.dev/hack" +) + +const ( + // HackScriptsDirEnvVar is the name of the environment variable that points + // to directory where knative-hack scripts will be extracted. + HackScriptsDirEnvVar = "KNATIVE_HACK_SCRIPTS_DIR" + // PermOwnerWrite is the permission bits for owner write. + PermOwnerWrite = 0o200 + // PermAllExecutable is the permission bits for executable. + PermAllExecutable = 0o111 +) + +// Printer is an interface for printing messages. +type Printer interface { + Print(i ...interface{}) + Println(i ...interface{}) + Printf(format string, i ...interface{}) + PrintErr(i ...interface{}) + PrintErrln(i ...interface{}) + PrintErrf(format string, i ...interface{}) +} + +// Operation is the main extract object that can extract scripts. +type Operation struct { + // ScriptName is the name of the script to extract. + ScriptName string + // Verbose will print more information. + Verbose bool +} + +// Extract will extract a script from the library to a temporary directory and +// provide the file path to it. +func (o Operation) Extract(prtr Printer) error { + l := logger{o.Verbose, prtr} + hackRootDir := os.Getenv(HackScriptsDirEnvVar) + if f, err := hack.Scripts.Open(o.ScriptName); err != nil { + return wrapErr(err, ErrUnexpected) + } else if err = f.Close(); err != nil { + return wrapErr(err, ErrUnexpected) + } + if hackRootDir == "" { + hackRootDir = path.Join(os.TempDir(), "knative", "hack", "scripts") + } + l.debugf("Extracting hack scripts to directory: %s", hackRootDir) + if err := copyDir(l, hack.Scripts, hackRootDir, "."); err != nil { + return err + } + scriptPath := path.Join(hackRootDir, o.ScriptName) + l.Println(scriptPath) + return nil +} + +func copyDir(l logger, inputFS fs.ReadDirFS, destRootDir, dir string) error { + return wrapErr(fs.WalkDir(inputFS, dir, func(filePath string, dirEntry fs.DirEntry, err error) error { + if err != nil { + return wrapErr(err, ErrBug) + } + return copyFile(l, inputFS, destRootDir, filePath, dirEntry) + }), ErrUnexpected) +} + +func copyFile( + l logger, + inputFS fs.ReadDirFS, + destRootDir, filePath string, + dirEntry fs.DirEntry, +) error { + inputFI, err := dirEntry.Info() + if err != nil { + return wrapErr(err, ErrBug) + } + + destPath := path.Join(destRootDir, filePath) + perm := inputFI.Mode().Perm() + + perm |= PermOwnerWrite + if dirEntry.IsDir() { + perm |= PermAllExecutable + if err = os.MkdirAll(destPath, perm); err != nil { + return wrapErr(err, ErrUnexpected) + } + return nil + } + + var ( + inputCS checksum + destCS checksum + destFI fs.FileInfo + bytes []byte + ) + inputCS = asChecksum(inputFI) + if destFI, err = os.Stat(destPath); err != nil { + if !os.IsNotExist(err) { + return wrapErr(err, ErrUnexpected) + } + } else { + destCS = asChecksum(destFI) + } + if inputCS == destCS { + l.debugf("%-30s up-to-date", filePath) + return nil + } + if bytes, err = fs.ReadFile(inputFS, filePath); err != nil { + return wrapErr(err, ErrBug) + } + if err = os.WriteFile(destPath, bytes, perm); err != nil { + return wrapErr(err, ErrUnexpected) + } + + sizeKB := int(inputFI.Size() / 1024) + size5k := int(math.Ceil(float64(sizeKB) / 5)) + l.debugf("%-30s %3d KiB %s", filePath, sizeKB, strings.Repeat("+", size5k)) + return nil +} + +func asChecksum(f fs.FileInfo) checksum { + return checksum{ + exists: true, + size: f.Size(), + } +} + +type checksum struct { + exists bool + size int64 +} diff --git a/vendor/knative.dev/hack/pkg/inflator/extract/logger.go b/vendor/knative.dev/hack/pkg/inflator/extract/logger.go new file mode 100644 index 00000000000..5f7e25b4623 --- /dev/null +++ b/vendor/knative.dev/hack/pkg/inflator/extract/logger.go @@ -0,0 +1,12 @@ +package extract + +type logger struct { + verbose bool + Printer +} + +func (l logger) debugf(format string, i ...interface{}) { + if l.verbose { + l.PrintErrf("[hack] "+format+"\n", i...) + } +} diff --git a/vendor/knative.dev/hack/pkg/retcode/retcode.go b/vendor/knative.dev/hack/pkg/retcode/retcode.go new file mode 100644 index 00000000000..886eb0e6ac4 --- /dev/null +++ b/vendor/knative.dev/hack/pkg/retcode/retcode.go @@ -0,0 +1,28 @@ +package retcode + +import "hash/crc32" + +var ( + // LowerBound is the lower bound of the POSIX retcode range. Use this to + // configure the package. + LowerBound = 1 + // UpperBound is the upper bound of the POSIX retcode range. Use this to + // configure the package. + UpperBound = 255 +) + +// Calc will calculate an POSIX retcode from an error. +func Calc(err error) int { + if err == nil { + return 0 + } + if r, ok := err.(retcodeErr); ok { + return r.Retcode() + } + upper := UpperBound - LowerBound + return int(crc32.ChecksumIEEE([]byte(err.Error())))%upper + LowerBound +} + +type retcodeErr interface { + Retcode() int +} diff --git a/vendor/knative.dev/hack/presubmit-tests.sh b/vendor/knative.dev/hack/presubmit-tests.sh index d210dc703f3..b96ffadf43e 100644 --- a/vendor/knative.dev/hack/presubmit-tests.sh +++ b/vendor/knative.dev/hack/presubmit-tests.sh @@ -72,9 +72,11 @@ function initialize_environment() { # Parameters: $1 - test group name (e.g., build) # $2 - result (0=passed, 1=failed) function results_banner() { - local result - [[ $2 -eq 0 ]] && result="PASSED" || result="FAILED" - header "$1 tests ${result}" + if [[ $2 -eq 0 ]]; then + header "$1 tests PASSED" + else + error "$1 tests FAILED" + fi } # Run build tests. If there's no `build_tests` function, run the default diff --git a/vendor/knative.dev/hack/release.sh b/vendor/knative.dev/hack/release.sh index cbc20f69016..7c5c2ba5227 100644 --- a/vendor/knative.dev/hack/release.sh +++ b/vendor/knative.dev/hack/release.sh @@ -17,7 +17,7 @@ # This is a helper script for Knative release scripts. # See README.md for instructions on how to use it. -source $(dirname "${BASH_SOURCE[0]}")/library.sh +source "$(dirname "${BASH_SOURCE[0]}")/library.sh" # Organization name in GitHub; defaults to Knative. readonly ORG_NAME="${ORG_NAME:-knative}" @@ -37,9 +37,9 @@ readonly RELEASE_SIGNING_IDENTITY="signer@knative-releases.iam.gserviceaccount.c readonly GEO_REPLICATION=(us eu asia) # Simple banner for logging purposes. -# Parameters: $1 - message to display. +# Parameters: $* - message to display. function banner() { - make_banner "@" "$1" + subheader "$*" } # Tag images in the yaml files if $TAG is not empty. diff --git a/vendor/modules.txt b/vendor/modules.txt index 07b2b0163a6..808859ea565 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1049,9 +1049,13 @@ k8s.io/utils/pointer k8s.io/utils/ptr k8s.io/utils/strings/slices k8s.io/utils/trace -# knative.dev/hack v0.0.0-20240607132042-09143140a254 -## explicit; go 1.18 +# knative.dev/hack v0.0.0-20240607132042-09143140a254 => github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 +## explicit; go 1.21 knative.dev/hack +knative.dev/hack/cmd/script +knative.dev/hack/pkg/inflator/cli +knative.dev/hack/pkg/inflator/extract +knative.dev/hack/pkg/retcode # knative.dev/hack/schema v0.0.0-20240607132042-09143140a254 ## explicit; go 1.18 knative.dev/hack/schema/commands @@ -1253,3 +1257,4 @@ sigs.k8s.io/structured-merge-diff/v4/value ## explicit; go 1.12 sigs.k8s.io/yaml sigs.k8s.io/yaml/goyaml.v2 +# knative.dev/hack => github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 From 030f1743eb30c0a258737577bbd5a96d7e43cf0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Thu, 18 Jul 2024 20:30:12 +0200 Subject: [PATCH 2/2] Use latest version of knative.dev/hack --- go.mod | 2 +- go.sum | 4 ++-- vendor/knative.dev/hack/codegen-library.sh | 9 +++++++-- vendor/knative.dev/hack/library.sh | 4 ++-- vendor/modules.txt | 4 ++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index a9cb0182934..25b2519d328 100644 --- a/go.mod +++ b/go.mod @@ -50,7 +50,7 @@ require ( sigs.k8s.io/yaml v1.4.0 ) -replace knative.dev/hack => github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 +replace knative.dev/hack => github.com/cardil/knative-hack v0.0.0-20240718135939-e922902857fa require ( contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect diff --git a/go.sum b/go.sum index 39efdc1342e..23216e3551f 100644 --- a/go.sum +++ b/go.sum @@ -61,8 +61,8 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= github.com/blendle/zapdriver v1.3.1 h1:C3dydBOWYRiOk+B8X9IVZ5IOe+7cl+tGOexN4QqHfpE= github.com/blendle/zapdriver v1.3.1/go.mod h1:mdXfREi6u5MArG4j9fewC+FGnXaBR+T4Ox4J2u4eHCc= -github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 h1:VsOr85OYA4TrKh+GrGrmWlOx2nz/VzsWhzQQLO1T8UA= -github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY= +github.com/cardil/knative-hack v0.0.0-20240718135939-e922902857fa h1:MNJ+q3m8/Gnw5j0Xk5/Sn6AnfrVVhXH8f2cR01cAz1U= +github.com/cardil/knative-hack v0.0.0-20240718135939-e922902857fa/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g= github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw= diff --git a/vendor/knative.dev/hack/codegen-library.sh b/vendor/knative.dev/hack/codegen-library.sh index bc37859fd07..01a43132dc6 100644 --- a/vendor/knative.dev/hack/codegen-library.sh +++ b/vendor/knative.dev/hack/codegen-library.sh @@ -152,12 +152,17 @@ function restore-gopath() { if __is_checkout_onto_gopath; then return fi - if [ -n "$CODEGEN_TMP_GOPATH" ] && [ -d "$CODEGEN_TMP_GOPATH" ]; then + if [ -d "$CODEGEN_TMP_GOPATH" ]; then chmod -R u+w "${CODEGEN_TMP_GOPATH}" rm -rf "${CODEGEN_TMP_GOPATH}" unset CODEGEN_TMP_GOPATH fi - unset CODEGEN_ORIGINAL_GOPATH GOPATH GOBIN + unset GOPATH GOBIN + # Restore the original GOPATH, if it was different from the default one. + if [ "$CODEGEN_ORIGINAL_GOPATH" != "$(go env GOPATH)" ]; then + export GOPATH="$CODEGEN_ORIGINAL_GOPATH" + fi + unset CODEGEN_ORIGINAL_GOPATH } add_trap cleanup-codegen EXIT diff --git a/vendor/knative.dev/hack/library.sh b/vendor/knative.dev/hack/library.sh index f4ef09c6a58..08f2dba16f1 100644 --- a/vendor/knative.dev/hack/library.sh +++ b/vendor/knative.dev/hack/library.sh @@ -774,7 +774,7 @@ function go_update_deps() { function __clean_goworksum_if_exists() { if [ -f "$REPO_ROOT_DIR/go.work.sum" ]; then log.step 'Cleaning the go.work.sum file' - true > "$REPO_ROOT_DIR/go.work.sum" + truncate --size 0 "$REPO_ROOT_DIR/go.work.sum" fi } @@ -844,7 +844,7 @@ function __go_update_deps_for_module() { if [[ "${FORCE_VENDOR:-false}" == "true" ]] || [ -d vendor ]; then group "Go mod vendor" # Call go work vendor for Go 1.22+ and go.work file exists. - if [ -f "$REPO_ROOT_DIR/go.work" ] && go help work vendor >/dev/null 2>&1; then + if [ -f "$REPO_ROOT_DIR/go.work" ] && go help work vendor &>/dev/null; then go work vendor else go mod vendor diff --git a/vendor/modules.txt b/vendor/modules.txt index 3d1bd4a5331..8f916d0f2aa 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1049,7 +1049,7 @@ k8s.io/utils/pointer k8s.io/utils/ptr k8s.io/utils/strings/slices k8s.io/utils/trace -# knative.dev/hack v0.0.0-20240704013904-b9799599afcf => github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 +# knative.dev/hack v0.0.0-20240704013904-b9799599afcf => github.com/cardil/knative-hack v0.0.0-20240718135939-e922902857fa ## explicit; go 1.21 knative.dev/hack knative.dev/hack/cmd/script @@ -1257,4 +1257,4 @@ sigs.k8s.io/structured-merge-diff/v4/value ## explicit; go 1.12 sigs.k8s.io/yaml sigs.k8s.io/yaml/goyaml.v2 -# knative.dev/hack => github.com/cardil/knative-hack v0.0.0-20240709153708-bc2fe4ea0e35 +# knative.dev/hack => github.com/cardil/knative-hack v0.0.0-20240718135939-e922902857fa