From 63e235283b117e0e8f0063e0ff6f3f1baa6834ac Mon Sep 17 00:00:00 2001 From: Hunter Beckmann Date: Thu, 22 Aug 2024 12:05:30 -0600 Subject: [PATCH] Use GOPROXY env var on version check (#46) * allow goproxy for version checks * up version --- version.txt | 2 +- version/version.go | 44 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/version.txt b/version.txt index a97fc44..3b34d22 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.7.1 +v1.7.2 diff --git a/version/version.go b/version/version.go index eb7bb5f..b410def 100644 --- a/version/version.go +++ b/version/version.go @@ -6,6 +6,8 @@ import ( "io" "net/http" "os" + "os/exec" + "strings" "golang.org/x/mod/semver" ) @@ -61,22 +63,44 @@ func isUpdateRequired(current string, latest string) bool { } func getLatestVersion() (string, error) { - resp, err := http.Get(fmt.Sprintf("https://proxy.golang.org/github.com/%s/%s/@latest", repoOwner, repoName)) + cmd := exec.Command("go", "env", "GOPROXY") + output, err := cmd.Output() if err != nil { return "", err } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return "", err + goproxy := strings.TrimSpace(string(output)) + if goproxy == "" { + goproxy = "https://proxy.golang.org" } - var version struct{ Version string } - err = json.Unmarshal(body, &version) - if err != nil { - return "", err + proxies := strings.Split(goproxy, ",") + for _, proxy := range proxies { + proxy = strings.TrimSpace(proxy) + proxy = strings.TrimRight(proxy, "/") + if proxy == "direct" { + continue + } + + url := fmt.Sprintf("%s/github.com/%s/%s/@latest", proxy, repoOwner, repoName) + resp, err := http.Get(url) + if err != nil { + continue + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var version struct{ Version string } + if err = json.Unmarshal(body, &version); err != nil { + return "", err + } + + return version.Version, nil } - return version.Version, nil + return "", fmt.Errorf("failed to fetch latest version from proxies") }