Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in china dl.google.com is not available, add flag -m to use china ustc mirror #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"compress/gzip"
"crypto/sha256"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -41,8 +42,12 @@ func Run(version string) {
log.Fatalf("%s: %v", version, err)
}

if len(os.Args) == 2 && os.Args[1] == "download" {
if err := install(root, version); err != nil {
if len(os.Args) >= 2 && os.Args[1] == "download" {
flagset := flag.NewFlagSet("download", flag.ExitOnError)
mirror := flagset.String("m", "google", "download mirror: ['google','ustc]")
flagset.Parse(os.Args[2:])

if err := install(root, version, *mirror); err != nil {
log.Fatalf("%s: download failed: %v", version, err)
}
os.Exit(0)
Expand Down Expand Up @@ -100,7 +105,7 @@ func fmtSize(size int64) string {

// install installs a version of Go to the named target directory, creating the
// directory as needed.
func install(targetDir, version string) error {
func install(targetDir, version, mirror string) error {
if _, err := os.Stat(filepath.Join(targetDir, unpackedOkay)); err == nil {
log.Printf("%s: already downloaded in %v", version, targetDir)
return nil
Expand All @@ -109,7 +114,8 @@ func install(targetDir, version string) error {
if err := os.MkdirAll(targetDir, 0755); err != nil {
return err
}
goURL := versionArchiveURL(version)
goURL := versionArchiveURL(version, mirror)
fmt.Printf("download from %s\n", goURL)
res, err := http.Head(goURL)
if err != nil {
return err
Expand Down Expand Up @@ -417,8 +423,20 @@ func getOS() string {
return runtime.GOOS
}

// getMirrors return the download url by mirror
func getMirrors(mirror string) string {
switch mirror {
case "ustc":
return "https://mirrors.ustc.edu.cn/golang/"
case "google":
fallthrough
default:
return "https://dl.google.com/go/"
}
}

// versionArchiveURL returns the zip or tar.gz URL of the given Go version.
func versionArchiveURL(version string) string {
func versionArchiveURL(version, mirror string) string {
goos := getOS()

ext := ".tar.gz"
Expand All @@ -429,7 +447,7 @@ func versionArchiveURL(version string) string {
if goos == "linux" && runtime.GOARCH == "arm" {
arch = "armv6l"
}
return "https://dl.google.com/go/" + version + "." + goos + "-" + arch + ext
return getMirrors(mirror) + version + "." + goos + "-" + arch + ext
}

const caseInsensitiveEnv = runtime.GOOS == "windows"
Expand Down