Restructure and clean up shell functions

This commit is contained in:
Simon Bruder 2020-12-19 16:15:45 +01:00
parent d8e4cd238d
commit 2fb7617a03
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F
12 changed files with 127 additions and 152 deletions

View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -eo pipefail
card=$(pactl list cards short | grep -E -o "bluez_card.*" | cut -f1)
# pactl does not support any easily parsable output format
profile=$(pactl list cards | rg -oU "Name: ${card}.*(\\n.*?)*.*Active Profile: (.*)" -r '$2')
pacmd set-card-profile $card off
pacmd set-card-profile $card "$profile"

View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -e
function cleanup {
fusermount -u "$library"
rm -rf "$library"
}
if [ -z "$1" ]; then
echo "USAGE: $0 host:path"
exit 1
fi
library=$(mktemp -p /tmp -d calibre-remote.XXXXXXXXXX)
sshfs "$1" "$library"
trap cleanup EXIT INT TERM
calibre --with-library="$library"

View file

@ -0,0 +1,2 @@
#!/usr/bin/env bash
mkvextract "$1" attachments $(mkvmerge --identify "$1" | grep "Attachment ID" | sed "s/Attachment ID \([0-9]*\): .*/\1/")

View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
file="$1"
shift
for attachment in $@; do
mkvpropedit --attachment-mime-type font/sfnt --add-attachment "$attachment" "$file"
done

View file

@ -0,0 +1,14 @@
#!/usr/bin/env bash
if (( $# < 1 )); then
echo "USAGE: $0 LOCATION|off" >&2
exit 1
fi
current_interfaces="$(ip -o a | grep -oE '[0-9]*:\ mullvad-(v6-)?[a-z][a-z][0-9]*' | uniq | cut -d: -f2 | tr -d ' ')"
for current_interface in $current_interfaces; do
sudo wg-quick down "$current_interface"
done
if [ "$1" != "off" ]; then
sudo wg-quick up mullvad-$1
fi

View file

@ -0,0 +1,16 @@
#!/usr/bin/env bash
if [ ! -e ./.envrc ]; then
echo "use nix" > .envrc
direnv allow
fi
if [[ ! -e shell.nix ]] && [[ ! -e default.nix ]]; then
cat > shell.nix << EOF
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = with pkgs; [
];
}
EOF
vim shell.nix
fi

View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
if (( $# < 2 )); then
echo "USAGE: $0 DIGITS FILES"
return 1
fi
digits="$1"
shift 1
i=1
for file in $@; do
mv -n "$file" "$(dirname $file)/$(printf %0${digits}d $i).${file##*.}"
i="$((i+1))"
done

View file

@ -1,151 +0,0 @@
# pass wrappers
function drone-add-hub() {
drone secret add --name docker_username --data sbruder "$1"
drone secret add --name docker_password --data "$(pass devops/docker|head -n 1)" "$1"
}
function drone-add-registry() {
drone secret add --name docker_username --data simon "$1"
drone secret add --name docker_password --data "$(pass sbruder.de/account|head -n 1)" "$1"
}
function drone-add-netlify() {
drone secret add --name netlify_auth_token --data "$(pass-field web/netlify.com Drone-Key)" "$1"
}
function drone-add-s3() {
drone secret add --name aws_access_key_id --data "$(pass-field sbruder.de/minio/personal User)" "$1"
drone secret add --name aws_secret_access_key --data "$(pass sbruder.de/minio/personal|head -n 1)" "$1"
}
function drone() (
export DRONE_SERVER="$(pass-field sbruder.de/drone Server)"
export DRONE_TOKEN="$(pass sbruder.de/drone|head -n 1)"
command drone $@
)
function docker-ls() (
export DOCKER_LS_PASSWORD="$(pass sbruder.de/account|head -n 1)"
command docker-ls $@
)
function docker-rm() (
export DOCKER_LS_PASSWORD="$(pass sbruder.de/account|head -n 1)"
command docker-rm $@
)
# Helpers
## resync pulseaudio bluetooth connection
function btsync() (
card=$(pactl list cards short|grep -E -o "bluez_card.*[[:space:]]")
pacmd set-card-profile $card off
pacmd set-card-profile $card a2dp_sink
)
## get field from pass entry
function pass-field() {
pass "$1"|grep "$2"|cut -d: -f2-|tr -d ' '
}
## Helper to create nix-shell direnv
function nixify() {
if [ ! -e ./.envrc ]; then
echo "use nix" > .envrc
direnv allow
fi
if [[ ! -e shell.nix ]] && [[ ! -e default.nix ]]; then
cat > shell.nix << EOF
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = with pkgs; [
];
}
EOF
vim shell.nix
fi
}
## Timer
function timer() (
if [ -z "$1" ] || [ -z "$2" ]; then
echo "USAGE: $0 [u|f] TIME/DURATION" >&2
return 1
fi
if [ "$1" != "u" ] && [ "$1" != "f" ]; then
echo "Invalid mode “$1” supplied. Valid modes are: u|f" >&2
return 2
fi
[ "$1" = "u" ] && mode="until"
[ "$1" = "f" ] && mode="for"
gosleep --${mode} "$2"
if [ "$?" = 0 ]; then
echo "Press CTRL-C to stop the alarm…" >&2
echo -en "\a" # highlight window
paplay ~/Documents/sound/ringtones/Bergentrückung.wav
else
echo "The sleep command failed. Please check the options." >&2
return 3
fi
)
function currency() (
amount=${1:-1}
fromcurrency=${2:-USD}
tocurrency=${3:-EUR}
rate=$(curl -s "https://api.exchangeratesapi.io/latest?base=$fromcurrency&symbols=$tocurrency"|jq ".rates.$tocurrency")
printf "$amount $fromcurrency: %.02f $tocurrency\n" $(($amount * $rate))
)
function urlencode() {
python3 -c "import urllib.parse; print(urllib.parse.quote(open(0, 'rb').read()))"
}
function renumber() (
if (( $# < 2 )); then
echo "USAGE: $0 DIGITS FILES"
return 1
fi
digits=$1
shift 1
i=1
for file in $@; do
mv -n "$file" "$(dirname $file)/$(printf %0${digits}d $i).${file##*.}"
i=$((i+1))
done
)
function mkvextract-all-attachments() {
mkvextract $1 attachments $(mkvmerge --identify $1|grep "Attachment ID"|sed "s/Attachment ID \([0-9]*\): .*/\1/")
}
function mkvpropedit-add-attachments() (
file="$1"
shift
for attachment in $@; do
mkvpropedit --attachment-mime-type font/sfnt --add-attachment "$attachment" "$file"
done
)
function ssim() {
ffmpeg -loglevel fatal -i "$1" -i "$2" -lavfi 'ssim=/dev/stdout' -f null -
}
function mullvad() (
if (( $# < 1 )); then
echo "USAGE: $0 LOCATION|off" >&2
fi
current_interfaces="$(ip -o a|grep -oE '[0-9]*:\ mullvad-(v6-)?[a-z][a-z][0-9]*'|uniq|cut -d: -f2|tr -d ' ')"
for current_interface in $current_interfaces; do
sudo wg-quick down "$current_interface"
done
if [ "$1" != "off" ]; then
sudo wg-quick up mullvad-$1
fi
)

View file

@ -0,0 +1,33 @@
function pass-field() {
pass show "$1" | grep "$2" | cut -d: -f2- | tr -d ' '
}
function drone() (
export DRONE_SERVER="$(pass-field sbruder.de/drone Server)"
export DRONE_TOKEN="$(pass sbruder.de/drone | head -n 1)"
command drone $@
)
function drone-add-registry() {
drone secret add --name docker_username --data simon "$1"
drone secret add --name docker_password --data "$(pass sbruder.de/account | head -n 1)" "$1"
}
function drone-add-netlify() {
drone secret add --name netlify_auth_token --data "$(pass-field web/netlify.com Drone-Key)" "$1"
}
function drone-add-s3() {
drone secret add --name aws_access_key_id --data "$(pass-field sbruder.de/minio/personal User)" "$1"
drone secret add --name aws_secret_access_key --data "$(pass sbruder.de/minio/personal | head -n 1)" "$1"
}
function docker-ls() (
export DOCKER_LS_PASSWORD="$(pass sbruder.de/account | head -n 1)"
command docker-ls $@
)
function docker-rm() (
export DOCKER_LS_PASSWORD="$(pass sbruder.de/account | head -n 1)"
command docker-rm $@
)

View file

@ -10,6 +10,7 @@
./mpv.nix
./neovim.nix
./programs.nix
./scripts.nix
./sway.nix
./tmux.nix
./vdirsyncer.nix

View file

@ -0,0 +1,17 @@
{ pkgs, ... }:
let
scripts = pkgs.stdenv.mkDerivation {
name = "scripts";
src = ../files/scripts;
dontBuild = true;
installPhase = ''
mkdir -p $out
cp -r . $out/bin
'';
};
in
{
home.packages = [ scripts ];
}

View file

@ -54,7 +54,7 @@ in
initExtra = ''
eval $(${pkgs.coreutils}/bin/dircolors -b ${dircolors-solarized}/dircolors.ansi-universal)
source ${../files/zsh/functions.zsh}
source ${../files/zsh/pass-wrappers.zsh}
'';
};
};