Skip to content

Commit 36333f9

Browse files
authored
[devbox] Add version detection to the go planner (#49)
## Summary Add version detection to the go planner based on the contents of `go.mod`. When we don't have a corresponding package in `nixpkgs` we default to `1.19` ## How was it tested? Compiled and ran against local project.
1 parent 1ad1bd7 commit 36333f9

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/spf13/cobra v1.5.0
1212
github.com/stretchr/testify v1.8.0
1313
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17
14+
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57
1415
gopkg.in/yaml.v3 v3.0.1
1516
)
1617

@@ -30,5 +31,6 @@ require (
3031
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
3132
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
3233
golang.org/x/text v0.3.7 // indirect
34+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
3335
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
3436
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63M
5757
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5858
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
5959
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
60+
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 h1:LQmS1nU0twXLA96Kt7U9qtHJEbBk3z6Q0V4UXjZkpr4=
61+
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
6062
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
6163
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
6264
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -66,6 +68,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6668
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
6769
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
6870
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
71+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6972
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7073
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
7174
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

planner/go_planner.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ package planner
66
import (
77
"os"
88
"path/filepath"
9+
10+
"golang.org/x/mod/modfile"
911
)
1012

1113
type GoPlanner struct{}
1214

15+
var versionMap = map[string]string{
16+
// Map go versions to the corresponding nixpkgs:
17+
"1.19": "go_1_19",
18+
"1.18": "go",
19+
"1.17": "go_1_17",
20+
}
21+
22+
const defaultPkg = "go_1_19" // Default to "latest" for cases where we can't determine a version.
23+
1324
// GoPlanner implements interface Planner (compile-time check)
1425
var _ Planner = (*GoPlanner)(nil)
1526

@@ -23,9 +34,10 @@ func (g *GoPlanner) IsRelevant(srcDir string) bool {
2334
}
2435

2536
func (g *GoPlanner) GetPlan(srcDir string) *Plan {
37+
goPkg := getGoPackage(srcDir)
2638
return &Plan{
2739
Packages: []string{
28-
"go",
40+
goPkg,
2941
},
3042
InstallStage: &Stage{
3143
Command: "go get",
@@ -39,6 +51,31 @@ func (g *GoPlanner) GetPlan(srcDir string) *Plan {
3951
}
4052
}
4153

54+
func getGoPackage(srcDir string) string {
55+
goModPath := filepath.Join(srcDir, "go.mod")
56+
goVersion := parseGoVersion(goModPath)
57+
v, ok := versionMap[goVersion]
58+
if ok {
59+
return v
60+
} else {
61+
// Should we be throwing an error instead, if we don't have a nix package
62+
// for the specified version of go?
63+
return defaultPkg
64+
}
65+
}
66+
67+
func parseGoVersion(gomodPath string) string {
68+
content, err := os.ReadFile(gomodPath)
69+
if err != nil {
70+
return ""
71+
}
72+
parsed, err := modfile.ParseLax(gomodPath, content, nil)
73+
if err != nil {
74+
return ""
75+
}
76+
return parsed.Go.Version
77+
}
78+
4279
func fileExists(path string) bool {
4380
_, err := os.Stat(path)
4481
return err == nil

0 commit comments

Comments
 (0)