forked from govend/govend
/
path.go
36 lines (29 loc) · 804 Bytes
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2016 govend. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package imports
import (
"errors"
"os"
"path/filepath"
)
// Path returns the import path of a package directory.
// It does so via $GOPATH.
func Path(dir string) (string, error) {
// check for relative path
if dir == "." {
dir = os.Args[0]
}
// determine the absolute file path for the provided directory
path, err := filepath.Abs(filepath.Dir(dir))
if err != nil {
return "", err
}
// check the env $GOPATH is valid
gopath := os.Getenv("GOPATH")
if len(gopath) == 0 {
return "", errors.New("$GOPATH not set")
}
// leverage the $GOPATH to strip out everything but the base git URL
return path[len(gopath+"/src/"):], nil
}