Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,26 @@ set -euo pipefail
# we need to resolve it before we can make path relative to this script's file.
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../scripts/lib.sh"

function command_exists() {
bin_name=$(basename "$1")

if command -v "$1" &> /dev/null; then
return 0
fi

warn "$bin_name CLI was not found. Ignoring it..."
return 1
}

files=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')

if [[ -z "$files" ]]; then
info "No files changed. Exiting the pre-commit hook..."
exit 0
fi

if command_exists typos; then
echo "$files" | step xargs typos
fi
echo "$files" | step xargs typos

if command_exists npx; then
echo "$files" | step xargs npx prettier --ignore-unknown --write
fi
./scripts/shellcheck.sh

if command_exists cargo; then
# `rustfmt` doesn't ignore non-rust files automatically
rust_files=$(echo "$files" | { grep -E '\.rs$' || true; })
echo "$files" | step xargs npx prettier --ignore-unknown --write

if [[ -n "$rust_files" ]]; then
echo "$rust_files" | step xargs cargo fmt --manifest-path native/Cargo.toml --
fi
# `rustfmt` doesn't ignore non-rust files automatically
rust_files=$(echo "$files" | { grep -E '\.rs$' || true; })
if [[ -n "$rust_files" ]]; then
echo "$rust_files" | step xargs cargo fmt --manifest-path native/Cargo.toml --
fi

if command_exists mix; then
echo "$files" | step xargs mix format
fi
echo "$files" | step xargs mix format

# Add the modified/prettified files to staging
echo "$files" | step xargs git add
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ jobs:
- uses: actions/checkout@v5
- uses: crate-ci/typos@master

shellcheck:
name: Shellcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- run: ./scripts/install/shellcheck.sh
- run: ./scripts/shellcheck.sh

cargo:
name: Rust Linting and Unit Tests
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions docker/app/purge-cache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env sh
# shellcheck disable=SC2034

# Run your custom purge command here.
#
Expand Down
4 changes: 2 additions & 2 deletions docker/app/run-test
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ mix format --check-formatted

# Sleep to allow OpenSearch to finish initializing
# if it's not done doing whatever it does yet
echo -n "Waiting for OpenSearch"
printf "Waiting for OpenSearch"

until wget -qO - opensearch:9200; do
echo -n "."
printf "."
sleep 2
done

Expand Down
48 changes: 0 additions & 48 deletions post-receive

This file was deleted.

10 changes: 10 additions & 0 deletions scripts/install/shellcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -euo pipefail

. "$(dirname "${BASH_SOURCE[0]}")/../lib.sh"

version=0.11.0

fetch https://github.com/koalaman/shellcheck/releases/download/v$version/shellcheck-v$version.linux.x86_64.tar.xz \
| step tar -xJf - -C /usr/local/bin --strip-components=1 shellcheck-v$version/shellcheck
12 changes: 12 additions & 0 deletions scripts/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ function colorize_command {
# and we use bash >= v5. If this ever becomes a problem, you know the why.
echo -e "\033[1;32m${program}\033[0m ${args[*]}"
}

# `curl` wrapper with better defaults for non-interactive scripts
function fetch {
step curl \
--fail \
--silent \
--show-error \
--location \
--retry 5 \
--retry-all-errors \
"$@"
}
23 changes: 23 additions & 0 deletions scripts/shellcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
#
# This script runs shellcheck on all shell scripts in the repository.
# It exists because shellcheck doesn't attempt to automatically discover
# shell scripts and requires specifying the file paths explicitly.
#
# This is somewhat understandable, because not all shell scripts use
# obvious file extensions like `.sh`. So, we discover such files by
# checking if they have one of the expected shebang lines at the beginning.

set -euo pipefail

. "$(dirname "${BASH_SOURCE[0]}")/lib.sh"

function shell_script_files {
git ls-files "*.sh"
git grep --files-with-matches '^#!/usr/bin/env bash'
git grep --files-with-matches '^#!/usr/bin/env sh'
Comment thread
MareStare marked this conversation as resolved.
}

mapfile -t files < <(shell_script_files "$@" | sort -u)

step shellcheck --source-path SCRIPTDIR "${files[@]}"