Skip to content

Commit d0a19e2

Browse files
ferhatelmask8s-ci-robot
authored andcommitted
lint: integrate shfmt and reformat (#331)
* lint: integrate shfmt and reformat * correct version generation per suggestion * set script dir automatically * install via go * drop formatter, add doc * run help * improve diffing
1 parent f7a36f5 commit d0a19e2

15 files changed

+223
-187
lines changed

docs/CONTRIBUTOR_GUIDE.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ and run:
3434
goimports -local sigs.k8s.io/krew -w cmd pkg integration_test
3535
```
3636

37+
Shell scripts are automatically formatted by `shfmt`, to install and to validate run:
38+
39+
```bash
40+
hach/run-lint.sh
41+
```
42+
43+
If format is in expected format, there will be no output.
44+
Otherwise, problematic scripts will be listed and a command will be
45+
provided to format them:
46+
47+
```bash
48+
shfmt -w -i=2 hack/
49+
```
50+
3751
In addition, a boilerplate license header is expected in all source files.
3852

3953
_All new code should be covered by tests._

hack/install-gox.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ set -euo pipefail
1919

2020
scratch_dir="$(mktemp -d)"
2121
cleanup() {
22-
rm -rf "${scratch_dir}"
22+
rm -rf "${scratch_dir}"
2323
}
2424
trap cleanup EXIT
2525

2626
install_gox() {
27-
gobin="$(go env GOPATH)/bin"
28-
cd "${scratch_dir}";
29-
env GOPATH="${scratch_dir}" \
30-
GO111MODULE=off \
31-
GOBIN="${gobin}" \
32-
go get github.com/mitchellh/gox
27+
gobin="$(go env GOPATH)/bin"
28+
cd "${scratch_dir}"
29+
env GOPATH="${scratch_dir}" \
30+
GO111MODULE=off \
31+
GOBIN="${gobin}" \
32+
go get github.com/mitchellh/gox
3333
}
3434

3535
ensure_gox() {
36-
command -v "gox" &>/dev/null
36+
command -v "gox" &>/dev/null
3737
}
3838

3939
install_gox
4040
if ! ensure_gox; then
41-
echo >&2 "gox not in PATH";
42-
exit 1
41+
echo >&2 "gox not in PATH"
42+
exit 1
4343
fi

hack/make-all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
set -e -o pipefail
1818

19-
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
19+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2020

2121
"${SCRIPTDIR}/make-binaries.sh"
2222
"${SCRIPTDIR}/make-release-artifacts.sh"

hack/make-binaries.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
# combinations specified via OSARCH variable).
1919

2020
set -e -o pipefail
21-
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2222

2323
if ! command -v "gox" &>/dev/null; then
2424
echo >&2 "gox not installed in PATH, run hack/install-gox.sh."
2525
exit 1
2626
fi
2727

28-
29-
3028
supported_platforms="darwin/amd64 windows/amd64 linux/amd64 linux/arm"
3129
version_pkg="sigs.k8s.io/krew/pkg/version"
3230

hack/make-binary.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# This script builds krew binary for the current OS/arch.
1818

1919
set -e -o pipefail
20-
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
20+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2121

2222
exec env OSARCH="$(go env GOOS)/$(go env GOARCH)" \
23-
"${SCRIPTDIR}/make-binaries.sh"
23+
"${SCRIPTDIR}/make-binaries.sh"

hack/make-release-artifacts.sh

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,35 @@
1616

1717
set -e -o pipefail
1818

19-
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
19+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2020
cd "${SCRIPTDIR}/.."
2121

2222
bin_dir="out/bin"
2323
if [[ ! -d "${bin_dir}" ]]; then
24-
echo >&2 "Binaries are not built (${bin_dir}), run hack/make-binaries.sh"
25-
exit 1
24+
echo >&2 "Binaries are not built (${bin_dir}), run hack/make-binaries.sh"
25+
exit 1
2626
fi
2727

2828
krew_tar_archive="krew.tar.gz"
2929
krew_zip_archive="krew.zip"
3030

3131
# consistent timestamps for files in bindir to ensure consistent checksums
32-
while IFS= read -r -d $'\0' f
33-
do
32+
while IFS= read -r -d $'\0' f; do
3433
echo "modifying atime/mtime for $f"
3534
TZ=UTC touch -at "0001010000" "$f"
3635
TZ=UTC touch -mt "0001010000" "$f"
3736
done < <(find "${bin_dir}" -print0)
3837

3938
echo >&2 "Creating ${krew_tar_archive} archive."
40-
( cd "${bin_dir}"; GZIP=-n tar czvf "${SCRIPTDIR}/../out/${krew_tar_archive}" ./*; )
39+
(
40+
cd "${bin_dir}"
41+
GZIP=-n tar czvf "${SCRIPTDIR}/../out/${krew_tar_archive}" ./*
42+
)
4143
echo >&2 "Creating ${krew_zip_archive} archive."
42-
( cd "${bin_dir}"; zip -X -q --verbose "${SCRIPTDIR}/../out/${krew_zip_archive}" ./*; )
44+
(
45+
cd "${bin_dir}"
46+
zip -X -q --verbose "${SCRIPTDIR}/../out/${krew_zip_archive}" ./*
47+
)
4348

4449
checksum_cmd="shasum -a 256"
4550
if hash sha256sum 2>/dev/null; then
@@ -49,22 +54,21 @@ fi
4954
tar_sumfile="out/${krew_tar_archive}.sha256"
5055
tar_checksum="$(eval "${checksum_cmd[@]}" "out/${krew_tar_archive}" | awk '{print $1;}')"
5156
echo >&2 "${krew_tar_archive} checksum: ${tar_checksum}"
52-
echo "${tar_checksum}" > "${tar_sumfile}"
57+
echo "${tar_checksum}" >"${tar_sumfile}"
5358
echo >&2 "Written ${tar_sumfile}."
5459

5560
zip_sumfile="out/${krew_zip_archive}.sha256"
5661
zip_checksum="$(eval "${checksum_cmd[@]}" "out/${krew_zip_archive}" | awk '{print $1;}')"
5762
echo >&2 "${krew_zip_archive} checksum: ${zip_checksum}"
58-
echo "${zip_checksum}" > "${zip_sumfile}"
63+
echo "${zip_checksum}" >"${zip_sumfile}"
5964
echo >&2 "Written ${zip_sumfile}."
6065

61-
6266
# Copy and process krew manifest
6367
git_describe="$(git describe --tags --dirty --always)"
6468
if [[ ! "${git_describe}" =~ v.* ]]; then
6569
# if tag cannot be inferred (e.g. CI/CD), still provide a valid
6670
# version field for krew.yaml
67-
git_describe="v0.0.0-${git_describe}"
71+
git_describe="v0.0.0-detached.${git_describe}"
6872
fi
6973
krew_version="${TAG_NAME:-$git_describe}"
7074
cp ./hack/krew.yaml ./out/krew.yaml

hack/make-release-notes.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020

2121
TAG="${TAG:?TAG environment variable must be set for this script}"
2222
if ! [[ "$TAG" =~ v.* ]]; then
23-
echo >&2 "TAG must be in format v.*"
24-
exit 1
23+
echo >&2 "TAG must be in format v.*"
24+
exit 1
2525
fi
2626

2727
readme="https://github.com/kubernetes-sigs/krew/blob/${TAG}/README.md"
2828
download_base="https://storage.googleapis.com/krew"
2929
download_assets=(
30-
krew.tar.gz
31-
krew.tar.gz.sha256
32-
krew.zip
33-
krew.zip.sha256
34-
krew.yaml
30+
krew.tar.gz
31+
krew.tar.gz.sha256
32+
krew.zip
33+
krew.zip.sha256
34+
krew.yaml
3535
)
3636

3737
echo "Installation"
@@ -45,12 +45,12 @@ echo "It is recommended to follow [installation instructions](${readme})"
4545
echo "and not using these artifacts directly."
4646
echo
4747
for f in "${download_assets[@]}"; do
48-
echo "- $download_base/${TAG}/${f}"
48+
echo "- $download_base/${TAG}/${f}"
4949
done
5050
echo
5151
echo "Thanks to our contributors for helping out with ${TAG}:"
52-
git log "$(git describe --tags --abbrev=0)..HEAD" --format=%an | \
53-
sort | uniq -c | sort -rn | \
54-
sed -E 's,^(\s+[0-9]+\s),- ,g'
52+
git log "$(git describe --tags --abbrev=0)..HEAD" --format=%an |
53+
sort | uniq -c | sort -rn |
54+
sed -E 's,^(\s+[0-9]+\s),- ,g'
5555
echo
5656
echo "(krew ${TAG} is tagged on $(date -u).)"

hack/run-in-docker.sh

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,38 @@
1818
# the local filesystem.
1919

2020
set -euo pipefail
21-
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2222
log() { echo >&2 "$*"; }
2323
log_ok() { log "$(tput setaf 2)$*$(tput sgr0)"; }
2424
log_fail() { log "$(tput setaf 1)$*$(tput sgr0)"; }
2525
image="krew:sandbox"
2626

2727
krew_bin="${SCRIPTDIR}/../out/bin/krew-linux_amd64"
2828
if [[ ! -f "${krew_bin}" ]]; then
29-
log "Building the ${krew_bin}."
30-
env OSARCH="linux/amd64" "${SCRIPTDIR}/make-binaries.sh"
29+
log "Building the ${krew_bin}."
30+
env OSARCH="linux/amd64" "${SCRIPTDIR}/make-binaries.sh"
3131
else
32-
log_ok "Using existing ${krew_bin}."
32+
log_ok "Using existing ${krew_bin}."
3333
fi
3434

35-
3635
docker build -f "${SCRIPTDIR}/sandboxed.Dockerfile" -q \
37-
--tag "${image}" "${SCRIPTDIR}/.."
36+
--tag "${image}" "${SCRIPTDIR}/.."
3837
log_ok "Sandbox image '${image}' built successfully."
3938

4039
kubeconfig="${KUBECONFIG:-$HOME/.kube/config}"
4140
if [[ ! -f "${kubeconfig}" ]]; then
42-
log_fail "Warning: kubeconfig not found at ${kubeconfig}, using /dev/null"
43-
kubeconfig=/dev/null
41+
log_fail "Warning: kubeconfig not found at ${kubeconfig}, using /dev/null"
42+
kubeconfig=/dev/null
4443
fi
4544

4645
log_ok "Starting docker container with volume mounts:"
47-
log " kubeconfig=${kubeconfig}"
48-
log " kubectl-krew=${krew_bin}"
46+
log " kubeconfig=${kubeconfig}"
47+
log " kubectl-krew=${krew_bin}"
4948
log_ok "You can rebuild with the following command without restarting the container:"
50-
log " env OSARCH=linux/amd64 hack/make-binaries.sh"
49+
log " env OSARCH=linux/amd64 hack/make-binaries.sh"
5150
exec docker run --rm --tty --interactive \
52-
--volume "${krew_bin}:/usr/local/bin/kubectl-krew" \
53-
--volume "${kubeconfig}:/etc/kubeconfig" \
54-
--env KUBECONFIG=/etc/kubeconfig \
55-
--hostname krew \
56-
"${image}"
51+
--volume "${krew_bin}:/usr/local/bin/kubectl-krew" \
52+
--volume "${kubeconfig}:/etc/kubeconfig" \
53+
--env KUBECONFIG=/etc/kubeconfig \
54+
--hostname krew \
55+
"${image}"

hack/run-integration-tests.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ set -euo pipefail
1818

1919
[[ -n "${DEBUG:-}" ]] && set -x
2020

21-
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2222
BINDIR="${SCRIPTDIR}/../out/bin"
2323
goos="$(go env GOOS)"
2424
goarch="$(go env GOARCH)"
2525
KREW_BINARY_DEFAULT="${BINDIR}/krew-${goos}_${goarch}"
2626

27-
if [[ "$#" -gt 0 && ( "$1" = '-h' || "$1" = '--help' ) ]]; then
27+
if [[ "$#" -gt 0 && ("$1" == '-h' || "$1" == '--help') ]]; then
2828
cat <<EOF
2929
Usage:
3030
$0 krew # uses the given krew binary for running integration tests
@@ -33,7 +33,6 @@ EOF
3333
exit 0
3434
fi
3535

36-
3736
KREW_BINARY="${1:-$KREW_BINARY_DEFAULT}" # needed for `kubectl krew` in tests
3837
if [[ ! -e "${KREW_BINARY}" ]]; then
3938
echo >&2 "Could not find $KREW_BINARY. You need to build krew for ${goos}/${goarch} before running the integration tests."

hack/run-lint.sh

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,53 @@ set -euo pipefail
2020

2121
gopath="$(go env GOPATH)"
2222

23-
if ! [[ -x "$gopath/bin/golangci-lint" ]]
24-
then
25-
echo >&2 'Installing golangci-lint'
26-
curl --silent --fail --location \
27-
https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b "$gopath/bin" v1.17.1
23+
if ! [[ -x "$gopath/bin/golangci-lint" ]]; then
24+
echo >&2 'Installing golangci-lint'
25+
curl --silent --fail --location \
26+
https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b "$gopath/bin" v1.17.1
2827
fi
2928

3029
# configured by .golangci.yml
3130
GO111MODULE=on "$gopath/bin/golangci-lint" run
3231

3332
install_impi() {
34-
impi_dir="$(mktemp -d)"
35-
trap 'rm -rf -- ${impi_dir}' EXIT
36-
37-
GOPATH="${impi_dir}" \
38-
GO111MODULE=off \
39-
GOBIN="${gopath}/bin" \
40-
go get github.com/pavius/impi/cmd/impi
33+
impi_dir="$(mktemp -d)"
34+
trap 'rm -rf -- ${impi_dir}' EXIT
35+
36+
GOPATH="${impi_dir}" \
37+
GO111MODULE=off \
38+
GOBIN="${gopath}/bin" \
39+
go get github.com/pavius/impi/cmd/impi
4140
}
4241

4342
# install impi that ensures import grouping is done consistently
44-
if ! [[ -x "${gopath}/bin/impi" ]]
45-
then
46-
echo >&2 'Installing impi'
47-
install_impi
43+
if ! [[ -x "${gopath}/bin/impi" ]]; then
44+
echo >&2 'Installing impi'
45+
install_impi
4846
fi
4947

5048
"$gopath/bin/impi" --local sigs.k8s.io/krew --scheme stdThirdPartyLocal ./...
49+
50+
install_shfmt() {
51+
shfmt_dir="$(mktemp -d)"
52+
trap 'rm -rf -- ${shfmt_dir}' EXIT
53+
54+
GOPATH="${shfmt_dir}" \
55+
GO111MODULE=off \
56+
GOBIN="${gopath}/bin" \
57+
go get mvdan.cc/sh/cmd/shfmt
58+
}
59+
60+
# install shfmt that ensures consistent format in shell scripts
61+
if ! [[ -x "${gopath}/bin/shfmt" ]]; then
62+
echo >&2 'Installing shfmt'
63+
install_shfmt
64+
fi
65+
66+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
67+
shfmt_out="$($gopath/bin/shfmt -l -i=2 ${SCRIPTDIR})"
68+
if [[ -n "${shfmt_out}" ]]; then
69+
echo >&2 "The following shell scripts need to be formatted, run: 'shfmt -w -i=2 ${SCRIPTDIR}'"
70+
echo >&2 "${shfmt_out}"
71+
exit 1
72+
fi

0 commit comments

Comments
 (0)