2022-08-01 19:47:46 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/juju/errors"
|
|
|
|
)
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// IsGoProject check if we are in a golang project (have go.mod).
|
2022-08-01 19:47:46 +00:00
|
|
|
func IsGoProject(path string) (bool, error) {
|
2022-09-02 18:50:04 +00:00
|
|
|
exist, err := FileExists(filepath.Join(path, "go.mod"))
|
2022-08-01 19:47:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Trace(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exist {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-09-02 18:50:04 +00:00
|
|
|
exist, err = FileExists(filepath.Join(path, "go.sum"))
|
2022-08-01 19:47:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Trace(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exist {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// IsInGoPath check if the given path is in the gopath directory.
|
2022-08-01 19:47:46 +00:00
|
|
|
func IsInGoPath(path string) bool {
|
|
|
|
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean path
|
|
|
|
gopath = filepath.Clean(gopath)
|
|
|
|
|
|
|
|
return strings.HasPrefix(path, gopath)
|
|
|
|
}
|