Created
October 16, 2018 22:45
-
-
Save ianfoo/ea6ff6ba2064e2bf5ed2fa8eb2db5ed5 to your computer and use it in GitHub Desktop.
Start a godoc server in the "background."
This file contains hidden or 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