From 2a6aa78ea03d2edce30d49b6ff0afe9e6d6f90ea Mon Sep 17 00:00:00 2001 From: Yuan Chen Date: Thu, 9 May 2024 16:27:35 -0700 Subject: [PATCH] Add verification scripts Signed-off-by: Yuan Chen Add verify and update to Makefile Signed-off-by: Yuan Chen Remove venv Revert changes Signed-off-by: Yuan Chen Add yamlfmt Signed-off-by: Yuan Chen --- .golangci.yaml | 196 ++++++++++++++++++++++++++++++++++++ .yamlfmt.yaml | 4 + .yamllint.conf | 14 +++ Makefile | 10 ++ hack/spelling.txt | 1 + hack/update-all.sh | 64 ++++++++++++ hack/update-ends-newline.sh | 42 ++++++++ hack/update-go-format.sh | 37 +++++++ hack/update-go-lint.sh | 26 +++++ hack/update-go-mod.sh | 24 +++++ hack/update-shell-format.sh | 37 +++++++ hack/update-spelling.sh | 47 +++++++++ hack/update-yaml-format.sh | 40 ++++++++ hack/verify-all.sh | 75 ++++++++++++++ hack/verify-ends-newline.sh | 51 ++++++++++ hack/verify-go-format.sh | 42 ++++++++ hack/verify-go-lint.sh | 26 +++++ hack/verify-go-mod.sh | 30 ++++++ hack/verify-shell-format.sh | 30 ++++++ hack/verify-shellcheck.sh | 94 +++++++++++++++++ hack/verify-spelling.sh | 30 ++++++ hack/verify-yaml-format.sh | 30 ++++++ hack/verify-yamllint.sh | 63 ++++++++++++ 23 files changed, 1013 insertions(+) create mode 100644 .golangci.yaml create mode 100644 .yamlfmt.yaml create mode 100644 .yamllint.conf create mode 100644 hack/spelling.txt create mode 100755 hack/update-all.sh create mode 100755 hack/update-ends-newline.sh create mode 100755 hack/update-go-format.sh create mode 100755 hack/update-go-lint.sh create mode 100755 hack/update-go-mod.sh create mode 100755 hack/update-shell-format.sh create mode 100755 hack/update-spelling.sh create mode 100755 hack/update-yaml-format.sh create mode 100755 hack/verify-all.sh create mode 100755 hack/verify-ends-newline.sh create mode 100755 hack/verify-go-format.sh create mode 100755 hack/verify-go-lint.sh create mode 100755 hack/verify-go-mod.sh create mode 100755 hack/verify-shell-format.sh create mode 100755 hack/verify-shellcheck.sh create mode 100755 hack/verify-spelling.sh create mode 100755 hack/verify-yaml-format.sh create mode 100755 hack/verify-yamllint.sh diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..331c1c5 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,196 @@ +# https://golangci-lint.run/usage/linters +linters: + disable-all: true + enable: + - asciicheck + - bodyclose + - depguard + - dogsled + - errcheck + - errorlint + - exportloopref + - gci + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - goprintffuncname + - gosec + - gosimple + - govet + - importas + - ineffassign + - misspell + - nakedret + - nilerr + - nolintlint + - prealloc + - revive + - staticcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - usestdlibvars + - whitespace + +linters-settings: + gci: + sections: + - standard + - default + - prefix(sigs.k8s.io/kwok) + - blank + - dot + skip-generated: true + custom-order: true + importas: + no-unaliased: true + alias: + # Kubernetes + - pkg: k8s.io/api/core/v1 + alias: corev1 + - pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1 + alias: apiextensionsv1 + - pkg: k8s.io/apimachinery/pkg/apis/meta/v1 + alias: metav1 + - pkg: k8s.io/apimachinery/pkg/api/errors + alias: apierrors + staticcheck: + go: "1.21" + stylecheck: + go: "1.21" + # STxxxx checks in https://staticcheck.io/docs/configuration/options/#checks + checks: ["all", "-ST1000", "-ST1003"] + depguard: + rules: + main: + deny: + # https://go.dev/doc/go1.16#ioutil + - pkg: "io/ioutil" + desc: "please see https://go.dev/doc/go1.16#ioutil" + # Use sigs.k8s.io/kwok/pkg/log, which is a wrapper around slog + - pkg: "k8s.io/klog/v2" + desc: "please use `sigs.k8s.io/kwok/pkg/log` instead" + - pkg: "k8s.io/klog" + desc: "please use `sigs.k8s.io/kwok/pkg/log` instead" + - pkg: "golang.org/x/exp/slog" + desc: "please use `sigs.k8s.io/kwok/pkg/log` instead" + - pkg: "log/slog" + desc: "please use `sigs.k8s.io/kwok/pkg/log` instead" + - pkg: "log" + desc: "please use `sigs.k8s.io/kwok/pkg/log` instead" + # Use sigs.k8s.io/kwok/pkg/utils/yaml, which is a wrapper around yaml + - pkg: "k8s.io/apimachinery/pkg/util/yaml" + desc: "please use `sigs.k8s.io/kwok/pkg/utils/yaml` instead" + - pkg: "sigs.k8s.io/yaml" + desc: "please use `sigs.k8s.io/kwok/pkg/utils/yaml` instead" + # Use sigs.k8s.io/kwok/pkg/utils/wait, which is a wrapper around wait + - pkg: "k8s.io/apimachinery/pkg/util/wait" + desc: "please use `sigs.k8s.io/kwok/pkg/utils/wait` instead" + # Use sigs.k8s.io/kwok/pkg/utils/slices, which is a wrapper around slices + - pkg: "k8s.io/utils/strings/slices" + desc: "please use `sigs.k8s.io/kwok/pkg/utils/slices` instead" + gocyclo: + min-complexity: 50 + gosec: + excludes: + - G110 + - G304 + config: + G301: "0750" + G302: "0640" + G306: "0640" + goconst: + min-len: 5 + min-occurrences: 10 + ignore-tests: true + match-constant: false + revive: + ignore-generated-header: true + severity: error + enable-all-rules: true + # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md + rules: + - name: add-constant + disabled: true + - name: argument-limit + disabled: true + - name: cognitive-complexity + disabled: true + - name: confusing-naming + disabled: true + - name: cyclomatic + disabled: true + - name: early-return + disabled: true + - name: enforce-map-style + disabled: true + - name: exported + disabled: true + - name: file-header + disabled: true + - name: flag-parameter + disabled: true + - name: function-length + disabled: true + - name: get-return + disabled: true + - name: if-return + disabled: true + - name: import-shadowing + disabled: true + - name: line-length-limit + disabled: true + - name: modifies-parameter + disabled: true + - name: nested-structs + disabled: true + - name: optimize-operands-order + disabled: true + - name: unused-parameter + disabled: true + - name: unused-receiver + disabled: true + - name: use-any + disabled: true + - name: unchecked-type-assertion + disabled: true + - name: function-result-limit + disabled: true + - name: deep-exit + disabled: true + - name: superfluous-else + disabled: true + - name: confusing-results + disabled: true + - name: defer + disabled: true + - name: var-naming + disabled: true + - name: max-public-structs + disabled: true + - name: comment-spacings + disabled: true + - name: struct-tag + disabled: true + - name: unhandled-error + disabled: true + - name: error-strings + disabled: true +issues: + max-same-issues: 0 + max-issues-per-linter: 0 + # We are disabling default golangci exclusions because we want to help reviewers to focus on reviewing the most relevant + # changes in PRs and avoid nitpicking. + exclude-use-default: false +run: + concurrency: 1 + timeout: 10m + allow-parallel-runners: false + skip-dirs-use-default: true + skip-files: + - ".*\\.zz_generated\\..*" + go: '1.21' diff --git a/.yamlfmt.yaml b/.yamlfmt.yaml new file mode 100644 index 0000000..56ca1d9 --- /dev/null +++ b/.yamlfmt.yaml @@ -0,0 +1,4 @@ +formatter: + type: basic + indentless_arrays: true + retain_line_breaks_single: true diff --git a/.yamllint.conf b/.yamllint.conf new file mode 100644 index 0000000..ed49eeb --- /dev/null +++ b/.yamllint.conf @@ -0,0 +1,14 @@ +# configuration file rules +# https://yamllint.readthedocs.io/en/stable/rules.html + +extends: default + +ignore-from-file: .gitignore + +rules: + indentation: disable + document-start: disable + comments: disable + line-length: disable + truthy: + check-keys: false diff --git a/Makefile b/Makefile index 4d49e15..be60da6 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,16 @@ IMAGE_REPO ?=docker.io/nvidia/knavigator GIT_REF =$(shell git rev-parse --abbrev-ref HEAD) IMAGE_TAG ?=$(GIT_REF) +## verify: Verify code +.PHONY: verify +verify: + @./hack/verify-all.sh + +## update: Update all the generated +.PHONY: update +update: + @./hack/update-all.sh + .PHONY: build build: @for target in $(TARGETS); do \ diff --git a/hack/spelling.txt b/hack/spelling.txt new file mode 100644 index 0000000..6daaf5e --- /dev/null +++ b/hack/spelling.txt @@ -0,0 +1 @@ +importas diff --git a/hack/update-all.sh b/hack/update-all.sh new file mode 100755 index 0000000..10fd631 --- /dev/null +++ b/hack/update-all.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +failed=() + +if [[ "${UPDATE_GO_FORMAT:-true}" == "true" ]]; then + echo "[*] Update go format..." + "${ROOT_DIR}"/hack/update-go-format.sh || failed+=(go-format) +fi + +if [[ "${UPDATE_GO_MOD:-true}" == "true" ]]; then + echo "[*] Update go mod..." + "${ROOT_DIR}"/hack/update-go-mod.sh || failed+=(go-mod) +fi + +if [[ "${UPDATE_GO_LINT:-true}" == "true" ]]; then + echo "[*] Update go lint..." + "${ROOT_DIR}"/hack/update-go-lint.sh || failed+=(go-lint) +fi + +if [[ "${UPDATE_ENDS_NEWLINE:-true}" == "true" ]]; then + echo "[*] Update ends newline..." + "${ROOT_DIR}"/hack/update-ends-newline.sh || failed+=(ends-newline) +fi + +if [[ "${UPDATE_SHELL_FORMAT:-true}" == "true" ]]; then + echo "[*] Update shell format..." + "${ROOT_DIR}"/hack/update-shell-format.sh || failed+=(shell-format) +fi + +if [[ "${UPDATE_YAML_FORMAT:-true}" == "true" ]]; then + echo "[*] Update yaml format..." + "${ROOT_DIR}"/hack/update-yaml-format.sh || failed+=(yaml-format) +fi + +if [[ "${UPDATE_SPELLING:-true}" == "true" ]]; then + echo "[*] Update spelling..." + "${ROOT_DIR}"/hack/update-spelling.sh || failed+=(spelling) +fi + +if [[ "${#failed[@]}" != 0 ]]; then + echo "Update failed for: ${failed[*]}" + exit 1 +fi diff --git a/hack/update-ends-newline.sh b/hack/update-ends-newline.sh new file mode 100755 index 0000000..ef82ec0 --- /dev/null +++ b/hack/update-ends-newline.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function update_ends_newline() { + find . \( \ + -iname "*.md" \ + -o -iname "*.sh" \ + -o -iname "*.go" \ + -o -iname "*.tpl" \ + -o -iname "*.yaml" \ + -o -iname "*.yml" \ + \) \ + -not \( \ + -path ./.git/\* \ + -o -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \) \ + -exec sh -c '[ -n "$(tail -c 1 $1)" ] && echo >> $1' sh {} \; +} + +cd "${ROOT_DIR}" && update_ends_newline diff --git a/hack/update-go-format.sh b/hack/update-go-format.sh new file mode 100755 index 0000000..e476d6a --- /dev/null +++ b/hack/update-go-format.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function format() { + echo "Update go format" + mapfile -t findfiles < <(find . \( \ + -iname "*.go" \ + \) \ + -not \( \ + -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \)) + gofmt -s -w "${findfiles[@]}" +} + +cd "${ROOT_DIR}" && format diff --git a/hack/update-go-lint.sh b/hack/update-go-lint.sh new file mode 100755 index 0000000..c1a5b5c --- /dev/null +++ b/hack/update-go-lint.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +COMMAND=(go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2) + +cd "${ROOT_DIR}" && "${COMMAND[@]}" run -c "${ROOT_DIR}/.golangci.yaml" --fix diff --git a/hack/update-go-mod.sh b/hack/update-go-mod.sh new file mode 100755 index 0000000..3fb386a --- /dev/null +++ b/hack/update-go-mod.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +cd "${ROOT_DIR}" && go mod tidy diff --git a/hack/update-shell-format.sh b/hack/update-shell-format.sh new file mode 100755 index 0000000..2e8069a --- /dev/null +++ b/hack/update-shell-format.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function format() { + echo "Update shell format" + mapfile -t findfiles < <(find . \( \ + -iname "*.sh" \ + \) \ + -not \( \ + -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \)) + go run mvdan.cc/sh/v3/cmd/shfmt@v3.7.0 -w -i=2 "${findfiles[@]}" +} + +cd "${ROOT_DIR}" && format diff --git a/hack/update-spelling.sh b/hack/update-spelling.sh new file mode 100755 index 0000000..b04dc3c --- /dev/null +++ b/hack/update-spelling.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +allowed_spelling_words="${ROOT_DIR}/hack/spelling.txt" + +function update() { + local ignore + ignore="$(tr <"${allowed_spelling_words}" '\n' ',')" + mapfile -t files < <(find . \( \ + -iname "*.md" \ + -o -iname "*.sh" \ + -o -iname "*.go" \ + -o -iname "*.tpl" \ + -o -iname "*.yaml" \ + -o -iname "*.yml" \ + \) \ + -not \( \ + -path ./.git/\* \ + -o -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \)) + go run github.com/client9/misspell/cmd/misspell@v0.3.4 \ + -locale US -w -i "${ignore}" "${files[@]}" +} + +cd "${ROOT_DIR}" && update diff --git a/hack/update-yaml-format.sh b/hack/update-yaml-format.sh new file mode 100755 index 0000000..65bf544 --- /dev/null +++ b/hack/update-yaml-format.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Copyright 2024 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function format() { + echo "Update yaml format" + mapfile -t findfiles < <(find . \( \ + -iname "*.yaml" \ + -o -iname "*.yml" \ + \) \ + -not \( \ + -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + -o -path ./kustomize/crd/bases/\* \ + -o -path ./kustomize/rbac/\* \ + \)) + go run github.com/google/yamlfmt/cmd/yamlfmt@v0.11.0 -conf .yamlfmt.yaml "${findfiles[@]}" +} + +cd "${ROOT_DIR}" && format diff --git a/hack/verify-all.sh b/hack/verify-all.sh new file mode 100755 index 0000000..5c25aa8 --- /dev/null +++ b/hack/verify-all.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# Copyright 2022 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +failed=() + +if [[ "${VERIFY_ENDS_NEWLINE:-true}" == "true" ]]; then + echo "[*] Verifying ends newline..." + "${ROOT_DIR}"/hack/verify-ends-newline.sh || failed+=(ends-newline) +fi + +if [[ "${VERIFY_GO_MOD:-true}" == "true" ]]; then + echo "[*] Verifying go mod..." + "${ROOT_DIR}"/hack/verify-go-mod.sh || failed+=(go-mod) +fi + +if [[ "${VERIFY_GO_FORMAT:-true}" == "true" ]]; then + echo "[*] Verifying go format..." + "${ROOT_DIR}"/hack/verify-go-format.sh || failed+=(go-format) +fi + +if [[ "${VERIFY_GO_LINT:-true}" == "true" ]]; then + echo "[*] Verifying go lint..." + "${ROOT_DIR}"/hack/verify-go-lint.sh || failed+=(go-lint) +fi + +if [[ "${VERIFY_YAMLLINT:-true}" == "true" ]]; then + echo "[*] Verifying YAML lint..." + "${ROOT_DIR}"/hack/verify-yamllint.sh || failed+=(yamllint) +fi + +if [[ "${VERIFY_SHELLCHECK:-true}" == "true" ]]; then + echo "[*] Verifying shell check..." + "${ROOT_DIR}"/hack/verify-shellcheck.sh || failed+=(shellcheck) +fi + +if [[ "${VERIFY_SHELL_FORMAT:-true}" == "true" ]]; then + echo "[*] Verifying shell format..." + "${ROOT_DIR}"/hack/verify-shell-format.sh || failed+=(shell-format) +fi + +if [[ "${VERIFY_YAML_FORMAT:-true}" == "true" ]]; then + echo "[*] Verifying yaml format..." + "${ROOT_DIR}"/hack/verify-yaml-format.sh || failed+=(yaml-format) +fi + +if [[ "${VERIFY_SPELLING:-true}" == "true" ]]; then + echo "[*] Verifying spelling..." + "${ROOT_DIR}"/hack/verify-spelling.sh || failed+=(spelling) +fi + +# exit based on verify scripts +if [[ "${#failed[@]}" != 0 ]]; then + echo "Verify failed for: ${failed[*]}" + exit 1 +fi diff --git a/hack/verify-ends-newline.sh b/hack/verify-ends-newline.sh new file mode 100755 index 0000000..5bcdcc8 --- /dev/null +++ b/hack/verify-ends-newline.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Copyright 2022 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function check_ends() { + find . \( \ + -iname "*.md" \ + -o -iname "*.sh" \ + -o -iname "*.go" \ + -o -iname "*.tpl" \ + -o -iname "*.yaml" \ + -o -iname "*.yml" \ + \) \ + -not \( \ + -path ./.git/\* \ + -o -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \) \ + -exec sh -c '[ -n "$(tail -c 1 "$1")" ] && echo "$1"' sh {} \; +} + +function check() { + out="$(check_ends)" + if [[ "${out}" != "" ]]; then + echo "Add a new line in ends for below files" + echo "${out}" + return 1 + fi +} + +cd "${ROOT_DIR}" && check diff --git a/hack/verify-go-format.sh b/hack/verify-go-format.sh new file mode 100755 index 0000000..a20e229 --- /dev/null +++ b/hack/verify-go-format.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# Copyright 2022 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function check() { + echo "Verify go format" + mapfile -t findfiles < <(find . \( \ + -iname "*.go" \ + \) \ + -not \( \ + -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \)) + out="$(gofmt -s -w "${findfiles[@]}")" + + if [[ -n "${out}" ]]; then + echo "${out}" + return 1 + fi +} + +cd "${ROOT_DIR}" && check diff --git a/hack/verify-go-lint.sh b/hack/verify-go-lint.sh new file mode 100755 index 0000000..16ca03d --- /dev/null +++ b/hack/verify-go-lint.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +COMMAND=(go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2) + +cd "${ROOT_DIR}" && "${COMMAND[@]}" run -c "${ROOT_DIR}/.golangci.yaml" diff --git a/hack/verify-go-mod.sh b/hack/verify-go-mod.sh new file mode 100755 index 0000000..c504008 --- /dev/null +++ b/hack/verify-go-mod.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# Copyright 2022 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function check() { + echo "Verify go.mod & go.sum" + go mod tidy + git --no-pager diff --exit-code go.mod go.sum +} + +cd "${ROOT_DIR}" && check diff --git a/hack/verify-shell-format.sh b/hack/verify-shell-format.sh new file mode 100755 index 0000000..fbb0b23 --- /dev/null +++ b/hack/verify-shell-format.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function check() { + echo "Verify shell format" + "${ROOT_DIR}"/hack/update-shell-format.sh + git --no-pager diff --exit-code +} + +cd "${ROOT_DIR}" && check diff --git a/hack/verify-shellcheck.sh b/hack/verify-shellcheck.sh new file mode 100755 index 0000000..b0a394f --- /dev/null +++ b/hack/verify-shellcheck.sh @@ -0,0 +1,94 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +SHELLCHECK_VERSION="0.9.0" + +# disabled lints +disabled=( + # this lint disallows non-constant source, which we use extensively without + # any known bugs + 1090 + # this lint warns when shellcheck cannot find a sourced file + # this wouldn't be a bad idea to warn on, but it fails on lots of path + # dependent sourcing, so just disable enforcing it + 1091 + # this lint prefers command -v to which, they are not the same + 2230 +) +# comma separate for passing to shellcheck +join_by() { + local IFS="$1" + shift + echo "$*" +} +SHELLCHECK_DISABLED="$(join_by , "${disabled[@]}")" +readonly SHELLCHECK_DISABLED + +mapfile -t findfiles < <(find . \( \ + -iname "*.sh" \ + \) \ + -not \( \ + -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \)) + +SHELLCHECK_OPTIONS=( + "--external-sources" + "--exclude=${SHELLCHECK_DISABLED}" + "--color=always" +) + +HAVE_SHELLCHECK=false +if command -v shellcheck; then + detected_version="$(shellcheck --version | grep 'version: .*')" + if [[ "${detected_version}" == "version: ${SHELLCHECK_VERSION}" ]]; then + HAVE_SHELLCHECK=true + fi +fi + +COMMAND=() +if ${HAVE_SHELLCHECK}; then + COMMAND=(shellcheck) +elif command -v "${ROOT_DIR}/bin/shellcheck"; then + COMMAND=("${ROOT_DIR}/bin/shellcheck") +elif [[ "$(uname -s)" == "Linux" ]] && [[ "$(uname -m)" == "x86_64" ]]; then + wget -qO- "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION?}/shellcheck-v${SHELLCHECK_VERSION?}.linux.x86_64.tar.xz" | tar -xJv + mkdir -p "${ROOT_DIR}"/bin + mv "shellcheck-v${SHELLCHECK_VERSION}/shellcheck" "${ROOT_DIR}/bin/" + COMMAND=("${ROOT_DIR}/bin/shellcheck") +elif command -v docker; then + COMMAND=( + docker run + --rm + -v "${ROOT_DIR}:${ROOT_DIR}" + -w "${ROOT_DIR}" + docker.io/koalaman/shellcheck-alpine:v0.9.0@sha256:e19ed93c22423970d56568e171b4512c9244fc75dd9114045016b4a0073ac4b7 + shellcheck + ) +else + echo "WARNING: shellcheck or docker not installed" >&2 + exit 1 +fi + +cd "${ROOT_DIR}" && "${COMMAND[@]}" "${SHELLCHECK_OPTIONS[@]}" "${findfiles[@]}" diff --git a/hack/verify-spelling.sh b/hack/verify-spelling.sh new file mode 100755 index 0000000..2cfc6b8 --- /dev/null +++ b/hack/verify-spelling.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function check() { + echo "Verify spelling" + "${ROOT_DIR}"/hack/update-spelling.sh + git --no-pager diff --exit-code +} + +cd "${ROOT_DIR}" && check diff --git a/hack/verify-yaml-format.sh b/hack/verify-yaml-format.sh new file mode 100755 index 0000000..38f448f --- /dev/null +++ b/hack/verify-yaml-format.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# Copyright 2024 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +function check() { + echo "Verify yaml format" + "${ROOT_DIR}"/hack/update-yaml-format.sh + git --no-pager diff --exit-code +} + +cd "${ROOT_DIR}" && check diff --git a/hack/verify-yamllint.sh b/hack/verify-yamllint.sh new file mode 100755 index 0000000..1b6f844 --- /dev/null +++ b/hack/verify-yamllint.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +DIR="$(dirname "${BASH_SOURCE[0]}")" + +ROOT_DIR="$(realpath "${DIR}/..")" + +COMMAND=() +if command -v yamllint; then + COMMAND=(yamllint) +elif command -v "${ROOT_DIR}/venv/bin/yamllint"; then + COMMAND=("${ROOT_DIR}/venv/bin/yamllint") +elif command -v python3; then + python3 -m venv "${ROOT_DIR}/venv" + source "${ROOT_DIR}/venv/bin/activate" + pip install yamllint + COMMAND=("${ROOT_DIR}/venv/bin/yamllint") +elif command -v docker; then + COMMAND=( + docker run + --rm -i + -v "${ROOT_DIR}:/workdir" + -w "/workdir" + --security-opt="label=disable" + "docker.io/cytopia/yamllint:1.26@sha256:1bf8270a671a2e5f2fea8ac2e80164d627e0c5fa083759862bbde80628f942b2" + ) +else + echo "WARNING: yamllint, python3 or docker not installed" >&2 + exit 1 +fi + +function check() { + echo "Verify go format" + mapfile -t findfiles < <(find . \( \ + -iname "*.yaml" \ + -o -iname "*.yml" \ + \) \ + -not \( \ + -path ./vendor/\* \ + -o -path ./demo/node_modules/\* \ + -o -path ./site/themes/\* \ + \)) + + "${COMMAND[@]}" -s -c .yamllint.conf "${findfiles[@]}" +} + +cd "${ROOT_DIR}" && check