Skip to content

Commit

Permalink
promote eval-on-empty-stdin, eval-on-not-empty-stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 29, 2024
1 parent b624ee8 commit 2e4f695
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 28 deletions.
14 changes: 0 additions & 14 deletions commands.beta/eval-on-empty-stdin

This file was deleted.

14 changes: 0 additions & 14 deletions commands.beta/eval-on-not-empty-stdin

This file was deleted.

71 changes: 71 additions & 0 deletions commands/eval-on-empty-stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

function eval_on_empty_stdin() (
source "$DOROTHY/sources/bash.bash"

# =====================================
# Arguments

function help {
cat <<-EOF >/dev/stderr
ABOUT:
Execute the <command> if stdin is empty; stdin content will continue to be directed to stdout.
Companion to [eval-on-not-empty-stdin].
USAGE:
eval-on-empty-stdin [--] ...<command>
EOF
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

# process
local item option_cmd=()
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--')
option_cmd+=("$@")
shift $#
break
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*)
option_cmd+=("$item" "$@")
shift $#
break
;;
esac
done

# =====================================
# Action

# doesn't work:
# if read -r; then
# __print_lines 'not empty'
# else
# __print_lines 'empty'
# fi

# doesn't work:
# if [[ -t 0 ]]; then
# __print_lines 'empty'
# else
# __print_lines 'not empty'
# fi

# works:
setup-util-moreutils --quiet # ifne
ifne -n "${option_cmd[@]}"
return
)

# fire if invoked standalone
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
eval_on_empty_stdin "$@"
fi
55 changes: 55 additions & 0 deletions commands/eval-on-not-empty-stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

function eval_on_not_empty_stdin() (
source "$DOROTHY/sources/bash.bash"

# =====================================
# Arguments

function help {
cat <<-EOF >/dev/stderr
ABOUT:
Execute the <command> if stdin is not empty; the stdin content will still be available to whatever eventually reads it.
Companion to [eval-on-empty-stdin].
USAGE:
eval-on-not-empty-stdin [--] ...<command>
EOF
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

# process
local item option_cmd=()
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--')
option_cmd+=("$@")
shift $#
break
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*)
option_cmd+=("$item" "$@")
shift $#
break
;;
esac
done

# =====================================
# Action

setup-util-moreutils --quiet # ifne
ifne "${option_cmd[@]}"
)

# fire if invoked standalone
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
eval_on_not_empty_stdin "$@"
fi

0 comments on commit 2e4f695

Please sign in to comment.