From 98fa74a7cab1462dd6c2e82370df1467cca416ec Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Wed, 16 Nov 2022 15:37:34 +0000 Subject: [PATCH] move part of export to lib file --- orcli | 47 +++++++++++++++++++++++---------------- src/export_tsv_command.sh | 21 ++--------------- src/lib/post_export.sh | 24 ++++++++++++++++++++ 3 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 src/lib/post_export.sh diff --git a/orcli b/orcli index 7972e71..37ff7b0 100755 --- a/orcli +++ b/orcli @@ -686,6 +686,32 @@ function log() { fi } +# src/lib/post_export.sh +# post to export-rows endpoint +# shellcheck shell=bash disable=SC2154 +function post_export() { + local curloptions + # post + mapfile -t curloptions < <(for d in "$@"; do + echo "--data" + echo "$d" + done) + if [[ ${args[--output]} ]]; then + if ! mkdir -p "$(dirname "${args[--output]}")"; then + error "unable to create parent directory for ${args[--output]}" + fi + curloptions+=("--output") + curloptions+=("${args[--output]}") + fi + if ! curl -fs "${curloptions[@]}" "${OPENREFINE_URL}/command/core/export-rows"; then + error "exporting ${args[project]} failed!" + else + if [[ ${args[--output]} ]]; then + log "exported ${args[project]}" "file: ${args[--output]}" "rows: $(wc -l <"${args[--output]}")" + fi + fi +} + # src/lib/post_import.sh # post to create-project endpoint and validate # shellcheck shell=bash disable=SC2154 @@ -998,25 +1024,8 @@ orcli_export_tsv_command() { options+=' }' data+=("options=${options}") - # post - mapfile -t curloptions < <(for d in "${data[@]}"; do - echo "--data" - echo "$d" - done) - if [[ ${args[--output]} ]]; then - if ! mkdir -p "$(dirname "${args[--output]}")"; then - error "unable to create parent directory for ${args[--output]}" - fi - curloptions+=("--output") - curloptions+=("${args[--output]}") - fi - if ! curl -fs "${curloptions[@]}" "${OPENREFINE_URL}/command/core/export-rows"; then - error "exporting ${args[project]} failed!" - else - if [[ ${args[--output]} ]]; then - log "exported ${args[project]}" "file: ${args[--output]}" "rows: $(wc -l <"${args[--output]}")" - fi - fi + # call post_export function to post data and validate results + post_export "${data[@]}" } diff --git a/src/export_tsv_command.sh b/src/export_tsv_command.sh index b92d3c3..b9cf078 100644 --- a/src/export_tsv_command.sh +++ b/src/export_tsv_command.sh @@ -14,22 +14,5 @@ fi options+=' }' data+=("options=${options}") -# post -mapfile -t curloptions < <(for d in "${data[@]}"; do - echo "--data" - echo "$d" -done) -if [[ ${args[--output]} ]]; then - if ! mkdir -p "$(dirname "${args[--output]}")"; then - error "unable to create parent directory for ${args[--output]}" - fi - curloptions+=("--output") - curloptions+=("${args[--output]}") -fi -if ! curl -fs "${curloptions[@]}" "${OPENREFINE_URL}/command/core/export-rows"; then - error "exporting ${args[project]} failed!" -else - if [[ ${args[--output]} ]]; then - log "exported ${args[project]}" "file: ${args[--output]}" "rows: $(wc -l <"${args[--output]}")" - fi -fi +# call post_export function to post data and validate results +post_export "${data[@]}" diff --git a/src/lib/post_export.sh b/src/lib/post_export.sh new file mode 100644 index 0000000..fd48304 --- /dev/null +++ b/src/lib/post_export.sh @@ -0,0 +1,24 @@ +# post to export-rows endpoint +# shellcheck shell=bash disable=SC2154 +function post_export() { + local curloptions + # post + mapfile -t curloptions < <(for d in "$@"; do + echo "--data" + echo "$d" + done) + if [[ ${args[--output]} ]]; then + if ! mkdir -p "$(dirname "${args[--output]}")"; then + error "unable to create parent directory for ${args[--output]}" + fi + curloptions+=("--output") + curloptions+=("${args[--output]}") + fi + if ! curl -fs "${curloptions[@]}" "${OPENREFINE_URL}/command/core/export-rows"; then + error "exporting ${args[project]} failed!" + else + if [[ ${args[--output]} ]]; then + log "exported ${args[project]}" "file: ${args[--output]}" "rows: $(wc -l <"${args[--output]}")" + fi + fi +}