start proxy download logic

This commit is contained in:
Shawn Smith
2021-11-10 22:07:41 +09:00
parent 1e3fdd8090
commit d2ac1b64f8
2 changed files with 69 additions and 0 deletions

64
download/proxy.go Normal file
View File

@@ -0,0 +1,64 @@
package download
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)
const (
proxyLatestURL = "https://proxy.golang.org/%s/@latest"
proxyZipURL = "https://proxy.golang.org/%s/@v/%s.zip"
reposDir = "_repos/src"
)
type moduleVersion struct {
Version string
}
// ProxyDownload downloads a package from proxy.golang.org
func ProxyDownload(path string) error {
resp, err := http.Get(fmt.Sprintf(proxyLatestURL, path))
if err != nil {
return err
}
defer resp.Body.Close()
var mv moduleVersion
err = json.NewDecoder(resp.Body).Decode(&mv)
if err != nil {
return err
}
fmt.Println(mv)
resp, err = http.Get(fmt.Sprintf(proxyZipURL, path, mv.Version))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("status %d", resp.StatusCode)
}
zipPath := filepath.Base(path) + "@" + mv.Version + ".zip"
out, err := os.Create(filepath.Join(reposDir, zipPath))
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}

View File

@@ -95,6 +95,11 @@ func newChecksResp(db *badger.DB, repo string, forceRefresh bool) (checksResp, e
return checksResp{}, fmt.Errorf("could not clone repo: %v", err)
}
// err = download.ProxyDownload(repo)
// if err != nil {
// fmt.Println("ERROR:", err)
// }
repo = repoRoot.Root
checkResult, err := check.Run(dirName(repo))
if err != nil {