Restructure internal package names

This commit is contained in:
adisbladis 2022-05-28 01:25:35 +08:00
parent 5cb8bd825d
commit 1d2041bfc6
5 changed files with 50 additions and 79 deletions

View file

@ -1,44 +0,0 @@
package gomod2nix
import (
"bytes"
"github.com/BurntSushi/toml"
"github.com/tweag/gomod2nix/types"
)
const schemaVersion = 1
type packageT struct {
Version string `toml:"version"`
Hash string `toml:"hash"`
ReplacedPath string `toml:"replaced,omitempty"`
}
type output struct {
SchemaVersion int `toml:"schema"`
Mod map[string]*packageT `toml:"mod"`
}
func Marshal(pkgs []*types.Package) ([]byte, error) {
out := &output{
SchemaVersion: schemaVersion,
Mod: make(map[string]*packageT),
}
for _, pkg := range pkgs {
out.Mod[pkg.GoPackagePath] = &packageT{
Version: pkg.Version,
Hash: pkg.Hash,
ReplacedPath: pkg.ReplacedPath,
}
}
var buf bytes.Buffer
e := toml.NewEncoder(&buf)
err := e.Encode(out)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

View file

@ -13,21 +13,10 @@ import (
log "github.com/sirupsen/logrus"
"github.com/tweag/gomod2nix/lib"
"github.com/tweag/gomod2nix/types"
schema "github.com/tweag/gomod2nix/schema"
"golang.org/x/mod/modfile"
)
type packageJob struct {
importPath string
goPackagePath string
sumVersion string
}
type packageResult struct {
pkg *types.Package
err error
}
type goModDownload struct {
Path string
Version string
@ -39,7 +28,7 @@ type goModDownload struct {
GoModSum string
}
func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, numWorkers int, keepGoing bool) ([]*types.Package, error) {
func GeneratePkgs(goModPath string, goMod2NixPath string, numWorkers int) ([]*schema.Package, error) {
log.WithFields(log.Fields{
"modPath": goModPath,
@ -65,9 +54,7 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
var modDownloads []*goModDownload
{
log.WithFields(log.Fields{
"sumPath": goSumPath,
}).Info("Downloading dependencies")
log.Info("Downloading dependencies")
stdout, err := exec.Command(
"go", "mod", "download", "--json",
@ -86,15 +73,13 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
modDownloads = append(modDownloads, dl)
}
log.WithFields(log.Fields{
"sumPath": goSumPath,
}).Info("Done downloading dependencies")
log.Info("Done downloading dependencies")
}
executor := lib.NewParallellExecutor(numWorkers)
var mux sync.Mutex
packages := []*types.Package{}
packages := []*schema.Package{}
for _, dl := range modDownloads {
dl := dl
@ -125,7 +110,7 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, num
}
hash := strings.TrimSpace(string(stdout))
pkg := &types.Package{
pkg := &schema.Package{
GoPackagePath: goPackagePath,
Version: dl.Version,
Hash: hash,

10
main.go
View file

@ -4,15 +4,14 @@ import (
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/tweag/gomod2nix/fetch"
"github.com/tweag/gomod2nix/formats/gomod2nix"
generate "github.com/tweag/gomod2nix/generate"
schema "github.com/tweag/gomod2nix/schema"
"io/ioutil"
"path/filepath"
)
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 outDirFlag = flag.String("outdir", "", "output directory (if different from project directory)")
@ -23,17 +22,16 @@ func main() {
outDir = *directory
}
goSumPath := filepath.Join(*directory, "go.sum")
goModPath := filepath.Join(*directory, "go.mod")
goMod2NixPath := filepath.Join(outDir, "gomod2nix.toml")
outFile := goMod2NixPath
pkgs, err := fetch.FetchPackages(goModPath, goSumPath, goMod2NixPath, *maxJobs, *keepGoing)
pkgs, err := generate.GeneratePkgs(goModPath, goMod2NixPath, *maxJobs)
if err != nil {
panic(err)
}
output, err := gomod2nix.Marshal(pkgs)
output, err := schema.Marshal(pkgs)
if err != nil {
panic(err)
}

40
schema/schema.go Normal file
View file

@ -0,0 +1,40 @@
package types
import (
"bytes"
"github.com/BurntSushi/toml"
)
const SchemaVersion = 1
type Package struct {
GoPackagePath string `toml:"-"`
Version string `toml:"version"`
Hash string `toml:"hash"`
ReplacedPath string `toml:"replaced,omitempty"`
}
type Output struct {
SchemaVersion int `toml:"schema"`
Mod map[string]*Package `toml:"mod"`
}
func Marshal(pkgs []*Package) ([]byte, error) {
out := &Output{
SchemaVersion: SchemaVersion,
Mod: make(map[string]*Package),
}
for _, pkg := range pkgs {
out.Mod[pkg.GoPackagePath] = pkg
}
var buf bytes.Buffer
e := toml.NewEncoder(&buf)
err := e.Encode(out)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

View file

@ -1,8 +0,0 @@
package types
type Package struct {
GoPackagePath string
ReplacedPath string
Version string
Hash string
}