| 68 | } |
| 69 | |
| 70 | func getLatestVersion() (string, error) { |
| 71 | goproxyDefault := "https://proxy.golang.org" |
| 72 | goproxy := goproxyDefault |
| 73 | cmd := exec.Command("go", "env", "GOPROXY") |
| 74 | output, err := cmd.Output() |
| 75 | if err == nil { |
| 76 | goproxy = strings.TrimSpace(string(output)) |
| 77 | } |
| 78 | |
| 79 | proxies := strings.Split(goproxy, ",") |
| 80 | if !slices.Contains(proxies, goproxyDefault) { |
| 81 | proxies = append(proxies, goproxyDefault) |
| 82 | } |
| 83 | |
| 84 | for _, proxy := range proxies { |
| 85 | proxy = strings.TrimSpace(proxy) |
| 86 | proxy = strings.TrimRight(proxy, "/") |
| 87 | if proxy == "direct" || proxy == "off" { |
| 88 | continue |
| 89 | } |
| 90 | |
| 91 | url := fmt.Sprintf("%s/github.com/%s/%s/@latest", proxy, repoOwner, repoName) |
| 92 | resp, err := http.Get(url) |
| 93 | if err != nil { |
| 94 | continue |
| 95 | } |
| 96 | defer resp.Body.Close() |
| 97 | |
| 98 | body, err := io.ReadAll(resp.Body) |
| 99 | if err != nil { |
| 100 | continue |
| 101 | } |
| 102 | |
| 103 | var version struct{ Version string } |
| 104 | if err = json.Unmarshal(body, &version); err != nil { |
| 105 | continue |
| 106 | } |
| 107 | |
| 108 | return version.Version, nil |
| 109 | } |
| 110 | |
| 111 | return "", fmt.Errorf("failed to fetch latest version") |
| 112 | } |