码迷,mamicode.com
首页 > 其他好文 > 详细

gvm--go版本管理工具

时间:2020-02-25 18:10:42      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:line   efault   which   amazon   inux   ssi   ror   start   term   

gvm是go的多版本管理工具。

安装

由于网络问题,部分国外网站无法访问:https://raw.githubusercontent.com https://github.com

安装依赖软件

yum -y install gcc bison

安装gvm

bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
如果以上命令没用,浏览器访问 https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer 获取脚本粘贴到server 再执行。
脚本内容:

cat gvm_installer 
#!/usr/bin/env bash

display_error() {
    tput sgr0
    tput setaf 1
    echo "ERROR: $1"
    tput sgr0
    exit 1
}

update_profile() {
    [ -f "$1" ] || return 1

    grep -F "$source_line" "$1" > /dev/null 2>&1
    if [ $? -ne 0 ]; then
            echo -e "\n$source_line" >> "$1"
    fi
}

check_existing_go() {

    if [ "$GOROOT" = "" ]; then
            if which go > /dev/null; then
                    GOROOT=$(go env | grep GOROOT | cut -d"=" -f2)
            else
                    echo "No existing Go versions detected"
                    return
            fi
    fi
    echo "Created profile for existing install of Go at $GOROOT"
    mkdir -p "$GVM_DEST/$GVM_NAME/environments" &> /dev/null || display_error "Failed to create environment directory"
    mkdir -p "$GVM_DEST/$GVM_NAME/pkgsets/system/global" &> /dev/null || display_error "Failed to create new package set"
    mkdir -p "$GVM_DEST/$GVM_NAME/gos/system" &> /dev/null || display_error "Failed to create new Go folder"
    cat << EOF > $GVM_DEST/$GVM_NAME/environments/system
# Automatically generated file. DO NOT EDIT!
export GVM_ROOT; GVM_ROOT="$GVM_DEST/$GVM_NAME"
export gvm_go_name; gvm_go_name="system"
export gvm_pkgset_name; gvm_pkgset_name="global"
export GOROOT; GOROOT="$GOROOT"
export GOPATH; GOPATH="$GVM_DEST/$GVM_NAME/pkgsets/system/global"
export PATH; PATH="$GVM_DEST/$GVM_NAME/pkgsets/system/global/bin:$GOROOT/bin:$GVM_ROOT/bin:\$PATH"
EOF
}

BRANCH=${1:-master}
GVM_DEST=${2:-$HOME}
GVM_NAME="gvm"
SRC_REPO=${SRC_REPO:-https://github.com/moovweb/gvm.git}

[ "$GVM_DEST" = "$HOME" ] && GVM_NAME=".gvm"

[ -d "$GVM_DEST/$GVM_NAME" ] && display_error     "Already installed! Remove old installation by running

    rm -rf $GVM_DEST/$GVM_NAME"

[ -d "$GVM_DEST" ] || mkdir -p "$GVM_DEST" > /dev/null 2>&1 || display_error "Failed to create $GVM_DEST"
[ -z "$(which git)" ] && display_error "Could not find git

  debian/ubuntu: apt-get install git
  redhat/centos: yum install git
  mac:   brew install git
"

# Is gvm-installer being called from the origin repo?
# If so, skip the clone and source locally!
# This prevents CI from breaking on non-merge commits.

GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)

if [[ -z "$GIT_ROOT" || "$(basename "$GIT_ROOT")" != "gvm" ]]
then
  echo "Cloning from $SRC_REPO to $GVM_DEST/$GVM_NAME"

  git clone --quiet "$SRC_REPO" "$GVM_DEST/$GVM_NAME" 2> /dev/null ||
      display_error "Failed to clone from $SRC_REPO into $GVM_DEST/$GVM_NAME"
else
  if [[ $GVM_DEST == *"$GIT_ROOT"* ]]
  then
    ln -s "$GIT_ROOT" "$GVM_DEST"
  else
    cp -r "$GIT_ROOT" "$GVM_DEST/$GVM_NAME"
  fi
fi

# GVM_DEST may be a non-relative path
# i.e: gvm-installer master foo
pushd . > /dev/null

cd "$GVM_DEST/$GVM_NAME" && git checkout --quiet "$BRANCH" 2> /dev/null ||  display_error "Failed to checkout $BRANCH branch"

popd > /dev/null

[ -z "$GVM_NO_GIT_BAK" ] && mv "$GVM_DEST/$GVM_NAME/.git" "$GVM_DEST/$GVM_NAME/git.bak"

source_line="[[ -s \"${GVM_DEST}/$GVM_NAME/scripts/gvm\" ]] && source \"${GVM_DEST}/$GVM_NAME/scripts/gvm\""
source_file="${GVM_DEST}/$GVM_NAME/scripts/gvm"

if [ -z "$GVM_NO_UPDATE_PROFILE" ] ; then
  if [ -n "$ZSH_NAME" ]; then
    update_profile "$HOME/.zshrc"
  elif [ "$(uname)" == "Linux" ]; then
    update_profile "$HOME/.bashrc" || update_profile "$HOME/.bash_profile"
  elif [ "$(uname)" == "Darwin" ]; then
    update_profile "$HOME/.profile" || update_profile "$HOME/.bash_profile"
  fi
fi

if [ -z "$GVM_NO_UPDATE_PROFILE" ] && [ "$?" != "0" ]; then
    echo "Unable to locate profile settings file(Something like $HOME/.bashrc or $HOME/.bash_profile)"
    echo
    echo " You will have to manually add the following line:"
    echo
    echo "  $source_line"
    echo
fi

echo "export GVM_ROOT=$GVM_DEST/$GVM_NAME" > "$GVM_DEST/$GVM_NAME/scripts/gvm"
echo ". \$GVM_ROOT/scripts/gvm-default" >> "$GVM_DEST/$GVM_NAME/scripts/gvm"
check_existing_go
[[ -s "$GVM_DEST/$GVM_NAME/scripts/gvm" ]] && source "$GVM_DEST/$GVM_NAME/scripts/gvm"
echo "Installed GVM v${GVM_VERSION}"
echo
echo "Please restart your terminal session or to get started right away run"
echo " \`source ${source_file}\`"
echo

脚本执行后会发现
技术图片
修改本地hosts,绕开dns

vim /etc/hosts
192.30.253.118  gist.github.com
192.30.253.112  github.com
219.76.4.4  github-cloud.s3.amazonaws.com

bash gvm_installer
source /root/.gvm/scripts/gvm 
gvm #该命令有了

然后删除新加的hosts域名解析

装某个版本的go

 gvm install go1.13 -B -s=https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz
 gvm list

技术图片

安装google/wire

gvm use go1.13
export GOPROXY=https://goproxy.cn
export GO111MODULE="on"
go get github.com/google/wire/cmd/wire

技术图片
技术图片

gvm--go版本管理工具

标签:line   efault   which   amazon   inux   ssi   ror   start   term   

原文地址:https://www.cnblogs.com/huandada/p/12074752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!