utilize filepath.Join liberally

fixes #176
This commit is contained in:
spf13 2015-11-06 11:08:12 -05:00
parent 1d9d665f6f
commit 2a6ed7a106
2 changed files with 9 additions and 10 deletions

View file

@ -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"))
}

View file

@ -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
}
}