From 76424df1df09a822cd7ec5c8b588740c86b1af19 Mon Sep 17 00:00:00 2001 From: Brycen Dorsay Date: Wed, 27 Apr 2022 19:08:12 -0700 Subject: [PATCH 1/4] updated usage text with proposed cli updates --- switch | 2 ++ 1 file changed, 2 insertions(+) diff --git a/switch b/switch index df2d5e0..07c08c1 100755 --- a/switch +++ b/switch @@ -25,12 +25,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 From 7ad12251777a5f7636b6fc1afe6cbcded636522e Mon Sep 17 00:00:00 2001 From: Brycen Dorsay Date: Wed, 27 Apr 2022 22:11:08 -0700 Subject: [PATCH 2/4] support optional group heirarchy --- switch | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/switch b/switch index 07c08c1..b7f2377 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 @@ -61,21 +63,59 @@ 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 "git clone -q git@$url:$org/${heirarchy}$repo.git $dir" + 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 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 @@ -114,6 +154,14 @@ do PLATFORM_URL=gitlab.com shift ;; + -g|--group) + GROUP_HIERARCHY="$2/" + shift + ;; + --debug) + DEBUG=1 + shift + ;; *) REPO_NAME=$1 shift @@ -123,7 +171,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 @@ -131,3 +184,4 @@ unset ORG_NAME unset SAVE_ORG_NAME unset REPO_NAME unset CONFIG_FILE +unset GROUP_HIERARCHY From 8c88d5d906b1b43cdf54653e798c045c383286f5 Mon Sep 17 00:00:00 2001 From: Brycen Date: Wed, 27 Apr 2022 22:47:49 -0700 Subject: [PATCH 3/4] macos polyfill for realpath --- switch | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/switch b/switch index b7f2377..3778c76 100755 --- a/switch +++ b/switch @@ -77,7 +77,7 @@ function switchToRepo { # clone repo if it doesn't exist locally [ ! -d $dir ] && clone $url $org $repo $dir $heirarchy cur_dir=$PWD - real_path=$(realpath $dir) + real_path=$(realpath "$dir") if [[ "$cur_dir" == "$real_path" ]]; then echo "You're already here!" else @@ -93,7 +93,8 @@ function clone { repo=$3 dir=$4 heirarchy=$5 - echo "git clone -q git@$url:$org/${heirarchy}$repo.git $dir" + 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" @@ -109,6 +110,12 @@ 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" From f348b2eea4875c42cb2ea021c8923e04cb506147 Mon Sep 17 00:00:00 2001 From: Brycen Date: Wed, 27 Apr 2022 22:50:28 -0700 Subject: [PATCH 4/4] unset DEBUG --- switch | 1 + 1 file changed, 1 insertion(+) diff --git a/switch b/switch index 3778c76..01820d0 100755 --- a/switch +++ b/switch @@ -192,3 +192,4 @@ unset SAVE_ORG_NAME unset REPO_NAME unset CONFIG_FILE unset GROUP_HIERARCHY +unset DEBUG