Skip to content

Commit

Permalink
ci: style check shell files with shfmt
Browse files Browse the repository at this point in the history
This commit introduces checking style formatting issues with [`shfmt`][1].

[1]: https://github.com/mvdan/sh
  • Loading branch information
jbergstroem committed May 18, 2021
1 parent 1208d8c commit 68335e3
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ jobs:
sudo cp "shellcheck-v${{ env.SHELLCHECK_VERSION }}/shellcheck" /usr/local/bin/
- name: verify shell scripts
run: shellcheck hadolint.sh lib/*.sh test/*.sh
shfmt:
runs-on: ubuntu-20.04
name: shfmt
env:
SHFMT_VERSION: 3.3.0
steps:
- uses: actions/[email protected]
- name: install shfmt
run: |
wget -q -O shfmt "https://github.com/mvdan/sh/releases/download/v${{ env.SHFMT_VERSION }}/shfmt_v${{ env.SHFMT_VERSION }}_linux_amd64"
chmod +x shfmt
sudo cp shfmt /usr/local/bin/
- name: lint shell scripts
run: shfmt -i 2 -d hadolint.sh lib/*.sh test/*.sh
prettier:
runs-on: ubuntu-20.04
name: prettier
Expand Down
4 changes: 2 additions & 2 deletions hadolint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[[ -n "${DEBUG}" ]] && set -x
set -euo pipefail

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

# shellcheck disable=SC1090 # this is intentional
. "${DIR}/lib/hadolint.sh"
Expand All @@ -14,4 +14,4 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# shellcheck disable=SC1090 # this is intentional
. "${DIR}/lib/validate.sh"

run
run
2 changes: 1 addition & 1 deletion lib/hadolint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ function output_hadolint_version() {
local OUTPUT=""
OUTPUT=$(eval "${HADOLINT_PATH}" --version | cut -d " " -f 4)
echo "::set-output name=hadolint_version::${OUTPUT}"
}
}
6 changes: 3 additions & 3 deletions lib/jq.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ function exit_if_found_in_json() {
# @TODO: support passing needle as array to avoid calling twice via "warning, error"
# https://github.com/jbergstroem/hadolint-gh-action/issues/3
jq -e --arg needle "${1}" \
-r 'def count(s): reduce s as $_ (0;.+1); count(.[] |
select(.level == $needle)) | . == 0' &> /dev/null
}
-r 'def count(s): reduce s as $_ (0;.+1); count(.[] |
select(.level == $needle)) | . == 0' &>/dev/null
}
12 changes: 6 additions & 6 deletions lib/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ function exit_with_error() {
function run() {
# Check for dependencies
for executable in "${HADOLINT_PATH}" jq; do
if ! command -v "${executable}" &> /dev/null; then
if ! command -v "${executable}" &>/dev/null; then
echo "Cannot find required binary ${executable}. Is it in \$PATH?"
exit 1
fi
done

# Set version
output_hadolint_version

validate_error_level "${ERRORLEVEL}" || exit_with_error "Provided error level is not supported. Valid values: -1, 0, 1, 2"
validate_annotate "${ANNOTATE}" || exit_with_error "Annotate needs to be set to true or false"
[[ -z "${OUTPUT_FORMAT}" ]] || (validate_output_format "${OUTPUT_FORMAT}" || exit_with_error "Invalid format. If set, output format needs to be one of: tty, json, checkstyle, codeclimate, gitlab_codeclimate")

local CONFIG=""
[[ -z "${CONFIG_FILE}" ]] || CONFIG="-c ${CONFIG_FILE}"

Expand All @@ -45,7 +45,7 @@ function run() {
local OUTPUT=""
OUTPUT=$(eval "${HADOLINT_PATH}" --no-fail --no-color "${CONFIG}" -f json "${DOCKERFILE}")
[[ "${ANNOTATE}" == "true" ]] && echo "${OUTPUT}" | json_to_annotation

# Different exit depending on verbosity
# Ignore all errors
[[ "${ERRORLEVEL}" == "-1" ]] && exit 0
Expand All @@ -57,10 +57,10 @@ function run() {
# @TODO: get to to handle this once
[[ "${ERRORLEVEL}" == "1" ]] && echo "${OUTPUT}" | exit_if_found_in_json "warning"
[[ "${ERRORLEVEL}" == "1" ]] && echo "${OUTPUT}" | exit_if_found_in_json "error"

# An empty json array would imply an error
[[ "${ERRORLEVEL}" == "2" && "${OUTPUT}" != "[]" ]] && exit 1

# You either did well or chose to become a better person
exit 0
}
2 changes: 1 addition & 1 deletion lib/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ function validate_output_format() {
[[ "${format}" == "${1}" ]] && return 0
done
return 1
}
}
2 changes: 1 addition & 1 deletion test/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ test_custom_output_format() {
assert "output_format=tty dockerfile=fixtures/Dockerfile-error ${HL} | grep '::set-output'"
# gitlab seems to output a fingerprint for errors
assert "output_format=gitlab_codeclimate dockerfile=fixtures/Dockerfile-error ${HL} | grep fingerprint"
}
}
2 changes: 1 addition & 1 deletion test/unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ function test_json_to_annotation() {
function test_exit_if_found_in_json() {
assert_status_code 1 'echo ${HADOLINT_JSON_RESPONSE} | exit_if_found_in_json "error"'
assert_status_code 0 'echo ${HADOLINT_JSON_RESPONSE} | exit_if_found_in_json "notice"'
}
}

0 comments on commit 68335e3

Please sign in to comment.