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

10
main.go
View file

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