Skip to content

Commit

Permalink
fix!: split "one enabled" command to "one backup" and "one enabled"
Browse files Browse the repository at this point in the history
Breaking Change:

one enabled list -> one enabled
one enabled backup -> one backup
  • Loading branch information
adoyle-h committed Jun 9, 2023
1 parent 0b8e1b6 commit 7bffe69
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 122 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -177,7 +177,7 @@ Usage:
one alias Manage aliases in ONE_REPO/aliases/
one completion Manage completions in ONE_REPO/completions/
one plugin Manage plugins in ONE_REPO/plugins/
one enabled Manage enabled modules (alias/completion/plugin)
one enabled List enabled modules (alias/completion/plugin)
one config Manage user's ONE_CONF
one commands List one commands
Expand Down Expand Up @@ -212,8 +212,8 @@ The modules have three types: `alias`, `completion`, `plugin`.
- All completions are put in `completions/` of each repo.
- All aliases are put in `aliases/` of each repo.
- All enabled modules are symbol linked in `$ONE_DIR/enabled/` directory.
- `one enabled list` to view enabled modules.
- `one enabled backup` to backup enabled modules to a file.
- `one enabled` to view enabled modules.
- `one backup` to backup enabled modules to a file.
- Read `one help <mod_type>` for usages.
- `one <mod_type> enable` to enable modules.
- `one <mod_type> disable` to disable modules.
Expand Down
6 changes: 3 additions & 3 deletions README.zh.md
Expand Up @@ -177,7 +177,7 @@ Usage:
one alias Manage aliases in ONE_REPO/aliases/
one completion Manage completions in ONE_REPO/completions/
one plugin Manage plugins in ONE_REPO/plugins/
one enabled Manage enabled modules (alias/completion/plugin)
one enabled List enabled modules (alias/completion/plugin)
one config Manage user's ONE_CONF
one commands List one commands
Expand Down Expand Up @@ -212,8 +212,8 @@ one.bash 使用模块来管理脚本。
- 所有 completions 放在每个 repo 的 `completions/` 目录。
- 所有 aliases 放在每个 repo 的 `aliases/` 目录。
- 所有启用的模块会在 `$ONE_DIR/enabled/` 目录下创建软链接。
- 使用 `one enabled list` 可以查询启用的模块。
- 使用 `one enabled backup` 备份启动的模块。
- 使用 `one enabled` 可以查询启用的模块。
- 使用 `one backup` 备份启动的模块。
- 使用 `one help <mod_type>` 显示使用方法。
- `one <mod_type> enable` 来启用模块。
- `one <mod_type> disable` 来禁用模块。
Expand Down
16 changes: 8 additions & 8 deletions bash/mod.bash
Expand Up @@ -345,7 +345,7 @@ print_list_item() {
declare -A MOD_TYPE_COLOR=(
['completion']=$GREEN
['plugin']=$BLUE
['alias']=$RED
['alias']=$PURPLE
)

while read -r line; do
Expand All @@ -355,18 +355,18 @@ print_list_item() {
local -a prints=()
local format=''

# mod type
if [[ ${opts['type']:-} != false ]]; then
format="$format%b%s "
prints+=("${MOD_TYPE_COLOR[${list[2]}]}" "${type^}")
fi

# load-priority
if [[ ${opts['priority']:-} != false ]]; then
format="$format%b%s "
prints+=("$YELLOW" "${list[0]}")
fi

# mod type
if [[ ${opts['type']:-} != false ]]; then
format="$format%b%s "
prints+=("${MOD_TYPE_COLOR[${list[2]}]}" "${type^}")
fi

# mod name -> real path
format="$format%b%s%b -> %b%s\n"
prints+=(
Expand Down Expand Up @@ -410,7 +410,7 @@ list_mods() {
list_enabled "$t" | tr '\n' ' '
printf '\n'
else
find "$ENABLED_DIR" -maxdepth 1 -name "*---*.$t.bash" | print_list_item
find "$ENABLED_DIR" -maxdepth 1 -name "*---*.$t.bash" | print_list_item | sort
fi
fi
}
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced-usage/module.md
Expand Up @@ -10,8 +10,8 @@ The modules have three types: `alias`, `completion`, `plugin`.
- All completions are put in `completions/` of each repo.
- All aliases are put in `aliases/` of each repo.
- All enabled modules are symbol linked in `$ONE_DIR/enabled/` directory.
- `one enabled list` to view enabled modules.
- `one enabled backup` to backup enabled modules to a file.
- `one enabled` to view enabled modules.
- `one backup` to backup enabled modules to a file.
- Read `one help <mod_type>` for usages.
- `one <mod_type> enable` to enable modules.
- `one <mod_type> disable` to disable modules.
Expand Down
101 changes: 101 additions & 0 deletions one-cmds/backup
@@ -0,0 +1,101 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit

# one.bash:completion
if [[ "${1:-}" == --complete ]]; then
if (( COMP_CWORD < 3 )); then
words=(-o -h --help)
printf '%s\n' "${words[@]}"
elif [[ $2 == -o ]]; then
result=$(compgen -f -- "$3")
if [[ -d $result ]]; then
compgen -f -- "$result/"
else
echo "${result[@]}"
fi
fi
exit 0
fi

usage() {
local cmd; cmd=$(basename "$0");
cat <<EOF
Usage: one $cmd [OPTIONS]
Desc: Output backup scripts for current enabled modules
Options:
-h, --help Show usage
-o, --output <PATH> Output the content to file.
EOF
}

# one.bash:usage
if [[ ${1:-} == -h ]] || [[ ${1:-} == --help ]]; then
usage
exit 0
fi

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

# shellcheck source=../../bash/load-config.bash
. "$ONE_DIR/bash/load-config.bash"

# shellcheck source=../../deps/colors.bash
. "$ONE_DIR/deps/colors.bash"

# shellcheck source=../../bash/log.bash
. "$ONE_DIR/bash/log.bash"

# shellcheck source=../../bash/mod.bash
. "$ONE_DIR/bash/mod.bash"

# shellcheck source=../../deps/lobash.bash
. "$ONE_DIR/deps/lobash.bash"

declare -A opts=()
declare -a args=()
l.parse_params opts args "$@"

list_enabled_for_backup() {
list_enabled "$1" | sort | tr '\n' ' '
printf '\n'
}

backup_content() {
cat <<EOF
#!/usr/bin/env bash
set -euE -o pipefail
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit
one plugin disable --all
one alias disable --all
one completion disable --all
EOF

printf "%s%s\n\n" "one plugin enable" "$(list_enabled_for_backup plugin)"
printf "%s%s\n\n" "one alias enable" "$(list_enabled_for_backup alias)"
printf "%s%s\n\n" "one completion enable" "$(list_enabled_for_backup completion)"
printf '\necho "[All Done]"\n'
}

backup() {
dist="${opts[o]:-${opts[output]:-/dev/stdout}}"

if [[ $dist == /dev/stdout ]]; then
backup_content
else
backup_content | tee -a "$dist"
printf '%b%s%b\n' "${GREEN}" "Created backup file: $dist" "${RESET_ALL}"
chmod +x "$dist"
fi
}
backup
120 changes: 14 additions & 106 deletions one-cmds/enabled
Expand Up @@ -8,68 +8,27 @@ set -o errtrace

# one.bash:completion
if [[ "${1:-}" == --complete ]]; then
actions=(backup list)

if (( COMP_CWORD < 3 )); then
words=("${actions[@]}" -h --help)
printf '%s\n' "${words[@]}"
elif [[ $2 == -h ]] || [[ $2 == --help ]]; then
printf '%s\n' "${actions[@]}"
elif [[ $2 == backup ]]; then
result=$(compgen -f -- "$2")
if [[ -d $result ]]; then
compgen -f -- "$result/"
else
echo "${result[@]}"
fi
fi
words=(-h --help --type --priority)
printf '%s\n' "${words[@]}"
exit 0
fi

usage() {
local cmd; cmd=$(basename "$0");
cat <<EOF
Usage: one $cmd [-h|--help] <ACTION>
one $cmd [-h|--help]
one $cmd <ACTION> [--help]
Desc: Manage enabled modules (alias/completion/plugin)
Action:
backup Create a backup file for current enabled modules
l, list List enabled modules
EOF
exit 0
}

usage_backup() {
local cmd; cmd=$(basename "$0");
cat <<EOF
Usage: one $cmd backup [OPTIONS]
Desc: Create a backup file for current enabled modules
Options:
-o, --output <PATH> The filepath of output [Default: restore-modules]
EOF
exit 0
}


usage_list() {
local cmd; cmd=$(basename "$0");
cat <<EOF
Usage: one $cmd list
Usage: one $cmd [OPTIONS]
Desc: List enabled modules
Options:
-h, --help Show usage
--type=true|false
--priority=true|false
EOF
exit 0
}
usage_l() { usage_list; }

# one.bash:usage
if (( $# == 0 )) then
usage;
elif [[ $1 == -h ]] || [[ $1 == --help ]]; then
if (( $# > 1 )); then usage_"$2"; else usage; fi
elif [[ ${*: -1} == --help ]] ; then
if (( $# > 1 )); then usage_"$1"; else usage; fi
if [[ ${1:-} == -h ]] || [[ ${1:-} == --help ]]; then
usage
exit 0
fi

if [[ -z ${ONE_DIR:-} ]]; then
Expand All @@ -89,65 +48,14 @@ fi
# shellcheck source=../../bash/mod.bash
. "$ONE_DIR/bash/mod.bash"

list_enabled_for_backup() {
list_enabled "$1" | sort | tr '\n' ' '
printf '\n'
}

backup() {
dist="${opts[o]:-${opts[output]:-restore-modules}}"

cat <<EOF >"$dist"
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit
one plugin disable --all
one alias disable --all
one completion disable --all
EOF

printf '%b%s\nContent:%b\n' "${GREEN}" "Created file: $dist" "${RESET_ALL}"

{
echo "one plugin enable $(list_enabled_for_backup plugin)"
echo ''
echo "one alias enable $(list_enabled_for_backup alias)"
echo ''
echo "one completion enable $(list_enabled_for_backup completion)"
} | tee -a "$dist"

printf '\necho "[All Done]"\n' >> "$dist"

chmod +x "$dist"
}

list() {
find "$ONE_DIR/enabled" -maxdepth 1 -name "*---*.bash" | print_list_item
}

action=$1
shift

# shellcheck source=../../deps/lobash.bash
. "$ONE_DIR/deps/lobash.bash"

declare -A opts=()
declare -a args=()
l.parse_params opts args "$@"

case "$action" in
backup) backup ;;

l|list) list ;;

*)
echo "Invalid action '$action'" >&2
exit 1
;;
esac
list() {
find "$ONE_DIR/enabled" -maxdepth 1 -name "*---*.bash" | print_list_item | sort
}
list

0 comments on commit 7bffe69

Please sign in to comment.