Implement reading from previous files as a cache mechanism

This commit is contained in:
adisbladis 2020-07-20 17:46:51 +02:00
parent 634635dc70
commit 23847ecd85
No known key found for this signature in database
GPG key ID: 110BFAD44C6249B7
3 changed files with 45 additions and 12 deletions

View file

@ -3,6 +3,7 @@ package fetch
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/tweag/gomod2nix/formats/gomod2nix"
"github.com/tweag/gomod2nix/types" "github.com/tweag/gomod2nix/types"
"golang.org/x/mod/modfile" "golang.org/x/mod/modfile"
"golang.org/x/tools/go/vcs" "golang.org/x/tools/go/vcs"
@ -23,9 +24,9 @@ type packageResult struct {
err error err error
} }
func worker(id int, replace map[string]string, jobs <-chan *packageJob, results chan<- *packageResult) { func worker(id int, cache map[string]*types.Package, jobs <-chan *packageJob, results chan<- *packageResult) {
for j := range jobs { for j := range jobs {
pkg, err := fetchPackage(j.importPath, j.goPackagePath, j.rev) pkg, err := fetchPackage(cache, j.importPath, j.goPackagePath, j.rev)
results <- &packageResult{ results <- &packageResult{
err: err, err: err,
pkg: pkg, pkg: pkg,
@ -33,7 +34,12 @@ func worker(id int, replace map[string]string, jobs <-chan *packageJob, results
} }
} }
func FetchPackages(goModPath string, goSumPath string, numWorkers int, keepGoing bool) ([]*types.Package, error) { // It's a relatively common idiom to tag storage/v1.0.0
func mkNewRev(goPackagePath string, repoRoot *vcs.RepoRoot, rev string) string {
return fmt.Sprintf("%s/%s", strings.TrimPrefix(goPackagePath, repoRoot.Root+"/"), rev)
}
func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, numWorkers int, keepGoing bool) ([]*types.Package, error) {
// Read go.mod // Read go.mod
data, err := ioutil.ReadFile(goModPath) data, err := ioutil.ReadFile(goModPath)
@ -47,6 +53,8 @@ func FetchPackages(goModPath string, goSumPath string, numWorkers int, keepGoing
return nil, err return nil, err
} }
cache := gomod2nix.LoadGomod2Nix(goMod2NixPath)
// // Parse require // // Parse require
// require := make(map[string]module.Version) // require := make(map[string]module.Version)
// for _, req := range mod.Require { // for _, req := range mod.Require {
@ -72,7 +80,7 @@ func FetchPackages(goModPath string, goSumPath string, numWorkers int, keepGoing
jobs := make(chan *packageJob, numJobs) jobs := make(chan *packageJob, numJobs)
results := make(chan *packageResult, numJobs) results := make(chan *packageResult, numJobs)
for i := 0; i <= numWorkers; i++ { for i := 0; i <= numWorkers; i++ {
go worker(i, replace, jobs, results) go worker(i, cache, jobs, results)
} }
for goPackagePath, rev := range revs { for goPackagePath, rev := range revs {
@ -112,12 +120,22 @@ func FetchPackages(goModPath string, goSumPath string, numWorkers int, keepGoing
return pkgs, nil return pkgs, nil
} }
func fetchPackage(importPath string, goPackagePath string, rev string) (*types.Package, error) { func fetchPackage(cache map[string]*types.Package, importPath string, goPackagePath string, rev string) (*types.Package, error) {
repoRoot, err := vcs.RepoRootForImportPath(importPath, false) repoRoot, err := vcs.RepoRootForImportPath(importPath, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
newRev := mkNewRev(goPackagePath, repoRoot, rev)
cached, ok := cache[goPackagePath]
if ok {
for _, rev := range []string{rev, newRev} {
if cached.Rev == rev {
return cached, nil
}
}
}
if repoRoot.VCS.Name != "Git" { if repoRoot.VCS.Name != "Git" {
return nil, fmt.Errorf("Only git repositories are supported") return nil, fmt.Errorf("Only git repositories are supported")
} }
@ -139,10 +157,7 @@ func fetchPackage(importPath string, goPackagePath string, rev string) (*types.P
"--url", repoRoot.Repo, "--url", repoRoot.Repo,
"--rev", rev).Output() "--rev", rev).Output()
if err != nil { if err != nil {
// It's a relatively common idiom to tag storage/v1.0.0
newRev := fmt.Sprintf("%s/%s", strings.TrimPrefix(goPackagePath, repoRoot.Root+"/"), rev)
originalErr := err originalErr := err
stdout, err = exec.Command( stdout, err = exec.Command(
"nix-prefetch-git", "nix-prefetch-git",
"--quiet", "--quiet",

View file

@ -53,5 +53,14 @@ func LoadGomod2Nix(filePath string) map[string]*types.Package {
return ret return ret
} }
for k, v := range result {
ret[k] = &types.Package{
GoPackagePath: k,
URL: v.URL,
Rev: v.Rev,
Sha256: v.Sha256,
}
}
return ret return ret
} }

17
main.go
View file

@ -14,16 +14,25 @@ func main() {
flag.Parse() flag.Parse()
numWorkers := 20 numWorkers := 1
keepGoing := true keepGoing := false
directory := "./" // directory := "./"
directory := "./testdata/vuls"
outFile := "gomod2nix.toml" outFile := "gomod2nix.toml"
pkgs, err := fetch.FetchPackages(filepath.Join(directory, "go.mod"), filepath.Join(directory, "go.sum"), numWorkers, keepGoing) goModPath := filepath.Join(directory, "go.mod")
goSumPath := filepath.Join(directory, "go.sum")
goMod2NixPath := "./gomod2nix.toml"
pkgs, err := fetch.FetchPackages(goModPath, goSumPath, goMod2NixPath, numWorkers, keepGoing)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if true {
panic("Success")
}
// output, err := buildgopackage.Marshal(pkgs) // output, err := buildgopackage.Marshal(pkgs)
output, err := gomod2nix.Marshal(pkgs) output, err := gomod2nix.Marshal(pkgs)
if err != nil { if err != nil {