Skip to content

Commit 0f92e72

Browse files
committed
feat(gomod): use goenv package
1 parent 9b1c67d commit 0f92e72

File tree

2 files changed

+4
-30
lines changed

2 files changed

+4
-30
lines changed

gomod/gomod.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path/filepath"
1212
"strings"
1313

14+
"github.com/ldez/grignotin/goenv"
1415
"golang.org/x/mod/modfile"
1516
)
1617

@@ -61,31 +62,15 @@ func GetModuleInfo() ([]ModInfo, error) {
6162
return infos, nil
6263
}
6364

64-
type goEnv struct {
65-
GOMOD string `json:"GOMOD"` //nolint:tagliatelle // Based on en var name.
66-
}
67-
6865
// GetGoModPath extracts go.mod path from "go env".
66+
// Deprecated: use goenv.GetOne(goenv.GOMOD) instead.
6967
func GetGoModPath() (string, error) {
70-
cmd := exec.Command("go", "env", "-json", "GOMOD")
71-
72-
out, err := cmd.Output()
73-
if err != nil {
74-
return "", fmt.Errorf("command %q: %w: %s", strings.Join(cmd.Args, " "), err, string(out))
75-
}
76-
77-
v := &goEnv{}
78-
err = json.NewDecoder(bytes.NewBuffer(out)).Decode(v)
79-
if err != nil {
80-
return "", err
81-
}
82-
83-
return v.GOMOD, nil
68+
return goenv.GetOne(goenv.GOMOD)
8469
}
8570

8671
// GetModulePath extracts module path from go.mod.
8772
func GetModulePath() (string, error) {
88-
p, err := GetGoModPath()
73+
p, err := goenv.GetOne(goenv.GOMOD)
8974
if err != nil {
9075
return "", err
9176
}

gomod/gomod_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gomod
22

33
import (
4-
"path/filepath"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
@@ -19,16 +18,6 @@ func TestGetModuleInfo(t *testing.T) {
1918
assert.True(t, info[0].Main)
2019
}
2120

22-
func TestGetGoModPath(t *testing.T) {
23-
p, err := GetGoModPath()
24-
require.NoError(t, err)
25-
26-
abs, err := filepath.Abs("..")
27-
require.NoError(t, err)
28-
29-
assert.Equal(t, filepath.Join(abs, "go.mod"), p)
30-
}
31-
3221
func TestGetModulePath(t *testing.T) {
3322
p, err := GetModulePath()
3423
require.NoError(t, err)

0 commit comments

Comments
 (0)