Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #205 from stanislas-m/203-support-multiple-gopath
Browse files Browse the repository at this point in the history
Fixed issue #203: multiple GOPATH are not supported by buffalo new.
  • Loading branch information
markbates committed Feb 1, 2017
2 parents 9c37370 + 21b5784 commit cd5d7b0
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions buffalo/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"

"github.com/gobuffalo/envy"
Expand Down Expand Up @@ -93,7 +94,24 @@ func validateInGoPath(name string) error {
return err
}

if !strings.HasPrefix(root, filepath.Join(gp, "src")) {
var gpMultiple []string

if runtime.GOOS == "windows" {
gpMultiple = strings.Split(gp, ";") // Windows uses a different separator
} else {
gpMultiple = strings.Split(gp, ":")
}
gpMultipleLen := len(gpMultiple)
foundInPath := false

for i := 0; i < gpMultipleLen; i++ {
if strings.HasPrefix(root, filepath.Join(gpMultiple[i], "src")) {
foundInPath = true
break
}
}

if !foundInPath {
u, err := user.Current()
if err != nil {
return err
Expand All @@ -113,6 +131,27 @@ func validateInGoPath(name string) error {
return nil
}

func goPath(root string) string {
var gpMultiple []string
gp := os.Getenv("GOPATH")

if runtime.GOOS == "windows" {
gpMultiple = strings.Split(gp, ";") // Windows uses a different separator
} else {
gpMultiple = strings.Split(gp, ":")
}
gpMultipleLen := len(gpMultiple)
path := ""

for i := 0; i < gpMultipleLen; i++ {
if strings.HasPrefix(root, filepath.Join(gpMultiple[i], "src")) {
path = gpMultiple[i]
break
}
}
return path
}

func rootPath(name string) (string, error) {
pwd, err := os.Getwd()
if err != nil {
Expand All @@ -123,7 +162,7 @@ func rootPath(name string) (string, error) {
}

func packagePath(rootPath string) string {
gosrcpath := strings.Replace(filepath.Join(os.Getenv("GOPATH"), "src"), "\\", "/", -1)
gosrcpath := strings.Replace(filepath.Join(goPath(rootPath), "src"), "\\", "/", -1)
rootPath = strings.Replace(rootPath, "\\", "/", -1)
return strings.Replace(rootPath, gosrcpath+"/", "", 2)
}
Expand Down

0 comments on commit cd5d7b0

Please sign in to comment.