2017-10-24 21:46:26 +02:00
|
|
|
# Usar una basura en vez de borrar sin mas
|
|
|
|
function rm() {
|
|
|
|
DIR=`date "+%y%m%dT%H%M%s"`
|
|
|
|
mkdir -p "$HOME/.basura/$DIR"
|
|
|
|
mv "$@" "$HOME/.basura/$DIR"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Busca en la basura
|
|
|
|
function busca() {
|
|
|
|
find ~/.basura/ -name "*$@*"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Sube cosas para compartir a mi servidor
|
|
|
|
function upload_stuff() {
|
|
|
|
for i in $@
|
|
|
|
do
|
|
|
|
rsync -P -e ssh $i drymer@daemons.it:/var/www/blog/mierdas/ > /dev/null
|
|
|
|
echo "https://daemons.it/mierdas/$i"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2018-02-04 22:05:09 +01:00
|
|
|
# Un bucle infinito facilitado
|
|
|
|
function always(){
|
|
|
|
while true;
|
|
|
|
do
|
|
|
|
"$@"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-10-24 21:46:26 +02:00
|
|
|
# Actualiza kubectl
|
|
|
|
function upgrade_kubectl (){
|
|
|
|
echo "Downloading kubectl..."
|
|
|
|
version=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt);
|
|
|
|
curl -s -L https://storage.googleapis.com/kubernetes-release/release/$version/bin/linux/amd64/kubectl -o /tmp/kubectl
|
|
|
|
chmod +x /tmp/kubectl
|
2017-12-16 00:46:51 +01:00
|
|
|
mv /tmp/kubectl ~/Scripts/bin/kubectl-$version
|
|
|
|
ln -fs ~/Scripts/bin/kubectl-$version ~/Scripts/bin/kubectl
|
2017-10-24 21:46:26 +02:00
|
|
|
echo "kubectl version: $version"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Actualiza kops
|
|
|
|
function upgrade_kops (){
|
|
|
|
echo "Downloading kops..."
|
2018-02-04 22:05:09 +01:00
|
|
|
uri=$(curl -s https://github.com/kubernetes/kops/releases/latest -L | grep "kops-linux-amd64" | grep -v strong | cut -d '"' -f2 | head -n1)
|
2017-10-24 21:46:26 +02:00
|
|
|
version=$(echo $uri | cut -d'/' -f6)
|
|
|
|
curl -s -L https://github.com/$uri -o /tmp/kops
|
|
|
|
chmod +x /tmp/kops
|
2017-12-16 00:46:51 +01:00
|
|
|
mv /tmp/kops ~/Scripts/bin/kops-$version
|
|
|
|
ln -fs ~/Scripts/bin/kops-$version ~/Scripts/bin/kops
|
2017-10-24 21:46:26 +02:00
|
|
|
echo "kops version: $version"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Limpiar ramas mergeadas
|
|
|
|
function delete_old_branches(){
|
|
|
|
git checkout master
|
|
|
|
for branch in `git branch --merged | grep -v master`
|
|
|
|
do
|
|
|
|
git branch -d $branch
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Entra en un contenedor docker
|
|
|
|
function enter(){
|
|
|
|
if [[ -z $2 ]]
|
|
|
|
then
|
|
|
|
command=bash
|
|
|
|
else
|
|
|
|
command=$4
|
|
|
|
fi
|
|
|
|
docker exec -ti $1 $command
|
|
|
|
}
|
|
|
|
|
|
|
|
# Entra en un contenedor k8s
|
|
|
|
function kenter(){
|
|
|
|
if [[ -z $4 ]]
|
|
|
|
then
|
|
|
|
command=bash
|
|
|
|
else
|
|
|
|
command="$4"
|
|
|
|
fi
|
|
|
|
kubectl exec -ti $1 $2 $3 $command
|
|
|
|
}
|
2017-12-16 00:46:51 +01:00
|
|
|
|
2018-06-10 21:30:58 +02:00
|
|
|
# Check that the directories of the main directory are updated git repos
|
2017-12-16 00:46:51 +01:00
|
|
|
function git_updated() {
|
2018-06-10 21:30:58 +02:00
|
|
|
local ROOT=`pwd`
|
|
|
|
local gitstatus
|
|
|
|
|
|
|
|
for dir in `ls --color=none | grep -v Archive`
|
2017-12-16 00:46:51 +01:00
|
|
|
do
|
2018-06-10 21:30:58 +02:00
|
|
|
if [[ -d $dir ]]
|
2017-12-16 00:46:51 +01:00
|
|
|
then
|
2018-06-10 21:30:58 +02:00
|
|
|
cd $dir
|
|
|
|
if [[ -d .git ]]
|
|
|
|
then
|
|
|
|
gitstatus="`git status -s | awk '{print $2}'`"
|
|
|
|
cd $ROOT
|
|
|
|
if [[ -n $gitstatus ]]
|
|
|
|
then
|
|
|
|
echo "- \033[0;31m$dir\033[0m:\n$gitstatus"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# Recursion at it's finest
|
|
|
|
git_updated
|
|
|
|
cd $ROOT
|
|
|
|
fi
|
2017-12-16 00:46:51 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Docker presentaciones
|
|
|
|
function reveal () {
|
|
|
|
function async ()
|
|
|
|
{
|
|
|
|
sleep 3 && xdg-open http:localhost:8000
|
|
|
|
}
|
|
|
|
async &!
|
|
|
|
docker run -ti --name revealjs --rm -v `pwd`:/revealjs/files/ -p 8000:8000 revealjs
|
|
|
|
}
|
|
|
|
|
|
|
|
# Autocompletados
|
|
|
|
## kubectl
|
|
|
|
KUBECTL_BIN=`which kubectl`
|
|
|
|
function kubectl (){
|
|
|
|
if [[ -z $KUBECTL_SHOW_CONTEXT ]]
|
|
|
|
then
|
|
|
|
source <($KUBECTL_BIN completion zsh)
|
|
|
|
source <(kops completion zsh)
|
|
|
|
fi
|
|
|
|
KUBECTL_SHOW_CONTEXT="k8s-$($KUBECTL_BIN config current-context)"
|
|
|
|
$KUBECTL_BIN "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
## molecule
|
|
|
|
MOLECULE_BIN=`which molecule`
|
|
|
|
function molecule() {
|
|
|
|
if [[ -z $MOLECULE_SET ]]
|
|
|
|
then
|
|
|
|
MOLECULE_SET=True
|
|
|
|
eval "$(_MOLECULE_COMPLETE=source molecule)"
|
|
|
|
fi
|
|
|
|
$MOLECULE_BIN "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
## helm
|
|
|
|
HELM_BIN=`which helm`
|
|
|
|
function helm (){
|
|
|
|
if [[ -z $HELM_SHOW_CONTEXT ]]
|
|
|
|
then
|
|
|
|
source <($HELM_BIN completion zsh)
|
|
|
|
fi
|
|
|
|
$HELM_BIN "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# fzf functions
|
|
|
|
## insert located file
|
|
|
|
fzf-locate-widget() {
|
|
|
|
local selected
|
|
|
|
if selected=$(locate / | fzf -q "$LBUFFER"); then
|
|
|
|
LBUFFER=$selected
|
|
|
|
fi
|
|
|
|
zle redisplay
|
|
|
|
}
|
|
|
|
zle -N fzf-locate-widget
|
|
|
|
bindkey '\ei' fzf-locate-widget
|
|
|
|
|
|
|
|
## Docker stuff: https://medium.com/@calbertts/docker-and-fuzzy-finder-fzf-4c6416f5e0b5
|
|
|
|
runc() {
|
|
|
|
export FZF_DEFAULT_OPTS='--height 90% --reverse --border'
|
|
|
|
local image=$(docker images --format '{{.Repository}}:{{.Tag}}' | fzf-tmux --reverse --multi)
|
|
|
|
if [[ $image != '' ]]; then
|
|
|
|
echo -e "\n \033[1mDocker image:\033[0m" $image
|
2018-05-06 20:37:32 +02:00
|
|
|
read -rs $'opt? \e[1mOptions: \e[0m'
|
|
|
|
# TODO: No lo coge con -z aun estando vacio
|
|
|
|
if [[ -z $opt ]]
|
|
|
|
then
|
|
|
|
opt="-it --rm"
|
|
|
|
fi
|
2017-12-16 00:46:51 +01:00
|
|
|
printf " \033[1mChoose the command: \033[0m"
|
|
|
|
local cmd=$(echo -e "/bin/bash\nsh" | fzf-tmux --reverse --multi)
|
|
|
|
if [[ $cmd == '' ]]; then
|
|
|
|
read -e -p $' \e[1mCustom command: \e[0m' cmd
|
|
|
|
fi
|
|
|
|
echo -e " \033[1mCommand: \033[0m" $cmd
|
|
|
|
|
|
|
|
export FZF_DEFAULT_COMMAND='find ./ -type d -maxdepth 1 -exec basename {} \;'
|
|
|
|
printf " \033[1mChoose the volume: \033[0m"
|
|
|
|
local volume=$(fzf-tmux --reverse --multi)
|
|
|
|
local curDir=${PWD##*/}
|
|
|
|
if [[ $volume == '.' ]]; then
|
|
|
|
echo -e " \033[1mVolume: \033[0m" $volume
|
|
|
|
volume="`pwd`:/$curDir -w /$curDir"
|
|
|
|
else
|
|
|
|
echo -e " \033[1mVolume: \033[0m" $volume
|
|
|
|
volume="`pwd`/$volume:/$volume -w /$volume"
|
|
|
|
fi
|
|
|
|
|
|
|
|
export FZF_DEFAULT_COMMAND=""
|
|
|
|
export FZF_DEFAULT_OPTS=""
|
|
|
|
|
|
|
|
history -s runc
|
2018-05-06 20:37:32 +02:00
|
|
|
history -s docker run $opt -v $volume $image $cmd
|
2017-12-16 00:46:51 +01:00
|
|
|
echo ''
|
|
|
|
docker run $options -v $volume $image $cmd
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
stopc() {
|
|
|
|
export FZF_DEFAULT_OPTS='--height 90% --reverse --border'
|
|
|
|
local container=$(docker ps --format '{{.Names}} => {{.Image}}' | fzf-tmux --reverse --multi | awk -F '\\=>' '{print $1}')
|
|
|
|
if [[ $container != '' ]]; then
|
|
|
|
echo -e "\n \033[1mDocker container:\033[0m" $container
|
|
|
|
printf " \033[1mRemove?: \033[0m"
|
|
|
|
local cmd=$(echo -e "No\nYes" | fzf-tmux --reverse --multi)
|
|
|
|
if [[ $cmd != '' ]]; then
|
|
|
|
if [[ $cmd == 'No' ]]; then
|
|
|
|
echo -e "\n Stopping $container ...\n"
|
|
|
|
history -s stopc
|
|
|
|
history -s docker stop $container
|
|
|
|
docker stop $container > /dev/null
|
|
|
|
else
|
|
|
|
echo -e "\n Stopping $container ..."
|
|
|
|
history -s stopc
|
|
|
|
history -s docker stop $container
|
|
|
|
docker stop $container > /dev/null
|
|
|
|
|
|
|
|
echo -e " Removing $container ...\n"
|
|
|
|
history -s stopc
|
|
|
|
history -s docker rm $container
|
|
|
|
docker rm $container > /dev/null
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
export FZF_DEFAULT_OPTS=""
|
|
|
|
}
|
|
|
|
|
|
|
|
runinc() {
|
|
|
|
export FZF_DEFAULT_OPTS='--height 90% --reverse --border'
|
|
|
|
local container=$(docker ps --format '{{.Names}} => {{.Image}}' | fzf-tmux --reverse --multi | awk -F '\\=>' '{print $1}')
|
|
|
|
if [[ $container != '' ]]; then
|
|
|
|
echo -e "\n \033[1mDocker container:\033[0m" $container
|
|
|
|
read -e -p $' \e[1mOptions: \e[0m' -i "-it" options
|
|
|
|
if [[ $@ == '' ]]; then
|
|
|
|
read -e -p $' \e[1mCommand: \e[0m' cmd
|
|
|
|
else
|
|
|
|
cmd="$@"
|
|
|
|
fi
|
|
|
|
echo ''
|
|
|
|
history -s runinc "$@"
|
|
|
|
history -s docker exec $options $container $cmd
|
|
|
|
docker exec $options $container $cmd
|
|
|
|
echo ''
|
|
|
|
fi
|
|
|
|
export FZF_DEFAULT_OPTS=""
|
|
|
|
}
|
|
|
|
|
|
|
|
showipc() {
|
|
|
|
export FZF_DEFAULT_OPTS='--height 90% --reverse --border'
|
|
|
|
local container=$(docker ps -a --format '{{.Names}} => {{.Image}}' | fzf-tmux --reverse --multi | awk -F '\\=>' '{print $1}')
|
|
|
|
|
|
|
|
if [[ $container != '' ]]; then
|
|
|
|
local network=$(docker inspect $container -f '{{.NetworkSettings.Networks}}' | awk -F 'map\\[|:' '{print $2}')
|
|
|
|
echo -e "\n \033[1mDocker container:\033[0m" $container
|
|
|
|
history -s showipc
|
|
|
|
history -s docker inspect -f "{{.NetworkSettings.Networks.${network}.IPAddress}}" $container
|
|
|
|
echo -e " \033[1mNetwork:\033[0m" $network
|
|
|
|
echo -e " \033[1mIP Address:\033[0m" $(docker inspect -f "{{.NetworkSettings.Networks.${network}.IPAddress}}" $container) "\n"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-05-06 20:37:32 +02:00
|
|
|
## Git stuff
|
|
|
|
# fbr - checkout git branch (including remote branches), sorted by most recent commit, limit 30 last branches
|
|
|
|
unalias gbr
|
|
|
|
gbr() {
|
|
|
|
local branches branch
|
|
|
|
branches=$(git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format="%(refname:short)") &&
|
|
|
|
branch=$(echo "$branches" |
|
|
|
|
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
|
|
|
|
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
|
|
|
|
}
|
|
|
|
|
|
|
|
# fco - checkout git branch/tag
|
|
|
|
unalias gco
|
|
|
|
gco() {
|
|
|
|
local tags branches target
|
|
|
|
tags=$(
|
|
|
|
git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
|
|
|
|
branches=$(
|
|
|
|
git branch --all | grep -v HEAD |
|
|
|
|
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
|
|
|
|
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
|
|
|
|
target=$(
|
|
|
|
(echo "$tags"; echo "$branches") |
|
|
|
|
fzf-tmux -l30 -- --no-hscroll --ansi +m -d "\t" -n 2) || return
|
|
|
|
git checkout $(echo "$target" | awk '{print $2}')
|
|
|
|
}
|
|
|
|
|
|
|
|
# fco_preview - checkout git branch/tag, with a preview showing the commits between the tag/branch and HEAD
|
|
|
|
gco_preview() {
|
|
|
|
local tags branches target
|
|
|
|
tags=$(
|
|
|
|
git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
|
|
|
|
branches=$(
|
|
|
|
git branch --all | grep -v HEAD |
|
|
|
|
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
|
|
|
|
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
|
|
|
|
target=$(
|
|
|
|
(echo "$tags"; echo "$branches") |
|
|
|
|
fzf --no-hscroll --no-multi --delimiter="\t" -n 2 \
|
|
|
|
--ansi --preview="git log -200 --pretty=format:%s $(echo {+2..} | sed 's/$/../' )" ) || return
|
|
|
|
git checkout $(echo "$target" | awk '{print $2}')
|
|
|
|
}
|
|
|
|
|
|
|
|
# fcoc - checkout git commit
|
|
|
|
fcoc() {
|
|
|
|
local commits commit
|
|
|
|
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
|
|
|
|
commit=$(echo "$commits" | fzf --tac +s +m -e) &&
|
|
|
|
git checkout $(echo "$commit" | sed "s/ .*//")
|
|
|
|
}
|
|
|
|
|
|
|
|
# fshow - git commit browser
|
|
|
|
fshow() {
|
|
|
|
git log --graph --color=always \
|
|
|
|
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
|
|
|
|
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
|
|
|
|
--bind "ctrl-m:execute:
|
|
|
|
(grep -o '[a-f0-9]\{7\}' | head -1 |
|
|
|
|
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
|
|
|
|
{}
|
|
|
|
FZF-EOF"
|
|
|
|
}
|
2017-12-16 00:46:51 +01:00
|
|
|
|
2018-05-06 20:37:32 +02:00
|
|
|
alias glNoGraph='git log --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr% C(auto)%an" "$@"'
|
|
|
|
local _gitLogLineToHash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
|
|
|
|
local _viewGitLogLine="$_gitLogLineToHash | xargs -I % sh -c 'git show --color=always % | diff-so-fancy'"
|
|
|
|
|
|
|
|
# fcoc_preview - checkout git commit with previews
|
|
|
|
fcoc_preview() {
|
|
|
|
local commit
|
|
|
|
commit=$( glNoGraph |
|
|
|
|
fzf --no-sort --reverse --tiebreak=index --no-multi \
|
|
|
|
--ansi --preview $_viewGitLogLine ) &&
|
|
|
|
git checkout $(echo "$commit" | sed "s/ .*//")
|
|
|
|
}
|
|
|
|
|
|
|
|
# fshow_preview - git commit browser with previews
|
|
|
|
fshow_preview() {
|
|
|
|
glNoGraph |
|
|
|
|
fzf --no-sort --reverse --tiebreak=index --no-multi \
|
|
|
|
--ansi --preview $_viewGitLogLine \
|
|
|
|
--header "enter to view, alt-y to copy hash" \
|
|
|
|
--bind "enter:execute:$_viewGitLogLine | less -R" \
|
|
|
|
--bind "alt-y:execute:$_gitLogLineToHash | xclip"
|
|
|
|
}
|
|
|
|
|
|
|
|
# fcs - get git commit sha
|
|
|
|
# example usage: git rebase -i `fcs`
|
|
|
|
fcs() {
|
|
|
|
local commits commit
|
|
|
|
commits=$(git log --color=always --pretty=oneline --abbrev-commit --reverse) &&
|
|
|
|
commit=$(echo "$commits" | fzf --tac +s +m -e --ansi --reverse) &&
|
|
|
|
echo -n $(echo "$commit" | sed "s/ .*//")
|
|
|
|
}
|
|
|
|
|
|
|
|
# fuzzy grep open via rg
|
|
|
|
vg() {
|
|
|
|
local file
|
|
|
|
|
|
|
|
file="$(ag --nobreak --noheading $@ | fzf -0 -1 | awk -F: '{print $1 " +" $2}')"
|
|
|
|
|
|
|
|
if [[ -n $file ]]
|
|
|
|
then
|
|
|
|
vim $file
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# fd - cd to selected directory
|
|
|
|
fd() {
|
|
|
|
local dir
|
|
|
|
dir=$(find ${1:-.} -path '*/\.*' -prune \
|
|
|
|
-o -type d -print 2> /dev/null | fzf +m) &&
|
|
|
|
cd "$dir"
|
|
|
|
}
|
|
|
|
# like normal z when used with arguments but displays an fzf prompt when used without.
|
|
|
|
unalias z 2> /dev/null
|
|
|
|
z() {
|
|
|
|
[ $# -gt 0 ] && _z "$*" && return
|
|
|
|
cd "$(_z -l 2>&1 | fzf --height 40% --nth 2.. --reverse --inline-info +s --tac --query "${*##-* }" | sed 's/^[0-9,.]* *//')"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Clean docker
|
2017-12-16 00:46:51 +01:00
|
|
|
function docker_clean(){
|
|
|
|
echo "Deleting stopped containers..."
|
|
|
|
docker rm $(docker ps -a -q | awk '{print $1}')
|
|
|
|
echo "Deleting volumes..."
|
|
|
|
docker volume rm $(docker volume ls)
|
|
|
|
echo "Deleting none images..."
|
|
|
|
docker rmi $(docker images -f "dangling=true" -q)
|
|
|
|
}
|