diff --git a/switch b/switch index df2d5e0..01820d0 100755 --- a/switch +++ b/switch @@ -5,7 +5,9 @@ PLATFORM_URL=github.com ORG_NAME= SAVE_ORG_NAME=0 REPO_NAME= +GROUP_HIERARCHY="" CONFIG_FILE=$HOME/.bryswitch +DEBUG=0 # create a config file if it doesn't exist @@ -25,12 +27,14 @@ function usage { echo -e " -O, --org-save string Same as -o but org name is persisted for future invocations." echo -e " -gh, --github Indicate that is a GitHub repository. Note: this is the default." echo -e " -gl, --gitlab Indicate that is a GitLab repository." + echo -e " -g, --group string Specify a group hierarchy." echo -e echo -e "Examples:" echo -e " $ switch slack-action" echo -e " $ switch --org facebook react" echo -e " $ switch --org-save google gson" echo -e " $ switch --gitlab --org-save fdroid fdroidclient" + echo -e " $ switch --gitlab --org gitlab-org --group charts knative" } # if no arguments @@ -59,21 +63,66 @@ function setOrg { ORG_NAME=$(toLower $org_name) - if [[ $save -eq 1 ]]; then - echo "ORG_NAME=$ORG_NAME\nPLATFORM_URL=$PLATFORM_URL" > $CONFIG_FILE - echo "$PLATFORM organization set to '$ORG_NAME'" - fi + [[ $save -eq 1 ]] && echo "$PLATFORM organization set to '$ORG_NAME'" } # function to clone and change directories # $1 is repo name function switchToRepo { - DIR=~/dev/$PLATFORM_URL/$ORG_NAME/$1 + url=$1 + org=$2 + repo=$3 + heirarchy=$4 + dir=~/dev/$url/$org/$repo # clone repo if it doesn't exist locally - [ ! -d $DIR ] && git clone git@$PLATFORM_URL:$ORG_NAME/$1.git $DIR - # change to local repo directory if clone was successful or already exists - echo "Switching to '$1'" - [[ -d $DIR ]] && cd $DIR + [ ! -d $dir ] && clone $url $org $repo $dir $heirarchy + cur_dir=$PWD + real_path=$(realpath "$dir") + if [[ "$cur_dir" == "$real_path" ]]; then + echo "You're already here!" + else + echo "Switching to '$repo'" + # change to local repo directory if clone was successful or already exists + [[ -d $dir ]] && cd $dir + fi +} + +function clone { + url=$1 + org=$2 + repo=$3 + dir=$4 + heirarchy=$5 + echo "cloning $repo..." + # try to clone with the hierarchy first, then without if it fails + if ! (git clone -q git@$url:$org/${heirarchy}$repo.git $dir) then + if ! (git clone -q git@$url:$org/$repo.git $dir) then + echo "$repo is not a valid repository name" + else + # if clone worked without the hierarchy then remove the stored value + GROUP_HIERARCHY="" + fi + fi +} + +# save values to .bryswitch file +function persist { + echo "ORG_NAME=$ORG_NAME\nPLATFORM_URL=$PLATFORM_URL\nGROUP_HIERARCHY=$GROUP_HIERARCHY" > $CONFIG_FILE +} + +function realpath { + shortpath=`eval echo "$1"` + folder=$(dirname "$shortpath") + echo $(cd "$folder"; pwd)/$(basename "$shortpath"); +} + +function debug { + echo "" + echo "Platform name: $PLATFORM" + echo "Platform URL: $PLATFORM_URL" + echo "Org name: $ORG_NAME" + echo "Clone URL: $PLATFORM_URL/$ORG_NAME/$REPO_NAME.git" + echo "Clone to: $HOME/dev/$PLATFORM_URL/$ORG_NAME/$REPO_NAME" } # return lower case of input @@ -112,6 +161,14 @@ do PLATFORM_URL=gitlab.com shift ;; + -g|--group) + GROUP_HIERARCHY="$2/" + shift + ;; + --debug) + DEBUG=1 + shift + ;; *) REPO_NAME=$1 shift @@ -121,7 +178,12 @@ done setOrg "$ORG_NAME" "$SAVE_ORG_NAME" -switchToRepo "$REPO_NAME" +switchToRepo "$PLATFORM_URL" "$ORG_NAME" "$REPO_NAME" "$GROUP_HIERARCHY" + +persist + +[[ $DEBUG -eq 1 ]] && debug + unset PLATFORM unset PLATFORM_URL @@ -129,3 +191,5 @@ unset ORG_NAME unset SAVE_ORG_NAME unset REPO_NAME unset CONFIG_FILE +unset GROUP_HIERARCHY +unset DEBUG