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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/tweag/gomod2nix/formats/buildgopackage"
|
||||||
"github.com/tweag/gomod2nix/formats/gomod2nix"
|
"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"
|
||||||
|
@ -24,9 +25,9 @@ type packageResult struct {
|
||||||
err error
|
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 {
|
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{
|
results <- &packageResult{
|
||||||
err: err,
|
err: err,
|
||||||
pkg: pkg,
|
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)
|
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
|
// Read go.mod
|
||||||
data, err := ioutil.ReadFile(goModPath)
|
data, err := ioutil.ReadFile(goModPath)
|
||||||
|
@ -53,7 +54,10 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache := gomod2nix.LoadGomod2Nix(goMod2NixPath)
|
caches := []map[string]*types.Package{
|
||||||
|
gomod2nix.LoadGomod2Nix(goMod2NixPath),
|
||||||
|
buildgopackage.LoadDepsNix(depsNixPath),
|
||||||
|
}
|
||||||
|
|
||||||
// // Parse require
|
// // Parse require
|
||||||
// require := make(map[string]module.Version)
|
// require := make(map[string]module.Version)
|
||||||
|
@ -80,7 +84,7 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
|
||||||
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, cache, jobs, results)
|
go worker(i, caches, jobs, results)
|
||||||
}
|
}
|
||||||
|
|
||||||
for goPackagePath, rev := range revs {
|
for goPackagePath, rev := range revs {
|
||||||
|
@ -120,18 +124,20 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
|
||||||
return pkgs, nil
|
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)
|
repoRoot, err := vcs.RepoRootForImportPath(importPath, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
newRev := mkNewRev(goPackagePath, repoRoot, rev)
|
newRev := mkNewRev(goPackagePath, repoRoot, rev)
|
||||||
cached, ok := cache[goPackagePath]
|
for _, cache := range caches {
|
||||||
if ok {
|
cached, ok := cache[goPackagePath]
|
||||||
for _, rev := range []string{rev, newRev} {
|
if ok {
|
||||||
if cached.Rev == rev {
|
for _, rev := range []string{rev, newRev} {
|
||||||
return cached, nil
|
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 {
|
func LoadDepsNix(filePath string) map[string]*types.Package {
|
||||||
ret := make(map[string]*types.Package)
|
ret := make(map[string]*types.Package)
|
||||||
|
|
||||||
|
if filePath == "" {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
stat, err := os.Stat(filePath)
|
stat, err := os.Stat(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -40,6 +40,10 @@ func Marshal(pkgs []*types.Package) ([]byte, error) {
|
||||||
func LoadGomod2Nix(filePath string) map[string]*types.Package {
|
func LoadGomod2Nix(filePath string) map[string]*types.Package {
|
||||||
ret := make(map[string]*types.Package)
|
ret := make(map[string]*types.Package)
|
||||||
|
|
||||||
|
if filePath == "" {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(filePath)
|
b, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
57
main.go
57
main.go
|
@ -4,7 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tweag/gomod2nix/fetch"
|
"github.com/tweag/gomod2nix/fetch"
|
||||||
// "github.com/tweag/gomod2nix/formats/buildgopackage"
|
"github.com/tweag/gomod2nix/formats/buildgopackage"
|
||||||
"github.com/tweag/gomod2nix/formats/gomod2nix"
|
"github.com/tweag/gomod2nix/formats/gomod2nix"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -12,37 +12,54 @@ import (
|
||||||
|
|
||||||
func main() {
|
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()
|
flag.Parse()
|
||||||
|
|
||||||
numWorkers := 1
|
goSumPath := filepath.Join(*directory, "go.sum")
|
||||||
keepGoing := false
|
goModPath := filepath.Join(*directory, "go.mod")
|
||||||
// directory := "./"
|
|
||||||
directory := "./testdata/vuls"
|
|
||||||
outFile := "gomod2nix.toml"
|
|
||||||
|
|
||||||
goModPath := filepath.Join(directory, "go.mod")
|
wrongFormatError := fmt.Errorf("Format not supported")
|
||||||
goSumPath := filepath.Join(directory, "go.sum")
|
|
||||||
goMod2NixPath := "./gomod2nix.toml"
|
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if true {
|
var output []byte
|
||||||
panic("Success")
|
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)
|
err = ioutil.WriteFile(*outFile, output, 0644)
|
||||||
output, err := gomod2nix.Marshal(pkgs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
fmt.Println(fmt.Sprintf("Wrote: %s", *outFile))
|
||||||
err = ioutil.WriteFile(outFile, output, 0644)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
fmt.Println(fmt.Sprintf("Wrote: %s", outFile))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue