Merge pull request #70 from opencultureconsulting/felixlohmeier/run-reliably-locate-69

use scriptpath to locate refine
This commit is contained in:
Felix Lohmeier 2022-10-22 23:43:35 +02:00 committed by GitHub
commit 53e06d7b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 47 deletions

View File

@ -34,7 +34,7 @@ wget https://github.com/opencultureconsulting/orcli/raw/main/orcli
chmod +x orcli chmod +x orcli
``` ```
3. Create a symlink in your $PATH (e.g. to ~/.local/bin) 3. Optional: Create a symlink in your $PATH (e.g. to ~/.local/bin)
```sh ```sh
ln -s "${PWD}/orcli" ~/.local/bin/ ln -s "${PWD}/orcli" ~/.local/bin/
@ -42,8 +42,6 @@ ln -s "${PWD}/orcli" ~/.local/bin/
## Usage ## Usage
Ensure you have OpenRefine running (i.e. available at http://localhost:3333 or another URL) or use the integrated start command first.
Use integrated help screens for available options and examples for each command. Use integrated help screens for available options and examples for each command.
```sh ```sh

47
orcli
View File

@ -590,9 +590,6 @@ function init_import() {
# shellcheck shell=bash # shellcheck shell=bash
function interactive() { function interactive() {
cat <<'EOF' cat <<'EOF'
if ! command -v orcli &>/dev/null; then
alias orcli="$orcli"
fi
PS1="(orcli) [\u@\h \W]\$ " PS1="(orcli) [\u@\h \W]\$ "
source <(orcli completions) source <(orcli completions)
echo '================================================================' echo '================================================================'
@ -863,22 +860,16 @@ orcli_run_command() {
OPENREFINE_URL="http://localhost:${args[--port]}" OPENREFINE_URL="http://localhost:${args[--port]}"
# locate orcli and OpenRefine # locate orcli and OpenRefine
if ! command -v orcli &>/dev/null; then scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
if [[ -x "$0" ]]; then if [[ -x "${scriptpath}/refine" ]]; then
orcli="$0" openrefine="${scriptpath}/refine"
else
error "orcli is not executable!" "Try: chmod + $0"
fi
fi
if [[ -x "refine" ]]; then
openrefine="./refine"
else else
error "OpenRefine's startup script (refine) not found!" "Did you put orcli in your OpenRefine app dir?" error "OpenRefine's startup script (refine) not found!" "Did you put orcli in your OpenRefine app dir?"
fi fi
# create tmp directory # create tmp directory
tmpdir="$(mktemp -d)" OPENREFINE_TMPDIR="$(mktemp -d)"
trap '{ rm -rf "$tmpdir"; }' 0 2 3 15 trap '{ rm -rf "$OPENREFINE_TMPDIR"; }' 0 2 3 15
# check if OpenRefine is already running # check if OpenRefine is already running
if curl -fs "${OPENREFINE_URL}" &>/dev/null; then if curl -fs "${OPENREFINE_URL}" &>/dev/null; then
@ -886,26 +877,29 @@ orcli_run_command() {
fi fi
# start OpenRefine with tmp workspace and autosave period 25 hours # start OpenRefine with tmp workspace and autosave period 25 hours
REFINE_AUTOSAVE_PERIOD=1440 $openrefine -d "$tmpdir" -m "${args[--memory]}" -p "${args[--port]}" -x refine.headless=true -v warn &>"$tmpdir/openrefine.log" & REFINE_AUTOSAVE_PERIOD=1440 $openrefine -d "$OPENREFINE_TMPDIR" -m "${args[--memory]}" -p "${args[--port]}" -x refine.headless=true -v warn &>"$OPENREFINE_TMPDIR/openrefine.log" &
openrefine_pid="$!" OPENREFINE_PID="$!"
# update trap to kill OpenRefine on error or exit # update trap to kill OpenRefine on error or exit
trap '{ rm -rf "$tmpdir"; kill -9 "$openrefine_pid"; }' 0 2 3 15 trap '{ rm -rf "$OPENREFINE_TMPDIR"; kill -9 "$OPENREFINE_PID"; }' 0 2 3 15
# wait until OpenRefine is running (timeout 20s) # 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 if ! curl -fs --retry 20 --retry-connrefused --retry-delay 1 "${OPENREFINE_URL}/command/core/get-version" &>/dev/null; then
error "starting OpenRefine server failed!" error "starting OpenRefine server failed!"
else else
log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${tmpdir}" "pid: ${openrefine_pid}" log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${OPENREFINE_TMPDIR}" "pid: ${OPENREFINE_PID}"
fi fi
# execute script(s) in subshell # execute script(s) in subshell
export orcli tmpdir OPENREFINE_URL openrefine_pid export OPENREFINE_TMPDIR OPENREFINE_URL OPENREFINE_PID
if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then if ! read -u 0 -t 0; then
# case 1: interactive mode if stdin is selected but not present # case 1: interactive mode if stdin is selected but not present
bash --rcfile <( bash --rcfile <(
cat ~/.bashrc cat ~/.bashrc
if ! command -v orcli &>/dev/null; then
echo "alias orcli=${scriptpath}/orcli"
fi
interactive interactive
) -i </dev/tty ) -i </dev/tty
exit exit
@ -915,8 +909,11 @@ orcli_run_command() {
# case 2: execute scripts and keep shell running # case 2: execute scripts and keep shell running
bash --rcfile <( bash --rcfile <(
cat ~/.bashrc cat ~/.bashrc
if ! command -v orcli &>/dev/null; then
echo "alias orcli=${scriptpath}/orcli"
fi
for i in "${!files[@]}"; do for i in "${!files[@]}"; do
log "execute script ${files[$i]}" log "executing script ${files[$i]}..."
awk 1 "${files[$i]}" awk 1 "${files[$i]}"
done done
interactive interactive
@ -924,8 +921,14 @@ orcli_run_command() {
else else
# case 3: just execute scripts # case 3: just execute scripts
for i in "${!files[@]}"; do for i in "${!files[@]}"; do
log "execute script ${files[$i]}" log "executing script ${files[$i]}..."
bash -e <(awk 1 "${files[$i]}") bash -e <(
if ! command -v orcli &>/dev/null; then
echo "shopt -s expand_aliases"
echo "alias orcli=${scriptpath}/orcli"
fi
awk 1 "${files[$i]}"
)
done done
fi fi

View File

@ -1,9 +1,6 @@
# shellcheck shell=bash # shellcheck shell=bash
function interactive() { function interactive() {
cat <<'EOF' cat <<'EOF'
if ! command -v orcli &>/dev/null; then
alias orcli="$orcli"
fi
PS1="(orcli) [\u@\h \W]\$ " PS1="(orcli) [\u@\h \W]\$ "
source <(orcli completions) source <(orcli completions)
echo '================================================================' echo '================================================================'

View File

@ -18,22 +18,16 @@ fi
OPENREFINE_URL="http://localhost:${args[--port]}" OPENREFINE_URL="http://localhost:${args[--port]}"
# locate orcli and OpenRefine # locate orcli and OpenRefine
if ! command -v orcli &>/dev/null; then scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
if [[ -x "$0" ]]; then if [[ -x "${scriptpath}/refine" ]]; then
orcli="$0" openrefine="${scriptpath}/refine"
else
error "orcli is not executable!" "Try: chmod + $0"
fi
fi
if [[ -x "refine" ]]; then
openrefine="./refine"
else else
error "OpenRefine's startup script (refine) not found!" "Did you put orcli in your OpenRefine app dir?" error "OpenRefine's startup script (refine) not found!" "Did you put orcli in your OpenRefine app dir?"
fi fi
# create tmp directory # create tmp directory
tmpdir="$(mktemp -d)" OPENREFINE_TMPDIR="$(mktemp -d)"
trap '{ rm -rf "$tmpdir"; }' 0 2 3 15 trap '{ rm -rf "$OPENREFINE_TMPDIR"; }' 0 2 3 15
# check if OpenRefine is already running # check if OpenRefine is already running
if curl -fs "${OPENREFINE_URL}" &>/dev/null; then if curl -fs "${OPENREFINE_URL}" &>/dev/null; then
@ -41,26 +35,29 @@ if curl -fs "${OPENREFINE_URL}" &>/dev/null; then
fi fi
# start OpenRefine with tmp workspace and autosave period 25 hours # start OpenRefine with tmp workspace and autosave period 25 hours
REFINE_AUTOSAVE_PERIOD=1440 $openrefine -d "$tmpdir" -m "${args[--memory]}" -p "${args[--port]}" -x refine.headless=true -v warn &>"$tmpdir/openrefine.log" & REFINE_AUTOSAVE_PERIOD=1440 $openrefine -d "$OPENREFINE_TMPDIR" -m "${args[--memory]}" -p "${args[--port]}" -x refine.headless=true -v warn &>"$OPENREFINE_TMPDIR/openrefine.log" &
openrefine_pid="$!" OPENREFINE_PID="$!"
# update trap to kill OpenRefine on error or exit # update trap to kill OpenRefine on error or exit
trap '{ rm -rf "$tmpdir"; kill -9 "$openrefine_pid"; }' 0 2 3 15 trap '{ rm -rf "$OPENREFINE_TMPDIR"; kill -9 "$OPENREFINE_PID"; }' 0 2 3 15
# wait until OpenRefine is running (timeout 20s) # 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 if ! curl -fs --retry 20 --retry-connrefused --retry-delay 1 "${OPENREFINE_URL}/command/core/get-version" &>/dev/null; then
error "starting OpenRefine server failed!" error "starting OpenRefine server failed!"
else else
log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${tmpdir}" "pid: ${openrefine_pid}" log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${OPENREFINE_TMPDIR}" "pid: ${OPENREFINE_PID}"
fi fi
# execute script(s) in subshell # execute script(s) in subshell
export orcli tmpdir OPENREFINE_URL openrefine_pid export OPENREFINE_TMPDIR OPENREFINE_URL OPENREFINE_PID
if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then if ! read -u 0 -t 0; then
# case 1: interactive mode if stdin is selected but not present # case 1: interactive mode if stdin is selected but not present
bash --rcfile <( bash --rcfile <(
cat ~/.bashrc cat ~/.bashrc
if ! command -v orcli &>/dev/null; then
echo "alias orcli=${scriptpath}/orcli"
fi
interactive interactive
) -i </dev/tty ) -i </dev/tty
exit exit
@ -70,8 +67,11 @@ if [[ ${args[--interactive]} ]]; then
# case 2: execute scripts and keep shell running # case 2: execute scripts and keep shell running
bash --rcfile <( bash --rcfile <(
cat ~/.bashrc cat ~/.bashrc
if ! command -v orcli &>/dev/null; then
echo "alias orcli=${scriptpath}/orcli"
fi
for i in "${!files[@]}"; do for i in "${!files[@]}"; do
log "execute script ${files[$i]}" log "executing script ${files[$i]}..."
awk 1 "${files[$i]}" awk 1 "${files[$i]}"
done done
interactive interactive
@ -79,7 +79,13 @@ if [[ ${args[--interactive]} ]]; then
else else
# case 3: just execute scripts # case 3: just execute scripts
for i in "${!files[@]}"; do for i in "${!files[@]}"; do
log "execute script ${files[$i]}" log "executing script ${files[$i]}..."
bash -e <(awk 1 "${files[$i]}") bash -e <(
if ! command -v orcli &>/dev/null; then
echo "shopt -s expand_aliases"
echo "alias orcli=${scriptpath}/orcli"
fi
awk 1 "${files[$i]}"
)
done done
fi fi