Skip to content

Commit

Permalink
Use GOPROXY env var on version check (#46)
Browse files Browse the repository at this point in the history
* allow goproxy for version checks

* up version
  • Loading branch information
hbeckmann committed Aug 22, 2024
1 parent 071eb59 commit 63e2352
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.7.1
v1.7.2
44 changes: 34 additions & 10 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"net/http"
"os"
"os/exec"
"strings"

"golang.org/x/mod/semver"
)
Expand Down Expand Up @@ -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")
}

0 comments on commit 63e2352

Please # to comment.