switch to getopt

This commit is contained in:
Iliyan Ivanov 2020-07-29 17:17:23 +03:00
parent f7dbd579af
commit 867bd32820
Signed by: iliyan
GPG Key ID: A20B95830B5794F3

View File

@ -5,8 +5,8 @@ set -euf -o pipefail
curp="$(cd "$(dirname "$0")" && pwd)" curp="$(cd "$(dirname "$0")" && pwd)"
create_org() { create_org() {
orgid=$(curl --silent -X POST "${apiurl}/orgs" \ orgid=$(curl --silent -X POST "${GITEA_SERVER_API_URL}/orgs" \
-H "Authorization: token ${token}" \ -H "Authorization: token ${GITEA_ADMIN_TOKEN}" \
-H "accept: application/json" \ -H "accept: application/json" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{ \"username\": \"$1\", \"visibility\": \"limited\"}" | jq -r '.id') -d "{ \"username\": \"$1\", \"visibility\": \"limited\"}" | jq -r '.id')
@ -14,21 +14,68 @@ create_org() {
} }
create_repo() { create_repo() {
repoid=$(curl --silent -X POST "${apiurl}$2" \ repoid=$(curl --silent -X POST "${GITEA_SERVER_API_URL}$2" \
-H "Authorization: token ${token}" \ -H "Authorization: token ${GITEA_ADMIN_TOKEN}" \
-H "accept: application/json" \ -H "accept: application/json" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{\"auto_init\": false, \"private\": true, \"name\": \"$1\"}" | jq -r '.id') -d "{\"auto_init\": false, \"private\": true, \"name\": \"$1\"}" | jq -r '.id')
[[ "$repoid" != "null" ]] && sleep 5 || true [[ "$repoid" != "null" ]] && sleep 5 || true
} }
serverurl="$1" ARGUMENT_LIST=(
token="$2" "url:"
admin="$3" "token:"
password="$4" "admin::"
repos="${@:5}" "password::"
pushuri="$(echo "$serverurl" | perl -pe "s#(https?://)#\${1}${admin}:${password}@#")" "pushurl::"
apiurl="$serverurl/api/v1" )
# defaults
: ${GITEA_ADMIN_USERNAME:=$USER}
: ${GITEA_ADMIN_PASSWORD:=}
# example ssh://git@gitea.example.com:3022
: ${GITEA_SERVER_PUSH_URL:=}
opts=$(
getopt \
--longoptions "$(printf "%s," "${ARGUMENT_LIST[@]}")" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
while [[ $# -gt 0 ]]; do
case "$1" in
--url)
GITEA_SERVER_URL="$2"
shift 2
;;
--token)
GITEA_ADMIN_TOKEN="$2"
shift 2
;;
--admin)
GITEA_ADMIN_USERNAME="$2"
shift 2
;;
--password)
GITEA_ADMIN_PASSWORD="$2"
shift 2
;;
--pushurl)
GITEA_SERVER_PUSH_URL="$2"
shift 2
;;
*)
break
;;
esac
done
[ -z "${GITEA_SERVER_PUSH_URL}" ] &&
GITEA_SERVER_PUSH_URL="$(echo "${GITEA_SERVER_URL}" | perl -pe "s#(https?://)#\${1}${GITEA_ADMIN_USERNAME}:${GITEA_ADMIN_PASSWORD}@#")"
GITEA_SERVER_API_URL="${GITEA_SERVER_URL}/api/v1"
# repo examples # repo examples
# /path/to/repo:orgname ( orgname only ) # /path/to/repo:orgname ( orgname only )
@ -37,14 +84,14 @@ apiurl="$serverurl/api/v1"
# /path/to/repo ( use name from path, no org ) # /path/to/repo ( use name from path, no org )
# init repos # init repos
for repo in ${repos}; do for repo in "$@"; do
repoPath="$(echo "$repo" | cut -d ':' -f1 | tr -d '[:space:]')" repoPath="$(echo "$repo" | cut -d ':' -f1 | tr -d '[:space:]')"
repoOrg="$(echo "$repo" | cut -d ':' -f2 | tr -d '[:space:]')" repoOrg="$(echo "$repo" | cut -d ':' -f2 | tr -d '[:space:]')"
repoName="$(echo "$repo" | cut -d ':' -f3 | tr -d '[:space:]')" repoName="$(echo "$repo" | cut -d ':' -f3 | tr -d '[:space:]')"
[ -z "$repoName" ] && repoName="$(basename "$repoPath")" [ -z "$repoName" ] && repoName="$(basename "$repoPath" ".git")"
if [ -z "$repoOrg" ]; then if [ -z "$repoOrg" ]; then
repoAPIEP="/user/repos" repoAPIEP="/user/repos"
repoOwner="${ADMIN_USER}" repoOwner="${GITEA_ADMIN_USERNAME}"
else else
create_org "$repoOrg" create_org "$repoOrg"
repoAPIEP="/org/$repoOrg/repos" repoAPIEP="/org/$repoOrg/repos"
@ -53,6 +100,6 @@ for repo in ${repos}; do
create_repo "${repoName}" "${repoAPIEP}" 1>&2 create_repo "${repoName}" "${repoAPIEP}" 1>&2
( (
cd "$repoPath" cd "$repoPath"
git push "$pushuri/${repoOwner}/${repoName}.git" master git push "${GITEA_SERVER_PUSH_URL}/${repoOwner}/${repoName}.git" master
) )
done done