Files
IronFox/scripts/utilities.sh
Akash Yadav aeb8d3a5f0 feat: lint shell scripts with shellcheck + shfmt in CI and pre-commit
ironfox-oss/IronFox!162
Add a blocking `lint` CI stage (runs first, on a lightweight Alpine
image) plus a core.hooksPath pre-commit hook, with scripts/lint.sh as
the shared runner for CI, the hook, and manual use. Checks are
configured in .shellcheckrc and formatting in .editorconfig.

Reformat all scripts with shfmt (2-space indent, switch-case indent,
space redirects) via .editorconfig, so a bare `shfmt` is consistent
everywhere. bootstrap.sh installs shellcheck + shfmt per-OS and
enables the pre-commit hook.

The hook is a convenience (bypassable with --no-verify); the CI
`lint-scripts` job is the enforcement gate. SC2086/SC2046 are
disabled for now as tracked follow-up.

MR-author: Akash Yadav <itsaky01@gmail.com>
Co-authored-by: Akash Yadav <contact@itsaky.com>
Approved-by: celenity <celenity@celenity.dev>
Merged-by: celenity <celenity@celenity.dev>
2026-07-31 02:33:10 +00:00

179 lines
4.6 KiB
Bash

#!/bin/bash
set -euo pipefail
# Utility functions for frequently performed tasks
# This file MUST NOT contain anything other than function definitions.
function echo_red_text() {
echo -e "\033[31m$1\033[0m"
}
function echo_green_text() {
echo -e "\033[32m$1\033[0m"
}
# Verify that an executable (corresponding to an environment variable) exists and is properly set-up
function verify_exec() {
function print_usage() {
echo "Usage: verify_exec /path/to/executable 'ENVIRONMENT_VARIABLE_FOR_EXECUTABLE'"
}
if [[ -z "${1+x}" ]]; then
echo_red_text 'ERROR: Please specify an executable!'
print_usage
exit 1
fi
if [[ -z "${2+x}" ]]; then
echo_red_text 'ERROR: Please specify an environment variable that corresponds to an executable!'
print_usage
exit 1
fi
local -r exec="$1"
local -r exec_env="$2"
if [[ -z "${exec_env+x}" ]]; then
echo_red_text "ERROR: Environment variable is missing!: ${exec_env}"
exit 1
fi
if [[ ! -f "${exec}" ]]; then
echo_red_text "ERROR: ${exec} is missing!"
echo_green_text "Please ensure that environment variable is set to a valid executable: ${exec_env}"
return 1
fi
if [[ ! -s "${exec}" ]]; then
echo_red_text "ERROR: ${exec} is empty!"
echo_green_text "Please ensure that environment variable is set to a valid executable: ${exec_env}"
return 1
fi
if [[ ! -x "${exec}" ]]; then
echo_red_text "ERROR: ${exec} is not executable!"
echo_green_text "Please ensure that environment variable is set to a valid executable: ${exec_env}"
return 1
fi
}
# Verify that a file exists and is not empty
function verify_file() {
function print_usage() {
echo "Usage: verify_file /path/to/file"
}
if [[ -z "${1+x}" ]]; then
echo_red_text 'ERROR: Please specify the path to a file to verify'
print_usage
exit 1
fi
local -r verify_file="$1"
if [[ ! -f "${verify_file}" ]]; then
echo_red_text "ERROR: ${verify_file} does not exist! Aborting..."
return 1
fi
if [[ ! -s "${verify_file}" ]]; then
echo_red_text "ERROR: ${verify_file} is empty! Aborting..."
return 1
fi
}
# Verify that a file (corresponding to an environment variable) exists and is not empty
function verify_file_with_env() {
function print_usage() {
echo "Usage: verify_file_with_env /path/to/file 'ENVIRONMENT_VARIABLE_FOR_FILE'"
}
if [[ -z "${1+x}" ]]; then
echo_red_text 'ERROR: Please specify the path to a file to verify'
print_usage
exit 1
fi
if [[ -z "${2+x}" ]]; then
echo_red_text 'ERROR: Please specify the environment variable that corresponds to the file to verify'
print_usage
exit 1
fi
local -r verify_file="$1"
local -r verify_file_env="$2"
if [[ -z "${verify_file_env+x}" ]]; then
echo_red_text "ERROR: Environment variable is missing!: ${verify_file_env}"
exit 1
fi
if [[ "${verify_file}" == 'null' ]]; then
echo_red_text "ERROR: Environment variable: ${verify_file_env} has not been specified! Aborting..."
return 1
fi
if [[ ! -f "${verify_file}" ]]; then
echo_red_text "ERROR: ${verify_file_env} is set, but ${verify_file} does not exist! Aborting..."
return 1
fi
if [[ ! -s "${verify_file}" ]]; then
echo_red_text "ERROR: ${verify_file_env} is set, but ${verify_file} is empty! Aborting..."
return 1
fi
}
# Verify that a directory exists
function verify_dir() {
function print_usage() {
echo "Usage: verify_dir /path/to/dir"
}
if [[ -z "${1+x}" ]]; then
echo_red_text 'ERROR: Please specify the path to a directory to verify'
print_usage
exit 1
fi
local -r verify_dir="$1"
if [[ ! -d "${verify_dir}" ]]; then
echo_red_text "ERROR: ${verify_dir} does not exist! Aborting..."
return 1
fi
}
# Verify that a directory (corresponding to an environment variable) exists
function verify_dir_with_env() {
function print_usage() {
echo "Usage: verify_dir_with_env /path/to/dir 'ENVIRONMENT_VARIABLE_FOR_DIR'"
}
if [[ -z "${1+x}" ]]; then
echo_red_text 'ERROR: Please specify the path to a directory to verify'
print_usage
exit 1
fi
if [[ -z "${2+x}" ]]; then
echo_red_text 'ERROR: Please specify the environment variable that corresponds to the directory to verify'
print_usage
exit 1
fi
local -r verify_dir="$1"
local -r verify_dir_env="$2"
if [[ -z "${verify_dir_env+x}" ]]; then
echo_red_text "ERROR: Environment variable is missing!: ${verify_dir_env}"
exit 1
fi
if [[ ! -d "${verify_dir}" ]]; then
echo_red_text "ERROR: ${verify_dir_env} is set, but ${verify_dir} does not exist! Aborting..."
return 1
fi
}