Merge pull request #118 from opencultureconsulting:116-export-tsv-select

export tsv --select
This commit is contained in:
Felix Lohmeier 2023-11-09 15:53:20 +01:00 committed by GitHub
commit 82a26c570c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 2 deletions

View File

@ -8,6 +8,10 @@ Usage:
orcli export tsv --help | -h
Options:
--select COLUMNS
filter result set to one or more columns (comma separated)
example: --select "foo,bar,baz"
--mode MODE
specify if project contains multi-row records
Allowed: rows, records
@ -38,6 +42,7 @@ Examples:
orcli export tsv "duplicates"
orcli export tsv "duplicates" --output "duplicates.tsv"
orcli export tsv "duplicates" --encoding "ISO-8859-1"
orcli export tsv "duplicates" --select "name,email,purchase"
orcli export tsv "duplicates" --facets '[ { "type": "text", "columnName":
"name", "mode": "regex", "caseSensitive": false, "invert": false, "query":
"^Ben" } ]'

38
orcli
View File

@ -1056,6 +1056,11 @@ orcli_export_tsv_usage() {
printf "%s\n" "Options:"
# :command.usage_flags
# :flag.usage
printf " %s\n" "--select COLUMNS"
printf " filter result set to one or more columns (comma separated)\n example: --select \"foo,bar,baz\"\n"
echo
# :flag.usage
printf " %s\n" "--mode MODE"
printf " specify if project contains multi-row records\n"
@ -1103,6 +1108,7 @@ orcli_export_tsv_usage() {
printf " orcli export tsv \"duplicates\"\n"
printf " orcli export tsv \"duplicates\" --output \"duplicates.tsv\"\n"
printf " orcli export tsv \"duplicates\" --encoding \"ISO-8859-1\"\n"
printf " orcli export tsv \"duplicates\" --select \"name,email,purchase\"\n"
printf " orcli export tsv \"duplicates\" --facets '[ { \"type\": \"text\", \"columnName\":\n \"name\", \"mode\": \"regex\", \"caseSensitive\": false, \"invert\": false, \"query\":\n \"^Ben\" } ]'\n"
printf " orcli export tsv \"duplicates\" --facets '[{ \"type\": \"list\", \"expression\":\n \"grel:filter([\\\\\"gender\\\\\",\\\\\"purchase\\\\\"],cn,cells[cn].value.find(/^F/).length()>0).length()>0\",\n \"columnName\": \"\", \"selection\": [{\"v\": {\"v\": true}}] }]'\n"
echo
@ -1617,7 +1623,7 @@ send_completions() {
echo $' ;;'
echo $''
echo $' \'export tsv\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --facets --help --mode --output --quiet -h -q")" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --facets --help --mode --output --quiet --select -h -q")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'transform\'*)'
@ -2367,6 +2373,21 @@ orcli_export_tsv_command() {
options+=', '
options+="\"encoding\": \"${args[--encoding]}\""
fi
if [[ ${args[--select]} ]]; then
options+=', '
options+='"columns": ['
IFS=',' read -ra columns <<< "${args[--select]}"
options+='{"name":"'
options+="${columns[0]}"
options+='"}'
for cn in "${columns[@]:1}"; do
options+=', '
options+='{"name":"'
options+="${cn}"
options+='"}'
done
options+="]"
fi
options+=' }'
data+=("options=${options}")
@ -4316,6 +4337,21 @@ orcli_export_tsv_parse_requirements() {
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--select)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
args['--select']="$2"
shift
shift
else
printf "%s\n" "--select requires an argument: --select COLUMNS" >&2
exit 1
fi
;;
# :flag.case
--mode)

View File

@ -360,6 +360,11 @@ commands:
args:
- *project
flags:
- long: --select
help: |-
filter result set to one or more columns (comma separated)
example: --select "foo,bar,baz"
arg: columns
- *mode
- *facets
- *output
@ -369,6 +374,7 @@ commands:
- orcli export tsv "duplicates"
- orcli export tsv "duplicates" --output "duplicates.tsv"
- orcli export tsv "duplicates" --encoding "ISO-8859-1"
- orcli export tsv "duplicates" --select "name,email,purchase"
- |-
orcli export tsv "duplicates" --facets '[ { "type": "text", "columnName": "name", "mode": "regex", "caseSensitive": false, "invert": false, "query": "^Ben" } ]'
- |-

View File

@ -11,6 +11,21 @@ if [[ ${args[--encoding]} ]]; then
options+=', '
options+="\"encoding\": \"${args[--encoding]}\""
fi
if [[ ${args[--select]} ]]; then
options+=', '
options+='"columns": ['
IFS=',' read -ra columns <<< "${args[--select]}"
options+='{"name":"'
options+="${columns[0]}"
options+='"}'
for cn in "${columns[@]:1}"; do
options+=', '
options+='{"name":"'
options+="${cn}"
options+='"}'
done
options+="]"
fi
options+=' }'
data+=("options=${options}")

View File

@ -71,7 +71,7 @@ send_completions() {
echo $' ;;'
echo $''
echo $' \'export tsv\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --facets --help --mode --output --quiet -h -q")" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --facets --help --mode --output --quiet --select -h -q")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'transform\'*)'

View File

@ -0,0 +1,35 @@
#!/bin/bash
t="export-tsv-select"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# input
cp data/duplicates.csv "${tmpdir}/${t}.csv"
# assertion
cat << "DATA" > "${tmpdir}/${t}.assert"
name email purchase
Danny Baron danny.baron@example1.com TV
Melanie White melanie.white@example2.edu iPhone
D. Baron danny.baron@example1.com Winter jacket
Ben Tyler ben.tyler@example3.org Flashlight
Arthur Duff arthur.duff@example4.com Dining table
Daniel Baron danny.baron@example1.com Bike
Jean Griffith jean.griffith@example5.org Power drill
Melanie White melanie.white@example2.edu iPad
Ben Morisson ben.morisson@example6.org Amplifier
Arthur Duff arthur.duff@example4.com Night table
DATA
# action
cd "${tmpdir}" || exit 1
orcli import csv "${t}.csv" --projectName "${t}"
orcli export tsv "${t}" \
--output "${t}.output" \
--select "name,email,purchase"
# test
diff -u "${t}.assert" "${t}.output"