From 1d2041bfc6740646af65bc18bc832f2b6d89295e Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sat, 28 May 2022 01:25:35 +0800 Subject: [PATCH] Restructure internal package names --- formats/gomod2nix/gomod2nix.go | 44 -------------------------- fetch/fetch.go => generate/generate.go | 27 ++++------------ main.go | 10 +++--- schema/schema.go | 40 +++++++++++++++++++++++ types/types.go | 8 ----- 5 files changed, 50 insertions(+), 79 deletions(-) delete mode 100644 formats/gomod2nix/gomod2nix.go rename fetch/fetch.go => generate/generate.go (79%) create mode 100644 schema/schema.go delete mode 100644 types/types.go diff --git a/formats/gomod2nix/gomod2nix.go b/formats/gomod2nix/gomod2nix.go deleted file mode 100644 index 4577ac6..0000000 --- a/formats/gomod2nix/gomod2nix.go +++ /dev/null @@ -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 -} diff --git a/fetch/fetch.go b/generate/generate.go similarity index 79% rename from fetch/fetch.go rename to generate/generate.go index d12eb8d..3a3e713 100644 --- a/fetch/fetch.go +++ b/generate/generate.go @@ -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, diff --git a/main.go b/main.go index de1dbf4..ed6f783 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/schema/schema.go b/schema/schema.go new file mode 100644 index 0000000..831a2ee --- /dev/null +++ b/schema/schema.go @@ -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 +} diff --git a/types/types.go b/types/types.go deleted file mode 100644 index 9d9ce0e..0000000 --- a/types/types.go +++ /dev/null @@ -1,8 +0,0 @@ -package types - -type Package struct { - GoPackagePath string - ReplacedPath string - Version string - Hash string -}