Refactor to use getopt for arg parsing

This commit is contained in:
Pat Riehecky 2014-06-26 14:38:49 -05:00 committed by Johnny Hughes
parent 828874b5c2
commit 64562123ea

View file

@ -1,51 +1,125 @@
#!/bin/bash #!/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 # Might want to drop this in ~/bin/ and chmod u+x it
#
surl="https://git.centos.org/sources/" # Initial Author: Karanbir Singh <kbsingh@centos.org>
# Updates:
# Mike McLean <mikem@redhat.com>
# Pat Riehecky <riehecky@fnal.gov>
# for setting any overrides, such as surl or f
#####################################################################
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/"
QUIET=0
BRANCH=''
CHECK=0
# for setting any overrides, such as SURL, default BRANCH, or force CHECK
if [ -f /etc/centos-git-common ]; then if [ -f /etc/centos-git-common ]; then
. /etc/centos-git-common . /etc/centos-git-common
fi fi
#parse command line args #####################################################################
BRANCH='' # setup args in the right order for making getopt evaluation
QUIET='' # nice and easy. You'll need to read the manpages for more info
CHECK=0 # utilizing 'while' construct rather than 'for arg' to avoid unnecessary
while (($# > 0)) # shifting of program args
do args=$(getopt -o hcqb: -l surl: -- "$@")
case $1 in eval set -- "$args"
--branch)
#specify branch instead of asking git while [[ 0 -eq 0 ]]; do
BRANCH=$2 case $1 in
shift 2 -- )
;; # end of getopt args, shift off the -- and get out of the loop
--surl) shift
#override sources url break
surl=$2 ;;
shift 2 -c )
;; # verify the sha1sum of the downloaded file
--check) CHECK=1
#verify the sha1sum of the downloaded file shift
CHECK=1 ;;
shift -q )
;; # suppress warnings
-q) QUIET=1
# Be less chatty shift
QUIET='--silent' ;;
shift -b )
;; # Check this particular branch
esac BRANCH=$2
shift
shift
;;
--surl )
# override sources url
SURL=$2
shift
shift
;;
-h )
# get help
usage
;;
esac
done 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 # check metadata file and extract package name
shopt -s nullglob shopt -s nullglob
set -- .*.metadata set -- .*.metadata
@ -100,7 +174,8 @@ while read -r fsha fname ; do
if [ ! -e "${fname}" ]; then if [ ! -e "${fname}" ]; then
for br in "${branches[@]}" for br in "${branches[@]}"
do 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 done
else else
echo "${fname} exists. skipping" echo "${fname} exists. skipping"