improve output when re-installing or switching go versions
[webi-installers/.git] / golang / install.bash
index 59ca0f4df08321a2c951a7bb9f464f79912e77e6..2ecf7d7b7cce042a4975a601f4093c3ae040e137 100644 (file)
@@ -10,7 +10,6 @@
 #   mkdir -p hello/
 #   pushd hello/
 #   ```
-#   <br/>
 #
 #   ```bash
 #   cat << EOF >> main.go
@@ -25,7 +24,6 @@
 #   }
 #   EOF
 #   ```
-#   <br/>
 #
 #   ```bash
 #   go fmt ./...
@@ -41,20 +39,44 @@ set -u
 # Install go #
 ###################
 
-new_go_home="${HOME}/.local/opt/go-v${WEBI_VERSION}"
 new_go="${HOME}/.local/opt/go-v${WEBI_VERSION}/bin/go"
+common_go_home="${HOME}/.local/opt/go"
+new_go_home="${HOME}/.local/opt/go-v${WEBI_VERSION}"
+common_go_bin="${HOME}/go"
+new_go_bin="${HOME}/.local/opt/go-bin-v${WEBI_VERSION}"
+
+update_go_home() {
+    rm -rf "$common_go_home" # should be a symlink
+    ln -s "$new_go_home" "$common_go_home"
+    # TODO get better output from pathman / output the path to add as return to webi bootstrap
+    webi_path_add "$common_go_home/bin"
+
+    rm -rf "$common_go_bin"
+    mkdir -p "$new_go_bin/bin"
+    ln -s "$new_go_bin" "$common_go_bin"
+    webi_path_add "$common_go_bin/bin"
+}
 
 # Test for existing version
 set +e
 cur_go="$(command -v go)"
 set -e
+cur_go_version=""
 if [ -n "$cur_go" ]; then
-  cur_ver=$(go version | cut -d' ' -f3 | sed 's:go::')
-  if [ "$cur_ver" == "$(echo $WEBI_VERSION | sed 's:\.0::g')" ]; then
-    echo "go v$WEBI_VERSION already installed at $cur_go"
+  cur_go_version=$(go version | cut -d' ' -f3 | sed 's:go::')
+
+  if [ "$cur_go_version" == "$WEBI_VERSION" ]; then
+    echo "go$WEBI_VERSION already installed at $cur_go"
     exit 0
-  elif [ "$cur_go" != "$new_go" ]; then
-    echo "WARN: possible conflict with go v$WEBI_VERSION at $cur_go"
+  else
+    if [ "$cur_go" != "$common_go_home/bin/go" ]; then
+      echo "WARN: possible conflict between go${WEBI_VERSION} and go${cur_go_version} at ${cur_go}"
+    fi
+    if [ -x "$new_go" ]; then
+      update_go_home
+      echo "switched to go${WEBI_VERSION} at $new_go_home"
+      exit 0
+    fi
   fi
 fi
 
@@ -83,6 +105,7 @@ pushd "$WEBI_TMP" 2>&1 >/dev/null
       cp -Hr ./go*/* "$new_go_home/" 2>/dev/null
       cp -Hr ./go*/.* "$new_go_home/" 2>/dev/null
     fi
+    rm -rf ./go*
 
     # Install x go
     $new_go_home/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null
@@ -95,9 +118,7 @@ popd 2>&1 >/dev/null
 #   Update PATH   #
 ###################
 
-# TODO get better output from pathman / output the path to add as return to webi bootstrap
-webi_path_add "$new_go_home/bin"
-webi_path_add "$HOME/go/bin"
+update_go_home
 
 echo "Installed 'go' (and go tools)"
 echo ""