From 1c2c69c6b2e2500a3ed5ce4c6740640013820fde Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sat, 12 Sep 2020 09:58:04 +0200 Subject: [PATCH] Updated bash configs --- .gitignore | 2 +- bash/{alias.bash => alias.sh} | 14 +--- bash/bashrc.bash | 49 ------------- bash/bashrc.sh | 47 +++++++++++++ bash/docker.bash | 17 ----- bash/{format.bash => format.sh} | 6 +- bash/{functions.bash => functions.sh} | 23 +++---- ...ns_internal.bash => functions_internal.sh} | 0 bash/{history.bash => history.sh} | 0 bash/iterm.bash | 31 --------- bash/overrides.sh | 4 ++ bash/path.bash | 28 -------- bash/path.sh | 13 ++++ bash/{prompt.bash => prompt.sh} | 5 +- bash/{gover.bash => tools/gover.sh} | 21 +++--- bash/tools/myc.sh | 68 +++++++++++++++++++ 16 files changed, 162 insertions(+), 166 deletions(-) rename bash/{alias.bash => alias.sh} (54%) delete mode 100644 bash/bashrc.bash create mode 100644 bash/bashrc.sh delete mode 100644 bash/docker.bash rename bash/{format.bash => format.sh} (93%) rename bash/{functions.bash => functions.sh} (66%) rename bash/{functions_internal.bash => functions_internal.sh} (100%) rename bash/{history.bash => history.sh} (100%) delete mode 100644 bash/iterm.bash create mode 100644 bash/overrides.sh delete mode 100644 bash/path.bash create mode 100644 bash/path.sh rename bash/{prompt.bash => prompt.sh} (95%) rename bash/{gover.bash => tools/gover.sh} (55%) create mode 100644 bash/tools/myc.sh diff --git a/.gitignore b/.gitignore index ecf2f52..337cc93 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ fish/nginx.conf git/git-achievements* git.pgp -bash/secrets.bash +bash/secrets/* diff --git a/bash/alias.bash b/bash/alias.sh similarity index 54% rename from bash/alias.bash rename to bash/alias.sh index 8d445c2..0ad87e2 100644 --- a/bash/alias.bash +++ b/bash/alias.sh @@ -1,11 +1,8 @@ # General - -alias ll="ls -lAFh" -alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl" +alias ll="ls -lAFh --color" alias dedup="awk '!visited[$0]++'" # Git -alias git="git-achievements" alias ga="git add -u; git add .; git status -sb" alias gb="git branch -v" alias gc="git ci --allow-empty -m" @@ -20,16 +17,11 @@ alias gcl="git co -" alias gml="git merge - --no-edit" # Ruby -alias bd="bundle install --jobs=8 --path vendor/bundle" +alias bd="bundle install --jobs=8" +alias bdc="bundle config set path 'vendor/bundle'" alias b="bundle exec" alias br="bundle exec rails" # Go alias goi="go install ./..." alias gg='rg -g "*.go"' - -# OSX -alias hide-desktop="defaults write com.apple.finder CreateDesktop -bool false; killall Finder" -alias show-desktop="defaults write com.apple.finder CreateDesktop -bool true; killall Finder" -alias prefs="/Applications/System\ Preferences.app/Contents/MacOS/System\ Preferences" -alias admin-prefs="sudo /Applications/System\ Preferences.app/Contents/MacOS/System\ Preferences" diff --git a/bash/bashrc.bash b/bash/bashrc.bash deleted file mode 100644 index 08272f9..0000000 --- a/bash/bashrc.bash +++ /dev/null @@ -1,49 +0,0 @@ -# Enable debug -# set -xv - -export TERM=xterm-256color -export EDITOR=vim - -# Colors -export CLICOLOR=1 -export LSCOLORS=Exfxcxdxbxegedabagacad - -export EDITOR=vim -export PROJECTS=$HOME/Code - -# Go -# export GOROOT=$HOME/go/lang -export GOPATH=$HOME/go/path - -function __bash_import { source $HOME/.bash/$1.bash; } -__bash_import "functions_internal" -__bash_import "history" -__bash_import "path" -__bash_import "format" -__bash_import "iterm" -__bash_import "alias" -__bash_import "prompt" -__bash_import "functions" -__bash_import "gover" -__bash_import "docker" -__bash_import "secrets" - -# Homebrew bash completions -if [ -f $(brew --prefix)/etc/bash_completion ]; then - . $(brew --prefix)/etc/bash_completion -fi - -# -# Misc -# - -alias ag=rg -# export SSL_CERT_FILE="/usr/local/opt/curl-ca-bundle/share/ca-bundle.crt" - -export PATH="/usr/local/opt/avr-gcc@8/bin:$PATH" - -export LDFLAGS="-L/usr/local/opt/openssl/lib" -export CPPFLAGS="-I/usr/local/opt/openssl/include" -export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig" - -export PATH="$HOME/.cargo/bin:$PATH" diff --git a/bash/bashrc.sh b/bash/bashrc.sh new file mode 100644 index 0000000..f6e0bb2 --- /dev/null +++ b/bash/bashrc.sh @@ -0,0 +1,47 @@ +# Enable debug +# set -xv + +source /etc/profile + +export TERM=xterm-256color +export EDITOR=vim + +# Colors +export CLICOLOR=1 +export LSCOLORS=Exfxcxdxbxegedabagacad + + +export EDITOR=vim +export PROJECTS=$HOME/Code + +# Go +# GOROOT can be easily derived from go binary path +# export GOROOT=$HOME/go/lang +export GOPATH=$HOME/go/path + +source $HOME/dotfiles/bash/functions_internal.sh +source $HOME/dotfiles/bash/history.sh +source $HOME/dotfiles/bash/path.sh +source $HOME/dotfiles/bash/format.sh +source $HOME/dotfiles/bash/alias.sh +source $HOME/dotfiles/bash/prompt.sh +source $HOME/dotfiles/bash/functions.sh +source $HOME/dotfiles/bash/overrides.sh + +for f in $HOME/dotfiles/bash/{tools,secrets}/*; do + source $f +done + +# Use bash-completion, if available +[[ $PS2 && -f /usr/share/bash-completion/bash_completion ]] && \ + . /usr/share/bash-completion/bash_completion + +# +# Misc +# + +export PATH="$HOME/.local/bin:$PATH" + +# Disable Ruby 2.7 deprecation warnings +# export RUBYOPT='-W:no-deprecated -W:no-experimental' +export RUBYOPT= diff --git a/bash/docker.bash b/bash/docker.bash deleted file mode 100644 index 8c37bbb..0000000 --- a/bash/docker.bash +++ /dev/null @@ -1,17 +0,0 @@ -function d-ssh { - docker exec -it $1 /bin/bash -} -function _d-ssh { - local cur=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=($(compgen -o nospace -W "$(docker ps -a --format '{{.Names}}')" -- $cur)) -} -complete -F _d-ssh d-ssh - -function d-ps { - docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}\t{{.CreatedAt}}" $@ -} - -function d-clean { - local images=$(docker images -a | grep -e '^' | awk '{print $3}' | cut -d: -f2) - [[ -n $images ]] && docker rmi -f $images -} diff --git a/bash/format.bash b/bash/format.sh similarity index 93% rename from bash/format.bash rename to bash/format.sh index 0de4e11..3279eaa 100644 --- a/bash/format.bash +++ b/bash/format.sh @@ -7,17 +7,17 @@ _DIM=$(__bash_escape 2) _ITALIC=$(__bash_escape 3) _UNDERLINED=$(__bash_escape 4) _BLINK=$(__bash_escape 5) -# _BRIGHT=$(__bash_escape 6) +_BRIGHT=$(__bash_escape 6) _INVERTED=$(__bash_escape 7) _HIDDEN=$(__bash_escape 8) # E.g. for passwords _RESET=$(__bash_escape 0) -_RESET_BOLD=$(__bash_escape 21) # Doesn't work? +_RESET_BOLD=$(__bash_escape 21) _RESET_DIM=$(__bash_escape 22) _RESET_ITALIC=$(__bash_escape 23) _RESET_UNDERLINED=$(__bash_escape 24) _RESET_BLINK=$(__bash_escape 25) -# _RESET_BRIGHT=$(__bash_escape 26) +_RESET_BRIGHT=$(__bash_escape 26) _RESET_INVERTED=$(__bash_escape 27) _RESET_HIDDEN=$(__bash_escape 28) diff --git a/bash/functions.bash b/bash/functions.sh similarity index 66% rename from bash/functions.bash rename to bash/functions.sh index f1ca5f3..3e583c6 100644 --- a/bash/functions.bash +++ b/bash/functions.sh @@ -1,18 +1,8 @@ function reload { - source $HOME/.bash_profile -} - -function ssh { - # Change tab title - local host=$(echo $1 | cut -d@ -f1) - __iterm_set_title $host - - # Change tab color and reset back when conneciton is closed - __iterm_set_bg_rgb 220 20 0 && \ - /usr/bin/ssh $@ && \ - __iterm_set_bg_reset + source $HOME/.bashrc } +# Count lines of code in files that are checked into the current Git repository function gkloc { cloc $(git ls-files) } @@ -23,6 +13,10 @@ function shuf { cut -d' ' -f2- } +function __window_title { + echo -ne "\033]0;$1\007" +} + function __bash_ssh_complete { __bash_complete "$(cat $HOME/.ssh/known_hosts | cut -d' ' -f1 | cut -d, -f1)" } @@ -42,7 +36,7 @@ complete -F _c c function cg { cd $GOPATH/src/github.com/$1; } function _cg { __bash_directory_complete $GOPATH/src/github.com; } -complete -F _cg cg +complete -W "$(dir -d $HOME/go/path/src/github.com/*/* | cut -d'/' -f8-)" cg # # Misc @@ -53,6 +47,5 @@ function __bash_directory_complete { } function __bash_complete { - local cur=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=($(compgen -o nospace -W "$1" -- $cur)) + COMPREPLY=($(compgen -o nospace -W "$1" -- ${COMP_WORDS[COMP_CWORD]})) } diff --git a/bash/functions_internal.bash b/bash/functions_internal.sh similarity index 100% rename from bash/functions_internal.bash rename to bash/functions_internal.sh diff --git a/bash/history.bash b/bash/history.sh similarity index 100% rename from bash/history.bash rename to bash/history.sh diff --git a/bash/iterm.bash b/bash/iterm.bash deleted file mode 100644 index 5db4317..0000000 --- a/bash/iterm.bash +++ /dev/null @@ -1,31 +0,0 @@ -# $1 - title -function __iterm_set_title { - echo -ne "\033]0;$1\007" -} - -# $1 - basedir -function __iterm_set_bg_color_by_dir { - case $1 in - "example" ) __iterm_set_bg_rgb 0 150 255 ;; - * ) __iterm_set_bg_reset ;; - esac -} - -# $1 red (0-255) -# $2 green (0-255) -# $3 blue (0-255) -function __iterm_set_bg_rgb { - __iterm_set_bg_channel red $1 - __iterm_set_bg_channel green $2 - __iterm_set_bg_channel blue $3 -} - -function __iterm_set_bg_reset { - echo -ne "\033]6;1;bg;*;default\a" -} - -# $1 channel (red, green, blue) -# $2 brightness (0-255) -function __iterm_set_bg_channel { - echo -ne "\033]6;1;bg;$1;brightness;$2\a" -} diff --git a/bash/overrides.sh b/bash/overrides.sh new file mode 100644 index 0000000..5c6adeb --- /dev/null +++ b/bash/overrides.sh @@ -0,0 +1,4 @@ +function htop { + __window_title "htop" + /sbin/htop +} diff --git a/bash/path.bash b/bash/path.bash deleted file mode 100644 index 9989377..0000000 --- a/bash/path.bash +++ /dev/null @@ -1,28 +0,0 @@ -export PATH=/usr/local/bin:$PATH # Homebrew tools -export PATH=$PATH:/usr/local/sbin # Homebrew apps -export PATH=$PATH:$HOME/Dotfiles/bin # My stuff -export PATH=$PATH:$HOME/.bin - -# NodeJS -# export PATH=$PATH:/usr/local/share/npm/bin - -# Ruby -export PATH=$HOME/.rbenv/versions/2.4.0/bin:$PATH # Gem binaries -export PATH=$HOME/.rbenv/shims:$PATH # Gem binaries - -# git achievements -export PATH=$PATH:$PROJECTS/git-achievements - -# Go -# export PATH=$PATH:$GOROOT/bin # Go runtime binaries -export PATH=$PATH:$HOME/go/lang/bin # Go package binaries -export PATH=$PATH:$GOPATH/bin # Go package binaries - -# Python -# WTF -export PKG_CONFIG_PATH=/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.4/lib/pkgconfig - -export PATH=$PATH:/usr/local/Cellar/llvm/8.0.0_1/bin - -# Rust -export PATH=$PATH:$HOME/.cargo/bin diff --git a/bash/path.sh b/bash/path.sh new file mode 100644 index 0000000..ebeadea --- /dev/null +++ b/bash/path.sh @@ -0,0 +1,13 @@ +# Dotfiles +export PATH=$PATH:$HOME/dotfiles/bin # Dotfiles scripts + +# Ruby +export PATH=$HOME/.rbenv/versions/$(cat $HOME/.rbenv/version)/bin:$PATH # Gem binaries +export PATH=$HOME/.rbenv/shims:$PATH # Gem binaries + +# Go +export PATH=$HOME/go/lang/bin:$PATH # Go package binaries +export PATH=$GOPATH/bin:$PATH # Go package binaries + +# Rust +export PATH=$PATH:$HOME/.cargo/bin diff --git a/bash/prompt.bash b/bash/prompt.sh similarity index 95% rename from bash/prompt.bash rename to bash/prompt.sh index 0b9c38e..5c33f71 100644 --- a/bash/prompt.bash +++ b/bash/prompt.sh @@ -1,9 +1,8 @@ -GIT="/usr/local/bin/git" +GIT="/sbin/git" function __bash_prompt { local baseDir=$(__bash_basedir) - __iterm_set_bg_color_by_dir $baseDir - __iterm_set_title $baseDir + __window_title $baseDir # Define building blocks local basedir="$_BOLD$_BLUE\W$_RESET_COLOR$_RESET" diff --git a/bash/gover.bash b/bash/tools/gover.sh similarity index 55% rename from bash/gover.bash rename to bash/tools/gover.sh index 5303719..6f5fab4 100644 --- a/bash/gover.bash +++ b/bash/tools/gover.sh @@ -3,25 +3,30 @@ # Step 2: mv ~/Downloads/go ~/go/versions/x.y.z # GOROOT=$HOME/go/lang # GOPATH=$HOME/go/path -function gover { ln -sfn $HOME/go/versions/$1 $HOME/go/lang; } + +platform=linux-amd64 + +function gover { + [[ "$1" == "" ]] && go version && return + # TODO: Check it exists + ln -sfn $HOME/go/versions/$1 $HOME/go/lang +} function _gover { __bash_directory_complete $HOME/go/versions; } complete -F _gover gover function godl { - wget https://dl.google.com/go/go$1.darwin-amd64.tar.gz -O /tmp/go$1.darwin-amd64.tar.gz + wget https://dl.google.com/go/go$1.$platform.tar.gz -O /tmp/go$1.$platform.tar.gz mkdir /tmp/go$1 - tar -xzf /tmp/go$1.darwin-amd64.tar.gz -C /tmp/go$1 + tar -xzf /tmp/go$1.$platform.tar.gz -C /tmp/go$1 mv /tmp/go$1/go $HOME/go/versions/$1 rmdir /tmp/go$1 } function __gover_download_list { curl -s https://golang.org/dl/ \ - | grep "https://dl.google.com" \ + | grep "/dl/go" \ | cut -d '"' -f6 \ - | cut -d/ -f5 \ - | cut -d. -f1-3 \ - | cut -c3- \ - | grep -e '[0-9]\.[0-9]*\.[0-9]' \ + | cut -d/ -f3 \ + | sed -n -e 's/^go\([0-9]\{1,\}\.[0-9]\{1,\}\(\.[0-9]\{1,\}\)\?\).*/\1/p' \ | sort -Vur } function _godl { diff --git a/bash/tools/myc.sh b/bash/tools/myc.sh new file mode 100644 index 0000000..2a4e44b --- /dev/null +++ b/bash/tools/myc.sh @@ -0,0 +1,68 @@ +# +# MySQL CLI manager +# + +# Requirements: mycli, KDE Wallet, jq + +export MYCLI_HISTFILE=$HOME/.config/mycli/history + +# XXX: Extend this function with custom definitions! +# Override the function (and completions) in dotfiles/secrets/myc.bash +function myc { + case $1 in + e | example ) + __myc_connect test example + ;; + * ) + echo "Usage: myc [serverid]" + ;; + esac +} + +# NOTE: Completions are based on function definition +complete -W "$(type myc | grep "|" | sed "s/)//" | sed "s/^[ \t]*//" | sed "s/ | / /" | tr '\n' ' ')" myc + +function __myc_connect { + envname=$1 + dbname=$2 + + __window_title "MySQL: $dbname @ $envname" + __myc_banner $envname $dbname + + creds=$(__myc_creds "$envname-$dbname") + [[ -z "$creds" ]] && echo "Missing credentials!" && return 1 + + host=$(echo $creds | jq -r .host) + user=$(echo $creds | jq -r .user) + pass=$(echo $creds | jq -r .pass) + defaultdb=$(echo $creds | jq -r .db) + + mycli -h $host -u $user -p $pass $defaultdb +} + +function __myc_creds { + wallet=kdewallet + folder=MySQL + entry=$1 + + kwallet-query -f $folder -r $entry $wallet | grep -v "Failed to read entry" +} + +function __myc_banner { + envname=$1 + db=$2 + echo -n "Connecting to ${_BOLD}${db}${_RESET} @ ${_BOLD}" + case $envname in + testing ) + echo -n $_YELLOW + ;; + production ) + echo -n $_RED + ;; + localhost ) + echo -n $_BLUE + ;; + esac + echo "${envname}${_RESET}" +} +