dotfiles/roles/shell-dotfiles/files/functions

344 lines
8.4 KiB
Plaintext

# 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
}
# Un bucle infinito facilitado
function always(){
while true;
do
"$@"
done
}
# 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
}
# Check that the directories of the main directory are updated git repos
function git_updated() {
local ROOT=`pwd`
local gitstatus
for dir in `ls --color=none | grep -v Archive`
do
if [[ -d $dir ]]
then
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
fi
done
}
# 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
## Git stuff
# 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/ .*//")
}
# 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 - 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"
}
# 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/ .*//")
}
# Clean docker
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)
docker system prune -f
}
# warn_me about stuff at any time
function warn_me (){
echo "notify-send --urgency=critical \"$1\"" | at $2
}
# Kubectl functions
function kube_dump_logs(){
local deploy="$1"
local namespace="$2"
local time="$3"
if [[ -z "$deploy" ]]
then
echo "You must pass a kubernetes deployment as an argument."
return 1
fi
if [[ -z "$time" ]]
then
time=1h
fi
if [[ -z "$namespace" ]]
then
namespace_param="-n default"
else
namespace_param="-n $namespace"
fi
mkdir -p "$deploy"
for pod in $(kube_get_deploy_pods $deploy $namespace)
do
echo Dumping "$pod" ...
eval kubectl logs "$pod" --since="$time" "$namespace_param" > "$deploy"/"$pod"
done
echo "Dumped $deploy logs."
}
function kube_get_deploy_pods(){
local deploy="$1"
local namespace="$2"
if [[ -z "$deploy" ]]
then
echo "You must pass a kubernetes deployment as an argument."
return 1
fi
if [[ -z "$namespace" ]]
then
namespace="-n default"
else
namespace="-n $namespace"
fi
eval kubectl get pods $namespace | grep "$deploy" | cut -d ' ' -f1
}
# Entra en un contenedor k8s
function kenter(){
if [[ -z $4 ]]
then
command=bash
else
command="$4"
fi
kubectl exec -ti $1 $2 $3 $command
}
# 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
mv /tmp/kubectl ~/Scripts/bin/kubectl-$version
ln -fs ~/Scripts/bin/kubectl-$version ~/Scripts/bin/kubectl
echo "kubectl version: $version"
}
# Actualiza kops
function upgrade_kops (){
echo "Downloading kops..."
uri=$(curl -s https://github.com/kubernetes/kops/releases/latest -L | grep "kops-linux-amd64" | grep -v strong | cut -d '"' -f2 | head -n1)
version=$(echo $uri | cut -d'/' -f6)
curl -s -L https://github.com/$uri -o /tmp/kops
chmod +x /tmp/kops
mv /tmp/kops ~/Scripts/bin/kops-$version
ln -fs ~/Scripts/bin/kops-$version ~/Scripts/bin/kops
echo "kops version: $version"
}
# Docker
## Reveal
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
}
## Tox
function docker-tox () {
docker run -ti -v `pwd`:/tox registry.daemons.it/tox:latest tox "$@"
}
# Autocompletados
## kubectl
KUBECTL_BIN=`which kubectl`
function kubectl (){
if [[ -z $KUBECTL_SHOW_CONTEXT ]]
then
source <($KUBECTL_BIN completion zsh)
fi
KUBECTL_SHOW_CONTEXT="k8s-$($KUBECTL_BIN config current-context)"
$KUBECTL_BIN "$@"
}
function ku (){
if [[ -z $KUBECTL_SHOW_CONTEXT ]]
then
source <($KUBECTL_BIN completion zsh | sed "s/kubectl/ku/g")
fi
KUBECTL_SHOW_CONTEXT="k8s-$($KUBECTL_BIN config current-context)"
$KUBECTL_BIN "$@"
}
## kops
KOPS_BIN=`which kops`
function kops(){
if [[ -z $KOPS_SHOW_CONTEXT ]]
then
source <($KOPS_BIN completion zsh)
fi
KOPS_SHOW_CONTEXT="k8s-$($KOPS_BIN config current-context)"
$KOPS_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 "$@"
}