diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f78bb90..94e1f1e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,4 +24,4 @@ repos: entry: shellcheck language: system types: [file, shell] - files: ^(bash_profile_switcher.sh|tests/profiles/snippets/.*\.sh)$ + files: ^(bash_profile_switcher.sh|tests/profiles/snippets/.*\.sh|snippets/.*\.sh)$ diff --git a/bash_profile_switcher.sh b/bash_profile_switcher.sh index 3a7e9c3..4e1504c 100644 --- a/bash_profile_switcher.sh +++ b/bash_profile_switcher.sh @@ -46,8 +46,8 @@ alias _reset_bash_profile='eval echo "unset SWITCH_PROFILE_CURRENT" > "$HOME/$SW alias _get_snippets='eval unset LOAD_SNIPPETS; declare -a LOAD_SNIPPETS; mapfile -c 1 -C _parse_profile -t <"${HOME}/${SWITCH_PROFILE_DIRECTORY}/${SWITCH_PROFILE_CURRENT}.profile"' # shellcheck disable=SC2154 -alias _load_bash_profile='_get_snippets; for ((n=0;n<${#LOAD_SNIPPETS[*]};n++)); do source "${LOAD_SNIPPETS[$n]}" load; done' -alias _unload_bash_profile='for ((n=-1;n>=-${#LOAD_SNIPPETS[*]};n--)); do source "${LOAD_SNIPPETS[$n]}" unload; done' +alias _load_bash_profile='_get_snippets; for ((n=0;n<${#LOAD_SNIPPETS[*]};n++)); do if ! _snippet search "$(basename "${LOAD_SNIPPETS[$n]}" .sh)"; then source "${LOAD_SNIPPETS[$n]}" load && _snippet push "$(basename "${LOAD_SNIPPETS[$n]}" .sh)"; fi; done' +alias _unload_bash_profile='for ((n=-1;n>=-${#LOAD_SNIPPETS[*]};n--)); do if _snippet search "$(basename "${LOAD_SNIPPETS[$n]}" .sh)"; then source "${LOAD_SNIPPETS[$n]}" unload && _snippet pop "$(basename "${LOAD_SNIPPETS[$n]}" .sh)"; fi; done' # _parse_profile # To be used with mapfile diff --git a/examples/snippet-example.sh b/examples/snippet-example.sh index c5e32d5..b66270f 100755 --- a/examples/snippet-example.sh +++ b/examples/snippet-example.sh @@ -1,19 +1,10 @@ # shellcheck shell=bash -SNIPPET_NAME="$(basename "${BASH_SOURCE[0]}" .sh)" case "$1" in load) - _snippet search "$SNIPPET_NAME" && return 0 - # load your settings here >>> export var1="test1 variable" - # <<< - _snippet push "$SNIPPET_NAME" 2>/dev/null ;; unload) - _snippet search "$SNIPPET_NAME" || return 0 - # unload your settings here >>> unset var1 - # <<< - _snippet pop "$SNIPPET_NAME" 2>/dev/null ;; *) true diff --git a/snippets/awscomp.sh b/snippets/awscomp.sh index 05eadf5..ac474e1 100755 --- a/snippets/awscomp.sh +++ b/snippets/awscomp.sh @@ -10,23 +10,14 @@ # - aws cli installed for command completion # - fzf https://github.com/junegunn/fzf#using-linux-package-managers for awp -SNIPPET_NAME="$(basename "${BASH_SOURCE[0]}" .sh)" case "$1" in load) - _snippet search "$SNIPPET_NAME" 2>/dev/null && return 0 - # load your settings here >>> alias awp='eval export AWS_PROFILE=$(grep --color=none -oP "(?<=\[profile )[^]]+(?=])" "${AWS_CONFIG_FILE:-$HOME/.aws/config}" | sort | fzf --height=6)' hash aws_completer && complete -C "$(hash -t aws_completer)" aws - # <<< - _snippet push "$SNIPPET_NAME" 2>/dev/null ;; unload) - _snippet search "$SNIPPET_NAME" 2>/dev/null || return 0 - # unload your settings here >>> unalias awp complete -p aws &>/dev/null && complete -r aws - # <<< - _snippet pop "$SNIPPET_NAME" 2>/dev/null ;; *) true diff --git a/snippets/dirtools.sh b/snippets/dirtools.sh index 340fada..7de9cd2 100755 --- a/snippets/dirtools.sh +++ b/snippets/dirtools.sh @@ -9,11 +9,8 @@ # requires: # - realpath (from coreutils) to use pathctl -SNIPPET_NAME="$(basename "${BASH_SOURCE[0]}" .sh)" case "$1" in load) - _snippet search "$SNIPPET_NAME" 2>/dev/null && return 0 - # load your settings here >>> mkcd() { [ -d "$1" ] || mkdir "$1" && cd "$1" || return 0; } pathctl() { local PATH_CMD Option OPTIND OPTARG PATH_ELEMENT @@ -87,16 +84,10 @@ load) export PATH return 0 } - # <<< - _snippet push "$SNIPPET_NAME" 2>/dev/null ;; unload) - _snippet search "$SNIPPET_NAME" 2>/dev/null || return 0 - # unload your settings here >>> unset -f mkcd unset -f path_append - # <<< - _snippet pop "$SNIPPET_NAME" 2>/dev/null ;; *) true diff --git a/snippets/kubecomp.sh b/snippets/kubecomp.sh index acd4019..1df8b64 100755 --- a/snippets/kubecomp.sh +++ b/snippets/kubecomp.sh @@ -8,22 +8,14 @@ # requires: # - kubectl installed for command completion -SNIPPET_NAME="$(basename "${BASH_SOURCE[0]}" .sh)" case "$1" in load) - _snippet search "$SNIPPET_NAME" 2>/dev/null && return 0 - # load your settings here >>> + # shellcheck disable=SC1090 hash kubectl && source <(kubectl completion bash) - # <<< - _snippet push "$SNIPPET_NAME" 2>/dev/null ;; unload) - _snippet search "$SNIPPET_NAME" 2>/dev/null || return 0 - # unload your settings here >>> declare -F | while read -r funcname; do [[ "$funcname" =~ kubectl ]] && unset -f "$funcname"; done complete -p kubectl &>/dev/null && complete -r kubectl - # <<< - _snippet pop "$SNIPPET_NAME" 2>/dev/null ;; *) true diff --git a/snippets/pytools.sh b/snippets/pytools.sh index 3ac14b7..9de5d2f 100644 --- a/snippets/pytools.sh +++ b/snippets/pytools.sh @@ -9,23 +9,14 @@ # requires: # - python3 with yaml and json module installed -SNIPPET_NAME="$(basename "${BASH_SOURCE[0]}" .sh)" case "$1" in load) - _snippet search "$SNIPPET_NAME" 2>/dev/null && return 0 - # load your settings here >>> alias yaml2json="python -c 'import sys,json,yaml; json.dump(yaml.safe_load(sys.stdin),sys.stdout,indent=2)'" alias json2yaml="python -c 'import sys,json,yaml; yaml.dump(json.load(sys.stdin),sys.stdout,indent=2)'" - # <<< - _snippet push "$SNIPPET_NAME" 2>/dev/null ;; unload) - _snippet search "$SNIPPET_NAME" 2>/dev/null || return 0 - # unload your settings here >>> unalias yaml2json unalias json2yaml - # <<< - _snippet pop "$SNIPPET_NAME" 2>/dev/null ;; *) true diff --git a/tests/profiles/snippets/vartest1.sh b/tests/profiles/snippets/vartest1.sh index 43da5d9..043855f 100755 --- a/tests/profiles/snippets/vartest1.sh +++ b/tests/profiles/snippets/vartest1.sh @@ -1,15 +1,12 @@ # shellcheck shell=bash -SNIPPET_NAME="$(basename "${BASH_SOURCE[0]}" .sh)" case "$1" in load) export check_var_test1="test1 variable" export common_value="test1 common" - _snippet push "$SNIPPET_NAME" 2>/dev/null ;; unload) unset check_var_test1 unset common_value - _snippet pop "$SNIPPET_NAME" 2>/dev/null ;; *) true diff --git a/tests/profiles/snippets/vartest2.sh b/tests/profiles/snippets/vartest2.sh index 43fe50e..67561ab 100755 --- a/tests/profiles/snippets/vartest2.sh +++ b/tests/profiles/snippets/vartest2.sh @@ -1,15 +1,12 @@ # shellcheck shell=bash -SNIPPET_NAME="$(basename "${BASH_SOURCE[0]}" .sh)" case "$1" in load) export check_var_test2="test2 variable" export common_value="test2 common" - _snippet push "$SNIPPET_NAME" 2>/dev/null ;; unload) unset check_var_test2 unset common_value - _snippet pop "$SNIPPET_NAME" 2>/dev/null ;; *) true