Skip to content

Commit

Permalink
Fix srcenv option validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ins0mniaque committed Dec 17, 2024
1 parent e53e558 commit a1e62ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
33 changes: 17 additions & 16 deletions srcenv
Original file line number Diff line number Diff line change
Expand Up @@ -429,29 +429,29 @@ parse() {
}

run() {
arg=; prefix=; suffix=; format=; export=; backup=; restore=; clear=; fixpath=; verbose=
arg=; input=; action=; prefix=; suffix=; format=; export=; backup=; restore=; clear=; fixpath=; verbose=

setformat "$shell"

while [ $# -gt 0 ] && [ "$1" != -- ]; do
[ "$arg" = 0 ] && arg=
arg=1
case $1 in
--cmd) src=$(parse "$1" "$2") || exit $?; [ $# -gt 1 ] && shift; arg=0; shift ;;
--cmd=*) src=$(parse "${1%%=*}" "${1#*=}") || exit $?; arg=0; shift ;;
--cmd) src=$(parse "$1" "$2") || exit $?; [ $# -gt 1 ] && shift; arg=; input=; action=; shift ;;
--cmd=*) src=$(parse "${1%%=*}" "${1#*=}") || exit $?; arg=; input=; action=; shift ;;

-f|--format) value=$(parse "$1" "$2") || exit $?; [ $# -gt 1 ] && shift
setformat "$value" || fmt "${value%\?\?*}" "${value#*\?\?}"; shift ;;
-f=*|--format=*) value=$(parse "${1%%=*}" "${1#*=}") || exit $?
setformat "$value" || fmt "${value%\?\?*}" "${value#*\?\?}"; shift ;;

-b|--backup) backup=1; clear=; arg=1; shift ;;
-r|--restore) restore=1; shift ;;
-c|--clear) backup=; clear=1; shift ;;
-n) backup=; restore=; arg=1; shift ;;
--no-backup) backup=; arg=1; shift ;;
--no-restore) restore=; arg=1; shift ;;
-e|--export) export=--export; arg=1; shift ;;
-l|--local) export=0; arg=1; shift ;;
-b|--backup) backup=1; clear=; input=1; shift ;;
-r|--restore) restore=1; action=1; shift ;;
-c|--clear) backup=; clear=1; action=1; shift ;;
-n) backup=; restore=; shift ;;
--no-backup) backup=; shift ;;
--no-restore) restore=; shift ;;
-e|--export) export=--export; input=1; shift ;;
-l|--local) export=0; input=1; shift ;;
-s|--sort) keys='keys[] as $k';
sort='to_entries | sort_by(.key) | from_entries'; shift ;;
-u|--unsorted) keys='keys_unsorted[] as $k'; sort=.; shift ;;
Expand Down Expand Up @@ -486,10 +486,11 @@ run() {
done

if [ $# = 0 ]; then
case $arg in
0) header; usage; desc; help; man; exit 0 ;;
1) noinput; usage; help; exit 2 ;;
esac
if [ -z "$arg" ]; then
header; usage; desc; help; man; exit 0
elif [ -n "$input" ] || [ -z "$action" ]; then
noinput; usage; help; exit 2
fi
fi

if [ -z "$format" ]; then
Expand Down
16 changes: 11 additions & 5 deletions srcenv.tests
Original file line number Diff line number Diff line change
Expand Up @@ -687,17 +687,23 @@ run_posix_tests() {

run "$when_no_stdin" "$2 sh --backup" '' 2
run "$when_no_stdin" "$2 sh --export" '' 2
run "$when_no_stdin" "$2 sh --restore --export" '' 2
run "$when_no_stdin" "$2 sh --sort" '' 2
run "$1" "$2 sh --restore --sort" ''
run "$1" "$2 sh --restore" ''
run "$1" "$2 sh --clear" 'unset SRCENV_RESTORE'
run "$1" "SRCENV_RESTORE='' $2 sh --restore" '' 5
run "$1" "$2 --restore" '' 2
run "$when_no_stdin" "$2 sh --backup --restore" '' 2

run "$1" "$2 sh --backup --restore --cmd src" ''
run "$when_no_stdin" "$2 sh --backup --restore --cmd src --backup" '' 2
run "$when_no_stdin" "$2 sh --backup --restore --cmd src --export" '' 2
run "$1" "$2 sh --backup --restore --cmd src --restore" ''
run "$1" "$2 sh --backup --restore --cmd src --clear" 'unset SRCENV_RESTORE'
run "$1" "$2 sh --backup --restore --cmd src" ''
run "$when_no_stdin" "$2 sh --backup --restore --cmd src --backup" '' 2
run "$when_no_stdin" "$2 sh --backup --restore --cmd src --export" '' 2
run "$when_no_stdin" "$2 sh --backup --restore --cmd src --restore --export" '' 2
run "$when_no_stdin" "$2 sh --backup --restore --cmd src --sort" '' 2
run "$1" "$2 sh --backup --restore --cmd src --restore --sort" ''
run "$1" "$2 sh --backup --restore --cmd src --restore" ''
run "$1" "$2 sh --backup --restore --cmd src --clear" 'unset SRCENV_RESTORE'

run "$1" "$2 sh --input 'echo something'" ''
run "$1" "$2 sh --input 'echo something; not_a_command'" '' 127
Expand Down

0 comments on commit a1e62ae

Please # to comment.