smeserver-gitutils/parse-list.sh

125 lines
3.4 KiB
Bash
Raw Normal View History

2023-06-02 03:38:02 +02:00
#!/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
2023-06-02 03:38:02 +02:00
fi
2023-06-03 08:42:01 +02:00
# parse the command line parameters
PROCESSORG=
EXTRAPARAMS=
2023-06-03 08:46:27 +02:00
if [[ $1 == org=* ]] ; then
2023-06-03 08:42:01 +02:00
# using a list of the repos in this organisatoin as input
2023-06-03 08:51:39 +02:00
PROCESSORG=${1#*=}
EXTRAPARAMS=$PROCESSORG # add the org as the first additional param
2023-06-03 08:42:01 +02:00
# 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
2023-06-02 03:38:02 +02:00
echo "Can NOT find $1 - Aborting"
exit 1
2023-06-03 08:42:01 +02:00
fi
2023-06-02 03:38:02 +02:00
fi
if [[ $(which $2 | grep "no $2") ]] ; then
echo "Can NOT find $2 - Aborting"
exit 1
fi
2023-06-05 08:18:06 +02:00
DEBUG=
2023-06-03 08:22:03 +02:00
REVIEW=
2023-06-02 03:38:02 +02:00
NOISY=
2023-06-03 08:42:01 +02:00
GITEAHOST=${remote_GITEAHOST}
ACCESSTOKEN=${remote_GITEAACCESSTOKEN}
2023-06-02 03:38:02 +02:00
for param in $3 $4 $5 $6; do
if [ $param ] ; then
case $param in
2023-06-03 08:22:03 +02:00
review )
REVIEW=true ;;
2023-06-02 03:38:02 +02:00
noisy )
NOISY=true ;;
2023-06-03 08:42:01 +02:00
local )
GITEAHOST=${local_GITEAHOST}
ACCESSTOKEN=${local_GITEAACCESSTOKEN}
2023-06-03 08:54:12 +02:00
EXTRAPARAMS=$EXTRAPARAMS" "$param
2023-06-03 08:42:01 +02:00
;;
2023-06-05 08:18:06 +02:00
debug )
DEBUG=true ;;
2023-06-02 03:38:02 +02:00
* )
EXTRAPARAMS=$EXTRAPARAMS" "$param ;;
esac
else
break
fi
done
# Build array of parameters to cycle through
PARAMLIST=
2023-06-03 08:22:03 +02:00
if [ $PROCESSORG ] ; then
# get a list of repositories in the source organisation and store in array of parameters
2023-06-03 08:22:03 +02:00
for page in {1..10} ; do
2023-06-05 08:18:06 +02:00
if [ $DEBUG ] ; then echo "getting page $page of repos from $GITEAHOST $PROCESSORG" ; fi
2023-06-03 08:57:10 +02:00
RESPONSE=$(curl -s -X 'GET' \
2023-06-03 08:42:01 +02:00
"$GITEAHOST/api/v1/orgs/$PROCESSORG/repos?page=$page" \
2023-06-03 08:22:03 +02:00
-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
2023-06-06 04:12:27 +02:00
PARAMLIST+=$repo
2023-06-03 08:22:03 +02:00
done
fi
done
else
# load array of parameters from input file
2023-06-06 03:49:16 +02:00
while read -r line ; do PARAMLIST+=$line ; done < $1
2023-06-03 08:22:03 +02:00
fi
# Cycle through array of parameters and execute script
for param in "${PARAMLIST[@]}"
do
if [[ $NOISY || $REVIEW ]] ; then echo "$2 $param $EXTRAPARAMS" ; fi
2023-06-06 04:12:27 +02:00
if [[ -z $REVIEW ]] ; then
2023-06-06 03:51:56 +02:00
if [[ $param ]] ; then $2 $param $EXTRAPARAMS ; fi
fi
done
2023-06-02 03:38:02 +02:00
exit 0