From aadad1ea6b91fd95dd7d3dcc2d489bc4008c2f66 Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Tue, 12 Apr 2022 16:26:43 +0000 Subject: [PATCH] improve logging --- orcli | 38 ++++++++++++++++++++++---------------- src/lib/error.sh | 5 ----- src/lib/get_csrf.sh | 2 -- src/lib/get_id.sh | 8 ++------ src/lib/logging.sh | 13 +++++++++++++ src/list_command.sh | 6 +++++- 6 files changed, 42 insertions(+), 30 deletions(-) delete mode 100644 src/lib/error.sh create mode 100644 src/lib/logging.sh diff --git a/orcli b/orcli index 60ed405..1ca47f3 100755 --- a/orcli +++ b/orcli @@ -212,13 +212,6 @@ inspect_args() { } # :command.user_lib -# src/lib/error.sh -# log error message to STDERR -# shellcheck shell=bash -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) # shellcheck shell=bash @@ -227,12 +220,10 @@ function get_csrf() { 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 fi echo "?csrf_token=$(echo "$response" | cut -d \" -f 4)" fi @@ -247,21 +238,32 @@ function get_id() { 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 + 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 + error "multiple projects found" "$projects" fi echo "$ids" } +# src/lib/logging.sh +# print messages to STDERR +# shellcheck shell=bash +function error() { + echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')] ERROR: $1" + shift + for msg in "$@"; do echo >&2 "$msg"; done + exit 1 +} +function log() { + echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')] $1" + shift + for msg in "$@"; do echo >&2 "$msg"; done +} + # :command.command_functions # :command.function orcli_info_command() { @@ -285,7 +287,11 @@ orcli_list_command() { 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)"' + if [[ "${response}" == '{"projects":{}}' ]]; then + log "${OPENREFINE_URL} contains zero projects" + else + echo "$response" | jq -r '.projects | keys[] as $k | "\($k):\(.[$k] | .name)"' + fi fi } diff --git a/src/lib/error.sh b/src/lib/error.sh deleted file mode 100644 index 32780ab..0000000 --- a/src/lib/error.sh +++ /dev/null @@ -1,5 +0,0 @@ -# log error message to STDERR -# shellcheck shell=bash -function error() { - echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')]: ERROR $*" -} diff --git a/src/lib/get_csrf.sh b/src/lib/get_csrf.sh index 1f8be05..21fbada 100644 --- a/src/lib/get_csrf.sh +++ b/src/lib/get_csrf.sh @@ -5,12 +5,10 @@ function get_csrf() { 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 fi echo "?csrf_token=$(echo "$response" | cut -d \" -f 4)" fi diff --git a/src/lib/get_id.sh b/src/lib/get_id.sh index c61b8ec..5c413b0 100644 --- a/src/lib/get_id.sh +++ b/src/lib/get_id.sh @@ -6,17 +6,13 @@ function get_id() { 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 + 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 + error "multiple projects found" "$projects" fi echo "$ids" } diff --git a/src/lib/logging.sh b/src/lib/logging.sh new file mode 100644 index 0000000..a577df0 --- /dev/null +++ b/src/lib/logging.sh @@ -0,0 +1,13 @@ +# print messages to STDERR +# shellcheck shell=bash +function error() { + echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')] ERROR: $1" + shift + for msg in "$@"; do echo >&2 "$msg"; done + exit 1 +} +function log() { + echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')] $1" + shift + for msg in "$@"; do echo >&2 "$msg"; done +} diff --git a/src/list_command.sh b/src/list_command.sh index 3e31ad0..6b26e36 100644 --- a/src/list_command.sh +++ b/src/list_command.sh @@ -3,5 +3,9 @@ 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)"' + if [[ "${response}" == '{"projects":{}}' ]]; then + log "${OPENREFINE_URL} contains zero projects" + else + echo "$response" | jq -r '.projects | keys[] as $k | "\($k):\(.[$k] | .name)"' + fi fi