From d8182df781aeff86407d9f2b1a6e0894ecffaddf Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Tue, 12 Apr 2022 10:54:16 +0000 Subject: [PATCH] remove csrf env --- orcli | 113 +++++++++++++++++++++++++++++++++++++----- src/bashly.yml | 5 +- src/import_command.sh | 2 + src/initialize.sh | 1 - src/lib/get_csrf.sh | 10 ++-- 5 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 src/import_command.sh diff --git a/orcli b/orcli index 1d3ba7a..60ed405 100755 --- a/orcli +++ b/orcli @@ -34,8 +34,9 @@ orcli_usage() { echo # :command.usage_commands printf "Commands:\n" - echo " info show project metadata" - echo " list list projects on OpenRefine server" + echo " info show project metadata" + echo " import " + echo " list list projects on OpenRefine server" echo if [[ -n $long_usage ]]; then @@ -56,12 +57,6 @@ orcli_usage() { 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" @@ -118,6 +113,33 @@ orcli_info_usage() { fi } +# :command.usage +orcli_import_usage() { + if [[ -n $long_usage ]]; then + printf "orcli import - \n" + echo + + else + printf "orcli import - \n" + echo + + fi + + printf "Usage:\n" + printf " orcli import\n" + printf " orcli import --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.usage orcli_list_usage() { if [[ -n $long_usage ]]; then @@ -201,9 +223,13 @@ function error() { # get CSRF token (introduced in OpenRefine 3.3) # shellcheck shell=bash function get_csrf() { - if [[ "${OPENREFINE_CSRF}" == true ]]; then - local response - response=$(curl -fs "${OPENREFINE_URL}/command/core/get-csrf-token") + local response + if ! response="$(curl -fs "${OPENREFINE_URL}/command/core/get-csrf-token")"; then + if ! response="$(curl -fs "${OPENREFINE_URL}/command/core/get-version")"; then + error "no OpenRefine reachable/running at ${OPENREFINE_URL}" + exit 1 + fi + else if ! [[ "${response}" == '{"token":"'* ]]; then error "getting CSRF token failed!" exit 1 @@ -244,6 +270,13 @@ orcli_info_command() { get_id } +# :command.function +orcli_import_command() { + # src/import_command.sh + # shellcheck shell=bash + get_csrf +} + # :command.function orcli_list_command() { # src/list_command.sh @@ -274,7 +307,6 @@ parse_requirements() { 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" @@ -298,6 +330,13 @@ parse_requirements() { shift $# ;; + import ) + action="import" + shift + orcli_import_parse_requirements "$@" + shift $# + ;; + list ) action="list" shift @@ -389,6 +428,47 @@ orcli_info_parse_requirements() { # :command.user_filter } +# :command.parse_requirements +orcli_import_parse_requirements() { + # :command.fixed_flags_filter + case "${1:-}" in + --help | -h ) + long_usage=yes + orcli_import_usage + exit + ;; + + esac + # :command.environment_variables_filter + # :command.dependencies_filter + # :command.command_filter + action="import" + # :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_list_parse_requirements() { # :command.fixed_flags_filter @@ -437,7 +517,6 @@ initialize() { set -e # src/initialize.sh - # shellcheck shell=bash } # :command.run @@ -456,6 +535,14 @@ run() { orcli_info_command fi + elif [[ $action == "import" ]]; then + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_import_usage + else + orcli_import_command + fi + elif [[ $action == "list" ]]; then if [[ ${args[--help]:-} ]]; then long_usage=yes diff --git a/src/bashly.yml b/src/bashly.yml index 321a0ed..0647884 100644 --- a/src/bashly.yml +++ b/src/bashly.yml @@ -11,9 +11,6 @@ environment_variables: - name: OPENREFINE_URL help: URL to OpenRefine server default: "http://localhost:3333" - - name: OPENREFINE_CSRF - default: "true" - help: set to false for OpenRefine < 3.3 examples: - orcli list @@ -31,5 +28,7 @@ commands: - info clipboard - info 1234567890123 + - name: import + - name: list help: list projects on OpenRefine server diff --git a/src/import_command.sh b/src/import_command.sh new file mode 100644 index 0000000..19e14dc --- /dev/null +++ b/src/import_command.sh @@ -0,0 +1,2 @@ +# shellcheck shell=bash +get_csrf \ No newline at end of file diff --git a/src/initialize.sh b/src/initialize.sh index 88ea9c1..e69de29 100644 --- a/src/initialize.sh +++ b/src/initialize.sh @@ -1 +0,0 @@ -# shellcheck shell=bash \ No newline at end of file diff --git a/src/lib/get_csrf.sh b/src/lib/get_csrf.sh index c795a66..1f8be05 100644 --- a/src/lib/get_csrf.sh +++ b/src/lib/get_csrf.sh @@ -1,9 +1,13 @@ # get CSRF token (introduced in OpenRefine 3.3) # shellcheck shell=bash function get_csrf() { - if [[ "${OPENREFINE_CSRF}" == true ]]; then - local response - response=$(curl -fs "${OPENREFINE_URL}/command/core/get-csrf-token") + local response + if ! response="$(curl -fs "${OPENREFINE_URL}/command/core/get-csrf-token")"; then + if ! response="$(curl -fs "${OPENREFINE_URL}/command/core/get-version")"; then + error "no OpenRefine reachable/running at ${OPENREFINE_URL}" + exit 1 + fi + else if ! [[ "${response}" == '{"token":"'* ]]; then error "getting CSRF token failed!" exit 1