From 1ba6707609ea359be7f7e0b06a3ce08df5ba82d7 Mon Sep 17 00:00:00 2001 From: binary4cat Date: Mon, 13 Dec 2021 11:24:32 +0800 Subject: [PATCH] Add the option to download the installation package from custom mirror. --- README.md | 16 ++++++++++++++ goinstall.sh | 23 +++++++++++++++++---- tests/install-from-custom-mirror-version.sh | 6 ++++++ tests/install-from-custom-mirror.sh | 6 ++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100755 tests/install-from-custom-mirror-version.sh create mode 100755 tests/install-from-custom-mirror.sh diff --git a/README.md b/README.md index caf8198..0705e14 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,22 @@ wget -q -O - https://raw.githubusercontent.com/canha/golang-tools-install-script curl https://raw.githubusercontent.com/canha/golang-tools-install-script/master/goinstall.sh | bash ``` +## :dvd: Downloads the installation package from the specified mirror URL + +Pass the `--mirror` option into the script including the mirror URL that you wish to download from, and you can also append `--version` option. + +##### Example: + +```shell +wget -q -O - https://git.io/vQhTU | bash -s -- --mirror https://mirrors.ustc.edu.cn/golang + +# or... +curl -L https://git.io/vQhTU | bash -s -- --mirror https://mirrors.ustc.edu.cn/golang + +# and use custom version... +curl -L https://git.io/vQhTU | bash -s -- --mirror https://mirrors.ustc.edu.cn/golang --version 1.13.2 +``` + ## :package: Install a custom Go version Pass the `--version` option into the script including the version that you wish to install. diff --git a/goinstall.sh b/goinstall.sh index 1cbf079..cdf49a3 100755 --- a/goinstall.sh +++ b/goinstall.sh @@ -3,6 +3,7 @@ set -e VERSION="1.17.3" +MIRROR="https://storage.googleapis.com/golang" [ -z "$GOROOT" ] && GOROOT="$HOME/.go" [ -z "$GOPATH" ] && GOPATH="$HOME/go" @@ -40,7 +41,8 @@ print_help() { echo "Usage: bash goinstall.sh OPTIONS" echo -e "\nOPTIONS:" echo -e " --remove\tRemove currently installed version" - echo -e " --version\tSpecify a version number to install" + echo -e " --mirror\tProvides a mirror URL for the installer to download" + echo -e " --version\tSpecify a version number to install, Can be appended to the --mirror parameter" } if [ -z "$PLATFORM" ]; then @@ -101,6 +103,19 @@ elif [ "$1" == "--version" ]; then else VERSION=$2 fi +elif [ "$1" == "--mirror" ]; then + if [ -z "$2" ];then + echo "Please provide a mirror URL for: $1" + else + MIRROR=$2 + fi + if [ "$3" == "--version" ]; then + if [ -z "$4" ]; then # Check if --version has a second positional parameter + echo "Please provide a version number for: $4" + else + VERSION=$4 + fi + fi elif [ ! -z "$1" ]; then echo "Unrecognized option: $1" exit 1 @@ -114,11 +129,11 @@ fi PACKAGE_NAME="go$VERSION.$PLATFORM.tar.gz" TEMP_DIRECTORY=$(mktemp -d) -echo "Downloading $PACKAGE_NAME ..." +echo "Downloading $MIRROR/$PACKAGE_NAME ..." if hash wget 2>/dev/null; then - wget https://storage.googleapis.com/golang/$PACKAGE_NAME -O "$TEMP_DIRECTORY/go.tar.gz" + wget $MIRROR/$PACKAGE_NAME -O "$TEMP_DIRECTORY/go.tar.gz" else - curl -o "$TEMP_DIRECTORY/go.tar.gz" https://storage.googleapis.com/golang/$PACKAGE_NAME + curl -o "$TEMP_DIRECTORY/go.tar.gz" $MIRROR/$PACKAGE_NAME fi if [ $? -ne 0 ]; then diff --git a/tests/install-from-custom-mirror-version.sh b/tests/install-from-custom-mirror-version.sh new file mode 100755 index 0000000..3a1de36 --- /dev/null +++ b/tests/install-from-custom-mirror-version.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +unset GOROOT +unset GOPATH +"$(dirname "$0")/../goinstall.sh" --mirror https://mirrors.ustc.edu.cn/golang --version "${REQVER}" diff --git a/tests/install-from-custom-mirror.sh b/tests/install-from-custom-mirror.sh new file mode 100755 index 0000000..cc99d06 --- /dev/null +++ b/tests/install-from-custom-mirror.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +unset GOROOT +unset GOPATH +"$(dirname "$0")/../goinstall.sh" --mirror https://mirrors.ustc.edu.cn/golang