diff --git a/shell_dotfiles/_aliases b/shell_dotfiles/_aliases index 9104156..743d53f 100644 --- a/shell_dotfiles/_aliases +++ b/shell_dotfiles/_aliases @@ -57,4 +57,3 @@ alias egrep='egrep --color=auto' alias dlyoutube="youtube-dl -f bestaudio --audio-quality 0 --audio-format aac " alias bat='upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage|capacity"' - diff --git a/shell_dotfiles/_functions b/shell_dotfiles/_functions index 847a1d8..344407f 100644 --- a/shell_dotfiles/_functions +++ b/shell_dotfiles/_functions @@ -1,6 +1,186 @@ -function appBoilerplate(){ - git clone -b boilerplates --depth=1 git@gitlab.dev.fsbtech.com:frontend/whitelabel-utils.git boilerplates \ - && cp -r boilerplates/core_app/ ./$1_app || rm -rf boilerplates \ - && rm -rf boilerplates \ - && for i in $1_app/*genericAppName* ; do mv "$i" "${i/genericAppName/$1}" ; done \ - } +#!/usr/bin/env bash + +function docker_cleanup() { + sudo docker rm -v $(sudo docker ps -a -q -f status=exited) + sudo docker rmi -f $(sudo docker images -f "dangling=true" -q) + docker volume ls -qf dangling=true | xargs -r docker volume rm +} + +function cleanup_docker() { + docker ps -f status=exited -q | xargs -r docker rm + docker images -f dangling=true -q | xargs -r docker rmi +} + +function cleanup_docker_aggressive() { + for i in $(docker images --no-trunc -q | sort -u); do + docker rmi $i 2>/dev/null + done +} + +# Determine size of a file or total size of a directory +function fs() { + if du -b /dev/null >/dev/null 2>&1; then + local arg=-sbh + else + local arg=-sh + fi + if [[ -n "$@" ]]; then + du $arg -- "$@" + else + du $arg .[^.]* ./* + fi +} + +# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression +function targz() { + local tmpFile="${@%/}.tar" + tar -cvf "${tmpFile}" --exclude={".DS_Store","node_modules",".idea",".git"} "${@}" || return 1 + + size=$( + stat -f"%z" "${tmpFile}" 2>/dev/null # macOS `stat` + stat -c"%s" "${tmpFile}" 2>/dev/null # GNU `stat` + ) + + local cmd="" + if ((size < 52428800)) && hash zopfli 2>/dev/null; then + # the .tar file is smaller than 50 MB and Zopfli is available; use it + cmd="zopfli" + else + if hash pigz 2>/dev/null; then + cmd="pigz" + else + cmd="gzip" + fi + fi + + echo "Compressing .tar ($((size / 1000)) kB) using \`${cmd}\`…" + "${cmd}" -v "${tmpFile}" || return 1 + [ -f "${tmpFile}" ] && rm "${tmpFile}" + + zippedSize=$( + stat -f"%z" "${tmpFile}.gz" 2>/dev/null # macOS `stat` + stat -c"%s" "${tmpFile}.gz" 2>/dev/null # GNU `stat` + ) + + echo "${tmpFile}.gz ($((zippedSize / 1000)) kB) created successfully." +} + +# Print README file +readme() { + for readme in {readme,README}.{md,MD,markdown,mkd,txt,TXT}; do + if [[ -x "$(command -v glow)" ]] && [[ -f "$readme" ]]; then + mdv "$readme" + elif [[ -f "$readme" ]]; then + cat "$readme" + fi + done +} + +# Get an information of IP address +ip-address() { + curl -s -H "Accept: application/json" "https://ipinfo.io/${1:-}" | jq "del(.loc, .postal, .readme)" +} + +topfiles() { + + du -M --max-depth=1 $1 | sort -n | tail -n 5 + +} + +store() { + + DNOW=$(date '+%F-%H-%M') + + local tmpFile="${@%/}-${DNOW}.tar" + tar -cvf "${tmpFile}" --exclude={".DS_Store","node_modules",".idea",".git"} "${@}" || return 1 + + size=$( + stat -f"%z" "${tmpFile}" 2>/dev/null # macOS `stat` + stat -c"%s" "${tmpFile}" 2>/dev/null # GNU `stat` + ) + + local cmd="" + if ((size < 52428800)) && hash zopfli 2>/dev/null; then + # the .tar file is smaller than 50 MB and Zopfli is available; use it + cmd="zopfli" + else + if hash pigz 2>/dev/null; then + cmd="pigz" + else + cmd="gzip" + fi + fi + + echo "Compressing .tar ($((size / 1000)) kB) using \`${cmd}\`…" + "${cmd}" -9 -v "${tmpFile}" || return 1 + [ -f "${tmpFile}" ] && rm "${tmpFile}" + + zippedSize=$( + stat -f"%z" "${tmpFile}.gz" 2>/dev/null # macOS `stat` + stat -c"%s" "${tmpFile}.gz" 2>/dev/null # GNU `stat` + ) + + echo "Used ${cmd} to compress" + echo "${tmpFile}.gz ($((zippedSize / 1000)) kB) created successfully." + +} + +wip() { + + WORK_DIR="/home/martin/dev/wip" + + echo "WIP Update:${@%}" + + DEST="$WORK_DIR/${@%}" + + echo $DEST + + mkdir -p $DEST + + # mkdir ./wip/${@%} + rsync -auzvh --itemize-changes --progress --delete --delete-excluded --ignore-errors --exclude 'node_modules' --exclude 'bower_components' --exclude '.git' --exclude '.tmp' ${@%}/ $DEST/ + + echo Compress $DEST/ + + store $DEST/ + + # mv $WORK_DIR/wip/*.tar.gz $HOME/dev/wip + +} + +gitdiff() { + + line=$(git log $1^..$2 --pretty=format:%s) + + SAVEIFS=$IFS # Save current IFS + IFS=$'\n' # Change IFS to new line + names=($line) # split to array $names + IFS=$SAVEIFS # Restore IFS + + testseq="Merge branch" + + # for (( i=0; i<${#names[@]}; i++ )) + # do + # echo "${names[$i]}" + # done + + for s in "${names[@]}"; do + + if ! [[ $s =~ $testseq ]]; then + # echo $s + parsed='\'$s + result=$(git log --all --grep="${parsed}" --pretty=format:"%h") + + echo $parsed + + for COMMIT in $result; do + echo "git $COMMIT" + ### echo $(git branch --contains $COMMIT --color "$3" | sed 's/[*]\+//g' ) + done + + echo "---" + fi + + done + +} diff --git a/shell_dotfiles/_gitconfig b/shell_dotfiles/_gitconfig index 589d69d..473f8fb 100644 --- a/shell_dotfiles/_gitconfig +++ b/shell_dotfiles/_gitconfig @@ -1,4 +1,9 @@ [alias] + # tidy up a dirty branch + + # cu = !"git stash --include-untracked" + + cu = !"git clean -df; git checkout --" # Better update than just git pull up = !"git remote update -p; git merge --ff-only @{u}" @@ -105,6 +110,7 @@ editor = 'nano' autocrlf = input + excludefile = /home/martin/.gitignore [color] branch = always @@ -197,4 +203,6 @@ insteadOf = "gist:" [user] name = Martin Donnelly - email = martin@fsbtech.com + email = martin.donnelly@beeksgroup.com +[credential] + helper = cache --timeout=7200 diff --git a/shell_dotfiles/_gitignore b/shell_dotfiles/_gitignore new file mode 100644 index 0000000..4a2cf64 --- /dev/null +++ b/shell_dotfiles/_gitignore @@ -0,0 +1,4 @@ +.DS_Store +.vscode +.idea +