Skip to content

Instantly share code, notes, and snippets.

@flosell
Last active May 12, 2024 16:32
Show Gist options
  • Save flosell/7efe9f21f4394614b158 to your computer and use it in GitHub Desktop.
Save flosell/7efe9f21f4394614b158 to your computer and use it in GitHub Desktop.
Template `./go` script as a main entrypoint into the development environment (http://www.thoughtworks.com/insights/blog/praise-go-script-part-i)
#!/bin/bash
set -e
SCRIPT_DIR=$(cd $(dirname $0) ; pwd -P)
die() {
red=$(tput setaf 1)
normal=$(tput sgr0)
echo "${red}${1}${normal}"
exit 1
}
goal_say_hello() { ## just says hello, without any parameters
echo "Hello World!"
echo "This script is in ${SCRIPT_DIR}"
}
goal_say_something() { #<message> [sayer]# say a message customized with parameters
message="${1}"
sayer="${2:-the script}"
test -n "${message}" || die "Needs a message"
echo "${message} from ${sayer}"
}
goal_help() { ## this help message
bold=$(tput bold)
normal=$(tput sgr0)
echo "usage: ${bold}$0 <goal>${normal}
goals:"
cat "$0" | sed -nr -e "s/goal_([a-zA-Z0-9_-]+).*#(.*)# *(.*)/ ${bold}\1${normal} \2 |--- \3/p" | column -ts '|' | sort
exit 1
}
if type -t "goal_$1" &>/dev/null; then
goal_$1 "${@:2}"
else
goal_help
fi
@netfirms
Copy link

Cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment