Created
August 8, 2014 01:13
-
-
Save markogresak/cb611d9e3afcb112388d to your computer and use it in GitHub Desktop.
Shell script for use with https://github.com/defunkt/dotjs. Allows easy add/edit via terminal.
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
# print help | accepts exit code arg, defaults to 0 | |
function help { | |
ec=$1 | |
if test $# -eq 0; then | |
ec=0 | |
fi | |
printf "================================================================================\n" | |
printf "================================= dotjs helper =================================\n" | |
printf "================================================================================\n" | |
printf "Creates ~/.js file or uses existing and opens it\n\n" | |
printf "usage: dotjs [options] webpageDomain\n" | |
printf "options:\n" | |
printf "\t--editor {editor}: set editor to open file (must be a command)\n\t\tnote: this overrides global \$EDITOR\n" | |
printf "\t-h | --help: print help documentation\n" | |
printf "\nexamples:\n" | |
printf "\tAdd a page: dotjs google.com [opens in global \$EDITOR=$EDITOR]\n" | |
printf "\tdotjs google.com --editor atom\n\n" | |
exit $ec | |
} | |
# if no args, print help message and exit with error code | |
if [ $# -eq 0 ]; then | |
help 1 | |
fi | |
# default editor is global $EDITOR | |
editor=$EDITOR | |
# parse params | |
while test $# -gt 0; do | |
case "$1" in | |
# print help message flag | |
-h|--help) | |
help | |
;; | |
# set editor flag | |
--editor) | |
shift | |
if test $# -gt 0; then | |
editor=$1 | |
fi | |
shift | |
;; | |
# domain param | |
*) | |
if test $# -gt 0; then | |
if ! [[ "$1" =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$ ]]; then | |
printf "ERROR: $1 is not domain, please enter a valid domain name\n" | |
exit 1 | |
fi | |
domain=$1 | |
fi | |
break | |
;; | |
esac | |
done | |
# exit with error message if $editor isn't set | |
if [ -z "$editor" ]; then | |
printf "ERROR: Editor command is not set\nPlease set editor command via --editor option or \$EDITOR global variable.\n" | |
exit 1 | |
fi | |
# set file path to ~/.js/{domain}.js | |
path="$HOME/.js/$domain.js" | |
# determine output message based on file existence | |
if [ ! -f "$path" ]; then | |
printf "creating new $path\n" | |
else | |
printf "editing $path\n" | |
fi | |
# start selected editor with $path argument (should open the file) | |
$editor $path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment