2022-06-13 14:20:55 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2022-08-27 18:27:35 +00:00
|
|
|
generate "github.com/nix-community/gomod2nix/internal/generate"
|
|
|
|
schema "github.com/nix-community/gomod2nix/internal/schema"
|
2022-06-13 14:20:55 +00:00
|
|
|
)
|
|
|
|
|
2022-06-13 19:26:50 +00:00
|
|
|
const directoryDefault = "./"
|
|
|
|
|
2022-06-13 14:20:55 +00:00
|
|
|
var (
|
|
|
|
flagDirectory string
|
|
|
|
flagOutDir string
|
2022-06-13 19:26:50 +00:00
|
|
|
maxJobs int
|
2022-06-13 14:20:55 +00:00
|
|
|
)
|
|
|
|
|
2022-06-13 19:26:50 +00:00
|
|
|
func generateFunc(cmd *cobra.Command, args []string) {
|
|
|
|
directory := flagDirectory
|
|
|
|
outDir := flagOutDir
|
|
|
|
|
|
|
|
// If we are dealing with a project packaged by passing packages on the command line
|
|
|
|
// we need to create a temporary project.
|
|
|
|
var tmpProj *generate.TempProject
|
|
|
|
if len(args) > 0 {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if directory != directoryDefault {
|
|
|
|
panic(fmt.Errorf("directory flag not supported together with import arguments"))
|
|
|
|
}
|
|
|
|
if outDir == "" {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outDir = pwd
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpProj, err = generate.NewTempProject(args)
|
2022-06-13 15:14:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-06-13 19:26:50 +00:00
|
|
|
defer func() {
|
|
|
|
err := tmpProj.Remove()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
directory = tmpProj.Dir
|
|
|
|
} else if outDir == "" {
|
|
|
|
// Default out to current working directory if we are developing some software in the current repo.
|
|
|
|
outDir = directory
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write gomod2nix.toml
|
|
|
|
{
|
|
|
|
goMod2NixPath := filepath.Join(outDir, "gomod2nix.toml")
|
|
|
|
outFile := goMod2NixPath
|
|
|
|
pkgs, err := generate.GeneratePkgs(directory, goMod2NixPath, maxJobs)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error generating pkgs: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
var goPackagePath string
|
2022-06-13 22:32:19 +00:00
|
|
|
var subPackages []string
|
2022-06-13 19:26:50 +00:00
|
|
|
|
|
|
|
if tmpProj != nil {
|
2022-06-13 22:32:19 +00:00
|
|
|
subPackages = tmpProj.SubPackages
|
2022-06-13 19:26:50 +00:00
|
|
|
goPackagePath = tmpProj.GoPackagePath
|
|
|
|
}
|
|
|
|
|
2022-06-13 22:32:19 +00:00
|
|
|
output, err := schema.Marshal(pkgs, goPackagePath, subPackages)
|
2022-06-13 19:26:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error marshaling output: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(outFile, output, 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error writing file: %v", err))
|
|
|
|
}
|
|
|
|
log.Info(fmt.Sprintf("Wrote: %s", outFile))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "gomod2nix",
|
|
|
|
Short: "Convert applications using Go modules -> Nix",
|
|
|
|
Run: generateFunc,
|
2022-06-13 14:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var generateCmd = &cobra.Command{
|
|
|
|
Use: "generate",
|
|
|
|
Short: "Run gomod2nix.toml generator",
|
2022-06-13 19:26:50 +00:00
|
|
|
Run: generateFunc,
|
2022-06-13 15:14:13 +00:00
|
|
|
}
|
2022-06-13 14:25:51 +00:00
|
|
|
|
2022-06-13 15:14:13 +00:00
|
|
|
var importCmd = &cobra.Command{
|
|
|
|
Use: "import",
|
|
|
|
Short: "Import Go sources into the Nix store",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-06-13 19:26:50 +00:00
|
|
|
err := generate.ImportPkgs(flagDirectory, maxJobs)
|
2022-06-13 15:14:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-06-13 14:20:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-06-13 19:26:50 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&flagDirectory, "dir", "./", "Go project directory")
|
2022-06-13 14:20:55 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&flagOutDir, "outdir", "", "Output directory (defaults to project directory)")
|
2022-06-13 19:26:50 +00:00
|
|
|
rootCmd.PersistentFlags().IntVar(&maxJobs, "jobs", 10, "Max number of concurrent jobs")
|
2022-06-13 14:25:51 +00:00
|
|
|
|
|
|
|
rootCmd.AddCommand(generateCmd)
|
2022-06-13 15:14:13 +00:00
|
|
|
rootCmd.AddCommand(importCmd)
|
2022-06-13 14:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|