diff --git a/orcli b/orcli index 69b9116..24c97a7 100755 --- a/orcli +++ b/orcli @@ -144,6 +144,11 @@ orcli_delete_usage() { echo # :command.usage_flags + # :flag.usage + echo " --force, -f" + printf " delete all projects with the same name\n" + echo + # :flag.usage echo " --quiet, -q" printf " suppress log output, print errors only\n" @@ -728,22 +733,22 @@ function get_csrf() { # get project id (derived from project name if needed) # shellcheck shell=bash function get_id() { - local response projects ids + local response projects projectid if ! response="$(curl -fs --get "${OPENREFINE_URL}/command/core/get-all-project-metadata")"; then error "no OpenRefine reachable/running at ${OPENREFINE_URL}" fi if ! projects="$(echo "$response" | jq -r '.projects | keys[] as $k | "\($k):\(.[$k] | .name)"' | grep -e ":$1$" -e "^$1:")"; then error "project $1 not found" fi - ids=$(echo "$projects" | cut -d : -f 1) - if ! [[ "${#ids}" == 13 ]]; then + projectid=$(echo "$projects" | cut -d : -f 1) + if ! [[ "${#projectid}" == 13 ]]; then error "multiple projects found" "$projects" fi - echo "$ids" + echo "$projectid" } function get_ids() { - local response projects ids + local response projects if ! response="$(curl -fs --get "${OPENREFINE_URL}/command/core/get-all-project-metadata")"; then error "no OpenRefine reachable/running at ${OPENREFINE_URL}" fi @@ -954,7 +959,7 @@ send_completions() { echo $' ;;' echo $'' echo $' \'delete\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet -h -q")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--force --help --quiet -f -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'import\'*)' @@ -1008,10 +1013,14 @@ orcli_delete_command() { # src/delete_command.sh # shellcheck shell=bash disable=SC2154 - # get project ids - projectids="$(get_ids "${args[project]}")" + # get project id(s) + if [[ ${args[--force]} ]]; then + projectids="$(get_ids "${args[project]}")" + else + projectids="$(get_id "${args[project]}")" + fi - # loop over multiple project ids + # loop over one or more project ids for projectid in ${projectids}; do # get csrf token and post data if response="$(curl -fs --data "project=${projectid}" "${OPENREFINE_URL}/command/core/delete-project$(get_csrf)")"; then @@ -1679,6 +1688,14 @@ orcli_delete_parse_requirements() { while [[ $# -gt 0 ]]; do key="$1" case "$key" in + # :flag.case + --force | -f) + + # :flag.case_no_arg + args[--force]=1 + shift + ;; + # :flag.case --quiet | -q) diff --git a/src/bashly.yml b/src/bashly.yml index 3ce01dc..0cadfb8 100644 --- a/src/bashly.yml +++ b/src/bashly.yml @@ -40,6 +40,9 @@ commands: help: project name or id required: true flags: + - long: --force + short: -f + help: delete all projects with the same name - long: --quiet short: -q help: suppress log output, print errors only diff --git a/src/delete_command.sh b/src/delete_command.sh index 3f57b52..4a5c1f8 100644 --- a/src/delete_command.sh +++ b/src/delete_command.sh @@ -1,9 +1,13 @@ # shellcheck shell=bash disable=SC2154 -# get project ids -projectids="$(get_ids "${args[project]}")" +# get project id(s) +if [[ ${args[--force]} ]]; then + projectids="$(get_ids "${args[project]}")" +else + projectids="$(get_id "${args[project]}")" +fi -# loop over multiple project ids +# loop over one or more project ids for projectid in ${projectids}; do # get csrf token and post data if response="$(curl -fs --data "project=${projectid}" "${OPENREFINE_URL}/command/core/delete-project$(get_csrf)")"; then diff --git a/src/lib/get_id.sh b/src/lib/get_id.sh index 72d3ba1..cc09e04 100644 --- a/src/lib/get_id.sh +++ b/src/lib/get_id.sh @@ -1,22 +1,22 @@ # get project id (derived from project name if needed) # shellcheck shell=bash function get_id() { - local response projects ids + local response projects projectid if ! response="$(curl -fs --get "${OPENREFINE_URL}/command/core/get-all-project-metadata")"; then error "no OpenRefine reachable/running at ${OPENREFINE_URL}" fi if ! projects="$(echo "$response" | jq -r '.projects | keys[] as $k | "\($k):\(.[$k] | .name)"' | grep -e ":$1$" -e "^$1:")"; then error "project $1 not found" fi - ids=$(echo "$projects" | cut -d : -f 1) - if ! [[ "${#ids}" == 13 ]]; then + projectid=$(echo "$projects" | cut -d : -f 1) + if ! [[ "${#projectid}" == 13 ]]; then error "multiple projects found" "$projects" fi - echo "$ids" + echo "$projectid" } function get_ids() { - local response projects ids + local response projects if ! response="$(curl -fs --get "${OPENREFINE_URL}/command/core/get-all-project-metadata")"; then error "no OpenRefine reachable/running at ${OPENREFINE_URL}" fi diff --git a/src/lib/send_completions.sh b/src/lib/send_completions.sh index 6ab5a7e..23b26fd 100644 --- a/src/lib/send_completions.sh +++ b/src/lib/send_completions.sh @@ -51,7 +51,7 @@ send_completions() { echo $' ;;' echo $'' echo $' \'delete\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet -h -q")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--force --help --quiet -f -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'import\'*)' diff --git a/tests/delete.sh b/tests/delete.sh index a636ecd..226f0be 100644 --- a/tests/delete.sh +++ b/tests/delete.sh @@ -19,6 +19,11 @@ orcli import csv "${t}.csv" orcli list | grep "${t} csv" orcli delete "${t} csv" orcli list | grep "${t} csv" > "${t}.output" || exit 0 +orcli import csv "${t}.csv" +orcli import csv "${t}.csv" +orcli list | grep "${t} csv" +orcli delete --force "${t} csv" +orcli list | grep "${t} csv" >> "${t}.output" || exit 0 # test diff -u "${t}.assert" "${t}.output"