This commit is contained in:
Felix Lohmeier 2020-07-07 12:56:22 +02:00 committed by GitHub
parent db73bca422
commit b5ee345a59
1 changed files with 54 additions and 45 deletions

View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# openrefine-bash-curl.sh, Felix Lohmeier, v0.4.1, 2020-07-04 # openrefine-bash-curl.sh, Felix Lohmeier, v0.5, 2020-07-07
# How to control OpenRefine 3.3+ with cURL (and jq) in Bash scripts # How to control OpenRefine 3.3+ with cURL (and jq) in Bash scripts
# https://gist.github.com/felixlohmeier/d76bd27fbc4b8ab6d683822cdf61f81d # https://gist.github.com/felixlohmeier/d76bd27fbc4b8ab6d683822cdf61f81d
# tested on Linux (Fedora 33), needs to be adapted to work on macOS # tested on Linux (Fedora 33), needs to be adapted to work on macOS
@ -131,6 +131,9 @@ exec &> >(tee -a "${workspace}/${date}.log")
function log() { function log() {
echo "$(date +%H:%M:%S.%3N) [ client] $1" echo "$(date +%H:%M:%S.%3N) [ client] $1"
} }
function error() {
echo 1>&2 "ERROR: $1"; stop; exit 1
}
# ======================= TEMPLATES FOR YOUR WORKFLOW ======================== # # ======================= TEMPLATES FOR YOUR WORKFLOW ======================== #
@ -163,11 +166,12 @@ $ \ '
DATA DATA
then then
store "${p}" "${workspace}/${filename}.id" \ store "${p}" "${workspace}/${filename}.id" \
|| { echo 1>&2 "ERROR: import of ${input} failed!"; stop; exit 1; } \ || error "import of ${input} failed!" \
&& log "imported ${input} as ${p} (${ids[$p]})"; echo && log "imported ${input} as ${p} (${ids[$p]})"
else else
echo 1>&2 "ERROR: import of ${input} failed!"; stop; exit 1 error "import of ${input} failed!"
fi fi
echo
# ----------------------------- IMPORT OPTION 2 ------------------------------ # # ----------------------------- IMPORT OPTION 2 ------------------------------ #
@ -193,11 +197,12 @@ if curl -fsS --write-out "%{redirect_url}\n" \
> "${workspace}/${filename}.id" > "${workspace}/${filename}.id"
then then
store "${p}" "${workspace}/${filename}.id" \ store "${p}" "${workspace}/${filename}.id" \
|| { echo 1>&2 "ERROR: import of ${input} failed!"; stop; exit 1; } \ || error "import of ${input} failed!" \
&& log "imported ${input} as ${p} (${ids[$p]})"; echo && log "imported ${input} as ${p} (${ids[$p]})"
else else
echo 1>&2 "ERROR: import of ${input} failed!"; stop; exit 1 error "import of ${input} failed!"
fi fi
echo
# ----------------------------- IMPORT OPTION 3 ------------------------------ # # ----------------------------- IMPORT OPTION 3 ------------------------------ #
@ -231,10 +236,10 @@ for i in "${!inputs[@]}"; do
wait "${pid[$i]}" wait "${pid[$i]}"
if [[ $(wait "${pid[$i]}") -eq 0 ]]; then if [[ $(wait "${pid[$i]}") -eq 0 ]]; then
store "${p}" "${workspace}/${filename}.id" \ store "${p}" "${workspace}/${filename}.id" \
|| { echo 1>&2 "ERROR: import of ${input} failed!"; stop; exit 1; } \ || error "import of ${input} failed!" \
&& log "imported ${inputs[$i]} as ${p} (${ids[$p]})" && log "imported ${inputs[$i]} as ${p} (${ids[$p]})"
else else
echo 1>&2 "ERROR: import of ${inputs[$i]} failed!"; stop; exit 1 error "import of ${inputs[$i]} failed!"
fi fi
done done
echo echo
@ -268,11 +273,10 @@ if curl -fsS \
"${endpoint}/command/core/apply-operations?csrf_token=$(csrf)" > /dev/null "${endpoint}/command/core/apply-operations?csrf_token=$(csrf)" > /dev/null
then then
log "transformed ${p} (${ids[$p]}) with ${input}" log "transformed ${p} (${ids[$p]}) with ${input}"
echo
else else
echo 1>&2 "ERROR: transform ${p} (${ids[$p]}) with ${input} failed!" error "transform ${p} (${ids[$p]}) with ${input} failed!"
stop; exit 1
fi fi
echo
# ---------------------------- TRANSFORM OPTION 2 ---------------------------- # # ---------------------------- TRANSFORM OPTION 2 ---------------------------- #
@ -300,10 +304,10 @@ if curl -fsS \
JSON JSON
then then
log "transformed ${p} (${ids[$p]})" log "transformed ${p} (${ids[$p]})"
echo
else else
echo 1>&2 "ERROR: transform ${p} (${ids[$p]}) failed!"; stop; exit 1 error "transform ${p} (${ids[$p]}) failed!"
fi fi
echo
# ---------------------------- TRANSFORM OPTION 3 ---------------------------- # # ---------------------------- TRANSFORM OPTION 3 ---------------------------- #
@ -334,10 +338,10 @@ if curl -fsS \
JSON JSON
then then
log "transformed ${p} (${ids[$p]})" log "transformed ${p} (${ids[$p]})"
echo
else else
echo 1>&2 "ERROR: transform ${p} (${ids[$p]}) failed!"; stop; exit 1 error "transform ${p} (${ids[$p]}) failed!"
fi fi
echo
# ---------------------------- TRANSFORM OPTION 4 ---------------------------- # # ---------------------------- TRANSFORM OPTION 4 ---------------------------- #
@ -372,10 +376,10 @@ if curl -fsS \
JSON JSON
then then
log "transformed ${p} (${ids[$p]})" log "transformed ${p} (${ids[$p]})"
echo
else else
echo 1>&2 "ERROR: transform ${p} (${ids[$p]}) failed!"; stop; exit 1 error "transform ${p} (${ids[$p]}) failed!"
fi fi
echo
# ---------------------------- TRANSFORM OPTION 5 ---------------------------- # # ---------------------------- TRANSFORM OPTION 5 ---------------------------- #
@ -401,10 +405,10 @@ if echo "${payload[@]}" | "${jq}" -s add | curl -fsS \
"${endpoint}/command/core/apply-operations?csrf_token=$(csrf)" > /dev/null "${endpoint}/command/core/apply-operations?csrf_token=$(csrf)" > /dev/null
then then
log "transformed ${p} (${ids[$p]})" log "transformed ${p} (${ids[$p]})"
echo
else else
echo 1>&2 "ERROR: transform ${p} (${ids[$p]}) failed!"; stop; exit 1 error "transform ${p} (${ids[$p]}) failed!"
fi fi
echo
# ----------------------------- EXPORT OPTION 1 ------------------------------ # # ----------------------------- EXPORT OPTION 1 ------------------------------ #
@ -418,10 +422,11 @@ if curl -fsS \
"${endpoint}/command/core/export-rows" "${endpoint}/command/core/export-rows"
then then
#log "printed export of ${p} (${ids[$p]})" #log "printed export of ${p} (${ids[$p]})"
echo :
else else
echo 1>&2 "ERROR: export of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "export of ${p} (${ids[$p]}) failed!"
fi fi
echo
# ----------------------------- EXPORT OPTION 2 ------------------------------ # # ----------------------------- EXPORT OPTION 2 ------------------------------ #
@ -437,10 +442,10 @@ if curl -fsS \
> "${output}" > "${output}"
then then
log "${p} (${ids[$p]}) saved to file ${output}" log "${p} (${ids[$p]}) saved to file ${output}"
echo
else else
echo 1>&2 "ERROR: export of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "export of ${p} (${ids[$p]}) failed!"
fi fi
echo
# ----------------------------- EXPORT OPTION 3 ------------------------------ # # ----------------------------- EXPORT OPTION 3 ------------------------------ #
@ -468,10 +473,10 @@ if echo "${template}" | head -c -2 | curl -fsS \
then then
echo echo
#log "printed export of ${p} (${ids[$p]})" #log "printed export of ${p} (${ids[$p]})"
echo
else else
echo 1>&2 "ERROR: export of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "export of ${p} (${ids[$p]}) failed!"
fi fi
echo
# ----------------------------- EXPORT OPTION 4 ------------------------------ # # ----------------------------- EXPORT OPTION 4 ------------------------------ #
@ -500,10 +505,10 @@ if echo "${template}" | head -c -2 | curl -fsS \
> "${output}" > "${output}"
then then
log "${p} (${ids[$p]}) saved to ${output}" log "${p} (${ids[$p]}) saved to ${output}"
echo
else else
echo 1>&2 "ERROR: export of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "export of ${p} (${ids[$p]}) failed!"
fi fi
echo
# ----------------------------- EXPORT OPTION 5 ------------------------------ # # ----------------------------- EXPORT OPTION 5 ------------------------------ #
@ -523,10 +528,11 @@ for p in "${ps[@]}"; do
done done
for i in "${!ps[@]}"; do for i in "${!ps[@]}"; do
p="${ps[$i]}" p="${ps[$i]}"
wait "${pid[$i]}"
if [[ $(wait "${pid[$i]}") -eq 0 ]]; then if [[ $(wait "${pid[$i]}") -eq 0 ]]; then
log "${p} (${ids[$p]}) saved to ${workspace}/${p}.${format}" log "${p} (${ids[$p]}) saved to ${workspace}/${p}.${format}"
else else
echo 1>&2 "ERROR: export of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "export of ${p} (${ids[$p]}) failed!"
fi fi
done done
echo echo
@ -540,10 +546,11 @@ if curl -fsS --get \
| "${jq}" -r '.projects | keys[] as $k | "\($k): \(.[$k] | .name)"' | "${jq}" -r '.projects | keys[] as $k | "\($k): \(.[$k] | .name)"'
then then
#log "printed list of projects" #log "printed list of projects"
echo :
else else
echo 1>&2 "ERROR: list projects failed!"; stop; exit 1 error "list projects failed!"
fi fi
echo
# ------------------------------- GET METADATA ------------------------------- # # ------------------------------- GET METADATA ------------------------------- #
@ -556,10 +563,11 @@ if curl -fsS --get \
| "${jq}" "{ id: ${ids[$p]} } + ." | "${jq}" "{ id: ${ids[$p]} } + ."
then then
#log "printed metadata of ${p} (${ids[$p]})" #log "printed metadata of ${p} (${ids[$p]})"
echo :
else else
echo 1>&2 "ERROR: getting metadata of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "getting metadata of ${p} (${ids[$p]}) failed!"
fi fi
echo
# ------------------------------ GET ROW COUNT ------------------------------- # # ------------------------------ GET ROW COUNT ------------------------------- #
@ -572,10 +580,11 @@ if curl -fsS --get \
| "${jq}" -r '.total' | "${jq}" -r '.total'
then then
#log "printed row count of ${p} (${ids[$p]})" #log "printed row count of ${p} (${ids[$p]})"
echo :
else else
echo 1>&2 "ERROR: getting rowcount of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "getting rowcount of ${p} (${ids[$p]}) failed!"
fi fi
echo
# ------------------------------- GET COLUMNS -------------------------------- # # ------------------------------- GET COLUMNS -------------------------------- #
@ -588,10 +597,11 @@ if curl -fsS --get \
| "${jq}" -r '.columnModel | .columns[] | .name' | "${jq}" -r '.columnModel | .columns[] | .name'
then then
#log "printed column names of ${p} (${ids[$p]})" #log "printed column names of ${p} (${ids[$p]})"
echo :
else else
echo 1>&2 "ERROR: getting columns of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "getting columns of ${p} (${ids[$p]}) failed!"
fi fi
echo
# -------------------------- GET OPERATIONS HISTORY -------------------------- # # -------------------------- GET OPERATIONS HISTORY -------------------------- #
@ -606,11 +616,10 @@ if curl -fsS --get \
> "${output}" > "${output}"
then then
log "ops history of ${p} (${ids[$p]}) saved to ${output}" log "ops history of ${p} (${ids[$p]}) saved to ${output}"
echo
else else
echo 1>&2 "ERROR: getting ops history of ${p} (${ids[$p]}) failed!" error "getting ops history of ${p} (${ids[$p]}) failed!"
stop; exit 1
fi fi
echo
# ---------------------------- GET IMPORT HISTORY ---------------------------- # # ---------------------------- GET IMPORT HISTORY ---------------------------- #
@ -623,11 +632,11 @@ if curl -fsS --get \
| "${jq}" ".importOptionMetadata[0]" | "${jq}" ".importOptionMetadata[0]"
then then
#log "printed import history of ${p} (${ids[$p]})" #log "printed import history of ${p} (${ids[$p]})"
echo :
else else
echo 1>&2 "ERROR: getting imp history of ${p} (${ids[$p]}) failed!" error "getting imp history of ${p} (${ids[$p]}) failed!"
stop; exit 1
fi fi
echo
# ---------------------------------- DELETE ---------------------------------- # # ---------------------------------- DELETE ---------------------------------- #
@ -639,10 +648,10 @@ if curl -fsS \
"${endpoint}/command/core/delete-project?csrf_token=$(csrf)" > /dev/null "${endpoint}/command/core/delete-project?csrf_token=$(csrf)" > /dev/null
then then
log "deleted ${p} (${ids[$p]})" log "deleted ${p} (${ids[$p]})"
echo
else else
echo 1>&2 "ERROR: deletion of ${p} (${ids[$p]}) failed!"; stop; exit 1 error "deletion of ${p} (${ids[$p]}) failed!"
fi fi
echo
# ------------------------------- STOP SERVER -------------------------------- # # ------------------------------- STOP SERVER -------------------------------- #