187 lines
4.2 KiB
Bash
187 lines
4.2 KiB
Bash
#!/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
|
|
|
|
}
|