Skip to content

Commit

Permalink
Fix srcenv not failing when no input is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ins0mniaque committed Dec 2, 2024
1 parent 6eda022 commit e0b333c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
30 changes: 20 additions & 10 deletions srcenv
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ ${BOLD}${UNDERLINE}Options${NORMAL}:
"
}

noinput() {
colors NORMAL BOLD RED

echo >&2 "${BOLD}${RED}error:${NORMAL} no input files or arguments"
}

nojq() {
colors NORMAL BOLD RED GREEN YELLOW

Expand Down Expand Up @@ -236,12 +242,6 @@ parse() {
}

run() {
if ! command -v "$jq" > /dev/null; then
nojq; exit 1
fi

snapshot=$("$jq" -n env) || exit 1

while [ $# -gt 0 ] && [ "$1" != -- ]; do
case $1 in
--ash|\
Expand Down Expand Up @@ -296,6 +296,20 @@ run() {
esac
done

if [ $# = 0 ] && [ -z "$undo" ]; then
noinput; exit 2
fi

[ -n "$backup" ] && setbackup
[ -z "$format" ] && noformat && fmt 'export \($k)=\(.[$k]|@sh)' 'unset \($k)'
[ "$format" = . ] && keys=$sort

if ! command -v "$jq" > /dev/null; then
nojq; exit 1
fi

snapshot=$("$jq" -n env) || exit 1

while [ $# -gt 0 ]; do
case $1 in
--) shift ;;
Expand All @@ -306,10 +320,6 @@ run() {
esac
done

[ -n "$backup" ] && setbackup
[ -z "$format" ] && noformat && fmt 'export \($k)=\(.[$k]|@sh)' 'unset \($k)'
[ "$format" = . ] && keys=$sort

"$jq" --argjson snapshot "$snapshot" \
${undo:+--argjson} ${undo:+undo} ${undo:+"${SRCENV_UNDO:-{}}"} \
-rn "$prefix $undo $diff $backup | $keys | $format $suffix"
Expand Down
2 changes: 2 additions & 0 deletions srcenv.tests
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ run './srcenv --version' 'srcenv 1.4.3'
run './srcenv --invalid' '' 2
run './srcenv not_a_file' '' 1
run './srcenv --arg not_a_command' '' 1
run './srcenv --export' '' 2
run './srcenv --undo' ''

run './srcenv --arg "echo something"' ''
run './srcenv --arg "echo something; not_a_command"' '' 1
Expand Down

0 comments on commit e0b333c

Please sign in to comment.