Add flags to CLI
This commit is contained in:
parent
23847ecd85
commit
51402965c7
4 changed files with 62 additions and 31 deletions
|
@ -3,6 +3,7 @@ package fetch
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/tweag/gomod2nix/formats/buildgopackage"
|
||||
"github.com/tweag/gomod2nix/formats/gomod2nix"
|
||||
"github.com/tweag/gomod2nix/types"
|
||||
"golang.org/x/mod/modfile"
|
||||
|
@ -24,9 +25,9 @@ type packageResult struct {
|
|||
err error
|
||||
}
|
||||
|
||||
func worker(id int, cache map[string]*types.Package, jobs <-chan *packageJob, results chan<- *packageResult) {
|
||||
func worker(id int, caches []map[string]*types.Package, jobs <-chan *packageJob, results chan<- *packageResult) {
|
||||
for j := range jobs {
|
||||
pkg, err := fetchPackage(cache, j.importPath, j.goPackagePath, j.rev)
|
||||
pkg, err := fetchPackage(caches, j.importPath, j.goPackagePath, j.rev)
|
||||
results <- &packageResult{
|
||||
err: err,
|
||||
pkg: pkg,
|
||||
|
@ -39,7 +40,7 @@ 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) {
|
||||
func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, depsNixPath string, numWorkers int, keepGoing bool) ([]*types.Package, error) {
|
||||
|
||||
// Read go.mod
|
||||
data, err := ioutil.ReadFile(goModPath)
|
||||
|
@ -53,7 +54,10 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
|
|||
return nil, err
|
||||
}
|
||||
|
||||
cache := gomod2nix.LoadGomod2Nix(goMod2NixPath)
|
||||
caches := []map[string]*types.Package{
|
||||
gomod2nix.LoadGomod2Nix(goMod2NixPath),
|
||||
buildgopackage.LoadDepsNix(depsNixPath),
|
||||
}
|
||||
|
||||
// // Parse require
|
||||
// require := make(map[string]module.Version)
|
||||
|
@ -80,7 +84,7 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
|
|||
jobs := make(chan *packageJob, numJobs)
|
||||
results := make(chan *packageResult, numJobs)
|
||||
for i := 0; i <= numWorkers; i++ {
|
||||
go worker(i, cache, jobs, results)
|
||||
go worker(i, caches, jobs, results)
|
||||
}
|
||||
|
||||
for goPackagePath, rev := range revs {
|
||||
|
@ -120,18 +124,20 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
|
|||
return pkgs, nil
|
||||
}
|
||||
|
||||
func fetchPackage(cache map[string]*types.Package, importPath string, goPackagePath string, rev string) (*types.Package, error) {
|
||||
func fetchPackage(caches []map[string]*types.Package, importPath string, goPackagePath string, rev string) (*types.Package, error) {
|
||||
repoRoot, err := vcs.RepoRootForImportPath(importPath, false)
|
||||
if err != nil {
|
||||
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
|
||||
for _, cache := range caches {
|
||||
cached, ok := cache[goPackagePath]
|
||||
if ok {
|
||||
for _, rev := range []string{rev, newRev} {
|
||||
if cached.Rev == rev {
|
||||
return cached, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,10 @@ func Marshal(pkgs []*types.Package) ([]byte, error) {
|
|||
func LoadDepsNix(filePath string) map[string]*types.Package {
|
||||
ret := make(map[string]*types.Package)
|
||||
|
||||
if filePath == "" {
|
||||
return ret
|
||||
}
|
||||
|
||||
stat, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return ret
|
||||
|
|
|
@ -40,6 +40,10 @@ func Marshal(pkgs []*types.Package) ([]byte, error) {
|
|||
func LoadGomod2Nix(filePath string) map[string]*types.Package {
|
||||
ret := make(map[string]*types.Package)
|
||||
|
||||
if filePath == "" {
|
||||
return ret
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
|
57
main.go
57
main.go
|
@ -4,7 +4,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"github.com/tweag/gomod2nix/fetch"
|
||||
// "github.com/tweag/gomod2nix/formats/buildgopackage"
|
||||
"github.com/tweag/gomod2nix/formats/buildgopackage"
|
||||
"github.com/tweag/gomod2nix/formats/gomod2nix"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
@ -12,37 +12,54 @@ import (
|
|||
|
||||
func main() {
|
||||
|
||||
var keepGoing = flag.Bool("keep-going", false, "Whether to panic or not if a rev cannot be resolved (default \"false\")")
|
||||
var directory = flag.String("dir", "./", "Go project directory")
|
||||
var maxJobs = flag.Int("jobs", 10, "Number of max parallel jobs")
|
||||
var outFile = flag.String("outfile", "gomod2nix.toml", "output file")
|
||||
var format = flag.String("format", "gomod2nix", "output format (gomod2nix, buildgopackage)")
|
||||
flag.Parse()
|
||||
|
||||
numWorkers := 1
|
||||
keepGoing := false
|
||||
// directory := "./"
|
||||
directory := "./testdata/vuls"
|
||||
outFile := "gomod2nix.toml"
|
||||
goSumPath := filepath.Join(*directory, "go.sum")
|
||||
goModPath := filepath.Join(*directory, "go.mod")
|
||||
|
||||
goModPath := filepath.Join(directory, "go.mod")
|
||||
goSumPath := filepath.Join(directory, "go.sum")
|
||||
goMod2NixPath := "./gomod2nix.toml"
|
||||
wrongFormatError := fmt.Errorf("Format not supported")
|
||||
|
||||
pkgs, err := fetch.FetchPackages(goModPath, goSumPath, goMod2NixPath, numWorkers, keepGoing)
|
||||
goMod2NixPath := ""
|
||||
depsNixPath := ""
|
||||
switch *format {
|
||||
case "gomod2nix":
|
||||
goMod2NixPath = filepath.Join(*directory, "gomod2nix.toml")
|
||||
case "buildgopackage":
|
||||
depsNixPath = filepath.Join(*directory, "deps.nix")
|
||||
default:
|
||||
panic(wrongFormatError)
|
||||
}
|
||||
|
||||
pkgs, err := fetch.FetchPackages(goModPath, goSumPath, goMod2NixPath, depsNixPath, *maxJobs, *keepGoing)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if true {
|
||||
panic("Success")
|
||||
var output []byte
|
||||
switch *format {
|
||||
case "gomod2nix":
|
||||
output, err = gomod2nix.Marshal(pkgs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
case "buildgopackage":
|
||||
output, err = buildgopackage.Marshal(pkgs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
default:
|
||||
panic(wrongFormatError)
|
||||
}
|
||||
|
||||
// output, err := buildgopackage.Marshal(pkgs)
|
||||
output, err := gomod2nix.Marshal(pkgs)
|
||||
err = ioutil.WriteFile(*outFile, output, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(outFile, output, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(fmt.Sprintf("Wrote: %s", outFile))
|
||||
fmt.Println(fmt.Sprintf("Wrote: %s", *outFile))
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue