Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Fix goPath trimming in NewPackage func
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorj authored and kamilchm committed Jun 29, 2017
1 parent 71ece49 commit 67b396b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,24 @@ func currentPackage(goPath string) (string, error) {
return "", err
}

for _, goPathDir := range strings.Split(goPath, ":") {
if packagePath, found := trimGopath(goPath, currDir); found {
return packagePath, nil
}

return "", fmt.Errorf("Current dir %v is outside of GOPATH(%v). "+
"Can't get current package name", currDir, goPath)
}

func trimGopath(goPath string, packagePath string) (trimmedPath string, trimmed bool) {
for _, goPathDir := range filepath.SplitList(goPath) {
goPathSrc, err := filepath.Abs(filepath.Join(goPathDir, "/src"))
if err != nil {
continue
}
if strings.HasPrefix(currDir, goPathSrc) {
return strings.TrimPrefix(currDir, goPathSrc+"/"), nil
if strings.HasPrefix(packagePath, goPathSrc) {
return strings.TrimPrefix(packagePath, goPathSrc+"/"), true
}
}

return "", fmt.Errorf("Current dir %v is outside of GOPATH(%v). "+
"Can't get current package name", currDir, goPath)
return packagePath, false
}
4 changes: 2 additions & 2 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewPackage(importPath string, goPath string) (*GoPackage, error) {
fullPath, err)
}

pkgRoot := strings.TrimPrefix(repoRoot, goPath+"/src/")
pkgRoot, _ := trimGopath(goPath, repoRoot)

if strings.Contains(pkgRoot, "/vendor/") {
return nil, nil
Expand Down Expand Up @@ -136,7 +136,7 @@ func NewPackage(importPath string, goPath string) (*GoPackage, error) {
}

func goPackageDir(importPath, goPath string) (string, error) {
for _, goPathDir := range strings.Split(goPath, ":") {
for _, goPathDir := range filepath.SplitList(goPath) {
expectedPath := filepath.Clean(goPathDir + "/src/" + importPath)
if _, err := os.Stat(expectedPath); err == nil {
return expectedPath, nil
Expand Down

0 comments on commit 67b396b

Please sign in to comment.