map operation names to commands
This commit is contained in:
parent
cf83aac692
commit
51c15ea0e0
35
orcli
35
orcli
|
@ -856,8 +856,7 @@ orcli_info_command() {
|
||||||
# :command.function
|
# :command.function
|
||||||
orcli_transform_command() {
|
orcli_transform_command() {
|
||||||
# src/transform_command.sh
|
# src/transform_command.sh
|
||||||
# shellcheck shell=bash disable=SC2154
|
# shellcheck shell=bash disable=SC2154 disable=SC2155
|
||||||
#get_id "${args[project]}"
|
|
||||||
|
|
||||||
# check if stdin is present if selected
|
# check if stdin is present if selected
|
||||||
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
|
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
|
||||||
|
@ -866,12 +865,15 @@ orcli_transform_command() {
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# catch args, convert the space delimited string to an array
|
# catch args, convert the space delimited string to an array
|
||||||
files=()
|
files=()
|
||||||
eval "files=(${args[file]})"
|
eval "files=(${args[file]})"
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
# download files if name starts with http:// or https://
|
# download files if name starts with http:// or https://
|
||||||
for i in "${!files[@]}"; do
|
for i in "${!files[@]}"; do
|
||||||
if [[ ${files[$i]} == "http://"* ]] || [[ ${files[$i]} == "https://"* ]]; then
|
if [[ ${files[$i]} == "http://"* ]] || [[ ${files[$i]} == "https://"* ]]; then
|
||||||
|
@ -882,16 +884,37 @@ orcli_transform_command() {
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# support multiple files and stdin
|
# support multiple files
|
||||||
readarray -t jsonlines < <(cat "${files[@]}" | jq --slurp --compact-output 'add | .[]')
|
for i in "${!files[@]}"; do
|
||||||
|
# read each operation into one line
|
||||||
|
mapfile -t jsonlines < <(jq -c '.[]' "${files[$i]}")
|
||||||
for line in "${jsonlines[@]}"; do
|
for line in "${jsonlines[@]}"; do
|
||||||
|
# parse operation into curl options
|
||||||
declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))"
|
declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))"
|
||||||
echo "${data[op]#core/}"
|
# map operation name to command
|
||||||
|
com="${data[op]#core/}"
|
||||||
|
if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi
|
||||||
unset "data[op]"
|
unset "data[op]"
|
||||||
|
# rename engineConfig to engine
|
||||||
|
data[engine]="${data[engineConfig]}"
|
||||||
|
unset "data[engineConfig]"
|
||||||
|
# drop description
|
||||||
unset "data[description]"
|
unset "data[description]"
|
||||||
for K in "${!data[@]}"; do echo "$K" --- "${data[$K]}"; done
|
mapfile -t curloptions < <(for K in "${!data[@]}"; do
|
||||||
|
echo "--data"
|
||||||
|
echo "$K=${data[$K]}"
|
||||||
|
done)
|
||||||
|
# get project id and csrf token; post data to it's individual endpoint
|
||||||
|
set -x
|
||||||
|
# debug: remove -fs
|
||||||
|
if ! curl --data "project=$(get_id "${args[project]}")" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)"; then
|
||||||
|
error "applying ${op} from ${files[$i]} failed!"
|
||||||
|
fi
|
||||||
|
set +x
|
||||||
unset data
|
unset data
|
||||||
done
|
done
|
||||||
|
done
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# :command.function
|
# :command.function
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# shellcheck shell=bash disable=SC2154
|
# shellcheck shell=bash disable=SC2154 disable=SC2155
|
||||||
|
|
||||||
# check if stdin is present if selected
|
# check if stdin is present if selected
|
||||||
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
|
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
|
||||||
|
@ -33,15 +33,22 @@ for i in "${!files[@]}"; do
|
||||||
for line in "${jsonlines[@]}"; do
|
for line in "${jsonlines[@]}"; do
|
||||||
# parse operation into curl options
|
# parse operation into curl options
|
||||||
declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))"
|
declare -A data="($(echo "$line" | jq -r 'to_entries | map("[\(.key)]=" + @sh "\(.value|tostring)") | .[]'))"
|
||||||
op="${data[op]#core/}"
|
# map operation name to command
|
||||||
|
com="${data[op]#core/}"
|
||||||
|
if [[ $com == "row-reorder" ]]; then com="reorder-rows"; fi
|
||||||
unset "data[op]"
|
unset "data[op]"
|
||||||
|
# rename engineConfig to engine
|
||||||
|
data[engine]="${data[engineConfig]}"
|
||||||
|
unset "data[engineConfig]"
|
||||||
|
# drop description
|
||||||
unset "data[description]"
|
unset "data[description]"
|
||||||
mapfile -t curloptions < <(for K in "${!data[@]}"; do
|
mapfile -t curloptions < <(for K in "${!data[@]}"; do
|
||||||
echo "--data-urlencode"
|
echo "--data"
|
||||||
echo "$K={data[$K]}"
|
echo "$K=${data[$K]}"
|
||||||
done)
|
done)
|
||||||
# get project id and csrf token; post data to it's individual endpoint
|
# get project id and csrf token; post data to it's individual endpoint
|
||||||
if ! curl -fs --data "project=$(get_id "${args[project]}")" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${op}$(get_csrf)"; then
|
# debug: remove -fs option temporarily
|
||||||
|
if ! curl --data "project=$(get_id "${args[project]}")" "${curloptions[@]}" "${OPENREFINE_URL}/command/core/${com}$(get_csrf)"; then
|
||||||
error "applying ${op} from ${files[$i]} failed!"
|
error "applying ${op} from ${files[$i]} failed!"
|
||||||
fi
|
fi
|
||||||
unset data
|
unset data
|
||||||
|
|
Loading…
Reference in New Issue