From 90eaebabcaaea74237f34cf05709625345f276cc Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 12 May 2022 12:01:54 -0300 Subject: Move Git repository into ~/.usr/.git/ --- bin/gc | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 bin/gc (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc new file mode 100755 index 0000000..ec6a8f1 --- /dev/null +++ b/bin/gc @@ -0,0 +1,105 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + gc + gc -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -h, --help show this message + EOF +} + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + +disk() { + df -h . | + awk 'NR == 2 { printf "%s - %s/%s\n", $4, $3, $2 }' +} + + +gc_guix() { + pass show velhinho/0-andreh-password | sudo -S guix system delete-generations + guix home delete-generations + guix gc -d +} + +gc_nohup() { + find ~/ -type f -name 'nohup.out' -exec rm -vf "{}" \; +} + +gc_trash() { + yes | trash-empty +} + +gc_tmpdir() { + find /tmp/ -type f -atime +10 -exec rm -vf "{}" \; ||: +} + +gc_docker() { + if command -v docker; then + yes | docker system prune -a + docker rmi "$(docker images -a -q)" ||: + docker rm "$(docker ps -a -f status=exited -q)" ||: + docker stop "$(docker ps -a -q)" ||: + docker rm "$(docker ps -a -q)" ||: + yes | docker volume prune + yes | docker container prune + fi +} + +gc_email() { + notmuch search --output=files --exclude=false tag:killed | + xargs -I{} rm -vf "{}" +} + + +BEFORE="$(disk)" +gc_guix +gc_nohup +gc_trash +gc_tmpdir +gc_docker +gc_email +wait +AFTER="$(disk)" + +printf 'Disk space:\n' +printf ' before: %s\n' "$BEFORE" +printf ' after: %s\n' "$AFTER" -- cgit v1.2.3 From a2324725adab4fda610d39eb21d8d28bdf8f35ba Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 13 Aug 2022 13:17:45 -0300 Subject: bin/: Add examples to help strings --- bin/gc | 10 ++++++++++ bin/mkdtemp | 40 ++++++++++++++++++++++++++++++++++++++++ bin/mkstemp | 40 ++++++++++++++++++++++++++++++++++++++++ bin/pre | 14 ++++++++++++++ bin/prompt | 9 +++++++++ bin/qr | 13 +++++++++++++ bin/tmp | 15 +++++++++++++++ bin/tmpname | 14 ++++++++++++++ bin/uuid | 9 +++++++++ bin/xmpp | 12 +++++++++++- 10 files changed, 175 insertions(+), 1 deletion(-) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index ec6a8f1..23a0436 100755 --- a/bin/gc +++ b/bin/gc @@ -14,6 +14,16 @@ help() { Options: -h, --help show this message + + + Free disk space system-wide. + + + Examples: + + Just run it: + + $ gc EOF } diff --git a/bin/mkdtemp b/bin/mkdtemp index d012175..3729dd4 100755 --- a/bin/mkdtemp +++ b/bin/mkdtemp @@ -15,10 +15,50 @@ help() { Options: -h, --help show this message + Create a new temporary file and echo its name back. + + + Examples: + + `cd` into temporary directory: + + $ cd "$(mkdtemp)" EOF } + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + name="$(tmpname)" mkdir "$name" echo "$name" diff --git a/bin/mkstemp b/bin/mkstemp index ec92c14..4097e59 100755 --- a/bin/mkstemp +++ b/bin/mkstemp @@ -15,10 +15,50 @@ help() { Options: -h, --help show this message + Create a new temporary file and echo its name back. + + + Examples: + + Capture output into temporary file: + + $ OUT="$(mkstemp)"; cmd > "$OUT" EOF } + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + name="$(tmpname)" touch "$name" echo "$name" diff --git a/bin/pre b/bin/pre index 79c212f..2b32f8f 100755 --- a/bin/pre +++ b/bin/pre @@ -15,6 +15,20 @@ help() { Options: -c COLOR ANSI color to be used on the prefix text -h, --help show this message + + Prefix STDIN with PREFIX. + + + Examples: + + Prefix with 'database': + + $ ./run-db.sh | pre 'database' + + + Prefix with yellow 'numbers': + + $ seq 10 | pre -c yellow numbers EOF } diff --git a/bin/prompt b/bin/prompt index 68dff61..247c81a 100755 --- a/bin/prompt +++ b/bin/prompt @@ -20,6 +20,15 @@ help() { Display a prompt and return a value corresponding to the response. + + + Examples: + + Conditionally run download command + + if prompt 'Download files?'; then + run_download; + fi EOF } diff --git a/bin/qr b/bin/qr index 4821a46..c0462e1 100755 --- a/bin/qr +++ b/bin/qr @@ -16,7 +16,20 @@ help() { -s PIXEL_SIZE size of the pixel (default 10) -h, --help show this help message + Read data from STDIN and present a QR image with said data. + + + Examples: + + Link to my homepage: + + $ printf 'https://euandre.org' | qr + + + Numbers with a smaller pixel size: + + $ seq 99 | qr -s 5 EOF } diff --git a/bin/tmp b/bin/tmp index e2d16b7..2dc0b48 100755 --- a/bin/tmp +++ b/bin/tmp @@ -16,6 +16,21 @@ help() { Options: -d delete the remote "tmp/" folder -h, --help show this message + + + Copies a file to the public server. + + + Examples: + + Copy f.txt: + + $ tmp f.txt + + + Cleanup the $REMOTE: + + $ tmp -d EOF } diff --git a/bin/tmpname b/bin/tmpname index d83fc87..89d7e4d 100755 --- a/bin/tmpname +++ b/bin/tmpname @@ -15,10 +15,24 @@ help() { Options: -h, --help show this message + Generate a temporary name. + + + Examples: + + Create a temporary file: + + $ OUT="$(tmpname)"; touch "$OUT"; cmd > "$OUT" + + + `cd` into a temporary directory: + + $ DIR="$(tmpname)"; mkdir -p "$DIR"; cd "$DIR" EOF } + for flag in "$@"; do case "$flag" in --) diff --git a/bin/uuid b/bin/uuid index 74d0fba..34b685f 100755 --- a/bin/uuid +++ b/bin/uuid @@ -15,7 +15,16 @@ help() { Options: -h, --help show this message + Generate UUID from /dev/random. + + + Examples: + + Generate a UUID: + + $ uuid + 755244c8-f955-16df-75cc-f25600c90422 EOF } diff --git a/bin/xmpp b/bin/xmpp index 472e9ca..b08a783 100755 --- a/bin/xmpp +++ b/bin/xmpp @@ -18,7 +18,17 @@ Options: -h, --help show this message FROM_JID the address used to send the message from - TO_JID the addresses where to send the message to""" + TO_JID the addresses where to send the message to + + +Send a one-off XMPP message. + + +Examples: + + Send a message to eu@euandreh.xyz: + + $ xmpp -m 'Hello, XMPP!' eu@euandreh.xyz""" class SendMsgBot(slixmpp.ClientXMPP): def __init__(self, jid, password, on_start): -- cgit v1.2.3 From 885630d44b51fd6e3dfcacd1d343fab6591d6255 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 13 Aug 2022 13:18:54 -0300 Subject: bin/gc: Fix password input to sudo --- bin/gc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index 23a0436..7fc92ec 100755 --- a/bin/gc +++ b/bin/gc @@ -65,7 +65,7 @@ disk() { gc_guix() { - pass show velhinho/0-andreh-password | sudo -S guix system delete-generations + pass show velhinho/0-andreh-password | head -n1 | sudo -iS guix system delete-generations guix home delete-generations guix gc -d } -- cgit v1.2.3 From c78a590ccbd01d22e9a6c43e4902e502f3597a2e Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 13 Aug 2022 13:19:53 -0300 Subject: bin/gc: Enable "set -x" option --- bin/gc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index 7fc92ec..1ef92f1 100755 --- a/bin/gc +++ b/bin/gc @@ -101,6 +101,7 @@ gc_email() { BEFORE="$(disk)" +set -x gc_guix gc_nohup gc_trash @@ -108,6 +109,7 @@ gc_tmpdir gc_docker gc_email wait +set +x AFTER="$(disk)" printf 'Disk space:\n' -- cgit v1.2.3 From 21a5bf4d7b1173bb6cffadfb8ad6c350e65e592f Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 25 Aug 2022 06:16:38 -0300 Subject: bin/gc: Use proper $TMPDIR over hardcoded /tmp path --- bin/gc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index 1ef92f1..9a1868b 100755 --- a/bin/gc +++ b/bin/gc @@ -79,7 +79,7 @@ gc_trash() { } gc_tmpdir() { - find /tmp/ -type f -atime +10 -exec rm -vf "{}" \; ||: + find "${TMPDIR:-/tmp}" -type f -atime +10 -exec rm -vf "{}" \; ||: } gc_docker() { -- cgit v1.2.3 From 777699f59d808dbb808d6a003167c6c49dddfb58 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sun, 23 Oct 2022 08:30:23 -0300 Subject: bin/gc: remove "current-guix" profile too --- bin/gc | 1 + 1 file changed, 1 insertion(+) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index 9a1868b..1570f84 100755 --- a/bin/gc +++ b/bin/gc @@ -66,6 +66,7 @@ disk() { gc_guix() { pass show velhinho/0-andreh-password | head -n1 | sudo -iS guix system delete-generations + pass show velhinho/0-andreh-password | head -n1 | sudo -iS guix gc -d guix home delete-generations guix gc -d } -- cgit v1.2.3 From 6f35eb1d26922dff508fe07103b7c0cd90e80a83 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 28 Oct 2022 20:05:24 -0300 Subject: bin/gc: Add gc_vcs() step --- bin/gc | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index 1570f84..e09ef13 100755 --- a/bin/gc +++ b/bin/gc @@ -100,6 +100,11 @@ gc_email() { xargs -I{} rm -vf "{}" } +gc_vcs() { + repos -e ~/dev/go/ -e ~/dev/quicklisp/ -e ~/dev/archive/ ~/dev/ | + xargs -I% -P4 x vcs -C% gc OR true +} + BEFORE="$(disk)" set -x @@ -109,6 +114,7 @@ gc_trash gc_tmpdir gc_docker gc_email +gc_vcs wait set +x AFTER="$(disk)" -- cgit v1.2.3 From 491fe0987f73149236d50e64a4ee8a6f1ec2b5ed Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 28 Oct 2022 20:46:20 -0300 Subject: bin/gc: Support TYPE argument --- bin/gc | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index e09ef13..7502e76 100755 --- a/bin/gc +++ b/bin/gc @@ -4,7 +4,7 @@ set -eu usage() { cat <<-'EOF' Usage: - gc + gc [TYPE] gc -h EOF } @@ -15,15 +15,29 @@ help() { Options: -h, --help show this message + TYPE what to do GC on (default: all): + - guix + - nohup + - trash + - tmpdir + - docker + - email + - vcs + Free disk space system-wide. Examples: - Just run it: + Just run it, for all: $ gc + + + Cleanup tmpdir: + + $ gc tmpdir EOF } @@ -106,17 +120,29 @@ gc_vcs() { } +gc_all() { + set -x + gc_guix + gc_nohup + gc_trash + gc_tmpdir + gc_docker + gc_email + gc_vcs + set +x +} + +TYPE="${1:-all}" +CMD=gc_"$TYPE" +if ! command -v "$CMD" >/dev/null; then + printf 'Invalid TYPE: "%s".\n\n' "$TYPE" >&2 + usage >&2 + exit 2 +fi + + BEFORE="$(disk)" -set -x -gc_guix -gc_nohup -gc_trash -gc_tmpdir -gc_docker -gc_email -gc_vcs -wait -set +x +"$CMD" AFTER="$(disk)" printf 'Disk space:\n' -- cgit v1.2.3 From 4652d61ea15ecbb2b09b87c5a95a0f1cc9ff731a Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 2 Nov 2022 22:15:47 -0300 Subject: bin/gc: Use "--optimize" for guix gc --- bin/gc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bin/gc') diff --git a/bin/gc b/bin/gc index 7502e76..60a6376 100755 --- a/bin/gc +++ b/bin/gc @@ -80,9 +80,9 @@ disk() { gc_guix() { pass show velhinho/0-andreh-password | head -n1 | sudo -iS guix system delete-generations - pass show velhinho/0-andreh-password | head -n1 | sudo -iS guix gc -d + pass show velhinho/0-andreh-password | head -n1 | sudo -iS guix gc --optimize -d guix home delete-generations - guix gc -d + guix gc --optimize -d } gc_nohup() { -- cgit v1.2.3