Skip to content

Commit

Permalink
ci: add script to fetch git org name
Browse files Browse the repository at this point in the history
This can be used by other repos to figure out the name of the org.

Signed-off-by: Tiago Castro <[email protected]>
  • Loading branch information
tiagolobocastro committed Jan 5, 2024
1 parent c2b4328 commit ff339e1
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions scripts/git-org-name.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash

set -euo pipefail

die() {
local _return="${2:-1}"
echo "$1" >&2
exit "${_return}"
}

# Returns the git orgname from a given git url (both https and git)
# Example: [email protected]:openebs/mayastor.git => openebs
urlOrgName() {
local gitUrl="$1"
if echo $gitUrl | grep -qa "^git@"; then
echo "$gitUrl" | awk -F/ '/git/ { print $1 }' | cut -d':' -f2
elif echo $gitUrl | grep -qa "^https://"; then
echo "$gitUrl" | awk -F/ '/https/ { print $4 }'
else
die "Unknown git url type: '$gitUrl'"
fi
}

orgName() {
local originUrl=$(git config --get remote.origin.url)
if [ $? -ne 0 ]; then
die "Failed to collect the remote origin url, not a git repo?"
fi
if [ "$originUrl" = "" ]; then
die "Remote origin url is empty!"
fi
echo "$originUrl"
}

toCase() {
local org="$1"
local case="$2"

case "$case" in
""|"original")
echo -n "$org"
;;
"lower")
echo -n "${org,,}"
;;
"upper")
echo -n "${org^^}"
;;
*)
die "Invalid case: '$case'"
;;
esac
}

help() {
cat <<EOF
Usage: $0 [OPTIONS]
Options:
-h/--help Display the help message and exit.
-c/--case <case> Displays the org name in either "original", "lower" or "upper" case.
<repo> If set, output org name from the specified location, otherwise from current location.
Examples:
$0 --case lower
$0 -c upper "git/io-engine"
EOF
}

REPO=
TO_CASE=
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
help
exit 0
;;
-c|--case)
test $# -lt 2 && die "Missing value for the optional argument: '$1'"
shift
TO_CASE="$1"
shift
;;
*)
REPO="$1"
shift
;;
esac
done

if [ -n "$REPO" ]; then
if [ ! -d "$REPO" ]; then
die "Given repo location is not valid: '$REPO'"
fi
cd $REPO
fi

toCase $(urlOrgName $(orgName)) "$TO_CASE"

0 comments on commit ff339e1

Please sign in to comment.