diff --git a/srcenv b/srcenv index 3164e8a..83169fc 100755 --- a/srcenv +++ b/srcenv @@ -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 @@ -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|\ @@ -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 ;; @@ -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" diff --git a/srcenv.tests b/srcenv.tests index 38a68c9..21b9dfc 100755 --- a/srcenv.tests +++ b/srcenv.tests @@ -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