I'm learning bash and my goal here is from any repository on my computer to be able to type github
and it will open the remote
in a browser for me.
This works but I feel like I over complicated the remote=$(..)
logic and arguably the get_ext()
method.
get_ext()
allows you to type github issues
and it will open the issues page for that repo on github. I know it could work better if there was a hash map equivalent in bash but from what I can find, there's not.
code:
#! /bin/bash
endpoint=$1
get_ext() {
case $1 in
*github*)
case $endpoint in
pr)
echo "/pulls";;
issues)
echo "/issues";;
*)
echo "";;
esac;;
*bitbucket*)
case $endpoint in
pr)
echo "/pull-requests";;
issues)
# issues doesn't exist for bitbucket
echo "";;
*)
echo "";;
esac;;
esac
}
open_site() {
# currently only works for SSH addresses
if [[ $remote == *@* ]]; then
name=$(echo $remote | sed "s/^.*:\(.*\)\.git/\1/g")
ext=$(get_ext $1)
open "$1/${name}${ext}"
fi
}
remote=$(grep -A 1 "remote \"origin\"" $(pwd)/.git/config | sed -n 2p)
case $remote in
*github*)
open_site "https://github.com";;
*bitbucket*)
open_site "https://bitbucket.org";;
esac