Skip to content

Commit

Permalink
fix: "one sub" should be enabled/disabled by users
Browse files Browse the repository at this point in the history
  • Loading branch information
adoyle-h committed Oct 4, 2023
1 parent 6c902ef commit d94d058
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 127 deletions.
1 change: 1 addition & 0 deletions bash/one-complete.bash
Expand Up @@ -92,6 +92,7 @@ _comp_one() {
b) cmd=bin;;
c) cmd=completion;;
p) cmd=plugin;;
s) cmd=sub;;
esac

if [[ -d "$ONE_DIR/one-cmds/$cmd" ]]; then
Expand Down
3 changes: 2 additions & 1 deletion bin/one
Expand Up @@ -119,7 +119,7 @@ one_sub_cmd() {
shift
exec "$cmd_path" "$@"
else
exec "$ONE_DIR/one-cmds/sub" list
exec one sub list
fi
}

Expand All @@ -132,6 +132,7 @@ one_cmd() {
b) cmd=bin;;
c) cmd=completion;;
p) cmd=plugin;;
s) cmd=sub;;
esac

if [[ -x $ONE_DIR/one-cmds/$cmd/main ]]; then
Expand Down
126 changes: 0 additions & 126 deletions one-cmds/sub

This file was deleted.

45 changes: 45 additions & 0 deletions one-cmds/sub/list.bash
@@ -0,0 +1,45 @@
usage_list() {
cat <<EOF
Usage: one sub list
Desc: List executable files in each REPO/sub
EOF
}

complete_list() {
echo '-f'
}

list() {
local path repo

# shellcheck disable=2153
for path in "${ONE_DIR}"/enabled/sub/*; do
printf '%b%20s%b -> %b%s\n' \
"$GREEN" "$(basename "$path")" "$GREY"\
"$WHITE" "$(readlink "$path")"
done
}

list_sub() {
shopt -s nullglob
local path repo name link repo_name

for repo in "${ONE_DIR}"/enabled/repos/* ; do
repo_name=$(basename "$repo")
printf '%b[%s]%b' "$BLUE" "$repo_name" "$RESET_ALL"

for path in "$repo/sub"/* ; do
name=$(basename "$path" '.opt.bash')
link=${ONE_DIR}/enabled/sub/$name

if [[ -h "$link" ]] && [[ $(readlink "$link") == "$path" ]]; then
printf ' %b%s%b' "$BOLD_GREEN" "$name" "$RESET_ALL"
else
printf ' %s' "$name"
fi
done
printf '\n'
done

printf "\n### The %bGREEN%b items are enabled. The WHITE items are availabled. ###\n" "$BOLD_GREEN" "$RESET_ALL"
}
27 changes: 27 additions & 0 deletions one-cmds/sub/main
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail -o errtrace
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit

if [[ -z ${ONE_DIR:-} ]]; then
SCRIPT_DIR="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
readonly SCRIPT_DIR
export ONE_DIR=$SCRIPT_DIR/..
fi

# shellcheck source=../../one-cmds/util.bash
. "$ONE_DIR/one-cmds/util.bash"

actions=(list info enable disable which)

declare -A alias_map=(
['e']=enable
['d']=disable
['l']=list
['i']=info
['w']=which
)

# one.bash:completion
parse_completion sub actions "$@"
# one.bash:usage
parse_cmd sub "$@"
15 changes: 15 additions & 0 deletions one-cmds/sub/usage.bash
@@ -0,0 +1,15 @@
usage() {
cat <<EOF
Usage: one sub [-h|--help] <ACTION>
one sub [-h|--help]
Desc: Manage ONE_SUB commands
ACTION:
e, enable Enable matched executable files
d, disable Disable matched executable files
l, list List executable files in each REPO/sub
i, info Show info of matched executable files
w, which <NAME> Show realpath of executable files
EOF
}
23 changes: 23 additions & 0 deletions one-cmds/sub/which.bash
@@ -0,0 +1,23 @@
usage_which() {
cat <<EOF
Usage: one sub which <cmd>
Desc: Show filepath of <cmd>
EOF
}

complete_which() {
# shellcheck source=../../bash/load-config.bash
. "$ONE_DIR/bash/load-config.bash"
one sub list
}

which_sub() {
local name=$1
local path

for path in "$ONE_DIR"/enabled/repos/*/sub/"$name"; do
if [[ -x $path ]]; then
echo "$path"
fi
done
}

0 comments on commit d94d058

Please sign in to comment.