Fixes issue #276.

This commit fixes issue #276. If $GOPATH includes multiple paths (as
allowed by the spec), Cobra cannot guess the project path, so now init
bails out if a relative path is given. The original behaviour is not
changed, if $GOPATH only contains a single path (as in most cases).
This commit is contained in:
Klaus Birkelund Jensen 2016-05-20 12:06:55 +02:00
parent f368244301
commit 4f42e96b84

View file

@ -100,15 +100,25 @@ func guessCmdDir() string {
func guessImportPath() string { func guessImportPath() string {
guessProjectPath() guessProjectPath()
if !strings.HasPrefix(projectPath, getSrcPath()) { for _, path := range getSrcPaths() {
if strings.HasPrefix(projectPath, path) {
return filepath.ToSlash(filepath.Clean(strings.TrimPrefix(projectPath, path)))
}
}
er("Cobra only supports project within $GOPATH") er("Cobra only supports project within $GOPATH")
// not reached
return ""
} }
return filepath.ToSlash(filepath.Clean(strings.TrimPrefix(projectPath, getSrcPath()))) func getSrcPaths() []string {
paths := strings.Split(os.Getenv("GOPATH"), ":")
for i, path := range paths {
paths[i] = filepath.Join(path, "src") + string(os.PathSeparator)
} }
func getSrcPath() string { return paths
return filepath.Join(os.Getenv("GOPATH"), "src") + string(os.PathSeparator)
} }
func projectName() string { func projectName() string {
@ -140,7 +150,7 @@ func guessProjectPath() {
} }
} }
srcPath := getSrcPath() srcPaths := getSrcPaths()
// if provided, inspect for logical locations // if provided, inspect for logical locations
if strings.ContainsRune(inputPath, os.PathSeparator) { if strings.ContainsRune(inputPath, os.PathSeparator) {
if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) { if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) {
@ -148,6 +158,11 @@ func guessProjectPath() {
projectPath = filepath.Clean(inputPath) projectPath = filepath.Clean(inputPath)
return return
} }
if len(srcPaths) > 1 {
er("Cobra can't guess project path because $GOPATH contains multiple paths")
}
// If not absolute but contains slashes, // If not absolute but contains slashes,
// assuming it means create it from $GOPATH // assuming it means create it from $GOPATH
count := strings.Count(inputPath, string(os.PathSeparator)) count := strings.Count(inputPath, string(os.PathSeparator))
@ -155,10 +170,10 @@ func guessProjectPath() {
switch count { switch count {
// If only one directory deep, assume "github.com" // If only one directory deep, assume "github.com"
case 1: case 1:
projectPath = filepath.Join(srcPath, "github.com", inputPath) projectPath = filepath.Join(srcPaths[0], "github.com", inputPath)
return return
case 2: case 2:
projectPath = filepath.Join(srcPath, inputPath) projectPath = filepath.Join(srcPaths[0], inputPath)
return return
default: default:
er("Unknown directory") er("Unknown directory")
@ -173,7 +188,11 @@ func guessProjectPath() {
} }
er(err) er(err)
} else { } else {
projectPath = filepath.Join(srcPath, projectBase, inputPath) if len(srcPaths) > 1 {
er("Cobra can't guess project path because $GOPATH contains multiple paths")
}
projectPath = filepath.Join(srcPaths[0], projectBase, inputPath)
return return
} }
} }