Skip to content

Commit e430ef9

Browse files
committed
feat(goenv,gomod): use context
1 parent a5f945e commit e430ef9

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

goenv/goenv.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@ package goenv
33

44
import (
55
"bytes"
6+
"context"
67
"encoding/json"
78
"fmt"
89
"os/exec"
910
"strings"
1011
)
1112

1213
// GetAll gets information from "go env".
13-
func GetAll() (map[string]string, error) {
14-
cmd := exec.Command("go", "env", "-json")
15-
16-
out, err := cmd.Output()
17-
if err != nil {
18-
return nil, fmt.Errorf("command %q: %w: %s", strings.Join(cmd.Args, " "), err, string(out))
19-
}
20-
21-
v := map[string]string{}
22-
err = json.NewDecoder(bytes.NewBuffer(out)).Decode(&v)
14+
func GetAll(ctx context.Context) (map[string]string, error) {
15+
v, err := Get(ctx)
2316
if err != nil {
2417
return nil, err
2518
}
@@ -28,16 +21,8 @@ func GetAll() (map[string]string, error) {
2821
}
2922

3023
// GetOne gets information from "go env" for one environment variable.
31-
func GetOne(name string) (string, error) {
32-
cmd := exec.Command("go", "env", "-json", name)
33-
34-
out, err := cmd.Output()
35-
if err != nil {
36-
return "", fmt.Errorf("command %q: %w: %s", strings.Join(cmd.Args, " "), err, string(out))
37-
}
38-
39-
v := map[string]string{}
40-
err = json.NewDecoder(bytes.NewBuffer(out)).Decode(&v)
24+
func GetOne(ctx context.Context, name string) (string, error) {
25+
v, err := Get(ctx, name)
4126
if err != nil {
4227
return "", err
4328
}
@@ -46,9 +31,9 @@ func GetOne(name string) (string, error) {
4631
}
4732

4833
// Get gets information from "go env" for one or several environment variables.
49-
func Get(name ...string) (map[string]string, error) {
34+
func Get(ctx context.Context, name ...string) (map[string]string, error) {
5035
args := append([]string{"env", "-json"}, name...)
51-
cmd := exec.Command("go", args...) //nolint:gosec // The env var names must be checked by the user.
36+
cmd := exec.CommandContext(ctx, "go", args...) //nolint:gosec // The env var names must be checked by the user.
5237

5338
out, err := cmd.Output()
5439
if err != nil {

goenv/goenv_test.go

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

33
import (
4+
"context"
45
"path/filepath"
56
"testing"
67

@@ -9,7 +10,7 @@ import (
910
)
1011

1112
func TestGetOne(t *testing.T) {
12-
p, err := GetOne(GOMOD)
13+
p, err := GetOne(context.Background(), GOMOD)
1314
require.NoError(t, err)
1415

1516
abs, err := filepath.Abs("..")
@@ -19,7 +20,7 @@ func TestGetOne(t *testing.T) {
1920
}
2021

2122
func TestGet(t *testing.T) {
22-
values, err := Get(GOMOD, GOCACHE)
23+
values, err := Get(context.Background(), GOMOD, GOCACHE)
2324
require.NoError(t, err)
2425

2526
assert.NotEmpty(t, values[GOMOD])

gomod/gomod.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gomod
33

44
import (
55
"bytes"
6+
"context"
67
"encoding/json"
78
"errors"
89
"fmt"
@@ -27,9 +28,9 @@ type ModInfo struct {
2728
}
2829

2930
// GetModuleInfo gets modules information from `go list`.
30-
func GetModuleInfo() ([]ModInfo, error) {
31+
func GetModuleInfo(ctx context.Context) ([]ModInfo, error) {
3132
// https://github.com/golang/go/issues/44753#issuecomment-790089020
32-
cmd := exec.Command("go", "list", "-m", "-json")
33+
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-json")
3334

3435
out, err := cmd.Output()
3536
if err != nil {
@@ -62,15 +63,9 @@ func GetModuleInfo() ([]ModInfo, error) {
6263
return infos, nil
6364
}
6465

65-
// GetGoModPath extracts go.mod path from "go env".
66-
// Deprecated: use goenv.GetOne(goenv.GOMOD) instead.
67-
func GetGoModPath() (string, error) {
68-
return goenv.GetOne(goenv.GOMOD)
69-
}
70-
7166
// GetModulePath extracts module path from go.mod.
72-
func GetModulePath() (string, error) {
73-
p, err := goenv.GetOne(goenv.GOMOD)
67+
func GetModulePath(ctx context.Context) (string, error) {
68+
p, err := goenv.GetOne(ctx, goenv.GOMOD)
7469
if err != nil {
7570
return "", err
7671
}
@@ -82,3 +77,9 @@ func GetModulePath() (string, error) {
8277

8378
return modfile.ModulePath(b), nil
8479
}
80+
81+
// GetGoModPath extracts go.mod path from "go env".
82+
// Deprecated: use `goenv.GetOne(context.Background(), goenv.GOMOD)` instead.
83+
func GetGoModPath() (string, error) {
84+
return goenv.GetOne(context.Background(), goenv.GOMOD)
85+
}

gomod/gomod_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package gomod
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89
)
910

1011
func TestGetModuleInfo(t *testing.T) {
11-
info, err := GetModuleInfo()
12+
info, err := GetModuleInfo(context.Background())
1213
require.NoError(t, err)
1314

1415
require.Len(t, info, 1)
@@ -19,7 +20,7 @@ func TestGetModuleInfo(t *testing.T) {
1920
}
2021

2122
func TestGetModulePath(t *testing.T) {
22-
p, err := GetModulePath()
23+
p, err := GetModulePath(context.Background())
2324
require.NoError(t, err)
2425

2526
assert.Equal(t, "github.com/ldez/grignotin", p)

0 commit comments

Comments
 (0)