Merge pull request #65 from opencultureconsulting/felixlohmeier/batch-interactive-61

resolves #61
This commit is contained in:
Felix Lohmeier 2022-10-15 22:59:25 +02:00 committed by GitHub
commit e52990c6b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 44 deletions

101
orcli
View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
# This script was generated by bashly 0.8.7 (https://bashly.dannyb.co)
# This script was generated by bashly 0.8.9 (https://bashly.dannyb.co)
# Modifying it manually is not recommended
# :wrapper.bash3_bouncer
@ -35,7 +35,7 @@ orcli_usage() {
# :command.usage_commands
printf "Commands:\n"
echo " completions Generate bash completions"
echo " batch run tmp OpenRefine workspace and execute shell script"
echo " batch run tmp OpenRefine workspace and execute shell script(s)"
echo " import import commands"
echo " list list projects on OpenRefine server"
echo " info show project metadata"
@ -121,11 +121,11 @@ orcli_completions_usage() {
# :command.usage
orcli_batch_usage() {
if [[ -n $long_usage ]]; then
printf "orcli batch - run tmp OpenRefine workspace and execute shell script\n"
printf "orcli batch - run tmp OpenRefine workspace and execute shell script(s)\n"
echo
else
printf "orcli batch - run tmp OpenRefine workspace and execute shell script\n"
printf "orcli batch - run tmp OpenRefine workspace and execute shell script(s)\n"
echo
fi
@ -157,6 +157,11 @@ orcli_batch_usage() {
printf " Default: 3333\n"
echo
# :flag.usage
echo " --debug"
printf " do not exit on error and keep shell open\n"
echo
# :command.usage_args
printf "Arguments:\n"
@ -579,6 +584,24 @@ function init_import() {
fi
}
# src/lib/interactive.sh
# 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 '================================================================'
echo 'Interactive Bash shell with OpenRefine running in the background'
echo 'Use the "orcli" command and tab completion to control OpenRefine'
echo 'Type "history -a FILE" to write out your session history'
echo 'Type "exit" or CTRL-D to destroy temporary OpenRefine workspace'
echo '================================================================'
EOF
}
# src/lib/logging.sh
# print messages to STDERR
# shellcheck shell=bash
@ -680,7 +703,7 @@ send_completions() {
echo $' ;;'
echo $''
echo $' \'batch\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --memory --port -h")" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--debug --help --memory --port -h")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'list\'*)'
@ -706,20 +729,17 @@ send_completions() {
# :command.function
orcli_completions_command() {
# src/completions_command.sh
# shellcheck shell=bash disable=SC2154
# Users can now enable bash completion for this script by running:
#
# $ eval "$(orcli completions)"
#
send_completions
}
# :command.function
orcli_batch_command() {
# src/batch_command.sh
# shellcheck shell=bash disable=SC2154
# check if stdin is present if selected
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]] && [ -t 0 ]; then
orcli_batch_usage
exit 1
fi
# shellcheck shell=bash disable=SC2154 source=/dev/null
# catch args, convert the space delimited string to an array
files=()
@ -729,12 +749,12 @@ orcli_batch_command() {
OPENREFINE_URL="http://localhost:${args[--port]}"
# locate orcli and OpenRefine
if command -v orcli &>/dev/null; then
orcli="orcli"
elif [[ -x "orcli" ]]; then
orcli="./orcli"
else
error "orcli is not executable!" "Try: chmod + ./orcli"
if ! command -v orcli &>/dev/null; then
if [[ -x "$0" ]]; then
orcli="$0"
else
error "orcli is not executable!" "Try: chmod + $0"
fi
fi
if [[ -x "refine" ]]; then
openrefine="./refine"
@ -765,9 +785,32 @@ orcli_batch_command() {
log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${tmpdir}" "pid: ${openrefine_pid}"
fi
# execute shell script
# execute script(s) in subshell
export orcli tmpdir OPENREFINE_URL openrefine_pid
bash -e <(awk 1 "${files[@]}")
# case 1: interactive mode if stdin is selected but not present
if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
bash --rcfile <(
cat ~/.bashrc
interactive
) -i < /dev/tty
exit
fi
fi
# case 2: execute scripts and keep shell running
if [[ ${args[--debug]} ]]; then
bash --rcfile <(
cat ~/.bashrc
for i in "${!files[@]}"; do log "execute script ${files[$i]}"; awk 1 "${files[$i]}"; done
interactive
) -i < /dev/tty
exit
fi
# case 3: execute scripts
for i in "${!files[@]}"; do
log "execute script ${files[$i]}"
bash -e <(awk 1 "${files[$i]}")
done
}
@ -780,9 +823,11 @@ orcli_import_csv_command() {
init_import
# check if stdin is present if selected
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]] && [ -t 0 ]; then
orcli_import_csv_usage
exit 1
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
orcli_import_csv_usage
exit 1
fi
fi
# assemble specific post data (some options require json format)
@ -1082,6 +1127,14 @@ orcli_batch_parse_requirements() {
fi
;;
# :flag.case
--debug )
# :flag.case_no_arg
args[--debug]=1
shift
;;
-?* )
printf "invalid option: %s\n" "$key" >&2
exit 1

View File

@ -37,7 +37,7 @@ commands:
Usage: eval "\$(orcli completions)"
- name: batch
help: run tmp OpenRefine workspace and execute shell script
help: run tmp OpenRefine workspace and execute shell script(s)
args:
- name: file
help: Path to one or more files. When FILE is -, read standard input.
@ -52,6 +52,8 @@ commands:
help: PORT on which OpenRefine should listen
arg: port
default: "3333"
- long: --debug
help: do not exit on error and keep shell open
examples:
- |-
orcli batch << EOF

View File

@ -1,10 +1,4 @@
# shellcheck shell=bash disable=SC2154
# check if stdin is present if selected
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]] && [ -t 0 ]; then
orcli_batch_usage
exit 1
fi
# shellcheck shell=bash disable=SC2154 source=/dev/null
# catch args, convert the space delimited string to an array
files=()
@ -14,12 +8,12 @@ eval "files=(${args[file]})"
OPENREFINE_URL="http://localhost:${args[--port]}"
# locate orcli and OpenRefine
if command -v orcli &>/dev/null; then
orcli="orcli"
elif [[ -x "orcli" ]]; then
orcli="./orcli"
else
error "orcli is not executable!" "Try: chmod + ./orcli"
if ! command -v orcli &>/dev/null; then
if [[ -x "$0" ]]; then
orcli="$0"
else
error "orcli is not executable!" "Try: chmod + $0"
fi
fi
if [[ -x "refine" ]]; then
openrefine="./refine"
@ -50,6 +44,29 @@ else
log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${tmpdir}" "pid: ${openrefine_pid}"
fi
# execute shell script
# execute script(s) in subshell
export orcli tmpdir OPENREFINE_URL openrefine_pid
bash -e <(awk 1 "${files[@]}")
# case 1: interactive mode if stdin is selected but not present
if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
bash --rcfile <(
cat ~/.bashrc
interactive
) -i < /dev/tty
exit
fi
fi
# case 2: execute scripts and keep shell running
if [[ ${args[--debug]} ]]; then
bash --rcfile <(
cat ~/.bashrc
for i in "${!files[@]}"; do log "execute script ${files[$i]}"; awk 1 "${files[$i]}"; done
interactive
) -i < /dev/tty
exit
fi
# case 3: execute scripts
for i in "${!files[@]}"; do
log "execute script ${files[$i]}"
bash -e <(awk 1 "${files[$i]}")
done

View File

@ -4,9 +4,11 @@
init_import
# check if stdin is present if selected
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]] && [ -t 0 ]; then
orcli_import_csv_usage
exit 1
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
orcli_import_csv_usage
exit 1
fi
fi
# assemble specific post data (some options require json format)

16
src/lib/interactive.sh Normal file
View File

@ -0,0 +1,16 @@
# 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 '================================================================'
echo 'Interactive Bash shell with OpenRefine running in the background'
echo 'Use the "orcli" command and tab completion to control OpenRefine'
echo 'Type "history -a FILE" to write out your session history'
echo 'Type "exit" or CTRL-D to destroy temporary OpenRefine workspace'
echo '================================================================'
EOF
}

View File

@ -51,7 +51,7 @@ send_completions() {
echo $' ;;'
echo $''
echo $' \'batch\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --memory --port -h")" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--debug --help --memory --port -h")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'list\'*)'