gomod2nix/main.go

76 lines
1.9 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-21 09:42:32 +00:00
log "github.com/sirupsen/logrus"
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")
2020-07-21 09:42:32 +00:00
var outDirFlag = flag.String("outdir", "", "output directory (if different from project directory)")
2020-07-21 07:20:36 +00:00
var format = flag.String("format", "gomod2nix", "output format (gomod2nix, buildgopackage)")
2020-07-20 10:52:58 +00:00
flag.Parse()
2020-07-21 09:42:32 +00:00
outDir := *outDirFlag
if outDir == "" {
outDir = *directory
}
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 := ""
2020-07-21 09:42:32 +00:00
outFile := ""
2020-07-21 07:20:36 +00:00
switch *format {
case "gomod2nix":
2020-07-21 09:42:32 +00:00
goMod2NixPath = filepath.Join(outDir, "gomod2nix.toml")
outFile = goMod2NixPath
2020-07-21 07:20:36 +00:00
case "buildgopackage":
2020-07-21 09:42:32 +00:00
depsNixPath = filepath.Join(outDir, "deps.nix")
outFile = depsNixPath
2020-07-21 07:20:36 +00:00
default:
panic(wrongFormatError)
}
2020-07-21 09:42:32 +00:00
log.Info(fmt.Sprintf("Using output format '%s'", *format))
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)
}
2020-07-21 09:42:32 +00:00
err = ioutil.WriteFile(outFile, output, 0644)
2020-07-20 12:32:17 +00:00
if err != nil {
panic(err)
}
2020-07-21 09:42:32 +00:00
log.Info(fmt.Sprintf("Wrote: %s", outFile))
2020-07-20 10:52:58 +00:00
}