mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
improving project path guessing
This commit is contained in:
parent
bc980b80a1
commit
1d9d665f6f
2 changed files with 8 additions and 7 deletions
|
@ -129,13 +129,13 @@ 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.Dir(x)
|
projectPath = filepath.Clean(filepath.Dir(x))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if projectPath == "" {
|
if projectPath == "" {
|
||||||
projectPath = x
|
projectPath = filepath.Clean(x)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ func guessProjectPath() {
|
||||||
if strings.ContainsRune(inputPath, os.PathSeparator) {
|
if strings.ContainsRune(inputPath, os.PathSeparator) {
|
||||||
if filepath.IsAbs(inputPath) {
|
if filepath.IsAbs(inputPath) {
|
||||||
// if Absolute, use it
|
// if Absolute, use it
|
||||||
projectPath = inputPath
|
projectPath = filepath.Clean(inputPath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// If not absolute but contains slashes.. assuming it means create it from $GOPATH
|
// If not absolute but contains slashes.. assuming it means create it from $GOPATH
|
||||||
|
@ -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 = srcPath + "github.com" + string(os.PathSeparator) + inputPath
|
projectPath = filepath.Clean(srcPath + "github.com" + string(os.PathSeparator) + inputPath)
|
||||||
return
|
return
|
||||||
case 2:
|
case 2:
|
||||||
projectPath = srcPath + inputPath
|
projectPath = filepath.Clean(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 = x + string(os.PathSeparator) + inputPath
|
projectPath = filepath.Clean(x + string(os.PathSeparator) + inputPath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
er(err)
|
er(err)
|
||||||
} else {
|
} else {
|
||||||
projectPath = srcPath + projectBase + string(os.PathSeparator) + inputPath
|
projectPath = filepath.Clean(srcPath + projectBase + string(os.PathSeparator) + inputPath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,4 +35,5 @@ func TestProjectPath(t *testing.T) {
|
||||||
checkGuess(t, "/bar/foo/cmd", "", "/bar/foo")
|
checkGuess(t, "/bar/foo/cmd", "", "/bar/foo")
|
||||||
checkGuess(t, "/bar/foo/command", "", "/bar/foo")
|
checkGuess(t, "/bar/foo/command", "", "/bar/foo")
|
||||||
checkGuess(t, "/bar/foo/commands", "", "/bar/foo")
|
checkGuess(t, "/bar/foo/commands", "", "/bar/foo")
|
||||||
|
checkGuess(t, "github.com/spf13/hugo/../hugo", "", "github.com/spf13/hugo")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue