Skip to content

Commit

Permalink
added mirror support and new test
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanisham committed Sep 13, 2024
1 parent f67a639 commit 0841446
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 69 deletions.
21 changes: 11 additions & 10 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ type zigVersion = map[string]any

type ZigOnlVersion = map[string][]map[string]string

// func (z *ZVM) loadVersionCache() error {
// ver, err := os.ReadFile(filepath.Join(z.zvmBaseDir, "versions.json"))
// if err != nil {
// return err
// }
// if err := json.Unmarshal(ver, &z.zigVersions); err != nil {
// return err
// }
// return nil
// }
// func (z *ZVM) loadVersionCache() error {
// ver, err := os.ReadFile(filepath.Join(z.zvmBaseDir, "versions.json"))
// if err != nil {
// return err
// }
// if err := json.Unmarshal(ver, &z.zigVersions); err != nil {
// return err
// }
// return nil
// }
//
// TODO switch to error so we can handle common typos. Make it return an (error, bool)
func validVmuAlis(version string) bool {
return version == "default" || version == "mach"
Expand Down
71 changes: 35 additions & 36 deletions cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,59 +197,58 @@ func (z *ZVM) Install(version string) error {
return nil
}
func requestWithMirror(tarURL string) (*http.Response, error) {
log.Debug("requestWithMirror", "tarURL", tarURL)
log.Debug("requestWithMirror", "tarURL", tarURL)

tarResp, err := attemptDownload(tarURL)
if err != nil {
return nil, err
}
tarResp, err := attemptDownload(tarURL)
if err != nil {
return nil, err
}

if tarResp.StatusCode == 200 {
return tarResp, nil
}
if tarResp.StatusCode == 200 {
return tarResp, nil
}

mirrors := []func(string) (string, error){mirrorHryx, mirrorMachEngine}
mirrors := []func(string) (string, error){mirrorHryx, mirrorMachEngine}

for i, mirror := range mirrors {
log.Debugf("requestWithMirror url #%d", i)
for i, mirror := range mirrors {
log.Debugf("requestWithMirror url #%d", i)

newURL, err := mirror(tarURL)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrDownloadFail, err)
}
newURL, err := mirror(tarURL)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrDownloadFail, err)
}

log.Debug(fmt.Sprintf("mirror %d", i), "url", newURL)
log.Debug(fmt.Sprintf("mirror %d", i), "url", newURL)

tarResp, err = attemptDownload(newURL)
if err != nil {
continue
}
tarResp, err = attemptDownload(newURL)
if err != nil {
continue
}

if tarResp.StatusCode == 200 {
return tarResp, nil
}
}
if tarResp.StatusCode == 200 {
return tarResp, nil
}
}

return nil, errors.Join(err, fmt.Errorf("all download attempts failed"))
return nil, errors.Join(err, fmt.Errorf("all download attempts failed"))
}

func attemptDownload(url string) (*http.Response, error) {
req, err := createDownloadReq(url)
if err != nil {
return nil, err
}
req, err := createDownloadReq(url)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

log.Debug("requestWithMirror", "status code", resp.StatusCode)
log.Debug("requestWithMirror", "status code", resp.StatusCode)

return resp, nil
return resp, nil
}


func createDownloadReq(tarURL string) (*http.Request, error) {
zigArch, zigOS := zigStyleSysInfo()

Expand Down
33 changes: 16 additions & 17 deletions cli/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,23 @@ func TestMirrors(t *testing.T) {
tarURL := "https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.1550+4fba7336a.tar.xz"
mirrors := []func(string) (string, error){mirrorHryx, mirrorMachEngine}

for i, mirror := range mirrors {
t.Logf("requestWithMirror url #%d", i)
for i, mirror := range mirrors {
t.Logf("requestWithMirror url #%d", i)

newURL, err := mirror(tarURL)
if err != nil {
t.Errorf("%q: %q", ErrDownloadFail, err)
}

newURL, err := mirror(tarURL)
if err != nil {
t.Errorf("%q: %q", ErrDownloadFail, err)
}
t.Logf("mirror %d; url: %s", i, newURL)

t.Logf("mirror %d; url: %s", i, newURL)
tarResp, err := attemptDownload(newURL)
if err != nil {
continue
}

tarResp, err := attemptDownload(newURL)
if err != nil {
continue
}

if tarResp.StatusCode != 200 {
t.Fail()
}
}
}
if tarResp.StatusCode != 200 {
t.Fail()
}
}
}
2 changes: 1 addition & 1 deletion cli/meta/cta.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ func CtaUpgradeAvailable(tag string) {
Foreground(lipgloss.Color("#fee12b"))

fmt.Printf("\nZVM %s is available. You are currently on %s.\n\nRun %s or download the latest release at\n%s\n\n", blueLink.Render(tag), blueLink.Render(VERSION), yellowText.Render("zvm upgrade"), blueLink.Render("https://github.com/tristanisham/zvm/releases/latest"))

}
4 changes: 2 additions & 2 deletions cli/meta/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ package meta
import "errors"

var (
ErrWinEscToAdmin = errors.New("unable to rerun as Windows Administrator")
ErrWinEscToAdmin = errors.New("unable to rerun as Windows Administrator")
ErrEscalatedSymlink = errors.New("unable to symlink as Administrator")
)
)
4 changes: 1 addition & 3 deletions cli/meta/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
)

const (
VERSION = "v0.7.4"
VERSION = "v0.7.5"
// VERSION = "v0.0.0" // For testing zvm upgrade

)


var (
VerCopy = fmt.Sprintf("%s %s/%s", VERSION, runtime.GOOS, runtime.GOARCH)
)

0 comments on commit 0841446

Please sign in to comment.