47 lines
780 B
Go
47 lines
780 B
Go
package helpers
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/juju/errors"
|
|
)
|
|
|
|
// IsGoProject check if we are in a golang project (have go.mod).
|
|
func IsGoProject(path string) (bool, error) {
|
|
exist, err := fileExists(filepath.Join(path, "go.mod"))
|
|
if err != nil {
|
|
return false, errors.Trace(err)
|
|
}
|
|
|
|
if exist {
|
|
return true, nil
|
|
}
|
|
|
|
exist, err = fileExists(filepath.Join(path, "go.sum"))
|
|
if err != nil {
|
|
return false, errors.Trace(err)
|
|
}
|
|
|
|
if exist {
|
|
return true, nil
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// IsInGoPath check if the given path is in the gopath directory.
|
|
func IsInGoPath(path string) bool {
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
if gopath == "" {
|
|
return false
|
|
}
|
|
|
|
// clean path
|
|
gopath = filepath.Clean(gopath)
|
|
|
|
return strings.HasPrefix(path, gopath)
|
|
}
|