mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
project path guessing updated
This commit is contained in:
parent
806b2bb86a
commit
88e2af3aff
2 changed files with 25 additions and 33 deletions
|
@ -141,42 +141,31 @@ func guessProjectPath() {
|
||||||
}
|
}
|
||||||
|
|
||||||
srcPath := getSrcPath()
|
srcPath := getSrcPath()
|
||||||
// if provided, inspect for logical locations
|
|
||||||
if strings.ContainsRune(inputPath, os.PathSeparator) {
|
|
||||||
if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) {
|
|
||||||
// if Absolute, use it
|
|
||||||
projectPath = filepath.Clean(inputPath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// If not absolute but contains slashes,
|
|
||||||
// assuming it means create it from $GOPATH
|
|
||||||
count := strings.Count(inputPath, string(os.PathSeparator))
|
|
||||||
|
|
||||||
switch count {
|
var base string
|
||||||
// If only one directory deep, assume "github.com"
|
// if provided, inspect for logical locations
|
||||||
case 1:
|
if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) {
|
||||||
projectPath = filepath.Join(srcPath, "github.com", inputPath)
|
// if Absolute, use it.
|
||||||
return
|
} else if projectBase != "" {
|
||||||
case 2:
|
// if projectBase specified any relative path starts with it
|
||||||
projectPath = filepath.Join(srcPath, inputPath)
|
base = filepath.Join(srcPath, projectBase)
|
||||||
return
|
} else if inputPath == "." || strings.HasPrefix(inputPath, "./") || strings.HasPrefix(inputPath, "../") {
|
||||||
default:
|
// relative to cwd like 'go test ./'
|
||||||
er("Unknown directory")
|
var err error
|
||||||
}
|
base, err = getWd()
|
||||||
} else {
|
if err != nil {
|
||||||
// hardest case.. just a word.
|
|
||||||
if projectBase == "" {
|
|
||||||
x, err := getWd()
|
|
||||||
if err == nil {
|
|
||||||
projectPath = filepath.Join(x, inputPath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
er(err)
|
er(err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
projectPath = filepath.Join(srcPath, projectBase, inputPath)
|
// relative, but not to cwd, so to $GOPATH/src
|
||||||
return
|
if dir, _ := filepath.Split(inputPath); dir == "" {
|
||||||
|
// If only one directory deep, assume "github.com"
|
||||||
|
base = filepath.Join(srcPath, "github.com")
|
||||||
|
} else {
|
||||||
|
base = srcPath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
projectPath = filepath.Join(base, inputPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// isEmpty checks if a given path is empty.
|
// isEmpty checks if a given path is empty.
|
||||||
|
|
|
@ -16,7 +16,7 @@ func checkGuess(t *testing.T, wd, input, expected string) {
|
||||||
guessProjectPath()
|
guessProjectPath()
|
||||||
|
|
||||||
if projectPath != expected {
|
if projectPath != expected {
|
||||||
t.Errorf("Unexpected Project Path. \n Got: %q\nExpected: %q\n", projectPath, expected)
|
t.Errorf("Unexpected Project Path. \nGot: %q\nExpected: %q\nArg: %v\nDir: %v", projectPath, expected, input, wd)
|
||||||
}
|
}
|
||||||
|
|
||||||
reset()
|
reset()
|
||||||
|
@ -30,9 +30,12 @@ func reset() {
|
||||||
|
|
||||||
func TestProjectPath(t *testing.T) {
|
func TestProjectPath(t *testing.T) {
|
||||||
checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo"))
|
checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo"))
|
||||||
checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo"))
|
checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPath(), "spf13", "hugo"))
|
||||||
checkGuess(t, "", filepath.Join("/", "bar", "foo"), filepath.Join("/", "bar", "foo"))
|
checkGuess(t, "", filepath.Join("/", "bar", "foo"), filepath.Join("/", "bar", "foo"))
|
||||||
checkGuess(t, "/bar/foo", "baz", filepath.Join("/", "bar", "foo", "baz"))
|
checkGuess(t, "/bar/foo", "baz", filepath.Join(getSrcPath(), "github.com", "baz"))
|
||||||
|
checkGuess(t, "/bar/foo", "gopkg.in/baz", filepath.Join(getSrcPath(), "gopkg.in", "baz"))
|
||||||
|
checkGuess(t, "/bar/foo", "./baz", filepath.Join("/", "bar", "foo", "baz"))
|
||||||
|
checkGuess(t, "/bar/foo", "./gopkg.in/baz", filepath.Join("/", "bar", "foo", "gopkg.in", "baz"))
|
||||||
checkGuess(t, "/bar/foo/cmd", "", filepath.Join("/", "bar", "foo"))
|
checkGuess(t, "/bar/foo/cmd", "", filepath.Join("/", "bar", "foo"))
|
||||||
checkGuess(t, "/bar/foo/command", "", filepath.Join("/", "bar", "foo"))
|
checkGuess(t, "/bar/foo/command", "", filepath.Join("/", "bar", "foo"))
|
||||||
checkGuess(t, "/bar/foo/commands", "", filepath.Join("/", "bar", "foo"))
|
checkGuess(t, "/bar/foo/commands", "", filepath.Join("/", "bar", "foo"))
|
||||||
|
|
Loading…
Reference in a new issue