github.com/jmigpin/editor@v1.6.0/util/goutil/env.go (about)

     1  package goutil
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"strings"
    10  
    11  	"github.com/jmigpin/editor/util/osutil"
    12  )
    13  
    14  func OsAndGoEnv(dir string) []string {
    15  	env := os.Environ()
    16  	return osutil.SetEnvs(env, GoEnv(dir))
    17  }
    18  
    19  func GoEnv(dir string) []string {
    20  	w, err := GoEnv2(dir)
    21  	if err != nil {
    22  		return nil
    23  	}
    24  	return w
    25  }
    26  func GoEnv2(dir string) ([]string, error) {
    27  	// not the same as os.Environ which has entries like PATH
    28  
    29  	args := []string{"go", "env"}
    30  	cmd := osutil.NewCmd(context.Background(), args...)
    31  	cmd.Dir = dir
    32  	bout, err := osutil.RunCmdStdoutAndStderrInErr(cmd, nil)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	env := strings.Split(string(bout), "\n")
    37  
    38  	// clear "set " prefix
    39  	if runtime.GOOS == "windows" {
    40  		for i, s := range env {
    41  			env[i] = strings.TrimPrefix(s, "set ")
    42  		}
    43  	}
    44  
    45  	env = osutil.UnquoteEnvValues(env)
    46  
    47  	return env, nil
    48  }
    49  
    50  //----------
    51  
    52  func GoRoot() string {
    53  	// doesn't work well in windows
    54  	//return runtime.GOROOT()
    55  
    56  	return GetGoRoot(GoEnv(""))
    57  }
    58  
    59  func GoPath() []string {
    60  	return GetGoPath(GoEnv(""))
    61  }
    62  
    63  func GoVersion() (string, error) {
    64  	return GetGoVersion(GoEnv(""))
    65  }
    66  
    67  //----------
    68  
    69  func GetGoRoot(env []string) string {
    70  	return osutil.GetEnv(env, "GOROOT")
    71  }
    72  
    73  func GetGoPath(env []string) []string {
    74  	//res := []string{}
    75  	//a := osutil.GetEnv(env, "GOPATH")
    76  	//if a != "" {
    77  	//	res = append(res, filepath.SplitList(a)...)
    78  	//} else {
    79  	//	// from go/build/build.go:274
    80  	//	res = append(res, filepath.Join(osutil.HomeEnvVar(), "go"))
    81  	//}
    82  	//return res
    83  
    84  	a := osutil.GetEnv(env, "GOPATH")
    85  	return filepath.SplitList(a)
    86  }
    87  
    88  // returns version as in "1.0" without the "go" prefix
    89  func GetGoVersion(env []string) (string, error) {
    90  	// get from env var, not present in <=go.15.x?
    91  	v := osutil.GetEnv(env, "GOVERSION")
    92  
    93  	if v == "" {
    94  		// get from file located in go root
    95  		d := GetGoRoot(env)
    96  		fp := filepath.Join(d, "VERSION")
    97  		b, err := ioutil.ReadFile(fp)
    98  		if err != nil {
    99  			return "", err
   100  		}
   101  		v = strings.TrimSpace(string(b))
   102  	}
   103  
   104  	// remove "go" prefix if present
   105  	v = strings.TrimPrefix(v, "go")
   106  
   107  	return v, nil
   108  }