From 2a6ed7a10652e2b3e25197e2e046404f191481de Mon Sep 17 00:00:00 2001 From: spf13 Date: Fri, 6 Nov 2015 11:08:12 -0500 Subject: [PATCH] utilize filepath.Join liberally fixes #176 --- cobra/cmd/add.go | 5 ++--- cobra/cmd/helpers.go | 14 +++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 9de58214..564033a9 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -15,7 +15,6 @@ package cmd import ( "fmt" - "os" "path/filepath" "strings" @@ -117,9 +116,9 @@ func init() { data["parentName"] = parentName() data["cmdName"] = cmdName - err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), cmdName+".go", template, data) + err := writeTemplateToFile(filepath.Join(ProjectPath(), guessCmdDir()), cmdName+".go", template, data) if err != nil { er(err) } - fmt.Println(cmdName, "created at", filepath.Join(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), cmdName+".go")) + fmt.Println(cmdName, "created at", filepath.Join(ProjectPath(), guessCmdDir(), cmdName+".go")) } diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index b90e3e7e..0035c399 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -104,11 +104,11 @@ func guessImportPath() string { er("Cobra only supports project within $GOPATH") } - return strings.TrimPrefix(projectPath, getSrcPath()) + return filepath.Clean(strings.TrimPrefix(projectPath, getSrcPath())) } func getSrcPath() string { - return os.Getenv("GOPATH") + string(os.PathSeparator) + "src" + string(os.PathSeparator) + return filepath.Join(os.Getenv("GOPATH"), "src") + string(os.PathSeparator) } func projectName() string { @@ -129,7 +129,7 @@ func guessProjectPath() { // if we are in the cmd directory.. back up for _, c := range cmdDirs { if base == c { - projectPath = filepath.Clean(filepath.Dir(x)) + projectPath = filepath.Dir(x) return } } @@ -154,10 +154,10 @@ func guessProjectPath() { switch count { // If only one directory deep assume "github.com" case 1: - projectPath = filepath.Clean(srcPath + "github.com" + string(os.PathSeparator) + inputPath) + projectPath = filepath.Join(srcPath, "github.com", inputPath) return case 2: - projectPath = filepath.Clean(srcPath + inputPath) + projectPath = filepath.Join(srcPath, inputPath) return default: er("Unknown directory") @@ -167,12 +167,12 @@ func guessProjectPath() { if projectBase == "" { x, err := getWd() if err == nil { - projectPath = filepath.Clean(x + string(os.PathSeparator) + inputPath) + projectPath = filepath.Join(x, inputPath) return } er(err) } else { - projectPath = filepath.Clean(srcPath + projectBase + string(os.PathSeparator) + inputPath) + projectPath = filepath.Join(srcPath, projectBase, inputPath) return } }