orcli/src/transform_command.sh

59 lines
2.1 KiB
Bash
Raw Normal View History

2022-11-07 22:03:20 +00:00
# shellcheck shell=bash disable=SC2154 disable=SC2155
2022-11-03 21:07:08 +00:00
# check if stdin is present if selected
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
orcli_transform_usage
exit 1
fi
fi
2022-11-06 21:39:40 +00:00
2022-11-03 21:07:08 +00:00
# catch args, convert the space delimited string to an array
files=()
eval "files=(${args[file]})"
2022-11-06 21:39:40 +00:00
2022-11-03 21:07:08 +00:00
# create tmp directory
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' 0 2 3 15
2022-11-06 21:39:40 +00:00
2022-11-03 21:07:08 +00:00
# download files if name starts with http:// or https://
for i in "${!files[@]}"; do
if [[ ${files[$i]} == "http://"* ]] || [[ ${files[$i]} == "https://"* ]]; then
if ! curl -fs --location "${files[$i]}" >"${tmpdir}/${files[$i]//[^A-Za-z0-9._-]/_}"; then
error "download of ${files[$i]} failed!"
fi
files[$i]="${tmpdir}/${files[$i]//[^A-Za-z0-9._-]/_}"
fi
done
2022-11-06 21:39:40 +00:00
# support multiple files
for i in "${!files[@]}"; do
# read each operation into one line
mapfile -t jsonlines < <(jq -c '.[]' "${files[$i]}")
for line in "${jsonlines[@]}"; do
2022-11-08 13:10:10 +00:00
# parse one line/operation into array
2022-11-06 21:39:40 +00:00
declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))"
2022-11-08 13:10:10 +00:00
# map operation names to command endpoints
2022-11-07 22:03:20 +00:00
com="${data[op]#core/}"
if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi
2022-11-06 21:39:40 +00:00
unset "data[op]"
2022-11-07 22:03:20 +00:00
# rename engineConfig to engine
data[engine]="${data[engineConfig]}"
unset "data[engineConfig]"
# drop description
2022-11-06 21:39:40 +00:00
unset "data[description]"
2022-11-08 13:10:10 +00:00
# prepare curl options
2022-11-06 21:39:40 +00:00
mapfile -t curloptions < <(for K in "${!data[@]}"; do
2022-11-07 22:03:20 +00:00
echo "--data"
echo "$K=${data[$K]}"
2022-11-06 21:39:40 +00:00
done)
2022-11-07 22:03:20 +00:00
# get project id and csrf token; post data to it's individual endpoint
2022-11-08 13:10:10 +00:00
if response="$(curl -fs --data "project=$(get_id "${args[project]}")" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)")"; then
log "applied ${com} to ${args[project]}" "Response: $(jq '.historyEntry.description' <<< "$response")"
else
error "applying ${com} from ${files[$i]} to ${args[project]} failed!"
2022-11-06 21:39:40 +00:00
fi
unset data
done
done