cutego/internal/utils/gopath.go

54 lines
1.1 KiB
Go

package utils
import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
)
const packageName = "github.com/therecipe/qt"
var mustGoPath string
var mustGoPathMutex = new(sync.Mutex)
//GOBIN returns the general GOBIN string
func GOBIN() string {
if dir, ok := os.LookupEnv("GOBIN"); ok {
return filepath.Clean(dir)
}
return filepath.Join(MustGoPath(), "bin")
}
// MustGoPath returns the GOPATH that holds this package
// it exits if any error occurres and also caches the result
func MustGoPath() string {
mustGoPathMutex.Lock()
if len(mustGoPath) == 0 {
mustGoPath = strings.TrimSpace(RunCmd(GoList("{{.Root}}", "github.com/therecipe/qt"), "get list gopath"))
if len(mustGoPath) == 0 {
mustGoPath = GOPATH()
}
}
mustGoPathMutex.Unlock()
return mustGoPath
}
// GOPATH returns the general GOPATH string
func GOPATH() string {
if dir, ok := os.LookupEnv("GOPATH"); ok {
return dir
}
home := "HOME"
if runtime.GOOS == "windows" {
home = "USERPROFILE"
}
if dir, ok := os.LookupEnv(home); ok {
return filepath.Join(dir, "go")
}
return ""
}