Skip to content

Commit

Permalink
rethink fs-* and sudo-helper escalations
Browse files Browse the repository at this point in the history
`--optional` and `--required` deprecated

`--sudo='' is now translated to `--sudo=no`

`--optional=13` is now `--sudo=13`
  • Loading branch information
balupton committed Jan 8, 2025
1 parent 1f6b37b commit bef3016
Show file tree
Hide file tree
Showing 33 changed files with 237 additions and 442 deletions.
30 changes: 14 additions & 16 deletions commands/echo-if-directory
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
function echo_if_directory_test() (
source "$DOROTHY/sources/bash.bash"
echo-style --h1="TEST: $0"
eval-tester --name='no args' --status=22 --ignore-stderr \

eval-tester --name='no args' \
-- echo-if-directory --

eval-tester --name='empty args' --status=22 \
eval-tester --name='empty args' \
-- echo-if-directory -- '' ''

eval-tester --name='missing' \
Expand All @@ -24,12 +25,6 @@ function echo_if_directory_test() (
eval-tester --name='dir then missing then file' --stdout="$DOROTHY" \
-- echo-if-directory -- "$DOROTHY" "$DOROTHY/this-does-not-exist" "$DOROTHY/README.md"

eval-tester --name='dir then file then invalid' --stdout="$DOROTHY" --status=22 \
-- echo-if-directory -- "$DOROTHY" "$DOROTHY/README.md" ''

eval-tester --name='dir then invalid then file' --stdout="$DOROTHY" --status=22 \
-- echo-if-directory -- "$DOROTHY" '' "$DOROTHY/README.md"

# test working symlinks
local dir_target dir_symlink file_target file_symlink
dir_target="$(fs-temp --directory='echo-if-directory' --directory='dir_target' --touch)"
Expand Down Expand Up @@ -71,11 +66,10 @@ function echo_if_directory() (
echo-lines ...<input> | echo-if-directory [...options]
OPTIONS:
--sudo
If specified, use sudo on filesystem interactions.
--sudo=<sudo>
--user=<user>
--group=<group>
If specified use this user and/or group for filesystem interactions.
Forwarded to [is-directory].
$(stdinargs_options_help --)
Expand All @@ -99,7 +93,7 @@ function echo_if_directory() (
}

# process
local item option_args=() option_sudo='no' option_user='' option_group=''
local item option_args=() option_sudo='' option_user='' option_group=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
Expand All @@ -123,13 +117,17 @@ function echo_if_directory() (
# =====================================
# Action

local inputs=()
local paths=()
function on_input {
inputs+=("$1")
local path="$1"
if is-directory --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$path"; then
paths+=("$path")
fi
}
function on_finish {
sudo-helper --inherit --optional --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- echo-if-directory.bash -- "${inputs[@]}"
return
if [[ ${#paths[@]} -ne 0 ]]; then
__print_lines "${paths[@]}"
fi
}

stdinargs "${option_args[@]}"
Expand Down
18 changes: 0 additions & 18 deletions commands/echo-if-directory.bash

This file was deleted.

30 changes: 14 additions & 16 deletions commands/echo-if-executable
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
function echo_if_executable_test() (
source "$DOROTHY/sources/bash.bash"
echo-style --h1="TEST: $0"
eval-tester --name='no args' --status=22 --ignore-stderr \

eval-tester --name='no args' \
-- echo-if-executable --

eval-tester --name='empty args' --status=22 \
eval-tester --name='empty args' \
-- echo-if-executable -- '' ''

eval-tester --name='missing' \
Expand All @@ -24,12 +25,6 @@ function echo_if_executable_test() (
eval-tester --name='dir then missing then file' --stdout="$DOROTHY"$'\n'"$DOROTHY/commands/dorothy" \
-- echo-if-executable -- "$DOROTHY" "$DOROTHY/this-does-not-exist" "$DOROTHY/commands/dorothy"

eval-tester --name='dir then file then invalid' --stdout="$DOROTHY"$'\n'"$DOROTHY/commands/dorothy" --status=22 \
-- echo-if-executable -- "$DOROTHY" "$DOROTHY/commands/dorothy" ''

eval-tester --name='dir then invalid then file' --stdout="$DOROTHY" --status=22 \
-- echo-if-executable -- "$DOROTHY" '' "$DOROTHY/commands/dorothy"

# test working symlinks
local dir_target dir_symlink file_target file_symlink
dir_target="$(fs-temp --directory='echo-if-executable' --directory='dir_target' --touch)"
Expand Down Expand Up @@ -74,11 +69,10 @@ function echo_if_executable() (
echo-lines ...<input> | echo-if-executable [...options]
OPTIONS:
--sudo
If specified, use sudo on filesystem interactions.
--sudo=<sudo>
--user=<user>
--group=<group>
If specified use this user and/or group for filesystem interactions.
Forwarded to [is-executable].
$(stdinargs_options_help --)
Expand All @@ -102,7 +96,7 @@ function echo_if_executable() (
}

# process
local item option_args=() option_sudo='no' option_user='' option_group=''
local item option_args=() option_sudo='' option_user='' option_group=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
Expand All @@ -126,13 +120,17 @@ function echo_if_executable() (
# =====================================
# Action

local inputs=()
local paths=()
function on_input {
inputs+=("$1")
local path="$1"
if is-executable --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$path"; then
paths+=("$path")
fi
}
function on_finish {
sudo-helper --inherit --optional --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- echo-if-executable.bash -- "${inputs[@]}"
return
if [[ ${#paths[@]} -ne 0 ]]; then
__print_lines "${paths[@]}"
fi
}

stdinargs "${option_args[@]}"
Expand Down
18 changes: 0 additions & 18 deletions commands/echo-if-executable.bash

This file was deleted.

31 changes: 14 additions & 17 deletions commands/echo-if-file
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
function echo_if_file_test() (
source "$DOROTHY/sources/bash.bash"
echo-style --h1="TEST: $0"
eval-tester --name='no args' --status=22 --ignore-stderr \

eval-tester --name='no args' \
-- echo-if-file --

eval-tester --name='empty args' --status=22 \
eval-tester --name='empty args' \
-- echo-if-file -- '' ''

eval-tester --name='missing' \
Expand All @@ -24,12 +25,6 @@ function echo_if_file_test() (
eval-tester --name='dir then missing then file' --stdout="$DOROTHY/README.md" \
-- echo-if-file -- "$DOROTHY" "$DOROTHY/this-does-not-exist" "$DOROTHY/README.md"

eval-tester --name='dir then file then invalid' --stdout="$DOROTHY/README.md" --status=22 \
-- echo-if-file -- "$DOROTHY" "$DOROTHY/README.md" ''

eval-tester --name='dir then invalid then file' --status=22 \
-- echo-if-file -- "$DOROTHY" '' "$DOROTHY/README.md"

# test working symlinks
local dir_target dir_symlink file_target file_symlink
dir_target="$(fs-temp --directory='echo-if-file' --directory='dir_target' --touch)"
Expand Down Expand Up @@ -71,11 +66,10 @@ function echo_if_file() (
echo-lines ...<input> | echo-if-file [...options]
OPTIONS:
--sudo
If specified, use sudo on filesystem interactions.
--sudo=<sudo>
--user=<user>
--group=<group>
If specified use this user and/or group for filesystem interactions.
Forwarded to [is-file].
$(stdinargs_options_help --)
Expand All @@ -99,7 +93,7 @@ function echo_if_file() (
}

# process
local item option_args=() option_sudo='no' option_user='' option_group=''
local item option_args=() option_sudo='' option_user='' option_group=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
Expand All @@ -123,15 +117,18 @@ function echo_if_file() (
# =====================================
# Action

local inputs=()
local paths=()
function on_input {
inputs+=("$1")
local path="$1"
if is-file --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$path"; then
paths+=("$path")
fi
}
function on_finish {
sudo-helper --inherit --optional --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- echo-if-file.bash -- "${inputs[@]}"
return
if [[ ${#paths[@]} -ne 0 ]]; then
__print_lines "${paths[@]}"
fi
}

stdinargs "${option_args[@]}"
)

Expand Down
18 changes: 0 additions & 18 deletions commands/echo-if-file.bash

This file was deleted.

30 changes: 14 additions & 16 deletions commands/echo-if-present
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
function echo_if_present_test() (
source "$DOROTHY/sources/bash.bash"
echo-style --h1="TEST: $0"
eval-tester --name='no args' --status=22 --ignore-stderr \

eval-tester --name='no args' \
-- echo-if-present --

eval-tester --name='empty args' --status=22 \
eval-tester --name='empty args' \
-- echo-if-present -- '' ''

eval-tester --name='missing' \
Expand All @@ -24,12 +25,6 @@ function echo_if_present_test() (
eval-tester --name='dir then missing then file' --stdout="$DOROTHY"$'\n'"$DOROTHY/README.md" \
-- echo-if-present -- "$DOROTHY" "$DOROTHY/this-does-not-exist" "$DOROTHY/README.md"

eval-tester --name='dir then file then invalid' --stdout="$DOROTHY"$'\n'"$DOROTHY/README.md" --status=22 \
-- echo-if-present -- "$DOROTHY" "$DOROTHY/README.md" ''

eval-tester --name='dir then invalid then file' --stdout="$DOROTHY" --status=22 \
-- echo-if-present -- "$DOROTHY" '' "$DOROTHY/README.md"

# test working symlinks
local dir_target dir_symlink file_target file_symlink
dir_target="$(fs-temp --directory='echo-if-present' --directory='dir_target' --touch)"
Expand Down Expand Up @@ -71,11 +66,10 @@ function echo_if_present() (
echo-lines ...<input> | echo-if-present [...options]
OPTIONS:
--sudo
If specified, use sudo on filesystem interactions.
--sudo=<sudo>
--user=<user>
--group=<group>
If specified use this user and/or group for filesystem interactions.
Forwarded to [is-present].
$(stdinargs_options_help --)
Expand All @@ -99,7 +93,7 @@ function echo_if_present() (
}

# process
local item option_args=() option_sudo='no' option_user='' option_group=''
local item option_args=() option_sudo='' option_user='' option_group=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
Expand All @@ -123,13 +117,17 @@ function echo_if_present() (
# =====================================
# Action

local inputs=()
local paths=()
function on_input {
inputs+=("$1")
local path="$1"
if is-present --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$path"; then
paths+=("$path")
fi
}
function on_finish {
sudo-helper --inherit --optional --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- echo-if-present.bash -- "${inputs[@]}"
return
if [[ ${#paths[@]} -ne 0 ]]; then
__print_lines "${paths[@]}"
fi
}

stdinargs "${option_args[@]}"
Expand Down
19 changes: 0 additions & 19 deletions commands/echo-if-present.bash

This file was deleted.

Loading

0 comments on commit bef3016

Please sign in to comment.