diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 7cd3be18..60da6661 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -26,10 +26,6 @@ import ( "github.com/spf13/viper" ) -// var BaseDir = "" -// var AppName = "" -// var CommandDir = "" - var funcMap template.FuncMap var projectPath = "" var inputPath = "" @@ -99,16 +95,24 @@ func guessCmdDir() string { func guessImportPath() string { guessProjectPath() - - if !strings.HasPrefix(projectPath, getSrcPath()) { - er("Cobra only supports project within $GOPATH") + srcPaths := getSrcPaths() + for _, srcPath := range srcPaths { + if strings.HasPrefix(projectPath, srcPath) { + return filepath.ToSlash(filepath.Clean(strings.TrimPrefix(projectPath, srcPath))) + } } - return filepath.ToSlash(filepath.Clean(strings.TrimPrefix(projectPath, getSrcPath()))) + er("Cobra only supports project within $GOPATH") + return "" } -func getSrcPath() string { - return filepath.Join(os.Getenv("GOPATH"), "src") + string(os.PathSeparator) +func getSrcPaths() []string { + paths := strings.Split(os.Getenv("GOPATH"), string(os.PathListSeparator)) + for i, gopath := range paths { + srcPath := filepath.Join(gopath, "src") + string(os.PathSeparator) + paths[i] = srcPath + } + return paths } func projectName() string { @@ -140,7 +144,7 @@ func guessProjectPath() { } } - srcPath := getSrcPath() + srcPaths := getSrcPaths() // if provided, inspect for logical locations if strings.ContainsRune(inputPath, os.PathSeparator) { if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) { @@ -155,10 +159,24 @@ func guessProjectPath() { switch count { // If only one directory deep, assume "github.com" case 1: - projectPath = filepath.Join(srcPath, "github.com", inputPath) + for _, srcPath := range srcPaths { + fpath := filepath.Join(srcPath, "github.com", inputPath) + if b, _ := exists(fpath); b { + projectPath = fpath + return + } + } + projectPath = filepath.Join(srcPaths[0], "github.com", inputPath) return case 2: - projectPath = filepath.Join(srcPath, inputPath) + for _, srcPath := range srcPaths { + fpath := filepath.Join(srcPath, inputPath) + if b, _ := exists(fpath); b { + projectPath = fpath + return + } + } + projectPath = filepath.Join(srcPaths[0], inputPath) return default: er("Unknown directory") @@ -167,13 +185,20 @@ func guessProjectPath() { // hardest case.. just a word. if projectBase == "" { x, err := getWd() - if err == nil { - projectPath = filepath.Join(x, inputPath) - return + if err != nil { + er(err) } - er(err) + projectPath = filepath.Join(x, inputPath) + return } else { - projectPath = filepath.Join(srcPath, projectBase, inputPath) + for _, srcPath := range srcPaths { + fpath := filepath.Join(srcPath, projectBase, inputPath) + if b, _ := exists(fpath); b { + projectPath = fpath + return + } + } + projectPath = filepath.Join(srcPaths[0], projectBase, inputPath) return } } diff --git a/cobra/cmd/helpers_test.go b/cobra/cmd/helpers_test.go index bd0f7595..ae2e9a69 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/cmd/helpers_test.go @@ -29,8 +29,8 @@ func reset() { } func TestProjectPath(t *testing.T) { - checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo")) - checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo")) + checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPaths()[0], "github.com", "spf13", "hugo")) + checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPaths()[0], "github.com", "spf13", "hugo")) checkGuess(t, "", filepath.Join("/", "bar", "foo"), filepath.Join("/", "bar", "foo")) checkGuess(t, "/bar/foo", "baz", filepath.Join("/", "bar", "foo", "baz")) checkGuess(t, "/bar/foo/cmd", "", filepath.Join("/", "bar", "foo")) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 065c8bf4..4a6636d4 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -33,7 +33,7 @@ This application is a tool to generate the needed files to quickly create a Cobra application.`, } -//Execute adds all child commands to the root command sets flags appropriately. +// Execute adds all child commands to the root command sets flags appropriately. func Execute() { if err := RootCmd.Execute(); err != nil { fmt.Println(err)