Created
November 3, 2012 20:32
-
-
Save djui/4008626 to your computer and use it in GitHub Desktop.
malcolm - most amazing list comprehender of log messages
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
#!/bin/zsh | |
PRODUCT="malcolm" | |
PRODUCT_LONG="most amazing list comprehender of log messages" | |
VERSION=1.0.0 | |
PORT=8080 | |
VERBOSE=false | |
_usage() { | |
echo "Usage: $(basename $0) [-V|--version] [-v|--verbose] <command> [<args>]" | |
echo | |
echo "Commands:" | |
echo " start Starts webserver." | |
echo " stop Stops webserver." | |
echo " add <url> <tag>... Add <url> and <tag>s." | |
echo " Tags are enclosed by \"[tag]\" and separated by comma or space." | |
echo " find <tag>... Find URLs matching <tag>s." | |
echo " publish [<remote>] Publish to a given remote." | |
echo " help Show help screen." | |
echo | |
} | |
_version() { | |
echo $VERSION | |
} | |
_debug() { | |
$VERBOSE && echo "[DEBUG] $@" | |
} | |
_info() { | |
echo "[INFO] $@" | |
} | |
_error() { | |
echo "[ERROR] $@" | |
exit 1 | |
} | |
_assert_initialized() { | |
_debug "asserting initialized..." | |
if [ ! -d ".git" ] ; then | |
_debug "initializing..." | |
git init > /dev/null 2>&1 || \ | |
_error "couldn't initialize!" | |
echo "$PRODUCT - $PRODUCT_LONG" > .git/description | |
_info "initialized" | |
fi | |
} | |
_assert_remote() { | |
_debug "asserting remote..." | |
git config --get remote.origi.url > /dev/null 2>&1 | |
if [ $? ] ; then | |
_error "couldn't initialize!" | |
fi | |
_debug "remote found" | |
} | |
_start() { | |
_assert_initialized | |
_debug "starting..." | |
git instaweb --port=$PORT --httpd=lighttpd --browser=dummy > /dev/null 2>&1 || \ | |
git instaweb --port=$PORT --httpd=webrick --browser=dummy > /dev/null 2>&1 || \ | |
_error "couldn't start server!" | |
_info "started at http://0:$PORT" | |
} | |
_stop() { | |
_debug "stopping..." | |
git instaweb stop | |
_info "stopped" | |
} | |
_add() { | |
_assert_initialized | |
url=$1 ; shift | |
tags=[$(echo "$@" | sed "s/[ ,]/][/")] | |
_debug "adding url $url with tags $tags..." | |
git commit --quiet --allow-empty --message="$tags($url)" || \ | |
_error "couldn't add $url!" | |
_info "added" | |
} | |
_find() { | |
_assert_initialized | |
_debug "finding..." | |
tag="\[$1\]" | |
git log --pretty=format:%s --grep="$tag" | sed "s/^.*(\(.*\))$/\1/" | |
} | |
_publish() { | |
_assert_initialized | |
_debug "publishing..." | |
if [ $1 ] ; then | |
remote="$1:$PRODUCT.git" | |
git remote add origin $remote | |
else | |
_assert_remote | |
fi | |
git pull origin master > /dev/null 2>&1 && \ | |
git push origin master > /dev/null 2>&1 || \ | |
_error "couldn't publish to $(git config --get remote.origin.url)!" | |
_info "published" | |
} | |
while [ $# -gt 0 ] ; do | |
arg=$1 ; shift | |
case $arg in | |
"start") _start ; exit 0 ;; | |
"stop") _stop ; exit 0 ;; | |
"add") _add $@ ; exit 0 ;; | |
"find") _find $@ ; exit 0 ;; | |
"publish") _publish $@ ; exit 0 ;; | |
"help") _usage ; exit 0 ;; | |
"-V"|"--version") _version ; exit 0 ;; | |
"-v"|"--verbose") VERBOSE=true ;; | |
*) _usage ; exit 1 ;; | |
esac | |
done | |
_usage ; exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment