bazel: add shfmt to tidy target

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-03-14 17:21:50 +01:00
parent 88c2e14c64
commit e3f37e9a38
6 changed files with 51 additions and 57 deletions

View File

@ -1,44 +0,0 @@
name: Shellfmt
on:
workflow_dispatch:
push:
branches:
- main
- "release/**"
paths:
- "**.sh"
- "**.bash"
- "image/**"
- ".github/workflows/test-shellfmt.yml"
pull_request:
paths:
- "**.sh"
- "**.bash"
- "image/**"
- ".github/workflows/test-shellfmt.yml"
jobs:
shellcheck:
name: Shellfmt
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
with:
ref: ${{ !github.event.pull_request.head.repo.fork && github.head_ref || '' }}
- name: Setup Go environment
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: "1.20.2"
- name: Install shellfmt
shell: bash
run: |
go install github.com/katexochen/sh/v3/cmd/shfmt@faf7f58964998201d22efe41fef41ae4e1953f3b # v3.6.0
- name: Run shellfmt
shell: bash
run: |
# shellcheck disable=SC2046
shfmt -i 2 -d -s -sr $(shfmt -f . | grep -v helm/charts/cilium)

View File

@ -87,13 +87,3 @@ add_test(NAME integration-node-operator COMMAND make test WORKING_DIRECTORY ${CM
add_test(NAME integration-csi COMMAND bash -c "go test -tags integration -c ./test/ && sudo ./test.test -test.v" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/csi)
add_test(NAME integration-dm COMMAND bash -c "go test -tags integration -c ./test/ && sudo ./test.test -test.v" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/disk-mapper/internal)
add_test(NAME integration-license COMMAND bash -c "go test -tags integration" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/internal/license)
#
# format
#
add_custom_target(format
COMMAND bash -c "${BAZEL} run //:tidy"
COMMAND bash -c "shfmt -i 2 -s -w -sr $(shfmt -f ${CMAKE_SOURCE_DIR} | grep -v helm/charts/cilium)"
COMMAND bash -c "gofumpt -l -w ${CMAKE_SOURCE_DIR}"
VERBATIM
)

View File

@ -63,6 +63,19 @@ sh_template(
template = "tidy.sh.in",
)
sh_template(
name = "shfmt",
data = [
":base_lib",
"@com_github_katexochen_sh_v3//cmd/shfmt",
],
substitutions = {
"@@BASE_LIB@@": "$(rootpath :base_lib)",
"@@SHFMT@@": "$(rootpath @com_github_katexochen_sh_v3//cmd/shfmt)",
},
template = "shfmt.sh.in",
)
repo_command(
name = "gofumpt",
args = [
@ -76,6 +89,7 @@ repo_command(
multirun(
name = "tidy",
commands = [
":shfmt",
":gofumpt",
":go_mod_tidy",
":gazelle_update_repos",

35
bazel/ci/shfmt.sh.in Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env bash
lib=$(realpath @@BASE_LIB@@) || exit 1
shfmt=$(realpath @@SHFMT@@) || exit 1
# shellcheck source=lib.bash
if ! source "${lib}"; then
echo "Error: could not find import"
exit 1
fi
cd "${BUILD_WORKSPACE_DIRECTORY}" || exit 1
scriptsStr=$(${shfmt} -f "${BUILD_WORKSPACE_DIRECTORY}")
readarray -t <<< "${scriptsStr}"
scripts=("${MAPFILE[@]}")
excludeDirs=(
"helm/charts/cilium"
)
for exclude in "${excludeDirs[@]}"; do
for i in "${!scripts[@]}"; do
if [[ ${scripts[i]} == *"${exclude}"* ]]; then
unset 'scripts[i]'
fi
done
done
echo "Formatting the following scripts with shfmt:"
for script in "${scripts[@]}"; do
echo " ${script}"
done
${shfmt} -i 2 -s -w -sr "${scripts[@]}"

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
lib=$(realpath @@BASE_LIB@@)
go=$(realpath @@GO@@)
lib=$(realpath @@BASE_LIB@@) || exit 1
go=$(realpath @@GO@@) || exit 1
# shellcheck source=lib.bash
if ! source "${lib}"; then

View File

@ -96,7 +96,6 @@ Use the provided scripts in `/image/measured-boot` to generated measurements for
Go dependencies are managed with [Go modules](https://blog.golang.org/using-go-modules) that are all linked from the main [go.work](/go.work) file.
[Follow the go documentation](https://go.dev/doc/modules/managing-dependencies) on how to use Go modules.
After updating a dependency, you will have to run `bazel run //:gazelle-update-repos` to update the Bazel workspace with changes from `go.mod` and `go.sum` files.
# Bazel