-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add script to fetch git org name
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
1 parent
c2b4328
commit f73bb87
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "$org" | ||
;; | ||
"lower") | ||
echo "${org,,}" | ||
;; | ||
"upper") | ||
echo "${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" |