Created
June 3, 2014 14:59
-
-
Save nkoneko/b405eb4528181ff5a6c2 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
usage() { | |
1>&2 cat <<EOS | |
Usage: ${0} --work-dir SVN_WORK_DIR --repo SVN_REPOS_URL --user SVN_USER:SVN_PASSWORD | |
EOS | |
} | |
log() { | |
level=${1} | |
msg=${2} | |
datetime=$(date +"%Y-%m-%d %H:%M:%S") | |
1>&2 cat <<EOS | |
[${level}] ${datetime} ${msg} | |
EOS | |
} | |
die() { | |
errno=${1} | |
errmsg=${2} | |
log "Error" "${errmsg}" | |
exit ${errno} | |
} | |
info() { | |
msg=${1} | |
log "Info" "${msg}" | |
} | |
warn() { | |
msg="${1}" | |
log "Warn" "${msg}" | |
} | |
# Args and opts | |
SVN_WORK_DIR= | |
SVN_REPOS_URL= | |
SVN_USER= | |
SVN_PASSWORD= | |
[ $# -ne 6 ] && usage && die 1 "Too many or too few parameters." | |
while [ "${1}" != "" ] | |
do | |
case "${1}" in | |
"--work-dir" | "-d" ) | |
SVN_WORK_DIR="${2}" | |
shift 2 | |
;; | |
"--repo" | "-r" ) | |
SVN_REPOS_URL="${2}" | |
shift 2 | |
;; | |
"--user" | "-u" ) | |
user_password="${2}" | |
SVN_USER=`cut -d ":" -f 1 <<<${user_password}` | |
SVN_PASSWORD=`cut -d ":" -f 2 <<<${user_password}` | |
shift 2 | |
;; | |
* ) | |
die 1 "Couldn't process an unknown option '${1}'" | |
;; | |
esac | |
done | |
[ -d $(readlink -f "${SVN_WORK_DIR}/..") ] || \ | |
die 1 "Couldn't checkout the repository into '${SVN_WORK_DIR}'. Directory not found." | |
[ -z "${SVN_REPOS_URL}" ] && \ | |
die 1 "Couldn't checkout the repository. URL to a repository cannot be empty." | |
[ -z "${SVN_USER}" ] && \ | |
die 1 "Username must not be empty." | |
[ -z "${SVN_PASSWORD} ] && \ | |
die 1 "Password must not be empty." | |
if [ -d "${SVN_WORK_DIR}" ]; then | |
if [ -d "${SVN_WORK_DIR}/.svn" ]; then | |
warn "Removing '${SVN_WORK_DIR}'" | |
rm -rf ${SVN_WORK_DIR} || die 1 "Failed to remove the directory '${SVN_WORK_DIR}'" | |
warn "Removed '${SVN_WORK_DIR}'" | |
else | |
die 1 "Failed to remove the directory '${SVN_WORK_DIR}'" | |
fi | |
else | |
info "Creating a directory '${SVN_WORK_DIR}'" | |
mkdir "${SVN_WORK_DIR}" || die 1 "Failed to create the directory." | |
info "Created the directory." | |
fi | |
info "Checking out '${SVN_REPOS_URL}' into '${SVN_WORK_DIR}'" | |
svn checkout ${SVN_REPOS_URL} ${SVN_WORK_DIR} --non-interactive --username ${SVN_USER} --password ${SVN_PASSWORD} | |
info "Checked out." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment