fix: .local/bin -> bin to avoid user pkg managers
This commit is contained in:
parent
87549c183f
commit
8f364ce723
8 changed files with 1 additions and 1 deletions
116
bin/dkprune
Executable file
116
bin/dkprune
Executable file
|
@ -0,0 +1,116 @@
|
|||
#!/bin/bash
|
||||
|
||||
function usage() {
|
||||
cat << EOF
|
||||
Usage: $0 [-hcCviIna]
|
||||
Prune Docker resources.
|
||||
If no option provided, defaults to -Cvn
|
||||
Options:
|
||||
-h Show this help and exits
|
||||
-c Remove stopped containers
|
||||
-C Remove all containers
|
||||
-v Remove unused volumes
|
||||
-i Remove build images
|
||||
-I Remove unused images
|
||||
-n Remove unused networks
|
||||
-a All of the above, equivalent of -CvIn
|
||||
EOF
|
||||
exit 0
|
||||
}
|
||||
|
||||
function add_opt() {
|
||||
local RESULT
|
||||
|
||||
RESULT="$1"
|
||||
for o in "$@"; do
|
||||
if [ "$((RESULT & o))" == 0 ]; then
|
||||
RESULT="$((RESULT + o))"
|
||||
fi
|
||||
done
|
||||
echo "$RESULT"
|
||||
}
|
||||
|
||||
if ! [ -w '/var/run/docker.sock' ] && [ "$EUID" != 0 ]; then
|
||||
exec sudo -- "$0" "$@"
|
||||
fi
|
||||
|
||||
c=1
|
||||
# C=2
|
||||
v=4
|
||||
i=8
|
||||
# I=16
|
||||
n=32
|
||||
# a="$((c + C + v + I + n))"
|
||||
|
||||
CLEAN=0
|
||||
while getopts hcCviIna o; do
|
||||
case "$o" in
|
||||
h)
|
||||
usage
|
||||
;;
|
||||
c)
|
||||
CLEAN="$(add_opt "$CLEAN" "$c")"
|
||||
;;
|
||||
C)
|
||||
CLEAN="$(add_opt "$CLEAN" "$c")"
|
||||
CON_OPT="-f"
|
||||
;;
|
||||
v)
|
||||
CLEAN="$(add_opt "$CLEAN" "$v")"
|
||||
;;
|
||||
i)
|
||||
CLEAN="$(add_opt "$CLEAN" "$i")"
|
||||
;;
|
||||
I)
|
||||
CLEAN="$(add_opt "$CLEAN" "$i")"
|
||||
IMG_OPT='-a'
|
||||
;;
|
||||
n)
|
||||
CLEAN="$(add_opt "$CLEAN" "$n")"
|
||||
;;
|
||||
a)
|
||||
CLEAN="$(add_opt "$CLEAN" "$c" "$v" "$i" "$n")"
|
||||
CON_OPT="-f"
|
||||
IMG_OPT='-a'
|
||||
;;
|
||||
*)
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$CLEAN" == 0 ]; then
|
||||
CLEAN="$((c + v + n))"
|
||||
CON_OPT="-f"
|
||||
fi
|
||||
|
||||
VALUES=(
|
||||
"$((c + i + v + n))"
|
||||
"$((c + i + n))"
|
||||
"$v"
|
||||
"$i"
|
||||
"$n"
|
||||
)
|
||||
|
||||
CON='container'
|
||||
PRUNE='prune -f'
|
||||
COMMANDS=(
|
||||
"docker system $PRUNE $IMG_OPT --volumes"
|
||||
"docker system $PRUNE $IMG_OPT"
|
||||
"docker volume $PRUNE"
|
||||
"docker image $PRUNE $IMG_OPT"
|
||||
"docker network $PRUNE"
|
||||
)
|
||||
|
||||
if [ "$((CLEAN & "$c"))" == "$c" ]; then
|
||||
docker "$CON" ls -aq | xargs docker "$CON" rm "$CON_OPT" 2> /dev/null
|
||||
fi
|
||||
|
||||
for idx in "${!VALUES[@]}"; do
|
||||
mask="${VALUES[$idx]}"
|
||||
|
||||
if [ "$((CLEAN & mask))" == "$mask" ]; then
|
||||
bash -c "${COMMANDS[$idx]}"
|
||||
CLEAN="$((CLEAN - mask))"
|
||||
fi
|
||||
done
|
Loading…
Add table
Add a link
Reference in a new issue