cutego/internal/utils/gopath.go

99 lines
2.3 KiB
Go
Raw Normal View History

2016-10-23 05:14:01 +03:00
package utils
import (
"errors"
"fmt"
"io/ioutil"
"os"
2016-10-23 05:14:01 +03:00
"path/filepath"
"runtime"
"strings"
2016-10-23 05:14:01 +03:00
)
const packageName = "github.com/therecipe/qt"
var goPath *string
2016-11-12 18:06:57 +03:00
func MustGoBin() string {
if dir := os.Getenv("GOBIN"); dir != "" {
return filepath.Clean(dir)
}
return filepath.Join(MustGoPath(), "bin")
}
2016-10-28 05:00:58 +03:00
// MustGoPath is same as GoPath but exits if any error ocurres
// it also caches the result
2016-10-23 05:14:01 +03:00
func MustGoPath() string {
if goPath != nil {
return *goPath
}
2016-10-28 05:00:58 +03:00
var out, err = gopath()
2016-10-23 05:14:01 +03:00
if err != nil {
2016-10-28 05:00:58 +03:00
Log.WithError(err).Errorf("failed to get GOPATH that holds %v", packageName)
2016-10-23 05:14:01 +03:00
}
goPath = &out
return out
}
2016-10-28 05:00:58 +03:00
// GoPath return the GOPATH that holds this package
func gopath() (goPath string, err error) {
2017-03-29 22:11:55 +03:00
var goPaths = GOPATH()
2016-10-23 05:14:01 +03:00
if len(goPaths) == 0 {
err = errors.New("GOPATH environment variable is unset")
return
}
2016-10-28 05:00:58 +03:00
//TODO: fix issue
var tries = make([]string, 0)
for _, path := range strings.Split(goPaths, string(os.PathListSeparator)) {
2016-10-23 05:14:01 +03:00
if _, err = ioutil.ReadDir(path); err != nil {
err = fmt.Errorf("GOPATH %q point to a non-existing directory", path)
continue
2016-10-23 05:14:01 +03:00
}
2016-11-02 16:15:36 +03:00
if strings.HasPrefix(fmt.Sprintf("%v%v", path, string(os.PathSeparator)), fmt.Sprintf("%v%v", runtime.GOROOT(), string(os.PathSeparator))) {
2016-10-23 05:14:01 +03:00
err = fmt.Errorf("GOPATH %q is or contains GOROOT", path)
continue
2016-10-23 05:14:01 +03:00
}
2016-11-02 16:15:36 +03:00
if strings.HasPrefix(fmt.Sprintf("%v%v", runtime.GOROOT(), string(os.PathSeparator)), fmt.Sprintf("%v%v", path, string(os.PathSeparator))) {
2016-10-28 05:00:58 +03:00
err = fmt.Errorf("GOROOT %q is or contains GOPATH", path)
continue
2016-10-23 05:14:01 +03:00
}
2016-10-28 05:00:58 +03:00
var packageDir = filepath.Join(path, "src", packageName)
2016-10-23 05:14:01 +03:00
if _, err = ioutil.ReadDir(packageDir); err == nil {
if len(goPath) > 0 {
2016-10-28 05:00:58 +03:00
err = fmt.Errorf("multiple copies of %s are in GOPATH: in %s and %s", packageName, goPath, path)
2016-10-23 05:14:01 +03:00
return
}
goPath = path
err = nil
break
2016-10-23 05:14:01 +03:00
} else {
tries = append(tries, packageDir)
}
}
if len(goPath) == 0 {
2016-10-28 05:00:58 +03:00
err = fmt.Errorf("failed to find %s in all %d GOPATH, tried %s", packageName, len(tries), strings.Join(tries, " "))
2016-10-23 05:14:01 +03:00
}
return
}
2017-03-29 22:11:55 +03:00
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 ""
}