Denis Isaev 3345c7136f fix #264: fix Windows compatibility
Fix skip-dirs regexps and 'go env' on Windows.
2018-11-05 12:54:40 +03:00

50 lines
867 B
Go

package goutil
import (
"context"
"encoding/json"
"os"
"os/exec"
"github.com/pkg/errors"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type Env struct {
vars map[string]string
log logutils.Log
debugf logutils.DebugFunc
}
func NewEnv(log logutils.Log) *Env {
return &Env{
vars: map[string]string{},
log: log,
debugf: logutils.Debug("env"),
}
}
func (e *Env) Discover(ctx context.Context) error {
out, err := exec.CommandContext(ctx, "go", "env", "-json").Output()
if err != nil {
return errors.Wrap(err, "failed to run 'go env'")
}
if err = json.Unmarshal(out, &e.vars); err != nil {
return errors.Wrap(err, "failed to parse go env json")
}
e.debugf("Read go env: %#v", e.vars)
return nil
}
func (e Env) Get(k string) string {
envValue := os.Getenv(k)
if envValue != "" {
return envValue
}
return e.vars[k]
}