reportist/build.sh

125 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Public Reportist CLI. Plugins own their documents; this script owns routing.
set -euo pipefail
root=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
usage() {
cat >&2 <<'EOF'
Usage:
build.sh render report|diagnostic|consumables INPUT --output PDF [--force] [--author NAME] [--theme NAME]
build.sh render-all report|diagnostic|consumables INPUT_DIR --output-dir DIR [--force] [--author NAME] [--theme NAME]
build.sh analyze INPUT --evidence-root DIR --output JSON --reference-time TIME --producer-version VERSION [--force]
build.sh analyze-all INPUT_DIR --evidence-root DIR --output-dir DIR --reference-time TIME --producer-version VERSION [--force]
build.sh validate report|diagnostic INPUT [--evidence-root DIR]
build.sh validate-all report|diagnostic INPUT_DIR [--evidence-root DIR]
build.sh export-json-all INPUT_DIR --output-dir DIR
EOF
exit 2
}
kind_command() {
case ${1:-} in
report) printf '%s\n' "$root/render.sh" ;;
diagnostic) printf '%s\n' "$root/plugins/diagnostics/render.sh" ;;
consumables) printf '%s\n' "$root/plugins/consumables/render.sh" ;;
*) usage ;;
esac
}
command_name=${1:-}
[[ -n "$command_name" ]] || usage
shift
case "$command_name" in
render)
(($# >= 4)) || usage
kind=$1
input=$2
shift 2
renderer=$(kind_command "$kind")
exec "$renderer" "$input" "$@"
;;
render-all)
(($# >= 4)) || usage
kind=$1
input_dir=$2
shift 2
kind_command "$kind" >/dev/null
output_dir=
options=()
while (($#)); do
case $1 in
--output-dir)
(($# >= 2)) || usage
output_dir=$2
shift 2
;;
--force)
options+=(--force)
shift
;;
--author | --theme)
(($# >= 2)) || usage
options+=("$1" "$2")
shift 2
;;
*) usage ;;
esac
done
[[ -n "$output_dir" && -d "$input_dir" ]] || usage
input_dir=$(realpath "$input_dir")
mkdir -p "$output_dir"
output_dir=$(realpath "$output_dir")
[[ "$output_dir" != "$input_dir" && "$output_dir" != "$input_dir"/* ]] || {
echo 'Output directory must be outside the input tree' >&2
exit 1
}
count=0
while IFS= read -r -d '' input; do
relative=${input#"$input_dir/"}
case "$kind:$input" in
report:*.yaml | report:*.yml | report:*.json | diagnostic:*.json | consumables:*.csv) ;;
*) continue ;;
esac
output="$output_dir/${relative%.*}.pdf"
mkdir -p "$(dirname "$output")"
"$root/build.sh" render "$kind" "$input" --output "$output" "${options[@]}"
((count += 1))
done < <(find "$input_dir" -type f -print0)
((count > 0)) || {
echo 'No matching input files' >&2
exit 1
}
echo "Rendered $count $kind PDFs into $output_dir"
;;
analyze)
exec "$root/plugins/diagnostics/analyse.sh" "$@"
;;
analyze-all)
exec "$root/plugins/diagnostics/analyse-all.sh" "$@"
;;
validate)
(($# >= 2)) || usage
kind=$1
shift
case $kind in
report) exec "$root/tools/validate.sh" "$@" ;;
diagnostic) exec "$root/plugins/diagnostics/tools/validate.sh" "$@" ;;
*) usage ;;
esac
;;
validate-all)
(($# >= 2)) || usage
kind=$1
shift
case $kind in
report) exec "$root/tools/validate-all.sh" "$@" ;;
diagnostic) exec "$root/plugins/diagnostics/tools/validate-all.sh" "$@" ;;
*) usage ;;
esac
;;
export-json-all)
exec "$root/tools/export-json-all.sh" "$@"
;;
*) usage ;;
esac