From e97c4b250b2ceb6ccc80fa27972364d2b26db115 Mon Sep 17 00:00:00 2001 From: ADoyle Date: Fri, 2 Dec 2022 17:11:24 +0800 Subject: [PATCH] feat: add "one sub which" --- one-cmds/sub | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/one-cmds/sub b/one-cmds/sub index f607da9..cb162de 100755 --- a/one-cmds/sub +++ b/one-cmds/sub @@ -11,27 +11,22 @@ if (( $# == 0 )) || [[ $1 == -h ]] || [[ $1 == --help ]]; then cmd=$(basename "$0") cat <] - one $cmd list [-f] - -Desc: List all ONE_SUB commands +Desc: Manage ONE_SUB commands ACTION: l, list List executable files in each REPO/sub + which Show filepath of cmd +--- +Usage: one $cmd list [-f] +Desc: List executable files in each REPO/sub Options: -f Show filepath of each executable file -EOF - exit 0 -fi -# one.bash:completion -if [[ "${1:-}" == --complete ]]; then - if (( COMP_CWORD < 3 )); then - words=(list --help) - printf '%s\n' "${words[@]}" - elif [[ $2 =~ l|list ]] && (( COMP_CWORD < 4 )); then - echo '-f' - fi +--- +Usage: one $cmd which +Desc: Show filepath of +EOF exit 0 fi @@ -61,6 +56,33 @@ list_subs() { done | uniq } +# one.bash:completion +if [[ "${1:-}" == --complete ]]; then + if (( COMP_CWORD < 3 )); then + words=(list which --help) + printf '%s\n' "${words[@]}" + elif [[ $2 =~ l|list ]] && (( COMP_CWORD < 4 )); then + echo '-f' + elif [[ $2 =~ which ]] && (( COMP_CWORD < 4 )); then + # shellcheck source=../../bash/load-config.bash + . "$ONE_DIR/bash/load-config.bash" + list_subs + fi + exit 0 +fi + +which_sub() { + local name=$1 + local path + + for ONE_REPO in "${ONE_REPOS[@]}"; do + if [[ -x $ONE_REPO/sub/$name ]]; then + echo "$ONE_REPO/sub/$name" + return 0 + fi + done +} + action=$1 shift @@ -70,6 +92,8 @@ shift case "$action" in l|list) list_subs "$@" ;; + which) which_sub "$@" ;; + *) echo "Invalid action '$action'" >&2 exit 1