Created
September 26, 2018 23:42
-
-
Save ianfoo/241874c27cf0edb91f258721254ef6ba to your computer and use it in GitHub Desktop.
A function to start, stop, or restart a local godoc server in the background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
godoc-server () { | |
declare -lr funcname=$0 | |
declare -lr godoc=$(which godoc) | |
start_server () { | |
declare -lr nohup=$(which gnohup || which nohup) | |
declare -lr logfile="/tmp/godoc-server.log" | |
$nohup $godoc $@ >$logfile 2>&1 & | |
} | |
stop_server() { | |
pkill -fn 'godoc -http' | |
} | |
restart() { | |
local cmd=$(pgrep -fln 'godoc -http') | |
if [[ -z $cmd ]]; then | |
>&2 echo "$funcname: no running godoc server found" | |
return 1 | |
fi | |
kill $(echo $cmd | cut -d' ' -f1) | |
local args="$(echo $cmd | awk '{$1=""; $2=""}1' | sed 's/^ *//g')" | |
start_server $args | |
} | |
local port=6060 | |
if [[ $# > 0 ]]; then | |
case $1 in | |
-p|--port) port=$1; shift;; | |
-r|--restart) restart; return;; | |
-s|--stop|--kill) stop_server; return;; | |
*) echo >&2 "$funcname: unrecognized argument $1" && return 1;; | |
esac | |
fi | |
start_server -http=:$port | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment