Skip to content

Commit

Permalink
Merge pull request #32 from 1pkg/use_some_defaults_from_go_env
Browse files Browse the repository at this point in the history
use some default envs from go env
  • Loading branch information
1pkg authored Dec 22, 2022
2 parents f685d38 + e112387 commit dafe6c2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "Kostiantyn Masliuk - github.com/1pkg"
},
"license": "MIT",
"version": "1.6.3",
"version": "1.7.0",
"categories": [
"Programming Languages",
"Formatters",
Expand Down
2 changes: 1 addition & 1 deletion gopium/opium.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gopium
// list of global registered gopium constants
const (
NAME = "gopium"
VERSION = "1.6.3"
VERSION = "1.7.0"
PKG = "https://github.com/1pkg/gopium"
STAMP = "🌺 gopium @1pkg"
)
22 changes: 21 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package main

import (
"context"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"

"github.com/1pkg/gopium/gopium"
"github.com/1pkg/gopium/runners"
Expand Down Expand Up @@ -161,7 +164,7 @@ Notes:
// package parser vars
args[1], // package name
ppath,
pbenvs,
defenv(cmd.Context(), pbenvs, "GOPATH", "GOCACHE", "GOTMPDIR"),
pbflags,
// gopium walker vars
args[0], // single walker
Expand Down Expand Up @@ -317,6 +320,23 @@ By default it is used and overrides other printer formatting parameters.
)
}

// defenv appends requested env variable from `go env`, if no custom variable provided
func defenv(ctx context.Context, envs []string, vars ...string) []string {
loop:
for _, v := range vars {
for _, env := range envs {
if strings.HasPrefix(env, v) {
continue loop
}
}
ebytes, err := exec.CommandContext(ctx, "go", "env", v).CombinedOutput()
if err == nil {
envs = append(envs, fmt.Sprintf("%s=%s", v, strings.TrimSpace(string(ebytes))))
}
}
return envs
}

// main gopium cli entry point
func main() {
// explicitly set number of threads
Expand Down
7 changes: 0 additions & 7 deletions runners/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ func NewCli(
if !filepath.IsAbs(path) {
root = build.Default.GOPATH
}
// https://github.com/1pkg/gopium/issues/18
if len(benvs) == 0 {
benvs = []string{
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
fmt.Sprintf("GOCACHE=%s", filepath.Join(build.Default.GOPATH, ".cache")),
}
}
// set up parser
xp := &typepkg.ParserXToolPackagesAst{
Pattern: pkg,
Expand Down
19 changes: 6 additions & 13 deletions runners/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"go/build"
"go/parser"
"path/filepath"
"reflect"
"regexp"
"testing"
Expand Down Expand Up @@ -90,12 +89,9 @@ func TestNewCli(t *testing.T) {
Root: build.Default.GOPATH,
Path: "test-path",
//nolint
ModeTypes: packages.LoadAllSyntax,
ModeAst: parser.ParseComments | parser.AllErrors,
BuildEnv: []string{
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
fmt.Sprintf("GOCACHE=%s", filepath.Join(build.Default.GOPATH, ".cache")),
},
ModeTypes: packages.LoadAllSyntax,
ModeAst: parser.ParseComments | parser.AllErrors,
BuildEnv: []string{},
BuildFlags: []string{},
},
Exposer: m,
Expand Down Expand Up @@ -143,12 +139,9 @@ func TestNewCli(t *testing.T) {
Root: build.Default.GOPATH,
Path: "test-path",
//nolint
ModeTypes: packages.LoadAllSyntax,
ModeAst: parser.ParseComments | parser.AllErrors,
BuildEnv: []string{
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
fmt.Sprintf("GOCACHE=%s", filepath.Join(build.Default.GOPATH, ".cache")),
},
ModeTypes: packages.LoadAllSyntax,
ModeAst: parser.ParseComments | parser.AllErrors,
BuildEnv: []string{},
BuildFlags: []string{},
},
Exposer: m,
Expand Down

0 comments on commit dafe6c2

Please sign in to comment.