Merge pull request #81 from opencultureconsulting:felixlohmeier/transform-error-handling-78

put get_id at the beginning
This commit is contained in:
Felix Lohmeier 2022-11-16 16:20:14 +01:00 committed by GitHub
commit 7032bb20b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 27 deletions

32
orcli
View File

@ -864,8 +864,9 @@ orcli_list_command() {
orcli_info_command() { orcli_info_command() {
# src/info_command.sh # src/info_command.sh
# shellcheck shell=bash disable=SC2154 # shellcheck shell=bash disable=SC2154
get_id "${args[project]}" # get project id
projectid="$(get_id "${args[project]}")"
echo "$projectid"
} }
# :command.function # :command.function
@ -885,6 +886,9 @@ orcli_transform_command() {
files=() files=()
eval "files=(${args[file]})" eval "files=(${args[file]})"
# get project id
projectid="$(get_id "${args[project]}")"
# create tmp directory # create tmp directory
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' 0 2 3 15 trap 'rm -rf "$tmpdir"' 0 2 3 15
@ -925,13 +929,13 @@ orcli_transform_command() {
fi fi
for line in "${jsonlines[@]}"; do for line in "${jsonlines[@]}"; do
# parse one line/operation into array # parse one line/operation into array
declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))" declare -A array="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))"
if [[ ! ${data[op]} ]]; then if [[ ! ${array[op]} ]]; then
error "parsing ${files[$i]} failed!" error "parsing ${files[$i]} failed!"
fi fi
# map operation names to command endpoints # map operation names to command endpoints
# https://github.com/OpenRefine/OpenRefine/blob/master/main/webapp/modules/core/MOD-INF/controller.js # https://github.com/OpenRefine/OpenRefine/blob/master/main/webapp/modules/core/MOD-INF/controller.js
com="${data[op]#core/}" com="${array[op]#core/}"
if [[ $com == "multivalued-cell-join" ]]; then com="join-multi-value-cells"; fi if [[ $com == "multivalued-cell-join" ]]; then com="join-multi-value-cells"; fi
if [[ $com == "multivalued-cell-split" ]]; then com="split-multi-value-cells"; fi if [[ $com == "multivalued-cell-split" ]]; then com="split-multi-value-cells"; fi
if [[ $com == "column-addition" ]]; then com="add-column"; fi if [[ $com == "column-addition" ]]; then com="add-column"; fi
@ -947,19 +951,19 @@ orcli_transform_command() {
if [[ $com == "row-flag" ]]; then com="annotate-rows"; fi if [[ $com == "row-flag" ]]; then com="annotate-rows"; fi
if [[ $com == "row-removal" ]]; then com="remove-rows"; fi if [[ $com == "row-removal" ]]; then com="remove-rows"; fi
if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi
unset "data[op]" unset "array[op]"
# rename engineConfig to engine # rename engineConfig to engine
data[engine]="${data[engineConfig]}" array[engine]="${array[engineConfig]}"
unset "data[engineConfig]" unset "array[engineConfig]"
# drop description # drop description
unset "data[description]" unset "array[description]"
# prepare curl options # prepare curl options
mapfile -t curloptions < <(for K in "${!data[@]}"; do mapfile -t curloptions < <(for K in "${!array[@]}"; do
echo "--data" echo "--data"
echo "$K=${data[$K]}" echo "$K=${array[$K]}"
done) done)
# get project id and csrf token; post data to it's individual endpoint # get csrf token and post data to it's individual endpoint
if response="$(curl -fs --data "project=$(get_id "${args[project]}")" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)")"; then if response="$(curl -fs --data "project=${projectid}" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)")"; then
response_code="$(jq -r '.code' <<<"$response")" response_code="$(jq -r '.code' <<<"$response")"
if [[ $response_code == "ok" ]]; then if [[ $response_code == "ok" ]]; then
log "transformed ${args[project]} with ${com}" "Response: $(jq -r '.historyEntry.description' <<<"$response")" log "transformed ${args[project]} with ${com}" "Response: $(jq -r '.historyEntry.description' <<<"$response")"
@ -969,7 +973,7 @@ orcli_transform_command() {
else else
error "transforming ${args[project]} with ${com} from ${files[$i]} failed!" error "transforming ${args[project]} with ${com} from ${files[$i]} failed!"
fi fi
unset data unset array
done done
done done

View File

@ -1,2 +1,4 @@
# shellcheck shell=bash disable=SC2154 # shellcheck shell=bash disable=SC2154
get_id "${args[project]}" # get project id
projectid="$(get_id "${args[project]}")"
echo "$projectid"

View File

@ -12,6 +12,9 @@ fi
files=() files=()
eval "files=(${args[file]})" eval "files=(${args[file]})"
# get project id
projectid="$(get_id "${args[project]}")"
# create tmp directory # create tmp directory
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' 0 2 3 15 trap 'rm -rf "$tmpdir"' 0 2 3 15
@ -52,13 +55,13 @@ for i in "${!files[@]}"; do
fi fi
for line in "${jsonlines[@]}"; do for line in "${jsonlines[@]}"; do
# parse one line/operation into array # parse one line/operation into array
declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))" declare -A array="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))"
if [[ ! ${data[op]} ]]; then if [[ ! ${array[op]} ]]; then
error "parsing ${files[$i]} failed!" error "parsing ${files[$i]} failed!"
fi fi
# map operation names to command endpoints # map operation names to command endpoints
# https://github.com/OpenRefine/OpenRefine/blob/master/main/webapp/modules/core/MOD-INF/controller.js # https://github.com/OpenRefine/OpenRefine/blob/master/main/webapp/modules/core/MOD-INF/controller.js
com="${data[op]#core/}" com="${array[op]#core/}"
if [[ $com == "multivalued-cell-join" ]]; then com="join-multi-value-cells"; fi if [[ $com == "multivalued-cell-join" ]]; then com="join-multi-value-cells"; fi
if [[ $com == "multivalued-cell-split" ]]; then com="split-multi-value-cells"; fi if [[ $com == "multivalued-cell-split" ]]; then com="split-multi-value-cells"; fi
if [[ $com == "column-addition" ]]; then com="add-column"; fi if [[ $com == "column-addition" ]]; then com="add-column"; fi
@ -74,19 +77,19 @@ for i in "${!files[@]}"; do
if [[ $com == "row-flag" ]]; then com="annotate-rows"; fi if [[ $com == "row-flag" ]]; then com="annotate-rows"; fi
if [[ $com == "row-removal" ]]; then com="remove-rows"; fi if [[ $com == "row-removal" ]]; then com="remove-rows"; fi
if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi
unset "data[op]" unset "array[op]"
# rename engineConfig to engine # rename engineConfig to engine
data[engine]="${data[engineConfig]}" array[engine]="${array[engineConfig]}"
unset "data[engineConfig]" unset "array[engineConfig]"
# drop description # drop description
unset "data[description]" unset "array[description]"
# prepare curl options # prepare curl options
mapfile -t curloptions < <(for K in "${!data[@]}"; do mapfile -t curloptions < <(for K in "${!array[@]}"; do
echo "--data" echo "--data"
echo "$K=${data[$K]}" echo "$K=${array[$K]}"
done) done)
# get project id and csrf token; post data to it's individual endpoint # get csrf token and post data to it's individual endpoint
if response="$(curl -fs --data "project=$(get_id "${args[project]}")" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)")"; then if response="$(curl -fs --data "project=${projectid}" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)")"; then
response_code="$(jq -r '.code' <<<"$response")" response_code="$(jq -r '.code' <<<"$response")"
if [[ $response_code == "ok" ]]; then if [[ $response_code == "ok" ]]; then
log "transformed ${args[project]} with ${com}" "Response: $(jq -r '.historyEntry.description' <<<"$response")" log "transformed ${args[project]} with ${com}" "Response: $(jq -r '.historyEntry.description' <<<"$response")"
@ -96,6 +99,6 @@ for i in "${!files[@]}"; do
else else
error "transforming ${args[project]} with ${com} from ${files[$i]} failed!" error "transforming ${args[project]} with ${com} from ${files[$i]} failed!"
fi fi
unset data unset array
done done
done done