From 64562123ea0d26199900e412042aead662d2e36d Mon Sep 17 00:00:00 2001 From: Pat Riehecky Date: Jun 26 2014 19:38:49 +0000 Subject: Refactor to use getopt for arg parsing --- diff --git a/get_sources.sh b/get_sources.sh index f1dd30c..33ef1aa 100755 --- a/get_sources.sh +++ b/get_sources.sh @@ -1,51 +1,125 @@ #!/bin/bash # -# Script to parse the non-text sources metadata file and download -# the required files from the lookaside cache -# -# Please note: this script is non-destructive, it wont replace -# files that already exist, regardless of their state, allowing you -# to have work-in-progress content that wont get overwritten. -# # Might want to drop this in ~/bin/ and chmod u+x it +# + +# Initial Author: Karanbir Singh +# Updates: +# Mike McLean +# Pat Riehecky + + +##################################################################### +usage() { + echo '' >&2 + echo "$0 [-hcq] [-b branch] [--surl url]" >&2 + echo '' >&2 + echo 'Script to parse the non-text sources metadata file' >&2 + echo ' and download the required files from the lookaside' >&2 + echo ' cache.' >&2 + echo '' >&2 + echo 'PLEASE NOTE: this script is non-destructive, it wont' >&2 + echo ' replace files that already exist, regardless of' >&2 + echo ' their state, allowing you to have work-in-progress' >&2 + echo ' content that wont get overwritten.' >&2 + echo '' >&2 + echo 'You need to run this from inside a sources git repo' >&2 + echo '' >&2 + echo ' -h: This help message' >&2 + echo '' >&2 + echo " $0 -b c7" >&2 + echo " $0 -q -b c7" >&2 + echo " $0 -c -b remotes/origin/c7" >&2 + echo " $0 -c -b c7 --surl '$SURL'" >&2 + echo " $0" >&2 + exit 1 +} -surl="https://git.centos.org/sources/" +##################################################################### -# for setting any overrides, such as surl or f +SURL="https://git.centos.org/sources/" + +QUIET=0 +BRANCH='' +CHECK=0 + +# for setting any overrides, such as SURL, default BRANCH, or force CHECK if [ -f /etc/centos-git-common ]; then . /etc/centos-git-common fi -#parse command line args -BRANCH='' -QUIET='' -CHECK=0 -while (($# > 0)) -do - case $1 in - --branch) - #specify branch instead of asking git - BRANCH=$2 - shift 2 - ;; - --surl) - #override sources url - surl=$2 - shift 2 - ;; - --check) - #verify the sha1sum of the downloaded file - CHECK=1 - shift - ;; - -q) - # Be less chatty - QUIET='--silent' - shift - ;; - esac +##################################################################### +# setup args in the right order for making getopt evaluation +# nice and easy. You'll need to read the manpages for more info +# utilizing 'while' construct rather than 'for arg' to avoid unnecessary +# shifting of program args +args=$(getopt -o hcqb: -l surl: -- "$@") +eval set -- "$args" + +while [[ 0 -eq 0 ]]; do + case $1 in + -- ) + # end of getopt args, shift off the -- and get out of the loop + shift + break + ;; + -c ) + # verify the sha1sum of the downloaded file + CHECK=1 + shift + ;; + -q ) + # suppress warnings + QUIET=1 + shift + ;; + -b ) + # Check this particular branch + BRANCH=$2 + shift + shift + ;; + --surl ) + # override sources url + SURL=$2 + shift + shift + ;; + -h ) + # get help + usage + ;; + esac done +# set curl options this way so defaults can be set in /etc/centos-git-common +# across multiple scripts +if [[ ${QUIET} -eq 1 ]]; then + QUIET='--silent' +else + QUIET='' +fi + +which git >/dev/null 2>&1 +if [[ $? -ne 0 ]]; then + echo 'You need git in PATH' >&2 + exit 1 +fi + +which curl >/dev/null 2>&1 +if [[ $? -ne 0 ]]; then + echo 'You need curl in PATH' >&2 + exit 1 +fi + +if [[ ${CHECK} -eq 1 ]]; then + which sha1sum >/dev/null 2>&1 + if [[ $? -ne 0 ]]; then + echo 'You need sha1sum in PATH' >&2 + exit 1 + fi +fi + # check metadata file and extract package name shopt -s nullglob set -- .*.metadata @@ -100,7 +174,8 @@ while read -r fsha fname ; do if [ ! -e "${fname}" ]; then for br in "${branches[@]}" do - curl ${QUIET} -f "${surl}/${pn}/${br}/${fsha}" -o "${fname}" && break + br=$(echo ${br}| sed -e s'|remotes/origin/||') + curl ${QUIET} -f "${SURL}/${pn}/${br}/${fsha}" -o "${fname}" && break done else echo "${fname} exists. skipping"