Skip to content

Commit 63e2352

Browse files
authored
Use GOPROXY env var on version check (#46)
* allow goproxy for version checks * up version
1 parent 071eb59 commit 63e2352

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.7.1
1+
v1.7.2

version/version.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io"
77
"net/http"
88
"os"
9+
"os/exec"
10+
"strings"
911

1012
"golang.org/x/mod/semver"
1113
)
@@ -61,22 +63,44 @@ func isUpdateRequired(current string, latest string) bool {
6163
}
6264

6365
func getLatestVersion() (string, error) {
64-
resp, err := http.Get(fmt.Sprintf("https://proxy.golang.org/github.com/%s/%s/@latest", repoOwner, repoName))
66+
cmd := exec.Command("go", "env", "GOPROXY")
67+
output, err := cmd.Output()
6568
if err != nil {
6669
return "", err
6770
}
68-
defer resp.Body.Close()
6971

70-
body, err := io.ReadAll(resp.Body)
71-
if err != nil {
72-
return "", err
72+
goproxy := strings.TrimSpace(string(output))
73+
if goproxy == "" {
74+
goproxy = "https://proxy.golang.org"
7375
}
7476

75-
var version struct{ Version string }
76-
err = json.Unmarshal(body, &version)
77-
if err != nil {
78-
return "", err
77+
proxies := strings.Split(goproxy, ",")
78+
for _, proxy := range proxies {
79+
proxy = strings.TrimSpace(proxy)
80+
proxy = strings.TrimRight(proxy, "/")
81+
if proxy == "direct" {
82+
continue
83+
}
84+
85+
url := fmt.Sprintf("%s/github.com/%s/%s/@latest", proxy, repoOwner, repoName)
86+
resp, err := http.Get(url)
87+
if err != nil {
88+
continue
89+
}
90+
defer resp.Body.Close()
91+
92+
body, err := io.ReadAll(resp.Body)
93+
if err != nil {
94+
return "", err
95+
}
96+
97+
var version struct{ Version string }
98+
if err = json.Unmarshal(body, &version); err != nil {
99+
return "", err
100+
}
101+
102+
return version.Version, nil
79103
}
80104

81-
return version.Version, nil
105+
return "", fmt.Errorf("failed to fetch latest version from proxies")
82106
}

0 commit comments

Comments
 (0)