Make 'run' POSIX-compliant

This commit is contained in:
Andrew Dunham 2021-07-10 23:16:01 -04:00
parent 1705039511
commit 68af0ac537
2 changed files with 64 additions and 45 deletions

View file

@ -1,4 +1,6 @@
all: all:
lint: lint:
find apply run push lib -type f -not -iname '*.*' | xargs shellcheck -s bash @find apply push lib -type f -not -iname '*.*' | xargs shellcheck -s bash
@shellcheck -s dash run
@echo -e "\033[032mOK\033[0m"

95
run
View file

@ -3,71 +3,90 @@
set -e set -e
set -u set -u
function usage() { RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
RESET="$(tput sgr0)"
usage() {
echo "usage: $(basename "$0") [-v] <target> [<target>...]" echo "usage: $(basename "$0") [-v] <target> [<target>...]"
exit 1 exit 1
} }
function root_path() { root_path() {
if [[ -z "${APPLY_ROOT:-}" ]]; then if [ -z "${APPLY_ROOT:-}" ]; then
cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd
else else
echo "$APPLY_ROOT" echo "$APPLY_ROOT"
fi fi
} }
function read_group() { read_group() {
local target="$*" local target="$*"
while read -r item; do while read -r item; do
if [[ ! "$item" == '#'* ]]; then # Ignore comments
case "$item" in
'#'*)
;;
*)
echo "$item" echo "$item"
fi ;;
esac
done < "$(root_path)/$target" done < "$(root_path)/$target"
} }
function run_group() { run_group() {
local target="$*" local target="$*"
for item in $(read_group "$target"); do for item in $(read_group "$target"); do
if [[ ! "$item" == '#'* ]]; then # Ignore comments
case "$item" in
'#'*)
;;
*)
run_pretty "$item" run_pretty "$item"
fi ;;
esac
done done
} }
function run_unit() { run_unit() {
/bin/bash -l -c "set -e; set -u; set -o pipefail; source lib; source '$(root_path)/$*'" /bin/bash -l -c "set -e; set -u; set -o pipefail; source lib; source '$(root_path)/$*'"
} }
function log_file() { log_file() {
echo -n "$LOG_DIR/" echo -n "$LOG_DIR/"
echo "${1/\//_}.log" echo -n "$(echo "$1" | sed 's/\//_/g')"
echo ".log"
} }
function check_target() { check_target() {
local target="$*" local target="$*"
if [[ ! -f "$(root_path)/$target" ]]; then if [ ! -f "$(root_path)/$target" ]; then
echo "missing $target" echo "missing $target"
return 1 return 1
fi fi
if [[ $target == groups/* ]]; then case "$target" in
groups/*)
for item in $(read_group "$target"); do for item in $(read_group "$target"); do
check_target "$item" check_target "$item"
done done
fi ;;
esac
} }
function run_pretty() { run_pretty() {
local target="$*" local target="$*"
case "$target" in case "$target" in
groups/*) groups/*)
echo -e "\033[33m** \033[34m$target \033[33mprocessing...\033[0m" printf "${YELLOW}** ${BLUE}%s ${YELLOW}processing...${RESET}\n" "$target"
run_group "$target" run_group "$target"
echo -e "\033[33m** \033[34m$target \033[32mOK\033[0m" printf "${YELLOW}** ${BLUE}%s ${GREEN}OK${RESET}\n" "$target"
;; ;;
units/*) units/*)
@ -79,17 +98,17 @@ function run_pretty() {
esac esac
} }
function run_pretty_unit() { run_pretty_unit() {
local rc local rc
local log_file local log_file
local target="$*" local target="$*"
echo -e -n "\033[33m** \033[34m$target\033[0m" printf "${YELLOW}** ${BLUE}%s${RESET}" "$target"
[[ "$VERBOSE" == "1" ]] && echo -e ": \033[33mstarting...\033[0m" [ "$VERBOSE" = "1" ] && echo ": ${YELLOW}starting...${RESET}"
log_file=$(log_file "$@") log_file=$(log_file "$@")
if [[ "$VERBOSE" == "1" ]]; then if [ "$VERBOSE" = "1" ]; then
set +e set +e
run_unit "$@" run_unit "$@"
rc=$? rc=$?
@ -101,17 +120,17 @@ function run_pretty_unit() {
set -e set -e
fi fi
if [[ "$VERBOSE" == "1" ]]; then if [ "$VERBOSE" = "1" ]; then
echo -e -n "\033[33m** \033[34m$target\033[0m: " printf "${YELLOW}** ${BLUE}%s${RESET}: " "$target"
else else
echo -n " " printf " "
fi fi
if [[ $rc -eq 0 ]]; then if [ $rc -eq 0 ]; then
echo -e "\033[32mOK\033[0m" echo "${GREEN}OK${RESET}"
else else
echo -e "\033[31mFAILED\033[0m" echo "${RED}FAILED${RESET}"
if [[ "$VERBOSE" == "1" ]]; then if [ "$VERBOSE" = "1" ]; then
echo "For details, see output above" 1>&2 echo "For details, see output above" 1>&2
else else
echo "Here are the last lines of output:" 1>&2 echo "Here are the last lines of output:" 1>&2
@ -122,30 +141,28 @@ function run_pretty_unit() {
fi fi
} }
if [[ $# -lt 1 ]]; then if [ $# -lt 1 ]; then
usage usage
fi fi
if [[ "$1" == "-v" ]]; then if [ "$1" = "-v" ]; then
VERBOSE="1" VERBOSE="1"
shift shift
else else
VERBOSE="0" VERBOSE="0"
fi fi
if [[ $# -lt 1 ]]; then if [ $# -lt 1 ]; then
usage usage
fi fi
targets=( "$@" )
# pre-flight checks # pre-flight checks
for target in "${targets[@]}"; do for target in "$@"; do
check_target "$target" check_target "$target"
done done
# fly! # fly!
LOG_DIR=$(mktemp -d) LOG_DIR="$(mktemp -d)"
for target in "${targets[@]}"; do for target in "$@"; do
run_pretty "$target" run_pretty "$target"
done done