From 3d54c200ee87ae592dc83b4b7388b4f78334d075 Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Wed, 16 Nov 2022 15:18:44 +0000 Subject: [PATCH] put get_id at the beginning --- orcli | 32 ++++++++++++++++++-------------- src/info_command.sh | 4 +++- src/transform_command.sh | 27 +++++++++++++++------------ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/orcli b/orcli index 96419b8..7972e71 100755 --- a/orcli +++ b/orcli @@ -864,8 +864,9 @@ orcli_list_command() { orcli_info_command() { # src/info_command.sh # shellcheck shell=bash disable=SC2154 - get_id "${args[project]}" - + # get project id + projectid="$(get_id "${args[project]}")" + echo "$projectid" } # :command.function @@ -885,6 +886,9 @@ orcli_transform_command() { files=() eval "files=(${args[file]})" + # get project id + projectid="$(get_id "${args[project]}")" + # create tmp directory tmpdir="$(mktemp -d)" trap 'rm -rf "$tmpdir"' 0 2 3 15 @@ -925,13 +929,13 @@ orcli_transform_command() { fi for line in "${jsonlines[@]}"; do # parse one line/operation into array - declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))" - if [[ ! ${data[op]} ]]; then + declare -A array="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))" + if [[ ! ${array[op]} ]]; then error "parsing ${files[$i]} failed!" fi # map operation names to command endpoints # 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-split" ]]; then com="split-multi-value-cells"; 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-removal" ]]; then com="remove-rows"; fi if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi - unset "data[op]" + unset "array[op]" # rename engineConfig to engine - data[engine]="${data[engineConfig]}" - unset "data[engineConfig]" + array[engine]="${array[engineConfig]}" + unset "array[engineConfig]" # drop description - unset "data[description]" + unset "array[description]" # prepare curl options - mapfile -t curloptions < <(for K in "${!data[@]}"; do + mapfile -t curloptions < <(for K in "${!array[@]}"; do echo "--data" - echo "$K=${data[$K]}" + echo "$K=${array[$K]}" done) - # get project id and csrf token; 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 + # get csrf token and post data to it's individual endpoint + if response="$(curl -fs --data "project=${projectid}" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)")"; then response_code="$(jq -r '.code' <<<"$response")" if [[ $response_code == "ok" ]]; then log "transformed ${args[project]} with ${com}" "Response: $(jq -r '.historyEntry.description' <<<"$response")" @@ -969,7 +973,7 @@ orcli_transform_command() { else error "transforming ${args[project]} with ${com} from ${files[$i]} failed!" fi - unset data + unset array done done diff --git a/src/info_command.sh b/src/info_command.sh index 85bbe49..bb89eee 100644 --- a/src/info_command.sh +++ b/src/info_command.sh @@ -1,2 +1,4 @@ # shellcheck shell=bash disable=SC2154 -get_id "${args[project]}" +# get project id +projectid="$(get_id "${args[project]}")" +echo "$projectid" \ No newline at end of file diff --git a/src/transform_command.sh b/src/transform_command.sh index 5c35f4a..58b9333 100644 --- a/src/transform_command.sh +++ b/src/transform_command.sh @@ -12,6 +12,9 @@ fi files=() eval "files=(${args[file]})" +# get project id +projectid="$(get_id "${args[project]}")" + # create tmp directory tmpdir="$(mktemp -d)" trap 'rm -rf "$tmpdir"' 0 2 3 15 @@ -52,13 +55,13 @@ for i in "${!files[@]}"; do fi for line in "${jsonlines[@]}"; do # parse one line/operation into array - declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))" - if [[ ! ${data[op]} ]]; then + declare -A array="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))" + if [[ ! ${array[op]} ]]; then error "parsing ${files[$i]} failed!" fi # map operation names to command endpoints # 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-split" ]]; then com="split-multi-value-cells"; 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-removal" ]]; then com="remove-rows"; fi if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi - unset "data[op]" + unset "array[op]" # rename engineConfig to engine - data[engine]="${data[engineConfig]}" - unset "data[engineConfig]" + array[engine]="${array[engineConfig]}" + unset "array[engineConfig]" # drop description - unset "data[description]" + unset "array[description]" # prepare curl options - mapfile -t curloptions < <(for K in "${!data[@]}"; do + mapfile -t curloptions < <(for K in "${!array[@]}"; do echo "--data" - echo "$K=${data[$K]}" + echo "$K=${array[$K]}" done) - # get project id and csrf token; 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 + # get csrf token and post data to it's individual endpoint + if response="$(curl -fs --data "project=${projectid}" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)")"; then response_code="$(jq -r '.code' <<<"$response")" if [[ $response_code == "ok" ]]; then log "transformed ${args[project]} with ${com}" "Response: $(jq -r '.historyEntry.description' <<<"$response")" @@ -96,6 +99,6 @@ for i in "${!files[@]}"; do else error "transforming ${args[project]} with ${com} from ${files[$i]} failed!" fi - unset data + unset array done done