gomod2nix/main.go

66 lines
1.6 KiB
Go
Raw Normal View History

2020-07-20 10:52:58 +00:00
package main // import "github.com/tweag/gomod2nix"
import (
"flag"
"fmt"
2020-07-20 11:38:32 +00:00
"github.com/tweag/gomod2nix/fetch"
2020-07-21 07:20:36 +00:00
"github.com/tweag/gomod2nix/formats/buildgopackage"
2020-07-20 12:26:57 +00:00
"github.com/tweag/gomod2nix/formats/gomod2nix"
2020-07-20 12:32:17 +00:00
"io/ioutil"
2020-07-20 10:52:58 +00:00
"path/filepath"
)
func main() {
2020-07-21 07:20:36 +00:00
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)")
2020-07-20 10:52:58 +00:00
flag.Parse()
2020-07-21 07:20:36 +00:00
goSumPath := filepath.Join(*directory, "go.sum")
goModPath := filepath.Join(*directory, "go.mod")
2020-07-20 10:52:58 +00:00
2020-07-21 07:20:36 +00:00
wrongFormatError := fmt.Errorf("Format not supported")
2020-07-21 07:20:36 +00:00
goMod2NixPath := ""
depsNixPath := ""
switch *format {
case "gomod2nix":
goMod2NixPath = filepath.Join(*directory, "gomod2nix.toml")
case "buildgopackage":
depsNixPath = filepath.Join(*directory, "deps.nix")
default:
panic(wrongFormatError)
}
2020-07-21 07:20:36 +00:00
pkgs, err := fetch.FetchPackages(goModPath, goSumPath, goMod2NixPath, depsNixPath, *maxJobs, *keepGoing)
if err != nil {
panic(err)
2020-07-20 10:52:58 +00:00
}
2020-07-21 07:20:36 +00:00
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)
}
err = ioutil.WriteFile(*outFile, output, 0644)
2020-07-20 12:32:17 +00:00
if err != nil {
panic(err)
}
2020-07-21 07:20:36 +00:00
fmt.Println(fmt.Sprintf("Wrote: %s", *outFile))
2020-07-20 10:52:58 +00:00
}