base.go 807 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package goutils
  2. import (
  3. "go/build"
  4. "os"
  5. "path/filepath"
  6. "go.mindeco.de/logging"
  7. "github.com/pkg/errors"
  8. )
  9. // LocatePackage searches for the import path and returns the os filesystem path location
  10. func LocatePackage(path string) (string, error) {
  11. p, err := build.Default.Import(path, "", build.FindOnly)
  12. if err != nil {
  13. return "", errors.Wrap(err, "LocatePackage: failed to find import")
  14. }
  15. cwd, err := os.Getwd()
  16. if err != nil {
  17. return "", errors.Wrap(err, "LocatePackage: could not get working directory")
  18. }
  19. p.Dir, err = filepath.Rel(cwd, p.Dir)
  20. if err != nil {
  21. return "", errors.Wrap(err, "LocatePackage: could not construct relative path")
  22. }
  23. return p.Dir, nil
  24. }
  25. func MustLocatePackage(path string) string {
  26. p, err := LocatePackage(path)
  27. logging.CheckFatal(err)
  28. return p
  29. }