|
6 | 6 | "io" |
7 | 7 | "net/http" |
8 | 8 | "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
9 | 11 |
|
10 | 12 | "golang.org/x/mod/semver" |
11 | 13 | ) |
@@ -61,22 +63,44 @@ func isUpdateRequired(current string, latest string) bool { |
61 | 63 | } |
62 | 64 |
|
63 | 65 | 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() |
65 | 68 | if err != nil { |
66 | 69 | return "", err |
67 | 70 | } |
68 | | - defer resp.Body.Close() |
69 | 71 |
|
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" |
73 | 75 | } |
74 | 76 |
|
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 |
79 | 103 | } |
80 | 104 |
|
81 | | - return version.Version, nil |
| 105 | + return "", fmt.Errorf("failed to fetch latest version from proxies") |
82 | 106 | } |
0 commit comments