Skip to content

Commit 302c961

Browse files
committed
parse go mod to read golang version
1 parent 3ec8d93 commit 302c961

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

pkg/docker/image.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package docker
55

66
import (
77
"fmt"
8+
"path/filepath"
89
"strings"
910

1011
"github.com/ava-labs/avalanche-cli/pkg/constants"
1112
"github.com/ava-labs/avalanche-cli/pkg/models"
13+
"github.com/ava-labs/avalanche-cli/pkg/utils"
1214
"github.com/ava-labs/avalanche-cli/pkg/ux"
1315
)
1416

@@ -40,7 +42,12 @@ func parseDockerImageListOutput(output []byte) []string {
4042

4143
// BuildDockerImage builds a docker image on a remote host.
4244
func BuildDockerImage(host *models.Host, image string, path string, dockerfile string) error {
43-
_, err := host.Command(fmt.Sprintf("cd %s && docker build -q --build-arg GO_VERSION=%s -t %s -f %s .", path, constants.BuildEnvGolangVersion, image, dockerfile), nil, constants.SSHLongRunningScriptTimeout)
45+
goVersion, err := utils.ReadGoVersion(filepath.Join(path, "go.mod"))
46+
if err != nil {
47+
//fall back to default
48+
goVersion = constants.BuildEnvGolangVersion
49+
}
50+
_, err = host.Command(fmt.Sprintf("cd %s && docker build -q --build-arg GO_VERSION=%s -t %s -f %s .", path, goVersion, image, dockerfile), nil, constants.SSHLongRunningScriptTimeout)
4451
return err
4552
}
4653

pkg/utils/file.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99

1010
"github.com/ava-labs/avalanche-cli/pkg/constants"
11+
"golang.org/x/mod/modfile"
1112
)
1213

1314
func NonEmptyDirectory(dirName string) (bool, error) {
@@ -127,3 +128,19 @@ func GetRemoteComposeServicePath(serviceName string, dirs ...string) string {
127128
servicePrefix := filepath.Join(constants.CloudNodeCLIConfigBasePath, "services", serviceName)
128129
return filepath.Join(append([]string{servicePrefix}, dirs...)...)
129130
}
131+
132+
// ReadGoVersion reads the Go version from the go.mod file
133+
func ReadGoVersion(filePath string) (string, error) {
134+
data, err := os.ReadFile(filePath)
135+
if err != nil {
136+
return "", err
137+
}
138+
modFile, err := modfile.Parse("go.mod", data, nil)
139+
if err != nil {
140+
return "", err
141+
}
142+
if modFile.Go != nil {
143+
return modFile.Go.Version, nil
144+
}
145+
return "", fmt.Errorf("go version not found in %s", filePath)
146+
}

pkg/utils/file_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,57 @@ func TestExpandHome(t *testing.T) {
4444
t.Errorf("ExpandHome failed for empty path: expected %s, got %s", expectedEmptyPath, expandedEmptyPath)
4545
}
4646
}
47+
48+
// createTempGoMod creates a temporary go.mod file with the provided content.
49+
func createTempGoMod(t *testing.T, content string) string {
50+
t.Helper()
51+
file, err := os.CreateTemp("", "go.mod")
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
56+
if _, err := file.Write([]byte(content)); err != nil {
57+
t.Fatal(err)
58+
}
59+
60+
if err := file.Close(); err != nil {
61+
t.Fatal(err)
62+
}
63+
64+
return file.Name()
65+
}
66+
67+
// TestReadGoVersion tests all scenarios in one function using sub-tests.
68+
func TestReadGoVersion(t *testing.T) {
69+
t.Run("Success", func(t *testing.T) {
70+
tempFile := createTempGoMod(t, "module example.com/test\n\ngo 1.18\n")
71+
defer os.Remove(tempFile) // Clean up the temp file
72+
73+
version, err := ReadGoVersion(tempFile)
74+
if err != nil {
75+
t.Fatalf("expected no error, got %v", err)
76+
}
77+
78+
expectedVersion := "1.18"
79+
if version != expectedVersion {
80+
t.Errorf("expected version %s, got %s", expectedVersion, version)
81+
}
82+
})
83+
84+
t.Run("NoVersion", func(t *testing.T) {
85+
tempFile := createTempGoMod(t, "module example.com/test\n")
86+
defer os.Remove(tempFile)
87+
88+
_, err := ReadGoVersion(tempFile)
89+
if err == nil {
90+
t.Fatalf("expected an error, but got none")
91+
}
92+
})
93+
94+
t.Run("InvalidFile", func(t *testing.T) {
95+
_, err := ReadGoVersion("nonexistent-go.mod")
96+
if err == nil {
97+
t.Fatalf("expected an error for nonexistent file, but got none")
98+
}
99+
})
100+
}

0 commit comments

Comments
 (0)