|
#!/bin/bash |
|
|
|
## |
|
# Archive.sh |
|
# By @ariporad, http://ariporad.com, [email protected] |
|
# |
|
# Instructions, usage and other information is avalible here: http://git.io/vEFqV. |
|
# |
|
# Licensed under the MIT License: http://ariporad.mit-license.org. |
|
## |
|
|
|
## |
|
# Config |
|
## |
|
VERSION="1.0.1" |
|
TMP_DIR=".archive-tmp-$RANDOM`date -u +%s`$$" # Random, PID, Datetime |
|
FOLDER="$1" |
|
CWD=`pwd` |
|
GITHUB_REPO="YOUR_GITHUB_USERNAME/archive" |
|
GIT_BRANCH="$(echo $FOLDER | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)" # https://automatthias.wordpress.com/2007/05/21/slugify-in-a-shell-script/ |
|
DATE=`date +%m-%d-%Y-%H%M` # http://stackoverflow.com/a/1401495/1928484 |
|
OUTPUT_ZIP="$TMP_DIR/$(basename $FOLDER)@$DATE" |
|
|
|
## |
|
# Helpers |
|
## |
|
print() { |
|
printf "$@\n" |
|
} |
|
|
|
section() { |
|
print "" |
|
print "==============================================================================" |
|
if [ "$#" -ne "0" ]; then |
|
print "$@..." |
|
print "==============================================================================" |
|
fi |
|
print "" |
|
} |
|
|
|
## |
|
# The Code |
|
## |
|
|
|
# Exiting |
|
|
|
exitBad() { |
|
print "NOT OK." |
|
# http://stackoverflow.com/a/13864829/1928484 |
|
if [[ -z "$1" ]]; then |
|
exit $1 |
|
else |
|
exit 1 |
|
fi |
|
} |
|
|
|
exitGood() { |
|
print "OK." |
|
exit 0; |
|
} |
|
|
|
# TmpDir |
|
removeTmpDir() { |
|
rm -rf "$TMP_DIR" |
|
} |
|
|
|
setupTmpDir() { |
|
section "Making temporary directory" |
|
|
|
removeTmpDir |
|
mkdir -p "$TMP_DIR" |
|
} |
|
|
|
cdToTmpDir() { |
|
section "\`cd\`-ing into the TmpDir" |
|
|
|
cd "$TMP_DIR" |
|
} |
|
|
|
cleanup() { |
|
section "Cleanup" |
|
|
|
cd "$CWD" |
|
rm -rf "$FOLDER" |
|
rm -rf "$TMP_DIR" |
|
} |
|
|
|
# Git |
|
setupGitRepo() { |
|
section "Creating git repo" |
|
|
|
git init |
|
git add . || true |
|
|
|
MSG="Archive $FOLDER. |
|
|
|
On `date`. |
|
|
|
By _The Archiver_ (v$VERSION)." |
|
|
|
git commit -am "$MSG" |
|
} |
|
|
|
setupGitRemotes() { |
|
git remote add origin [email protected]:ariporad/archive.git |
|
} |
|
|
|
pushToGithub() { |
|
section "Pushing to GitHub" |
|
|
|
setupGitRemotes |
|
|
|
# http://stackoverflow.com/a/22010339/1928484 |
|
{ |
|
git push origin "master:$GIT_BRANCH" && |
|
print "\n\nPush Successful!" |
|
} || { |
|
print "\n\nERROR! Push Failed! Attempting to fix it..." |
|
section "Attempting to push" |
|
|
|
{ |
|
git fetch # Get what's already up there |
|
git rebase "origin/$GIT_BRANCH" # Re-play our commit on top of it |
|
git push origin "master:$GIT_BRANCH" && |
|
print "\n\nPush Successful!" |
|
} || { |
|
cat <<EOF |
|
\n\nPush Failed! We're not really sure why (See http://xkcd.com/1597), but see above |
|
for whatever git said it was. |
|
|
|
Run this command to move in to the tmp dir and fix it: |
|
cd $TMP_DIR |
|
|
|
When you're done, run: |
|
cd $CWD && rm -rf $TMP_DIR |
|
|
|
EOF |
|
exitBad |
|
} |
|
} |
|
} |
|
|
|
# Moving |
|
moveProject() { |
|
section "Zipping \`$FOLDER\` to \`$OUTPUT_ZIP\`" |
|
|
|
zip -r "$OUTPUT_ZIP" "$FOLDER" |
|
} |
|
|
|
## |
|
# GO‽‽‽ |
|
## |
|
|
|
clear |
|
|
|
# This is `cat` because `<<EOF` goes to stdin. |
|
cat <<EOF |
|
___________.__ |
|
\__ ___/| |__ ____ |
|
| | | | \_/ __ \ |
|
| | | Y \ ___/ |
|
|____| |___| /\___ > |
|
\/ \/ |
|
_____ .__ .__ |
|
/ _ \_______ ____ | |__ |__|__ __ ___________ |
|
/ /_\ \_ __ \_/ ___\| | \| \ \/ // __ \_ __ \\ |
|
/ | \ | \/\ \___| Y \ |\ /\ ___/| | \/ |
|
\____|__ /__| \___ >___| /__| \_/ \___ >__| |
|
\/ \/ \/ \/ |
|
|
|
|
|
Welcome to the Archiver! |
|
This utility makes it easy to archive a project you no longer need! |
|
|
|
It works if it ends with 'OK'. |
|
|
|
Here's what's about to happen: |
|
1. The project (a.k.a the folder \`$1\`) will get zipped in to a |
|
temporary directory (./$OUTPUT_ZIP). |
|
2. That dir will then be pushed to an orphan branch called \`$GIT_BRANCH\` |
|
in \`$GITHUB_REPO\` on GitHub. (\`$GITHUB_REPO#$GIT_BRANCH\`). |
|
3. \`./$1\` will be moved to trash. |
|
EOF |
|
|
|
section |
|
|
|
read -p "Press [Enter] to begin..." |
|
|
|
################################################################################ |
|
|
|
setupTmpDir |
|
moveProject |
|
cdToTmpDir |
|
setupGitRepo |
|
pushToGithub |
|
cleanup |
|
|
|
section "Done" |
|
exitGood |