smeserver-gitutils/parse-list.sh

119 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
#
# Run script against every item in input file
if [[ -z $1 ]] ; then
echo "parse a list of parameters and execute script with those parameters"
echo "parse-list.sh <param file> <script> [<noisy> <additional> <additional> <additional>]"
echo "<param file|org='organisation'> name of file containing parameters"
echo " or the organisation list of repos"
echo "<script> script to run (e.g. rename-e-smith-pkh.sh)"
echo "optional params can appear in any order"
echo " <review> show line being executed but do NOTHING!"
echo " <noisy> show line being executed"
echo " <additional> additional params to be passed (up to 3)"
exit 0
fi
# parse the command line parameters
PROCESSORG=
if [[ $1 == org=* ]] ; then
# using a list of the repos in this organisatoin as input
PROCESSORG=${1#*=}
# Get the ini file and relevant parameters - only needed for org processing
inifilename=$(echo ~)"/.smegit/config"
if [ ! -e $inifilename ] ; then
# Not here, look at system default
if [ ! -e /etc/smegit.ini ] ; then
echo "No ini file found $inifiename or /etc/smegit.ini"
exit 1
else
initfilename="/etc/smegit.ini"
fi
fi
while read -r line || [[ -n "$line" ]]; do
if [[ $line =~ ^\[.*\]$ ]] ; then
section=${line#*[}
section=${section%]*}
else
if [[ $line =~ ^[^#]*= ]] ; then
key=${line%=*}
value=${line#*=}
declare "${section}_${key}=$value"
fi
fi
done < "$inifilename"
else
# using a file as input
if [[ ! -f $1 ]] ; then
echo "Can NOT find $1 - Aborting"
exit 1
fi
fi
if [[ $(which $2 | grep "no $2") ]] ; then
echo "Can NOT find $2 - Aborting"
exit 1
fi
REVIEW=
NOISY=
EXTRAPARAMS=
GITEAHOST=${remote_GITEAHOST}
ACCESSTOKEN=${remote_GITEAACCESSTOKEN}
for param in $3 $4 $5 $6; do
if [ $param ] ; then
case $param in
review )
REVIEW=true ;;
noisy )
NOISY=true ;;
local )
GITEAHOST=${local_GITEAHOST}
ACCESSTOKEN=${local_GITEAACCESSTOKEN}
EXTRAPARAMS=$EXTRAPARAMS" "$param
;;
* )
EXTRAPARAMS=$EXTRAPARAMS" "$param ;;
esac
else
break
fi
done
if [ $PROCESSORG ] ; then
# cycle through the repos in the specified organisation
# get a list of repositories in the source organisation
for page in {1..10} ; do
if [ $DEBUG ] ; then echo "getting page $page of repos from $GITHOST $PROCESSORG" ; fi
RESPONSE=$(curl -s -X 'GET' \
"$GITEAHOST/api/v1/orgs/$PROCESSORG/repos?page=$page" \
-H 'accept: application/json' \
-H "Authorization: token $ACCESSTOKEN"
)
if [ $DEBUG ] ; then echo "RESPONSE=$RESPONSE" ; fi
if [[ $RESPONSE == '[]' ]] ; then
# we have them all
break
else
echo $RESPONSE | grep -oP '(?<="name":").+?(?=")' | while read repo; do
if [[ $NOISY || $REVIEW ]] ; then echo "$2 $repo $EXTRAPARAMS" ; fi
if [[ ! $REVIEW ]] ; then
if [[ $repo ]] ; then $2 $repo $EXTRAPARAMS ; fi
fi
done
fi
done
else
# read through input file and run script using params from file
while read -r line
do
if [[ $NOISY || $REVIEW ]] ; then echo "$2 $line $EXTRAPARAMS" ; fi
if [[ ! $REVIEW ]] ; then
if [[ $line ]] ; then $2 $line $EXTRAPARAMS ; fi
fi
done < $1
fi
exit 0