install.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. # Author: Jrohy
  3. # Github: https://github.com/Jrohy/go-install
  4. # cancel centos alias
  5. [[ -f /etc/redhat-release ]] && unalias -a
  6. sudo=""
  7. os="Linux"
  8. proxy_url="https://goproxy.cn"
  9. #######color code########
  10. red="31m"
  11. green="32m"
  12. yellow="33m"
  13. blue="36m"
  14. fuchsia="35m"
  15. color_echo(){
  16. echo -e "\033[$1${@:2}\033[0m"
  17. }
  18. #######get params#########
  19. while [[ $# > 0 ]];do
  20. case "$1" in
  21. -v|--version)
  22. install_version="$2"
  23. echo -e "准备安装$(color_echo ${blue} $install_version)版本golang..\n"
  24. shift
  25. ;;
  26. -f)
  27. force_mode=1
  28. echo -e "强制更新golang..\n"
  29. ;;
  30. *)
  31. # unknown option
  32. ;;
  33. esac
  34. shift # past argument or value
  35. done
  36. #############################
  37. ip_is_connect(){
  38. ping -c2 -i0.3 -W1 $1 &>/dev/null
  39. if [ $? -eq 0 ];then
  40. return 0
  41. else
  42. return 1
  43. fi
  44. }
  45. setup_env(){
  46. if [[ $sudo == "" ]];then
  47. profile_path="/etc/profile"
  48. elif [[ -e ~/.zshrc ]];then
  49. profile_path="$HOME/.zprofile"
  50. fi
  51. if [[ $sudo == "" && -z `echo $GOPATH` ]];then
  52. while :
  53. do
  54. read -p "默认GOPATH路径: `color_echo $blue /home/go`, 回车直接使用或者输入自定义绝对路径: " GOPATH
  55. if [[ $GOPATH ]];then
  56. if [[ ${GOPATH:0:1} != "/" ]];then
  57. color_echo $yellow "请输入绝对路径!"
  58. continue
  59. fi
  60. else
  61. GOPATH="/home/go"
  62. fi
  63. break
  64. done
  65. echo "GOPATH值为: `color_echo $blue $GOPATH`"
  66. echo "export GOPATH=$GOPATH" >> $profile_path
  67. echo 'export PATH=$PATH:$GOPATH/bin' >> $profile_path
  68. mkdir -p $GOPATH
  69. fi
  70. if [[ -z `echo $PATH|grep /usr/local/go/bin` ]];then
  71. echo 'export PATH=$PATH:/usr/local/go/bin' >> $profile_path
  72. fi
  73. source $profile_path
  74. }
  75. check_network(){
  76. ip_is_connect "golang.org"
  77. [[ ! $? -eq 0 ]] && can_google=0
  78. }
  79. setup_proxy(){
  80. if [[ $can_google == 0 && `go env|grep proxy.golang.org` ]]; then
  81. go env -w GO111MODULE=on
  82. go env -w GOPROXY=$proxy_url,direct
  83. color_echo $green "当前网络环境为国内环境, 成功设置goproxy代理!"
  84. fi
  85. }
  86. sys_arch(){
  87. arch=$(uname -m)
  88. if [[ `uname -s` == "Darwin" ]];then
  89. os="Darwin"
  90. if [[ "$arch" == "arm64" ]];then
  91. vdis="darwin-arm64"
  92. else
  93. vdis="darwin-amd64"
  94. fi
  95. else
  96. if [[ "$arch" == "i686" ]] || [[ "$arch" == "i386" ]]; then
  97. vdis="linux-386"
  98. elif [[ "$arch" == *"armv7"* ]] || [[ "$arch" == "armv6l" ]]; then
  99. vdis="linux-armv6l"
  100. elif [[ "$arch" == *"armv8"* ]] || [[ "$arch" == "aarch64" ]]; then
  101. vdis="linux-arm64"
  102. elif [[ "$arch" == *"s390x"* ]]; then
  103. vdis="linux-s390x"
  104. elif [[ "$arch" == "ppc64le" ]]; then
  105. vdis="linux-ppc64le"
  106. elif [[ "$arch" == "x86_64" ]]; then
  107. vdis="linux-amd64"
  108. fi
  109. fi
  110. [ $(id -u) != "0" ] && sudo="sudo"
  111. }
  112. install_go(){
  113. if [[ -z $install_version ]];then
  114. echo "正在获取最新版golang..."
  115. if [[ $can_google == 0 ]];then
  116. install_version=`curl -s https://golang.google.cn/dl/|grep -w downloadBox|grep src|grep -oE '[0-9]+\.[0-9]+\.?[0-9]*'|head -n 1`
  117. else
  118. install_version=`curl -s https://github.com/golang/go/tags|grep releases/tag|grep -v rc|grep -v beta|grep -oE '[0-9]+\.[0-9]+\.?[0-9]*'|head -n 1`
  119. fi
  120. [[ ${install_version: -1} == '.' ]] && install_version=${install_version%?}
  121. [[ -z $install_version ]] && { color_echo $yellow "\n获取go版本号失败!"; exit 1; }
  122. echo "最新版golang: `color_echo $blue $install_version`"
  123. if [[ -z $force_mode && `command -v go` ]];then
  124. if [[ `go version|awk '{print $3}'|grep -Eo "[0-9.]+"` == $install_version ]];then
  125. return
  126. fi
  127. fi
  128. fi
  129. file_name="go${install_version}.$vdis.tar.gz"
  130. local temp_path=`mktemp -d`
  131. curl -H 'Cache-Control: no-cache' -L https://dl.google.com/go/$file_name -o $file_name
  132. tar -C $temp_path -xzf $file_name
  133. if [[ $? != 0 ]];then
  134. color_echo $yellow "\n解压失败! 正在重新下载..."
  135. rm -rf $file_name
  136. curl -H 'Cache-Control: no-cache' -L https://dl.google.com/go/$file_name -o $file_name
  137. tar -C $temp_path -xzf $file_name
  138. [[ $? != 0 ]] && { color_echo $yellow "\n解压失败!"; rm -rf $temp_path $file_name; exit 1; }
  139. fi
  140. [[ -e /usr/local/go ]] && $sudo rm -rf /usr/local/go
  141. $sudo mv $temp_path/go /usr/local/
  142. rm -rf $temp_path $file_name
  143. }
  144. install_updater(){
  145. if [[ $os == "Linux" ]];then
  146. if [[ ! -e /usr/local/bin/goupdate || -z `cat /usr/local/bin/goupdate|grep '$@'` ]];then
  147. echo 'source <(curl -L https://go-install.netlify.app/install.sh) $@' > /usr/local/bin/goupdate
  148. chmod +x /usr/local/bin/goupdate
  149. fi
  150. elif [[ $os == "Darwin" ]];then
  151. if [[ ! -e $HOME/go/bin/goupdate || -z `cat $HOME/go/bin/goupdate|grep '$@'` ]];then
  152. cat > $HOME/go/bin/goupdate << 'EOF'
  153. #!/bin/zsh
  154. source <(curl -L https://go-install.netlify.app/install.sh) $@
  155. EOF
  156. chmod +x $HOME/go/bin/goupdate
  157. fi
  158. fi
  159. }
  160. main(){
  161. sys_arch
  162. check_network
  163. install_go
  164. setup_env
  165. setup_proxy
  166. install_updater
  167. echo -e "golang `color_echo $blue $install_version` 安装成功!"
  168. }
  169. main