diff --git a/zsh/aliases.zsh b/zsh/aliases.zsh index 5a0629f..e6dbe37 100644 --- a/zsh/aliases.zsh +++ b/zsh/aliases.zsh @@ -1,9 +1,8 @@ -alias s="ssh" alias z="exec zsh" alias t="tmux a || tmux new-session -s $(whoami) -c ~" alias n=nvim_ve # alias vim=nvim_ve -alias ssh-hosts="grep -P \"^Host ([^*]+)$\" $HOME/.ssh/config | sed 's/Host //'" +alias s="ssh-menu" # navigation alias cdgit="cd $GGROOT" diff --git a/zsh/scripts.zsh b/zsh/scripts.zsh index a127565..6340917 100644 --- a/zsh/scripts.zsh +++ b/zsh/scripts.zsh @@ -1,125 +1,138 @@ - function nvim_ve { - if [[ -e "$VIRTUAL_ENV" && -f "$VIRTUAL_ENV/bin/activate" ]]; then - source "$VIRTUAL_ENV/bin/activate" - command nvim $@ - deactivate - else - command nvim $@ - fi + if [[ -e $VIRTUAL_ENV && -f "$VIRTUAL_ENV/bin/activate" ]]; then + source "$VIRTUAL_ENV/bin/activate" + command nvim $@ + deactivate + else + command nvim $@ + fi } alias nvim=nvim_ve function ffb { - if ! [[ -f package.json ]]; then - echo "no package.json" - return - fi + if ! [[ -f package.json ]]; then + echo "no package.json" + return + fi - if [[ -f yarn.lock ]]; then - PACKAGE_MANAGER=yarn - else - PACKAGE_MANAGER=npm - fi + if [[ -f yarn.lock ]]; then + PACKAGE_MANAGER=yarn + else + PACKAGE_MANAGER=npm + fi - if [[ -n $(grep "react-scripts" package.json) ]]; then - BROWSER=none FORCE_COLOR=true "$PACKAGE_MANAGER" start | cat - fi + if [[ -n $(grep "react-scripts" package.json) ]]; then + BROWSER=none FORCE_COLOR=true "$PACKAGE_MANAGER" start | cat + fi } function afm { - RES=$(curl -s "https://$1/api/monitoring/requirements") - COUNT=$(echo "$RES" | grep -c $2) - if [[ $COUNT -ge 0 ]]; then - VER=$(echo "$RES" | jq '.[] | select(.name | contains("'"$2"'")).version') - # OP=$(echo "$RES" | jq '.[] | select(.name | contains("'"$2"'")).requirement?[0][0]') - # echo "$2""$(echo "$OP" | tr -d '"')""$(echo "$VER" | tr -d '"')" - echo "$2""==""$(echo "$VER" | tr -d '"')" - fi + RES=$(curl -s "https://$1/api/monitoring/requirements") + COUNT=$(echo "$RES" | grep -c $2) + if [[ $COUNT -ge 0 ]]; then + VER=$(echo "$RES" | jq '.[] | select(.name | contains("'"$2"'")).version') + # OP=$(echo "$RES" | jq '.[] | select(.name | contains("'"$2"'")).requirement?[0][0]') + # echo "$2""$(echo "$OP" | tr -d '"')""$(echo "$VER" | tr -d '"')" + echo "${2}==$(echo "$VER" | tr -d '"')" + fi } - function nukepip { - pip uninstall $(pip freeze) -y - pip install -r $(pyenv root)/default-packages + pip uninstall $(pip freeze) -y + pip install -r $(pyenv root)/default-packages } function pyinit { - if [[ -f .python-version ]]; then - echo "found .python-version, stopping" - return - fi + if [[ -f .python-version ]]; then + echo "found .python-version, stopping" + return + fi - DIRNAME=$(basename "$PWD") + DIRNAME=$(basename "$PWD") - if [[ $(pyenv versions | grep -E "/${DIRNAME}$") ]]; then - echo "found existing version with this name, setting..." + if [[ $(pyenv versions | grep -E "/${DIRNAME}$") ]]; then + echo "found existing version with this name, setting..." + pyenv local "$DIRNAME" + return + fi + + if [[ -z $1 ]]; then + echo "no python version provided, defaulting to $(pyenv version-name)" + VERSION=$(pyenv version-name) + else + VERSION=$1 + fi + + pyenv virtualenv "$VERSION" "$DIRNAME" pyenv local "$DIRNAME" - return - fi - - if [[ -z $1 ]]; then - echo "no python version provided, defaulting to $(pyenv version-name)" - VERSION=$(pyenv version-name) - else - VERSION=$1 - fi - - pyenv virtualenv "$VERSION" "$DIRNAME" - pyenv local "$DIRNAME" } function lh { - if [[ -z $1 ]]; then - PORT=8000 - else - PORT=$1 - fi + if [[ -z $1 ]]; then + PORT=8000 + else + PORT=$1 + fi - open "http://localhost:$PORT" + open "http://localhost:$PORT" } function dps { - if [[ -z $1 ]]; then - docker ps --format table'{{ .ID }}\t{{ .Image }}\t{{ .Ports}}\t{{ .Names }}' - else - docker ps --format table'{{ .ID }}\t{{ .Image }}\t{{ .Ports}}\t{{ .Names }}' | grep $1 - fi + if [[ -z $1 ]]; then + docker ps --format table'{{ .ID }}\t{{ .Image }}\t{{ .Ports}}\t{{ .Names }}' + else + docker ps --format table'{{ .ID }}\t{{ .Image }}\t{{ .Ports}}\t{{ .Names }}' | grep $1 + fi } - function get_dump { - if [[ -z $1 ]]; then - echo "provide ssh host" - return - fi + if [[ -z $1 ]]; then + echo "provide ssh host" + return + fi - if [[ -z $2 ]]; then - echo "provide dump destination" - return - fi + if [[ -z $2 ]]; then + echo "provide dump destination" + return + fi - selected=$(ssh "$1" docker ps --format "{{.Names}}" | fzf) + selected=$(ssh "$1" docker ps --format "{{.Names}}" | fzf) - if [[ -z $selected ]]; then - return - fi + if [[ -z $selected ]]; then + return + fi - ssh "$1" docker exec "$selected" pg_dump -U db --format=c db > "$2$(date +'%Y-%m-%dT%H.%M.%S%z')" + ssh "$1" docker exec "$selected" pg_dump -U db --format=c db >"$2$(date +'%Y-%m-%dT%H.%M.%S%z')" } function ggcompile { - find -L $GGROOT -mindepth 1 -maxdepth 5 -type d -name .git -prune | xargs -n 1 dirname > "$GGROOT/compiled" + find -L "$GGROOT" -mindepth 1 -maxdepth 5 -type d -name .git -prune \ + -exec dirname {} \; >"$GGROOT/compiled" } function tssh { - if [[ -z $1 ]]; then - echo "provide ssh host" - return - fi + if [[ -z $1 ]]; then + echo "provide ssh host" + return + fi - ssh -t "$@" "command -v tmux && (tmux a || tmux new-session -s gorazd -c ~) || bash" + ssh -t "$@" "command -v tmux && (tmux a || tmux new-session -s gorazd -c ~) || bash" +} + +function ssh-menu { + selected=$( + grep 'Host .*' ~/.ssh/config \ + | grep -v '*' \ + | sed 's/Host \(.*\)/\1/' \ + | fzf --cycle --bind 'tab:toggle-up,btab:toggle-down' + ) + + if [[ -z $selected ]]; then + return + fi + + ssh "$selected" }