orcli/src/run_command.sh

86 lines
2.6 KiB
Bash
Raw Normal View History

2022-10-07 12:57:11 +02:00
# shellcheck shell=bash disable=SC2154 source=/dev/null
2022-04-20 12:27:53 +02:00
2022-10-07 11:02:05 +02:00
# catch args, convert the space delimited string to an array
files=()
eval "files=(${args[file]})"
2022-10-16 23:13:59 +02:00
# check if stdin is present if selected
if ! [[ ${args[--interactive]} ]]; then
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
orcli_run_usage
exit 1
fi
fi
fi
2022-10-07 11:02:05 +02:00
# update OPENREFINE_URL env
OPENREFINE_URL="http://localhost:${args[--port]}"
2022-04-20 12:27:53 +02:00
# locate orcli and OpenRefine
2022-10-07 12:57:11 +02:00
if ! command -v orcli &>/dev/null; then
if [[ -x "$0" ]]; then
orcli="$0"
else
error "orcli is not executable!" "Try: chmod + $0"
fi
2022-04-20 12:27:53 +02:00
fi
if [[ -x "refine" ]]; then
openrefine="./refine"
else
error "OpenRefine's startup script (refine) not found!" "Did you put orcli in your OpenRefine app dir?"
fi
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "$tmpdir"; }' 0 2 3 15
# check if OpenRefine is already running
if curl -fs "${OPENREFINE_URL}" &>/dev/null; then
error "OpenRefine is already running on port ${args[--port]}." "Hint: Stop the other process or use another port."
fi
# start OpenRefine with tmp workspace and autosave period 25 hours
2022-04-20 12:51:51 +02:00
REFINE_AUTOSAVE_PERIOD=1440 $openrefine -d "$tmpdir" -m "${args[--memory]}" -p "${args[--port]}" -x refine.headless=true -v warn &>"$tmpdir/openrefine.log" &
2022-04-20 12:27:53 +02:00
openrefine_pid="$!"
# update trap to kill OpenRefine on error or exit
trap '{ rm -rf "$tmpdir"; kill -9 "$openrefine_pid"; }' 0 2 3 15
# wait until OpenRefine is running (timeout 20s)
if ! curl -fs --retry 20 --retry-connrefused --retry-delay 1 "${OPENREFINE_URL}/command/core/get-version" &>/dev/null; then
error "starting OpenRefine server failed!"
else
log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${tmpdir}" "pid: ${openrefine_pid}"
fi
2022-10-15 22:57:07 +02:00
# execute script(s) in subshell
2022-10-06 13:28:47 +02:00
export orcli tmpdir OPENREFINE_URL openrefine_pid
2022-10-15 22:57:07 +02:00
if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
2022-10-16 23:13:59 +02:00
# case 1: interactive mode if stdin is selected but not present
2022-10-15 22:57:07 +02:00
bash --rcfile <(
cat ~/.bashrc
interactive
) -i </dev/tty
2022-10-15 22:57:07 +02:00
exit
fi
fi
2022-10-16 23:13:59 +02:00
if [[ ${args[--interactive]} ]]; then
# case 2: execute scripts and keep shell running
2022-10-07 12:57:11 +02:00
bash --rcfile <(
cat ~/.bashrc
for i in "${!files[@]}"; do
log "execute script ${files[$i]}"
awk 1 "${files[$i]}"
done
2022-10-07 12:57:11 +02:00
interactive
) -i </dev/tty
2022-10-16 23:13:59 +02:00
else
# case 3: just execute scripts
for i in "${!files[@]}"; do
log "execute script ${files[$i]}"
bash -e <(awk 1 "${files[$i]}")
done
2022-10-07 12:57:11 +02:00
fi