468 lines
9.7 KiB
Bash
Executable File
468 lines
9.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# This script was generated by bashly 0.7.8 (https://bashly.dannyb.co)
|
|
# Modifying it manually is not recommended
|
|
|
|
# :script.bash3_bouncer
|
|
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
|
|
printf "bash version 4 or higher is required\n"
|
|
exit 1
|
|
fi
|
|
|
|
# :command.version_command
|
|
version_command() {
|
|
echo "$version"
|
|
}
|
|
|
|
# :command.usage
|
|
orcli_usage() {
|
|
if [[ -n $long_usage ]]; then
|
|
printf "orcli - OpenRefine command-line interface written in Bash\n"
|
|
echo
|
|
|
|
else
|
|
printf "orcli - OpenRefine command-line interface written in Bash\n"
|
|
echo
|
|
|
|
fi
|
|
|
|
printf "Usage:\n"
|
|
printf " orcli [command]\n"
|
|
printf " orcli [command] --help | -h\n"
|
|
printf " orcli --version | -v\n"
|
|
echo
|
|
# :command.usage_commands
|
|
printf "Commands:\n"
|
|
echo " info show project metadata"
|
|
echo " list list projects on OpenRefine server"
|
|
echo
|
|
|
|
if [[ -n $long_usage ]]; then
|
|
printf "Options:\n"
|
|
# :command.usage_fixed_flags
|
|
echo " --help, -h"
|
|
printf " Show this help\n"
|
|
echo
|
|
echo " --version, -v"
|
|
printf " Show version number\n"
|
|
echo
|
|
|
|
# :command.usage_environment_variables
|
|
printf "Environment Variables:\n"
|
|
|
|
# :environment_variable.usage
|
|
echo " OPENREFINE_URL"
|
|
printf " URL to OpenRefine server\n"
|
|
printf " Default: http://localhost:3333\n"
|
|
echo
|
|
|
|
# :environment_variable.usage
|
|
echo " OPENREFINE_CSRF"
|
|
printf " set to false for OpenRefine < 3.3\n"
|
|
printf " Default: true\n"
|
|
echo
|
|
# :command.usage_examples
|
|
printf "Examples:\n"
|
|
|
|
printf " orcli list\n"
|
|
printf " orcli info clipboard\n"
|
|
printf " orcli info 1234567890123\n"
|
|
echo
|
|
|
|
fi
|
|
}
|
|
|
|
# :command.usage
|
|
orcli_info_usage() {
|
|
if [[ -n $long_usage ]]; then
|
|
printf "orcli info - show project metadata\n"
|
|
echo
|
|
|
|
else
|
|
printf "orcli info - show project metadata\n"
|
|
echo
|
|
|
|
fi
|
|
|
|
printf "Usage:\n"
|
|
printf " orcli info PROJECT\n"
|
|
printf " orcli info --help | -h\n"
|
|
echo
|
|
|
|
if [[ -n $long_usage ]]; then
|
|
printf "Options:\n"
|
|
# :command.usage_fixed_flags
|
|
echo " --help, -h"
|
|
printf " Show this help\n"
|
|
echo
|
|
|
|
# :command.usage_args
|
|
printf "Arguments:\n"
|
|
|
|
# :argument.usage
|
|
echo " PROJECT"
|
|
printf " project name or id\n"
|
|
echo
|
|
|
|
# :command.usage_examples
|
|
printf "Examples:\n"
|
|
|
|
printf " info clipboard\n"
|
|
printf " info 1234567890123\n"
|
|
echo
|
|
|
|
fi
|
|
}
|
|
|
|
# :command.usage
|
|
orcli_list_usage() {
|
|
if [[ -n $long_usage ]]; then
|
|
printf "orcli list - list projects on OpenRefine server\n"
|
|
echo
|
|
|
|
else
|
|
printf "orcli list - list projects on OpenRefine server\n"
|
|
echo
|
|
|
|
fi
|
|
|
|
printf "Usage:\n"
|
|
printf " orcli list\n"
|
|
printf " orcli list --help | -h\n"
|
|
echo
|
|
|
|
if [[ -n $long_usage ]]; then
|
|
printf "Options:\n"
|
|
# :command.usage_fixed_flags
|
|
echo " --help, -h"
|
|
printf " Show this help\n"
|
|
echo
|
|
|
|
fi
|
|
}
|
|
|
|
# :command.normalize_input
|
|
normalize_input() {
|
|
local arg flags
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
arg="$1"
|
|
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
|
|
input+=("${BASH_REMATCH[1]}")
|
|
input+=("${BASH_REMATCH[2]}")
|
|
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
|
|
input+=("${BASH_REMATCH[1]}")
|
|
input+=("${BASH_REMATCH[2]}")
|
|
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
|
|
flags="${BASH_REMATCH[1]}"
|
|
for (( i=0 ; i < ${#flags} ; i++ )); do
|
|
input+=("-${flags:i:1}")
|
|
done
|
|
else
|
|
input+=("$arg")
|
|
fi
|
|
|
|
shift
|
|
done
|
|
}
|
|
# :command.inspect_args
|
|
inspect_args() {
|
|
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
|
|
if (( ${#args[@]} )); then
|
|
echo args:
|
|
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
|
|
else
|
|
echo args: none
|
|
fi
|
|
|
|
if (( ${#other_args[@]} )); then
|
|
echo
|
|
echo other_args:
|
|
echo "- \${other_args[*]} = ${other_args[*]}"
|
|
for i in "${!other_args[@]}"; do
|
|
echo "- \${other_args[$i]} = ${other_args[$i]}"
|
|
done
|
|
fi
|
|
}
|
|
|
|
# :command.user_lib
|
|
# :src/lib/error.sh
|
|
# log error message to STDERR
|
|
function error {
|
|
echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')]: ERROR $*"
|
|
}
|
|
|
|
# :src/lib/get_csrf.sh
|
|
# get CSRF token (introduced in OpenRefine 3.3)
|
|
function get_csrf {
|
|
if [[ "${OPENREFINE_CSRF}" == true ]]; then
|
|
local response
|
|
response=$(curl -fs "${OPENREFINE_URL}/command/core/get-csrf-token")
|
|
if ! [[ "${response}" == '{"token":"'* ]]; then
|
|
error "getting CSRF token failed!"
|
|
exit 1
|
|
fi
|
|
echo "?csrf_token=$(echo "$response" | cut -d \" -f 4)"
|
|
fi
|
|
}
|
|
|
|
# :src/lib/get_id.sh
|
|
# get project id (derived from project name if needed)
|
|
function get_id {
|
|
local response
|
|
local projects
|
|
local ids
|
|
if ! response="$(curl -fs --get "${OPENREFINE_URL}/command/core/get-all-project-metadata")"; then
|
|
error "no OpenRefine reachable/running at ${OPENREFINE_URL}"
|
|
exit 1
|
|
fi
|
|
if ! projects="$(echo "$response" | jq -r '.projects | keys[] as $k | "\($k):\(.[$k] | .name)"' | grep "${args[project]}")"; then
|
|
error "project ${args[project]} not found"
|
|
exit 1
|
|
fi
|
|
ids=$(echo "$projects" | cut -d : -f 1)
|
|
if ! [[ "${#ids}" == 13 ]]; then
|
|
error "multiple projects found"
|
|
echo >&2 "$projects"
|
|
exit 1
|
|
fi
|
|
echo "$ids"
|
|
}
|
|
|
|
# :command.command_functions
|
|
# :command.function
|
|
orcli_info_command() {
|
|
# :src/info_command.sh
|
|
echo "# this file is located in 'src/info_command.sh'"
|
|
echo "# code for 'openrefine-cli.sh info' goes here"
|
|
echo "# you can edit it freely and regenerate (it will not be overwritten)"
|
|
inspect_args
|
|
get_id
|
|
}
|
|
|
|
# :command.function
|
|
orcli_list_command() {
|
|
# :src/list_command.sh
|
|
# get all project metadata and reshape json to print a list
|
|
if ! response="$(curl -fs --get "${OPENREFINE_URL}/command/core/get-all-project-metadata")"
|
|
then
|
|
error "no OpenRefine reachable/running at ${OPENREFINE_URL}"
|
|
else
|
|
echo "$response" | jq -r '.projects | keys[] as $k | "\($k):\(.[$k] | .name)"'
|
|
fi
|
|
}
|
|
|
|
# :command.parse_requirements
|
|
parse_requirements() {
|
|
# :command.fixed_flag_filter
|
|
case "${1:-}" in
|
|
--version | -v )
|
|
version_command
|
|
exit
|
|
;;
|
|
|
|
--help | -h )
|
|
long_usage=yes
|
|
orcli_usage
|
|
exit
|
|
;;
|
|
|
|
esac
|
|
# :command.environment_variables_filter
|
|
export OPENREFINE_URL="${OPENREFINE_URL:-http://localhost:3333}"
|
|
export OPENREFINE_CSRF="${OPENREFINE_CSRF:-true}"
|
|
# :command.dependencies_filter
|
|
if ! [[ -x "$(command -v curl)" ]]; then
|
|
printf "missing dependency: curl\n"
|
|
exit 1
|
|
fi
|
|
if ! [[ -x "$(command -v jq)" ]]; then
|
|
printf "missing dependency: jq\n"
|
|
exit 1
|
|
fi
|
|
# :command.command_filter
|
|
action=${1:-}
|
|
|
|
case $action in
|
|
-* )
|
|
;;
|
|
|
|
info )
|
|
action="info"
|
|
shift
|
|
orcli_info_parse_requirements "$@"
|
|
shift $#
|
|
;;
|
|
|
|
list )
|
|
action="list"
|
|
shift
|
|
orcli_list_parse_requirements "$@"
|
|
shift $#
|
|
;;
|
|
|
|
# :command.command_fallback
|
|
* )
|
|
orcli_usage
|
|
exit 1
|
|
;;
|
|
|
|
esac
|
|
# :command.parse_requirements_while
|
|
while [[ $# -gt 0 ]]; do
|
|
key="$1"
|
|
case "$key" in
|
|
|
|
-?* )
|
|
printf "invalid option: %s\n" "$key"
|
|
exit 1
|
|
;;
|
|
|
|
* )
|
|
# :command.parse_requirements_case
|
|
printf "invalid argument: %s\n" "$key"
|
|
exit 1
|
|
;;
|
|
|
|
esac
|
|
done
|
|
# :command.required_args_filter
|
|
# :command.required_flags_filter
|
|
# :command.catch_all_filter
|
|
# :command.default_assignments
|
|
# :command.whitelist_filter
|
|
# :command.user_filter
|
|
}
|
|
|
|
# :command.parse_requirements
|
|
orcli_info_parse_requirements() {
|
|
# :command.fixed_flag_filter
|
|
case "${1:-}" in
|
|
--help | -h )
|
|
long_usage=yes
|
|
orcli_info_usage
|
|
exit
|
|
;;
|
|
|
|
esac
|
|
# :command.environment_variables_filter
|
|
# :command.dependencies_filter
|
|
# :command.command_filter
|
|
action="info"
|
|
# :command.parse_requirements_while
|
|
while [[ $# -gt 0 ]]; do
|
|
key="$1"
|
|
case "$key" in
|
|
|
|
-?* )
|
|
printf "invalid option: %s\n" "$key"
|
|
exit 1
|
|
;;
|
|
|
|
* )
|
|
# :command.parse_requirements_case
|
|
if [[ -z ${args[project]+x} ]]; then
|
|
# :argument.validations
|
|
args[project]=$1
|
|
shift
|
|
else
|
|
printf "invalid argument: %s\n" "$key"
|
|
exit 1
|
|
fi
|
|
;;
|
|
|
|
esac
|
|
done
|
|
# :command.required_args_filter
|
|
if [[ -z ${args[project]+x} ]]; then
|
|
printf "missing required argument: PROJECT\nusage: orcli info PROJECT\n"
|
|
exit 1
|
|
fi
|
|
# :command.required_flags_filter
|
|
# :command.catch_all_filter
|
|
# :command.default_assignments
|
|
# :command.whitelist_filter
|
|
# :command.user_filter
|
|
}
|
|
|
|
# :command.parse_requirements
|
|
orcli_list_parse_requirements() {
|
|
# :command.fixed_flag_filter
|
|
case "${1:-}" in
|
|
--help | -h )
|
|
long_usage=yes
|
|
orcli_list_usage
|
|
exit
|
|
;;
|
|
|
|
esac
|
|
# :command.environment_variables_filter
|
|
# :command.dependencies_filter
|
|
# :command.command_filter
|
|
action="list"
|
|
# :command.parse_requirements_while
|
|
while [[ $# -gt 0 ]]; do
|
|
key="$1"
|
|
case "$key" in
|
|
|
|
-?* )
|
|
printf "invalid option: %s\n" "$key"
|
|
exit 1
|
|
;;
|
|
|
|
* )
|
|
# :command.parse_requirements_case
|
|
printf "invalid argument: %s\n" "$key"
|
|
exit 1
|
|
;;
|
|
|
|
esac
|
|
done
|
|
# :command.required_args_filter
|
|
# :command.required_flags_filter
|
|
# :command.catch_all_filter
|
|
# :command.default_assignments
|
|
# :command.whitelist_filter
|
|
# :command.user_filter
|
|
}
|
|
|
|
# :command.initialize
|
|
initialize() {
|
|
version="0.1.0"
|
|
long_usage=''
|
|
set -e
|
|
|
|
# :src/initialize.sh
|
|
}
|
|
|
|
# :command.run
|
|
run() {
|
|
declare -A args=()
|
|
declare -a other_args=()
|
|
declare -a input=()
|
|
normalize_input "$@"
|
|
parse_requirements "${input[@]}"
|
|
|
|
if [[ $action == "info" ]]; then
|
|
if [[ ${args[--help]:-} ]]; then
|
|
long_usage=yes
|
|
orcli_info_usage
|
|
else
|
|
orcli_info_command
|
|
fi
|
|
|
|
elif [[ $action == "list" ]]; then
|
|
if [[ ${args[--help]:-} ]]; then
|
|
long_usage=yes
|
|
orcli_list_usage
|
|
else
|
|
orcli_list_command
|
|
fi
|
|
|
|
elif [[ $action == "root" ]]; then
|
|
root_command
|
|
fi
|
|
}
|
|
|
|
initialize
|
|
run "$@"
|