From f1c37c947e74fca218c7ed0d5e68ee16d9df7168 Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Thu, 20 Oct 2022 21:09:28 +0000 Subject: [PATCH 1/3] use scriptpath to locate refine --- README.md | 4 +--- orcli | 37 ++++++++++++++++++++++++------------- src/run_command.sh | 31 +++++++++++++++++++------------ 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index bf06c20..aaf75bc 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ wget https://github.com/opencultureconsulting/orcli/raw/main/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 ln -s "${PWD}/orcli" ~/.local/bin/ @@ -42,8 +42,6 @@ ln -s "${PWD}/orcli" ~/.local/bin/ ## 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. ```sh diff --git a/orcli b/orcli index 89b44e2..f32a6b9 100755 --- a/orcli +++ b/orcli @@ -863,22 +863,23 @@ orcli_run_command() { OPENREFINE_URL="http://localhost:${args[--port]}" # locate orcli and OpenRefine + scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")") if ! command -v orcli &>/dev/null; then - if [[ -x "$0" ]]; then - orcli="$0" + if [[ -x "${scriptpath}/orcli" ]]; then + orcli="${scriptpath}/orcli" else - error "orcli is not executable!" "Try: chmod + $0" + error "orcli is not executable!" "Try: chmod + ${scriptpath}/orcli" fi fi - if [[ -x "refine" ]]; then - openrefine="./refine" + if [[ -x "${scriptpath}/refine" ]]; then + openrefine="${scriptpath}/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 + OPENREFINE_TMPDIR="$(mktemp -d)" + trap '{ rm -rf "$OPENREFINE_TMPDIR"; }' 0 2 3 15 # check if OpenRefine is already running if curl -fs "${OPENREFINE_URL}" &>/dev/null; then @@ -886,26 +887,29 @@ orcli_run_command() { fi # 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" & - openrefine_pid="$!" + 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="$!" # 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) 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}" + log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${OPENREFINE_TMPDIR}" "pid: ${OPENREFINE_PID}" fi # 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 ! read -u 0 -t 0; then # case 1: interactive mode if stdin is selected but not present bash --rcfile <( cat ~/.bashrc + if [[ $orcli ]]; then + echo "alias orcli=$orcli" + fi interactive ) -i /dev/null; then - if [[ -x "$0" ]]; then - orcli="$0" + if [[ -x "${scriptpath}/orcli" ]]; then + orcli="${scriptpath}/orcli" else - error "orcli is not executable!" "Try: chmod + $0" + error "orcli is not executable!" "Try: chmod + ${scriptpath}/orcli" fi fi -if [[ -x "refine" ]]; then - openrefine="./refine" +if [[ -x "${scriptpath}/refine" ]]; then + openrefine="${scriptpath}/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 +OPENREFINE_TMPDIR="$(mktemp -d)" +trap '{ rm -rf "$OPENREFINE_TMPDIR"; }' 0 2 3 15 # check if OpenRefine is already running if curl -fs "${OPENREFINE_URL}" &>/dev/null; then @@ -41,26 +42,29 @@ if curl -fs "${OPENREFINE_URL}" &>/dev/null; then fi # 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" & -openrefine_pid="$!" +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="$!" # 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) 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}" + log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${OPENREFINE_TMPDIR}" "pid: ${OPENREFINE_PID}" fi # 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 ! read -u 0 -t 0; then # case 1: interactive mode if stdin is selected but not present bash --rcfile <( cat ~/.bashrc + if [[ $orcli ]]; then + echo "alias orcli=$orcli" + fi interactive ) -i Date: Thu, 20 Oct 2022 21:11:02 +0000 Subject: [PATCH 2/3] update orcli --- orcli | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/orcli b/orcli index f32a6b9..26e9b31 100755 --- a/orcli +++ b/orcli @@ -930,13 +930,9 @@ orcli_run_command() { ) -i Date: Sat, 22 Oct 2022 21:42:49 +0000 Subject: [PATCH 3/3] refactor alias and solve case 3 --- orcli | 30 +++++++++++++----------------- src/lib/interactive.sh | 3 --- src/run_command.sh | 27 +++++++++++++-------------- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/orcli b/orcli index 26e9b31..d84968c 100755 --- a/orcli +++ b/orcli @@ -590,9 +590,6 @@ function init_import() { # shellcheck shell=bash function interactive() { cat <<'EOF' -if ! command -v orcli &>/dev/null; then - alias orcli="$orcli" -fi PS1="(orcli) [\u@\h \W]\$ " source <(orcli completions) echo '================================================================' @@ -864,13 +861,6 @@ orcli_run_command() { # locate orcli and OpenRefine scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")") - if ! command -v orcli &>/dev/null; then - if [[ -x "${scriptpath}/orcli" ]]; then - orcli="${scriptpath}/orcli" - else - error "orcli is not executable!" "Try: chmod + ${scriptpath}/orcli" - fi - fi if [[ -x "${scriptpath}/refine" ]]; then openrefine="${scriptpath}/refine" else @@ -907,8 +897,8 @@ orcli_run_command() { # case 1: interactive mode if stdin is selected but not present bash --rcfile <( cat ~/.bashrc - if [[ $orcli ]]; then - echo "alias orcli=$orcli" + if ! command -v orcli &>/dev/null; then + echo "alias orcli=${scriptpath}/orcli" fi interactive ) -i /dev/null; then + echo "alias orcli=${scriptpath}/orcli" fi for i in "${!files[@]}"; do - log "execute script ${files[$i]}" + log "executing script ${files[$i]}..." awk 1 "${files[$i]}" done interactive @@ -931,8 +921,14 @@ orcli_run_command() { else # case 3: just execute scripts for i in "${!files[@]}"; do - log "execute script ${files[$i]}" - bash -e <(awk 1 "${files[$i]}") + log "executing script ${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 fi diff --git a/src/lib/interactive.sh b/src/lib/interactive.sh index 9ae432e..be9dc19 100644 --- a/src/lib/interactive.sh +++ b/src/lib/interactive.sh @@ -1,9 +1,6 @@ # shellcheck shell=bash function interactive() { cat <<'EOF' -if ! command -v orcli &>/dev/null; then - alias orcli="$orcli" -fi PS1="(orcli) [\u@\h \W]\$ " source <(orcli completions) echo '================================================================' diff --git a/src/run_command.sh b/src/run_command.sh index dffd794..a7908f7 100644 --- a/src/run_command.sh +++ b/src/run_command.sh @@ -19,13 +19,6 @@ OPENREFINE_URL="http://localhost:${args[--port]}" # locate orcli and OpenRefine scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")") -if ! command -v orcli &>/dev/null; then - if [[ -x "${scriptpath}/orcli" ]]; then - orcli="${scriptpath}/orcli" - else - error "orcli is not executable!" "Try: chmod + ${scriptpath}/orcli" - fi -fi if [[ -x "${scriptpath}/refine" ]]; then openrefine="${scriptpath}/refine" else @@ -62,8 +55,8 @@ if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then # case 1: interactive mode if stdin is selected but not present bash --rcfile <( cat ~/.bashrc - if [[ $orcli ]]; then - echo "alias orcli=$orcli" + if ! command -v orcli &>/dev/null; then + echo "alias orcli=${scriptpath}/orcli" fi interactive ) -i /dev/null; then + echo "alias orcli=${scriptpath}/orcli" fi for i in "${!files[@]}"; do - log "execute script ${files[$i]}" + log "executing script ${files[$i]}..." awk 1 "${files[$i]}" done interactive @@ -86,7 +79,13 @@ if [[ ${args[--interactive]} ]]; then else # case 3: just execute scripts for i in "${!files[@]}"; do - log "execute script ${files[$i]}" - bash -e <(awk 1 "${files[$i]}") + log "executing script ${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 fi