From 1d9d665f6fe8c1b09f9dda83d2ffbc62b3a03ccb Mon Sep 17 00:00:00 2001 From: spf13 Date: Fri, 6 Nov 2015 10:44:59 -0500 Subject: [PATCH] improving project path guessing --- cobra/cmd/helpers.go | 14 +++++++------- cobra/cmd/helpers_test.go | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 28f7003..b90e3e7 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -129,13 +129,13 @@ func guessProjectPath() { // if we are in the cmd directory.. back up for _, c := range cmdDirs { if base == c { - projectPath = filepath.Dir(x) + projectPath = filepath.Clean(filepath.Dir(x)) return } } if projectPath == "" { - projectPath = x + projectPath = filepath.Clean(x) return } } @@ -145,7 +145,7 @@ func guessProjectPath() { if strings.ContainsRune(inputPath, os.PathSeparator) { if filepath.IsAbs(inputPath) { // if Absolute, use it - projectPath = inputPath + projectPath = filepath.Clean(inputPath) return } // If not absolute but contains slashes.. assuming it means create it from $GOPATH @@ -154,10 +154,10 @@ func guessProjectPath() { switch count { // If only one directory deep assume "github.com" case 1: - projectPath = srcPath + "github.com" + string(os.PathSeparator) + inputPath + projectPath = filepath.Clean(srcPath + "github.com" + string(os.PathSeparator) + inputPath) return case 2: - projectPath = srcPath + inputPath + projectPath = filepath.Clean(srcPath + inputPath) return default: er("Unknown directory") @@ -167,12 +167,12 @@ func guessProjectPath() { if projectBase == "" { x, err := getWd() if err == nil { - projectPath = x + string(os.PathSeparator) + inputPath + projectPath = filepath.Clean(x + string(os.PathSeparator) + inputPath) return } er(err) } else { - projectPath = srcPath + projectBase + string(os.PathSeparator) + inputPath + projectPath = filepath.Clean(srcPath + projectBase + string(os.PathSeparator) + inputPath) return } } diff --git a/cobra/cmd/helpers_test.go b/cobra/cmd/helpers_test.go index 26737c9..564ddbe 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/cmd/helpers_test.go @@ -35,4 +35,5 @@ func TestProjectPath(t *testing.T) { checkGuess(t, "/bar/foo/cmd", "", "/bar/foo") checkGuess(t, "/bar/foo/command", "", "/bar/foo") checkGuess(t, "/bar/foo/commands", "", "/bar/foo") + checkGuess(t, "github.com/spf13/hugo/../hugo", "", "github.com/spf13/hugo") }