tsunami  up.sh

File up.sh from the latest check-in


#!/usr/bin/env bash
# [ʞ] up.sh
#  ~ lexi hale <lexi@hale.su>
#  © CC0
#  > ./up.sh [-q: don't display explanatory messages]

# this  is  a  simple  script to  automate  publishing  a
# website generated by `tsunami`  to github pages. on its
# first  run  it will  ask  you  for some  authentication
# details,  initialize a  git repository,  and store  the
# parameters  you supply  there. on  subsequent runs,  it
# will push all changes to the repository.

quiet=$1

msg() { echo " [1;3$1m*" "$2" ''; }
die() { ((0 == $1)) || { msg 1 "$3"; exit $2; } }
log() { while read ln; do echo " | $ln"; done }
say() { test "$quiet" = "-q" || echo $@; }
ask() {
	test "$2" == "" &&
		prompt="$1:" ||
		prompt="$1 [$2]:" 

	read -p " - $prompt " $1 || {
		echo
		msg 1 'user cancelled setup; bailing';
		exit 1
	}

	test "${!1}" = "" && {
		test "$2" == "" && {
			msg 1 'invalid value supplied; bailing'
			exit 2
		} || {
			read $1 <<< $2
		}
	}
}

cd out >/dev/null 2>&1; die $? 9 "directory 'out' does not exist"
test ! -e .git && {
	# first run, set up a repo
	say 'to use the tsunami upload script, you need to specify a'
	say 'few parameters. firstly, what is your github username?'
	ask username
	say 'you also need to specify an email to associate with your'
	say 'commits. this WILL be public. you should either use the'
	say 'same email that is associated with your github account,'
	say 'or a “cloak” email that github has generated for you.'
	ask email
	say 'up.sh uses oauth to communicate with github. generate an'
	say 'access token and paste it in now.'
	ask token
	say 'what repository do you wish to store your site in? hit'
	say 'enter if you want to use your account repo'
	ask repo $username.github.io
	test -e ../src/CNAME || {
		say 'you don’t have a CNAME file in your src/ directory. if'
		say 'you want to use your own domain to host this website,'
		say 'key it in now; otherwise, just hit enter.'
		ask domain .
		test "$domain" == "." || {
			echo "$domain" > ../src/CNAME
			echo "$domain" > CNAME
		}
	}

	msg 6 "initializing $repo.git"

	git init 2>&1 | log
		die ${PIPESTATUS[0]} 6 "could not initialize repo in out/"
	git config --local user.name $username 2>&1 | log
	git config --local user.email $email 2>&1 | log
	git remote add origin "https://$token:x-oauth-basic@github.com/$username/$repo.git" 2>&1 | log
		die ${PIPESTATUS[0]} 7 "could not add remote"

	test "$repo" = "$username.github.io" || {
		msg 6 "renaming branch “master” → “gh-pages”"
		git branch -m master gh-pages 2>&1 | log
		die ${PIPESTATUS[0]} 8 "could not rename branch"
	}
	msg 6 'making initial commit'
} || {
	msg 6 'making new commit'
}

git add . 2>&1 | log
	die ${PIPESTATUS[0]} 3 "failed to add files"
git commit -a -m "update site" 2>&1 | log
	die ${PIPESTATUS[0]} 4 "failed to commit"
git push -u origin master --force 2>&1 | log
	die ${PIPESTATUS[0]} 5 "failed to push"