forked from mirrors/gomod2nix
a4bed25a86
This creates an `mkGoEnv` function which takes care of adding the correct Go package to your development environment and installs development dependencies from tools.go in a Nix derivation. The "normal" workflow around Go with tools.go just sticks development dependencies in $GOBIN which isn't ideal since you have no separation between projects.
57 lines
750 B
Go
57 lines
750 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"go/parser"
|
|
"go/token"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
)
|
|
|
|
const filename = "tools.go"
|
|
|
|
func main() {
|
|
fset := token.NewFileSet()
|
|
|
|
var src []byte
|
|
{
|
|
f, err := os.Open(filename)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
src, err = io.ReadAll(f)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
f, err := parser.ParseFile(fset, filename, src, parser.ImportsOnly)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
for _, s := range f.Imports {
|
|
path, err := strconv.Unquote(s.Path.Value)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
cmd := exec.Command("go", "install", path)
|
|
|
|
fmt.Printf("Executing '%s'\n", cmd)
|
|
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|