From a741adf035da85b9f58d63e910d959cccde24c47 Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Tue, 15 Nov 2022 23:06:41 +0000 Subject: [PATCH] check existence of files --- orcli | 40 ++++++++++++++++++++++++++++++++-------- src/lib/init_import.sh | 2 +- src/run_command.sh | 22 +++++++++++++++------- src/transform_command.sh | 16 ++++++++++++++++ 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/orcli b/orcli index 2440e18..96419b8 100755 --- a/orcli +++ b/orcli @@ -638,7 +638,7 @@ function init_import() { data+=("project-file=@-") else if ! path=$(readlink -e "${file}"); then - error "file ${file} not found!" + error "cannot open ${file} (no such file)!" fi data+=("project-file=@${path}") fi @@ -899,6 +899,22 @@ orcli_transform_command() { fi done + # check existence of files and stdin + for i in "${!files[@]}"; do + if [[ "${files[$i]}" == '-' ]] || [[ "${files[$i]}" == '"-"' ]]; then + # exit if stdin is selected but not present + if ! read -u 0 -t 0; then + orcli_transform_usage + exit 1 + fi + else + # exit if file does not exist + if ! [[ -f "${files[$i]}" ]]; then + error "cannot open ${files[$i]} (no such file)!" + fi + fi + done + # support multiple files for i in "${!files[@]}"; do # read each operation into one line @@ -1009,15 +1025,23 @@ orcli_run_command() { files=() eval "files=(${args[file]})" - # check if stdin is present if selected - if ! [[ ${args[--interactive]} ]]; then - if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then - if ! read -u 0 -t 0; then - orcli_run_usage - exit 1 + # check existence of files and stdin + for i in "${!files[@]}"; do + if [[ "${files[$i]}" == '-' ]] || [[ "${files[$i]}" == '"-"' ]]; then + # exit if stdin is selected but not present + if ! [[ ${args[--interactive]} ]]; then + if ! read -u 0 -t 0; then + orcli_run_usage + exit 1 + fi + fi + else + # exit if file does not exist + if ! [[ -f "${files[$i]}" ]]; then + error "cannot open ${files[$i]} (no such file)!" fi fi - fi + done # assume that quiet flag shall suppress log output generally in batch mode if [[ ${args[--quiet]} ]]; then diff --git a/src/lib/init_import.sh b/src/lib/init_import.sh index 3786213..976af81 100644 --- a/src/lib/init_import.sh +++ b/src/lib/init_import.sh @@ -40,7 +40,7 @@ function init_import() { data+=("project-file=@-") else if ! path=$(readlink -e "${file}"); then - error "file ${file} not found!" + error "cannot open ${file} (no such file)!" fi data+=("project-file=@${path}") fi diff --git a/src/run_command.sh b/src/run_command.sh index a008213..c2480fc 100644 --- a/src/run_command.sh +++ b/src/run_command.sh @@ -4,15 +4,23 @@ files=() eval "files=(${args[file]})" -# check if stdin is present if selected -if ! [[ ${args[--interactive]} ]]; then - if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then - if ! read -u 0 -t 0; then - orcli_run_usage - exit 1 +# check existence of files and stdin +for i in "${!files[@]}"; do + if [[ "${files[$i]}" == '-' ]] || [[ "${files[$i]}" == '"-"' ]]; then + # exit if stdin is selected but not present + if ! [[ ${args[--interactive]} ]]; then + if ! read -u 0 -t 0; then + orcli_run_usage + exit 1 + fi + fi + else + # exit if file does not exist + if ! [[ -f "${files[$i]}" ]]; then + error "cannot open ${files[$i]} (no such file)!" fi fi -fi +done # assume that quiet flag shall suppress log output generally in batch mode if [[ ${args[--quiet]} ]]; then diff --git a/src/transform_command.sh b/src/transform_command.sh index 8da451a..5c35f4a 100644 --- a/src/transform_command.sh +++ b/src/transform_command.sh @@ -26,6 +26,22 @@ for i in "${!files[@]}"; do fi done +# check existence of files and stdin +for i in "${!files[@]}"; do + if [[ "${files[$i]}" == '-' ]] || [[ "${files[$i]}" == '"-"' ]]; then + # exit if stdin is selected but not present + if ! read -u 0 -t 0; then + orcli_transform_usage + exit 1 + fi + else + # exit if file does not exist + if ! [[ -f "${files[$i]}" ]]; then + error "cannot open ${files[$i]} (no such file)!" + fi + fi +done + # support multiple files for i in "${!files[@]}"; do # read each operation into one line