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 ( import (
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -117,9 +116,9 @@ func init() {
data["parentName"] = parentName() data["parentName"] = parentName()
data["cmdName"] = cmdName 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 { if err != nil {
er(err) 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") er("Cobra only supports project within $GOPATH")
} }
return strings.TrimPrefix(projectPath, getSrcPath()) return filepath.Clean(strings.TrimPrefix(projectPath, getSrcPath()))
} }
func getSrcPath() string { 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 { func projectName() string {
@ -129,7 +129,7 @@ func guessProjectPath() {
// if we are in the cmd directory.. back up // if we are in the cmd directory.. back up
for _, c := range cmdDirs { for _, c := range cmdDirs {
if base == c { if base == c {
projectPath = filepath.Clean(filepath.Dir(x)) projectPath = filepath.Dir(x)
return return
} }
} }
@ -154,10 +154,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.Clean(srcPath + "github.com" + string(os.PathSeparator) + inputPath) projectPath = filepath.Join(srcPath, "github.com", inputPath)
return return
case 2: case 2:
projectPath = filepath.Clean(srcPath + inputPath) projectPath = filepath.Join(srcPath, inputPath)
return return
default: default:
er("Unknown directory") er("Unknown directory")
@ -167,12 +167,12 @@ func guessProjectPath() {
if projectBase == "" { if projectBase == "" {
x, err := getWd() x, err := getWd()
if err == nil { if err == nil {
projectPath = filepath.Clean(x + string(os.PathSeparator) + inputPath) projectPath = filepath.Join(x, inputPath)
return return
} }
er(err) er(err)
} else { } else {
projectPath = filepath.Clean(srcPath + projectBase + string(os.PathSeparator) + inputPath) projectPath = filepath.Join(srcPath, projectBase, inputPath)
return return
} }
} }