Skip to content

Commit

Permalink
Merge pull request #20 from gianluca-mascolo/pushsnippets
Browse files Browse the repository at this point in the history
pushsnippets
  • Loading branch information
gianluca-mascolo authored Sep 27, 2023
2 parents 4518e05 + 2db1bb4 commit af8b1c6
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)$
4 changes: 2 additions & 2 deletions bash_profile_switcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions examples/snippet-example.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 0 additions & 9 deletions snippets/awscomp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions snippets/dirtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions snippets/kubecomp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions snippets/pytools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions tests/profiles/snippets/vartest1.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 0 additions & 3 deletions tests/profiles/snippets/vartest2.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit af8b1c6

Please sign in to comment.