From de7ea7034252ceae0b9dbf8b93e748b236502eca 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] Using scripts without relying on vendor dir --- 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 | 130 +++++++++++++++-- vendor/knative.dev/hack/embed.go | 2 +- vendor/knative.dev/hack/go.work | 2 +- vendor/knative.dev/hack/go.work.sum | 79 ++++------ vendor/knative.dev/hack/library.sh | 74 +++++++--- .../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/modules.txt | 7 +- 29 files changed, 725 insertions(+), 117 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/go.mod b/go.mod index 55a5c979746..2308068e1e5 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-20240627155243-4b5dad135546 + 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..92f2bccbc38 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-20240627155243-4b5dad135546 h1:sbjR3wfWYfIEfJx1Th2rxdBDhUyzlHRDjFCkpnSP86M= +github.com/cardil/knative-hack v0.0.0-20240627155243-4b5dad135546/go.mod h1:yk2OjGDsbEnQjfxdm0/HJKS2WqTLEFg/N6nUs6Rqx3Q= 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..1e097c0cc7b 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 \ +"${GOGIN}/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..9078fa6bf8f 100644 --- a/vendor/knative.dev/hack/codegen-library.sh +++ b/vendor/knative.dev/hack/codegen-library.sh @@ -16,21 +16,127 @@ # 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 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 + +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 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 -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}")} +popd > /dev/null +GOPATH=$(go_mod_gopath_hack) +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 generated code if it differs only in the boilerplate year +function cleanup-codegen() { + 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" +} + +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..e48d294ce5a 100644 --- a/vendor/knative.dev/hack/go.work +++ b/vendor/knative.dev/hack/go.work @@ -1,4 +1,4 @@ -go 1.18 +go 1.22 use ( . diff --git a/vendor/knative.dev/hack/go.work.sum b/vendor/knative.dev/hack/go.work.sum index 1cc0cde9370..e02e1f30c31 100644 --- a/vendor/knative.dev/hack/go.work.sum +++ b/vendor/knative.dev/hack/go.work.sum @@ -1,61 +1,38 @@ -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/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/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/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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +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/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= +github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= 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/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/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/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= +golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= +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..464cfb0bffd 100644 --- a/vendor/knative.dev/hack/library.sh +++ b/vendor/knative.dev/hack/library.sh @@ -142,7 +142,12 @@ function calcRetcode() { # 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 + gum_style \ + --foreground '#D00' \ + --padding '1 3' \ + --border double \ + --border-foreground '#D00' \ + "ERROR: $*" readonly abort_retcode="${abort_retcode:-$(calcRetcode "$*")}" exit "$abort_retcode" } @@ -150,6 +155,7 @@ function abort() { # Display a box banner. # Parameters: $1 - character to use for the box. # $2 - banner message. +# Deprecated: Use `gum_style` instead. function make_banner() { local msg="$1$1$1$1 $2 $1$1$1$1" local border="${msg//[^$1]/$1}" @@ -163,18 +169,44 @@ function make_banner() { # Simple header for logging purposes. function header() { - local upper="$(echo $1 | tr a-z A-Z)" - make_banner "=" "${upper}" + local upper="$(echo "$*" | tr a-z A-Z)" + gum_style \ + --padding '1 3' \ + --border double \ + "$upper" } # Simple subheader for logging purposes. function subheader() { - make_banner "-" "$1" + gum_style \ + --padding '1 3' \ + --border rounded \ + "$*" +} + +# 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() { - make_banner '!' "WARN: $*" >&2 + gum_style \ + --foreground '#DD0' \ + --padding '1 3' \ + --border rounded \ + --border-foreground '#DD0' \ + "WARN: $*" +} + +# Simple info banner for logging purposes. +function gum_style() { + go_run github.com/charmbracelet/gum@v0.14.1 style "$@" > /dev/stderr } # Checks whether the given function exists. @@ -187,7 +219,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 +230,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 } @@ -682,18 +714,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 +738,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 +784,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; then + go work vendor + else + go mod vendor + fi else go mod download -x fi @@ -796,9 +833,11 @@ function go_mod_gopath_hack() { 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_DIR TMP_REPO_PATH + TMP_DIR="$TMPDIR/go" + TMP_REPO_PATH="${TMP_DIR}/src/$(go_mod_module_name)" + mkdir -p "$(dirname "${TMP_REPO_PATH}")" + ln -s "${REPO_ROOT_DIR}" "${TMP_REPO_PATH}" echo "${TMP_DIR}" } @@ -1014,6 +1053,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/modules.txt b/vendor/modules.txt index 07b2b0163a6..ecb907a4256 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 +# knative.dev/hack v0.0.0-20240607132042-09143140a254 => github.com/cardil/knative-hack v0.0.0-20240627155243-4b5dad135546 ## explicit; go 1.18 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-20240627155243-4b5dad135546