~dricottone/my-utils

598fcd5f3c9242b5f38809c982c7ca9045c33857 — Dominic Ricottone 4 years ago 0fc0112
Added wg*, fw*, docker*, android*, pactl*, vimsplit; Update to enumerate
M Makefile => Makefile +32 -0
@@ 7,13 7,25 @@ install: clean
	install -m755 src/myparse.bash $(LIB_DIR)/myparse.bash
	install -m755 src/myminiparse.sh $(LIB_DIR)/myminiparse.sh
	install -m755 src/unittest-color.awk $(LIB_DIR)/unittest-color.awk
	install -m755 src/android-emulator $(BIN_DIR)/android-emulator
	install -m755 src/check-x $(BIN_DIR)/check-x
	install -m755 src/ctdir $(BIN_DIR)/ctdir
	install -m755 src/debom $(BIN_DIR)/debom
	install -m755 src/docker-clean $(BIN_DIR)/docker-clean
	install -m755 src/docker-get-all $(BIN_DIR)/docker-get-all
	install -m755 src/docker-get-exited $(BIN_DIR)/docker-get-exited
	install -m755 src/docker-restart $(BIN_DIR)/docker-restart
	install -m755 src/docker-up $(BIN_DIR)/docker-up
	install -m755 src/epub $(BIN_DIR)/epub
	install -m755 src/enumerate $(BIN_DIR)/enumerate
	install -m755 src/fw-status $(BIN_DIR)/fw-status
	install -m755 src/mkbak $(BIN_DIR)/mkbak
	install -m755 src/mktar $(BIN_DIR)/mktar
	install -m755 src/pactl-get-running-sink $(BIN_DIR)/pactl-get-running-sink
	install -m755 src/pactl-mute $(BIN_DIR)/pactl-mute
	install -m755 src/pactl-volume-down $(BIN_DIR)/pactl-volume-down
	install -m755 src/pactl-volume-printf $(BIN_DIR)/pactl-volume-printf
	install -m755 src/pactl-volume-up $(BIN_DIR)/pactl-volume-up
	install -m755 src/rand $(BIN_DIR)/rand
	install -m755 src/rebom $(BIN_DIR)/rebom
	install -m755 src/rmtar $(BIN_DIR)/rmtar


@@ 22,6 34,10 @@ install: clean
	install -m755 src/tarls $(BIN_DIR)/tarls
	install -m755 src/unittest $(BIN_DIR)/unittest
	install -m755 src/untar $(BIN_DIR)/untar
	install -m755 src/vimsplit $(BIN_DIR)/vimsplit
	install -m755 src/wg-off $(BIN_DIR)/wg-off
	install -m755 src/wg-on $(BIN_DIR)/wg-on
	install -m755 src/wg-status $(BIN_DIR)/wg-status
	install -m755 src/whichcat $(BIN_DIR)/whichcat
	install -m755 src/whiched $(BIN_DIR)/whiched
	install -m755 src/whichhead $(BIN_DIR)/whichhead


@@ 34,13 50,25 @@ uninstall:
	rm $(LIB_DIR)/myparse.bash
	rm $(LIB_DIR)/myminiparse.sh
	rm $(LIB_DIR)/unittest-color.awk
	rm $(BIN_DIR)/android-emulator
	rm $(BIN_DIR)/check-x
	rm $(BIN_DIR)/ctdir
	rm $(BIN_DIR)/debom
	rm $(BIN_DIR)/docker-clean
	rm $(BIN_DIR)/docker-get-all
	rm $(BIN_DIR)/docker-get-exited
	rm $(BIN_DIR)/docker-restart
	rm $(BIN_DIR)/docker-up
	rm $(BIN_DIR)/epub
	rm $(BIN_DIR)/enumerate
	rm $(BIN_DIR)/fw-status
	rm $(BIN_DIR)/mkbak
	rm $(BIN_DIR)/mktar
	rm $(BIN_DIR)/pactl-get-running-sink
	rm $(BIN_DIR)/pactl-mute
	rm $(BIN_DIR)/pactl-volume-down
	rm $(BIN_DIR)/pactl-volume-printf
	rm $(BIN_DIR)/pactl-volume-up
	rm $(BIN_DIR)/rand
	rm $(BIN_DIR)/rebom
	rm $(BIN_DIR)/rmtar


@@ 49,6 77,10 @@ uninstall:
	rm $(BIN_DIR)/tarls
	rm $(BIN_DIR)/unittest
	rm $(BIN_DIR)/untar
	rm $(BIN_DIR)/vimsplit
	rm $(BIN_DIR)/wg-off
	rm $(BIN_DIR)/wg-on
	rm $(BIN_DIR)/wg-status
	rm $(BIN_DIR)/whichcat
	rm $(BIN_DIR)/whiched
	rm $(BIN_DIR)/whichhead

A src/android-emulator => src/android-emulator +7 -0
@@ 0,0 1,7 @@
#!/bin/sh

adb start-server
trap "adb kill-server" EXIT

emulator -avd $(emulator -list-avds) -dns-server 8.8.8.8


A src/docker-clean => src/docker-clean +6 -0
@@ 0,0 1,6 @@
#!/bin/sh
for id in $(docker ps --all --quiet); do
  docker rm "$id" --force
done
docker system prune --all --force


A src/docker-get-all => src/docker-get-all +3 -0
@@ 0,0 1,3 @@
#!/bin/sh
docker ps --format "{{.ID}}" --all


A src/docker-get-exited => src/docker-get-exited +3 -0
@@ 0,0 1,3 @@
#!/bin/sh
docker ps --format "{{.ID}}" --filter exited=1 --all


A src/docker-restart => src/docker-restart +5 -0
@@ 0,0 1,5 @@
#!/bin/sh
for container in $(docker-get-exited); do
  docker start "$container"
done


A src/docker-up => src/docker-up +3 -0
@@ 0,0 1,3 @@
#!/bin/sh
docker-compose up --detach


M src/enumerate => src/enumerate +2 -1
@@ 6,7 6,7 @@ read -r -d '' help_message <<-EOF
	Rename files in current directory into sequential numbers
	Usage: enumerate [OPTIONS]
	Options:
	 -d, --dry-run     print current and intended filenames side-by-side
	 -d, --dry-run     print, not execute, changes (Default)
	 -f P, --filter P  filter targets with filename pattern P
	 -h, --help        print this message
	 -q, --quiet       suppress error messages


@@ 15,6 15,7 @@ read -r -d '' help_message <<-EOF
	 -v, --verbose     show additional messages
	 -V, --version     print version number and exit
	 -w N, --width N   rename files to numbers N wide (Default: 4)
	 -x, --execute     execute changes
EOF

source /usr/local/lib/mylib.bash

A src/fw-status => src/fw-status +3 -0
@@ 0,0 1,3 @@
#!/bin/sh
sudo ufw status


M src/mylib.bash => src/mylib.bash +9 -2
@@ 10,6 10,7 @@
# help_msg                   prints built-in documentation
# usage_msg                  prints usage instructions to STDERR, except if $quiet==1
# version_msg                prints version to STDERR, except if $quiet==1
# assert_root                checks if user is root, else errors
# is_integer VALUE           checks if VALUE is an integer
# is_natural VALUE           checks if VALUE is an integer >= 0
# is_positive_integer VALUE  checks if VALUE is an integer


@@ 167,6 168,13 @@ version_msg() {
}


assert_root() {
  if [ "${EUID}" -ne 0 ]; then
    error_msg "Must be run as root"
  fi
}


# is integer -> 0
# else       -> 1
is_integer() {


@@ 208,7 216,7 @@ contains() {
prompt_overwrite() {
  code=0
  if [[ -f "$1" ]]; then
    prompt "File '${1}' already exists. Overwrite? "
    prompt "File '${1}' already exists. Overwrite? [Y/n] "
    case "$response" in
    [Yy]*)
      msg "Overwriting..."


@@ 245,4 253,3 @@ fn_extension() {
  fi
}



A src/pactl-get-running-sink => src/pactl-get-running-sink +4 -0
@@ 0,0 1,4 @@
#!/bin/sh

pactl list sinks short | grep -F 'RUNNING' | cut -f 1


A src/pactl-mute => src/pactl-mute +9 -0
@@ 0,0 1,9 @@
#!/bin/sh

SINK="$(pactl-get-running-sink)"
if [ -z "$SINK" ]; then
  SINK=0
fi

pactl set-sink-mute "$SINK" toggle


A src/pactl-volume-down => src/pactl-volume-down +17 -0
@@ 0,0 1,17 @@
#!/bin/sh

SINK="$(pactl-get-running-sink)"
if [ -z "$SINK" ]; then
  SINK=0
fi

if [ $# -eq 0 ]; then
  VOLUME="-10%"
elif echo "$1" | grep -F "%"; then
  VOLUME="-${1}"
else
  VOLUME="-${1}%"
fi

pactl set-sink-volume "$SINK" "$VOLUME"


A src/pactl-volume-printf => src/pactl-volume-printf +17 -0
@@ 0,0 1,17 @@
#!/bin/sh

volumes=$(pactl list sinks \
  | grep -F 'Volume:' \
  | grep -o -e '[0-9]\+%' \
  | head -n 2)

if [ $# -ge 1 ]; then
  format="$1"
else
  format="%s/%s\n"
fi

if [ ! -z "$volumes" ]; then
  printf "$format" $volumes
fi


A src/pactl-volume-up => src/pactl-volume-up +17 -0
@@ 0,0 1,17 @@
#!/bin/sh

SINK="$(pactl-get-running-sink)"
if [ -z "$SINK" ]; then
  SINK=0
fi

if [ $# -eq 0 ]; then
  VOLUME="+10%"
elif echo "$1" | grep -F "%"; then
  VOLUME="+${1}"
else
  VOLUME="+${1}%"
fi

pactl set-sink-volume "$SINK" "$VOLUME"


A src/vimsplit => src/vimsplit +58 -0
@@ 0,0 1,58 @@
#!/bin/sh

name="vimsplit"
version="1.0"
help_message=$(/usr/bin/cat <<-EOF
	Open two files in split windows
	Usage: vimsplit FILE1 FILE2
	Options:
	 -h, --help     print this message and exit
	 -v, --version  print version number and exit
EOF
)

. /usr/local/lib/myminiparse.sh

# check if right number of arguments
if [ "$#" -eq 0 ]; then
  (>&2 /usr/bin/printf "Usage: vimsplit FILE1 FILE2\n")
  exit 1
elif [ "$quiet" -eq 1 ] && [ "$#" -ne 3 ]; then
  exit 1
elif [ "$quiet" -ne 1 ] && [ "$#" -ne 2 ]; then
  (>&2 /usr/bin/printf "%s: Expected 2 arguments (given $#)\n" "$name")
  exit 1
fi

# get first two files
for arg; do
  case "$arg" in
  -q|--quiet)
    #ignore these
    ;;

  *)
    if [ -z "$file1" ]; then
      file1="$arg"
    elif [ -z "$file2" ]; then
      file2="$arg"
    fi
    ;;
  esac
done

# check files
if [ ! -f "$file1" ]; then
  if [ "$quiet" -eq 1 ]; then
    (>&2 /usr/bin/printf "%s: No such file '%s'\n" "$name" "$file1")
  fi
  exit 1
elif [ ! -f "$file2" ]; then
  if [ "$quiet" -eq 1 ]; then
    (>&2 /usr/bin/printf "%s: No such file '%s'\n" "$name" "$file2")
  fi
  exit 1
fi

/usr/bin/nvim "$file1" --cmd ":sp $file2"


A src/wg-off => src/wg-off +4 -0
@@ 0,0 1,4 @@
#!/bin/sh
sudo wg-quick down wg0
sudo wg show


A src/wg-on => src/wg-on +4 -0
@@ 0,0 1,4 @@
#!/bin/sh
sudo wg-quick up wg0
sudo wg show


A src/wg-status => src/wg-status +3 -0
@@ 0,0 1,3 @@
#!/bin/sh
sudo wg show