122 lines
2.9 KiB
Bash
Executable File
122 lines
2.9 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# pmgr -- proxy manager
|
|
|
|
PMGR_VERSION=0.0.2
|
|
PMGR_WORKDIR="${PMGR_WORKDIR:-"$HOME"/.config/pmgr}"
|
|
PMGR_ENV_DIR="${PMGR_ENV_DIR:-"$PMGR_WORKDIR"/environment}"
|
|
|
|
mkdir -p "$PMGR_WORKDIR"
|
|
mkdir -p "$PMGR_ENV_DIR"
|
|
|
|
usage()
|
|
{
|
|
printf \
|
|
'pmgr -- proxy manager based on systemd user units. See pmgr(1)
|
|
|
|
Usage: pmgr [options] [<arguments>]
|
|
|
|
Options:
|
|
-l list proxies
|
|
-e enable (start) proxy
|
|
-d disable (stop) proxy
|
|
-r restart proxy
|
|
-s show proxy status
|
|
-j view proxy logs
|
|
-o print PMGR_ENV_DIR
|
|
-R run systemctl --user daemon-reload
|
|
-a add to autostart
|
|
-A remove from autostart
|
|
-h print this help message and exit
|
|
-v print version and exit
|
|
|
|
Environment:
|
|
PMGR_WORKDIR [default: ~/.config/pmgr]
|
|
PMGR_ENV_DIR [default: $PMGR_WORKDIR/environment]
|
|
' | sed 's/^ //g'
|
|
}
|
|
|
|
list_proxies()
|
|
{
|
|
|
|
{
|
|
printf '\e[1mHOST PROTO FILE STATUS AUTOSTART\033[0m\n'
|
|
for file in $(find "$PMGR_ENV_DIR" -type f -follow); do
|
|
unit="${file##*/}"
|
|
if systemctl --user is-active "$unit" >/dev/null 2>&1; then
|
|
active='\033[32mactive\033[0m'
|
|
else
|
|
active=inactive
|
|
fi
|
|
printf '%s %s %s %b %s\n' \
|
|
"${unit#*@}" \
|
|
"${unit%@*}" \
|
|
"$(echo "$file" | sed "s%$HOME%~%")" \
|
|
"$active" \
|
|
"$(systemctl --user is-enabled "$unit")"
|
|
done
|
|
} | column -t
|
|
}
|
|
|
|
[ $# -eq 0 ] && { usage; exit 0; }
|
|
|
|
while getopts le:d:r:s:j:oRa:A:hv opt; do
|
|
case "$opt" in
|
|
l)
|
|
list_proxies
|
|
exit "$?"
|
|
;;
|
|
e)
|
|
for unit in $(find "$PMGR_ENV_DIR" -type f -printf '%f\n'); do
|
|
if systemctl --user is-active "$unit" >/dev/null 2>&1; then
|
|
systemctl --user stop "$unit"
|
|
fi
|
|
done
|
|
systemctl --user start "$OPTARG"
|
|
exit "$?"
|
|
;;
|
|
d)
|
|
systemctl --user stop "$OPTARG"
|
|
exit "$?"
|
|
;;
|
|
r)
|
|
systemctl --user restart "$OPTARG"
|
|
exit "$?"
|
|
;;
|
|
s)
|
|
systemctl --user status "$OPTARG"
|
|
exit "$?"
|
|
;;
|
|
j)
|
|
journalctl --user --unit "$OPTARG"
|
|
exit "$?"
|
|
;;
|
|
o)
|
|
echo "$PMGR_ENV_DIR"
|
|
exit 0
|
|
;;
|
|
R)
|
|
systemctl --user daemon-reload
|
|
exit "$?"
|
|
;;
|
|
a)
|
|
systemctl --user enable "$OPTARG"
|
|
exit "$?"
|
|
;;
|
|
A)
|
|
systemctl --user disable "$OPTARG"
|
|
exit "$?"
|
|
;;
|
|
h)
|
|
usage
|
|
exit 0
|
|
;;
|
|
v)
|
|
printf 'v%s\n' "$PMGR_VERSION"
|
|
exit 0
|
|
;;
|
|
*)
|
|
:
|
|
esac
|
|
done
|