mirror of
https://git.centos.org/centos-git-common.git
synced 2025-02-23 16:22:56 +00:00
Refactor to use getopt for arg parsing
This commit is contained in:
parent
828874b5c2
commit
64562123ea
1 changed files with 113 additions and 38 deletions
151
get_sources.sh
151
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
|
||||
#
|
||||
|
||||
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
|
||||
. /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"
|
||||
|
|
Loading…
Add table
Reference in a new issue