Skip to content

Commit dafe6c2

Browse files
authored
Merge pull request #32 from 1pkg/use_some_defaults_from_go_env
use some default envs from go env
2 parents f685d38 + e112387 commit dafe6c2

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

extensions/vscode/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"name": "Kostiantyn Masliuk - github.com/1pkg"
88
},
99
"license": "MIT",
10-
"version": "1.6.3",
10+
"version": "1.7.0",
1111
"categories": [
1212
"Programming Languages",
1313
"Formatters",

gopium/opium.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package gopium
33
// list of global registered gopium constants
44
const (
55
NAME = "gopium"
6-
VERSION = "1.6.3"
6+
VERSION = "1.7.0"
77
PKG = "https://github.com/1pkg/gopium"
88
STAMP = "🌺 gopium @1pkg"
99
)

main.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"os"
8+
"os/exec"
79
"os/signal"
810
"path/filepath"
911
"runtime"
12+
"strings"
1013

1114
"github.com/1pkg/gopium/gopium"
1215
"github.com/1pkg/gopium/runners"
@@ -161,7 +164,7 @@ Notes:
161164
// package parser vars
162165
args[1], // package name
163166
ppath,
164-
pbenvs,
167+
defenv(cmd.Context(), pbenvs, "GOPATH", "GOCACHE", "GOTMPDIR"),
165168
pbflags,
166169
// gopium walker vars
167170
args[0], // single walker
@@ -317,6 +320,23 @@ By default it is used and overrides other printer formatting parameters.
317320
)
318321
}
319322

323+
// defenv appends requested env variable from `go env`, if no custom variable provided
324+
func defenv(ctx context.Context, envs []string, vars ...string) []string {
325+
loop:
326+
for _, v := range vars {
327+
for _, env := range envs {
328+
if strings.HasPrefix(env, v) {
329+
continue loop
330+
}
331+
}
332+
ebytes, err := exec.CommandContext(ctx, "go", "env", v).CombinedOutput()
333+
if err == nil {
334+
envs = append(envs, fmt.Sprintf("%s=%s", v, strings.TrimSpace(string(ebytes))))
335+
}
336+
}
337+
return envs
338+
}
339+
320340
// main gopium cli entry point
321341
func main() {
322342
// explicitly set number of threads

runners/cli.go

-7
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@ func NewCli(
7474
if !filepath.IsAbs(path) {
7575
root = build.Default.GOPATH
7676
}
77-
// https://github.com/1pkg/gopium/issues/18
78-
if len(benvs) == 0 {
79-
benvs = []string{
80-
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
81-
fmt.Sprintf("GOCACHE=%s", filepath.Join(build.Default.GOPATH, ".cache")),
82-
}
83-
}
8477
// set up parser
8578
xp := &typepkg.ParserXToolPackagesAst{
8679
Pattern: pkg,

runners/cli_test.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"go/build"
88
"go/parser"
9-
"path/filepath"
109
"reflect"
1110
"regexp"
1211
"testing"
@@ -90,12 +89,9 @@ func TestNewCli(t *testing.T) {
9089
Root: build.Default.GOPATH,
9190
Path: "test-path",
9291
//nolint
93-
ModeTypes: packages.LoadAllSyntax,
94-
ModeAst: parser.ParseComments | parser.AllErrors,
95-
BuildEnv: []string{
96-
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
97-
fmt.Sprintf("GOCACHE=%s", filepath.Join(build.Default.GOPATH, ".cache")),
98-
},
92+
ModeTypes: packages.LoadAllSyntax,
93+
ModeAst: parser.ParseComments | parser.AllErrors,
94+
BuildEnv: []string{},
9995
BuildFlags: []string{},
10096
},
10197
Exposer: m,
@@ -143,12 +139,9 @@ func TestNewCli(t *testing.T) {
143139
Root: build.Default.GOPATH,
144140
Path: "test-path",
145141
//nolint
146-
ModeTypes: packages.LoadAllSyntax,
147-
ModeAst: parser.ParseComments | parser.AllErrors,
148-
BuildEnv: []string{
149-
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
150-
fmt.Sprintf("GOCACHE=%s", filepath.Join(build.Default.GOPATH, ".cache")),
151-
},
142+
ModeTypes: packages.LoadAllSyntax,
143+
ModeAst: parser.ParseComments | parser.AllErrors,
144+
BuildEnv: []string{},
152145
BuildFlags: []string{},
153146
},
154147
Exposer: m,

0 commit comments

Comments
 (0)