Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor feature: Add -a/--all option to brl which to list all strata that provide a given binary #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 107 additions & 25 deletions src/slash-bedrock/libexec/brl-which
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options:
${color_cmd}-f${color_norm}, ${color_cmd}--file ${color_norm}which ${color_term}stratum${color_norm} provides a given file path
${color_cmd}-p${color_norm}, ${color_cmd}--pid ${color_norm}which ${color_term}stratum${color_norm} provides a given process ID
${color_cmd}-x${color_norm}, ${color_cmd}--xwindow ${color_norm}which ${color_term}stratum${color_norm} provides a given X11 window (requires xprop)
${color_cmd}-a${color_norm}, ${color_cmd}--all ${color_norm}search in all ${color_term}strata${color_norm} (files and binaries only)
${color_cmd}-h${color_norm}, ${color_cmd}--help ${color_norm}print this message

Guessing reasoning for ${color_sub}<blank>${color_norm} based on identifier:
Expand Down Expand Up @@ -67,6 +68,19 @@ which_bin() {
done
}

which_bin_all() (
min_args "${#}" "1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't test this before pushing, and it appears min_args doesn't work reliably from inside functions. Currently, this happens, and it's not immediately obvious to me how to fix it:

$ brl which -ab
ERROR: Insufficient arguments, see `--help`.
ERROR: Unexpected error occurred.


while [ -n "${1:-}" ]; do
Copy link
Contributor Author

@cptpcrd cptpcrd Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normal brl which interface doesn't really work properly with -a. For example, this is very ambiguous:

$ brl which -a cmake git
void
void-musl
arch

Some of the formats that leapt to mind include:

$ brl which -a cmake git
void: cmake
void-musl: cmake
arch: git

$ brl which -a cmake git
cmake: void
cmake: void-musl
git: arch

$ brl which -a cmake git
cmake: void void-musl
git: arch

$ brl which -a cmake git
void: cmake git
void-musl: cmake
arch: git

I'm not really sure which is best.

Copy link

@jake-87 jake-87 Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally

$ brl which -a cmake git
void: cmake git
void-musl: cmake
arch: git 

makes the most sense to me, as this quickly shows what has what, while still having a per distro view.

for stratum in $(list_strata); do
if path="$(strat -r "${stratum}" /bedrock/libexec/busybox which "$1" 2>/dev/null)"; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add handling for "file/binary not found" both here and in which_file_all, but what should be the behavior? Something like this?

$ brl which -a git 0ad cmake
arch: git
WARNING: 0ad not found in any strata
void: cmake
# exit with status 1

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes much sense to phrase it as a warning, as it's really just the absence of something. Perhaps something closer to just 0ad not found in any strata?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would probably depend on the exact format; see above. e.g. for some it could be 0ad not found; for others 0ad: not found might make more sense. Colorization might help too.

which_file "/bedrock/strata/${stratum}/${path}"
fi
done
shift
done
)

which_file() {
while [ -n "${1:-}" ]; do
if ! [ -e "${1}" ]; then
Expand All @@ -90,6 +104,19 @@ which_file() {
done
}

which_file_all() (
min_args "${#}" "1"

while [ -n "${1:-}" ]; do
for stratum in $(list_strata); do
if [ -e "/bedrock/strata/${stratum}/$1" ]; then
which_file "/bedrock/strata/${stratum}/$1"
fi
done
shift
done
)

which_pid() {
while [ -n "${1:-}" ]; do
# Pick a mount point visible to the process.
Expand Down Expand Up @@ -135,30 +162,12 @@ which_xwindow() {
fi
}

handle_help "${@:-}"
which_auto() {
if [ "${#}" -eq 0 ]; then
which_current
return
fi

case "${1:-}" in
"-c" | "--current" | "")
[ -n "${1:-}" ] && shift
which_current "${@}"
;;
"-b" | "--bin")
shift
which_bin "${@}"
;;
"-f" | "--file")
shift
which_file "${@}"
;;
"-p" | "--pid")
shift
which_pid "${@}"
;;
"-x" | "--xwindow")
shift
which_xwindow
;;
*)
while [ -n "${1:-}" ]; do
if echo "${1}" | grep -q '^[0-9][0-9]*$'; then
which_pid "${1}"
Expand All @@ -169,7 +178,80 @@ case "${1:-}" in
fi
shift
done
;;
esac
}

which_auto_all() {
min_args "${#}" "1"

while [ -n "${1:-}" ]; do
if echo "${1}" | grep -q '/'; then
which_file_all "${1}"
else
which_bin_all "${1}"
fi
shift
done
}

handle_help "${@:-}"

OPTL="all,bin,file,current,pid,xwindow"
OPTO="abcfpx"
eval set -- "$(getopt -q -l "${OPTL}" -- "${OPTO}" "${@}")" || true

list_all=false
list_type=auto

while [ -n "${1:-}" ]; do
case "${1:-}" in
"-c" | "--current")
shift
list_type=current
;;
"-b" | "--bin")
shift
list_type=bin
;;
"-a" | "--all")
shift
list_all=true
;;
"-f" | "--file")
shift
list_type=file
;;
"-p" | "--pid")
shift
list_type=pid
;;
"-x" | "--xwindow")
shift
list_type=xwindow
;;
--)
shift
break
;;
-*)
abort "Unrecognized argument: ${1}"
;;
*)
break
;;
esac
done

if [ "${list_all}" = true ]; then
case "${list_type}" in
auto|file|bin)
list_type="${list_type}_all"
;;
*)
abort "-a/--all can only be used with -f/--file or -b/--bin"
;;
esac
fi

which_${list_type} "$@"

exit_success
1 change: 1 addition & 0 deletions src/slash-bedrock/share/bash/completion/brl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ _brl() {
opts="${opts} $(_brl_unused_all "-f --file" "${COMP_WORDS[@]}")"
opts="${opts} $(_brl_unused_all "-p --pid" "${COMP_WORDS[@]}")"
opts="${opts} $(_brl_unused_all "-x --xwindow" "${COMP_WORDS[@]}")"
opts="${opts} $(_brl_unused_all "-a --all" "${COMP_WORDS[@]}")"
fi
case "${COMP_WORDS[2]}" in
"-c" | "--current") ;;
Expand Down
2 changes: 2 additions & 0 deletions src/slash-bedrock/share/fish/completion/brl.fish
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -o 'p' -d 'which
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -l 'pid' -d 'which stratum provides a given process ID'
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -o 'x' -d 'which stratum provides a given process ID'
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -l 'xwindow' -d 'which stratum provides a given X11 window'
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -o 'a' -d 'search in all strata (files and binaries only)'
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -l 'all' -d 'search in all strata (files and binaries only)'
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_arg_is 3 -b' -a '(_brl_which_bin)' -d 'binary'
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_arg_is 3 --bin' -a '(_brl_which_bin)' -d 'binary'
complete -f -c brl -n '_brl_arg_is 2 which; and _brl_arg_is 3 -f' -a '(__fish_complete_path)' -d 'file'
Expand Down
1 change: 1 addition & 0 deletions src/slash-bedrock/share/zsh/completion/_brl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ case "${words[2]}" in
args+=('(-f --file)'{-f,--file}'[which stratum provides a given file path]')
args+=('(-p --pid)'{-p,--pid}'[which stratum provides a given process ID]')
args+=('(-x --xwindow)'{-x,--xwindow}'[which stratum provides a given X11 window]')
args+=('(-a --all)'{-a,--all}'[search in all strata (files and binaries only)]')
fi
case "${words[3]}" in
"-c"|"--current")
Expand Down