From 1593f2eccb4de21e4a48059c8869ed35712df707 Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Sat, 22 Oct 2022 21:42:49 +0000 Subject: [PATCH] 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