[CentOS-devel] [PATCH 3/3] Refactor to use getopt for arg parsing

Johnny Hughes johnny at centos.org
Thu Jun 26 19:43:17 UTC 2014


All 3 patches in this set pushed.

On 06/26/2014 11:11 AM, Pat Riehecky wrote:
> From: Pat Riehecky <riehecky at fnal.gov>
>
> ---
>  get_sources.sh |  151 ++++++++++++++++++++++++++++++++++++++++++--------------
>  1 files changed, 113 insertions(+), 38 deletions(-)
>
> 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 <kbsingh at centos.org>
> +#         Updates:
> +#                  Mike McLean <mikem at redhat.com>
> +#                  Pat Riehecky <riehecky at fnal.gov>
> +
> +
> +#####################################################################
> +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"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.centos.org/pipermail/centos-devel/attachments/20140626/f4ae9818/attachment.sig>


More information about the CentOS-devel mailing list