From 70ca0b2f149ff5e811968939755b384174076f49 Mon Sep 17 00:00:00 2001 From: bonfy Date: Thu, 12 Apr 2018 16:05:36 +0800 Subject: [PATCH] add os.PathSeparator to the return value of findCmdDir func add func findCmdSuffix fix comment change findCmdSuffix to findCmdPath fix --- cobra/cmd/project.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index 7ddb8258..6cf0449e 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -92,7 +92,8 @@ func NewProjectFromPath(absPath string) *Project { } p := new(Project) - p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath)) + + p.absPath = findCmdPath(absPath) p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath())) return p } @@ -130,6 +131,15 @@ func (p *Project) CmdPath() string { return p.cmdPath } +// findCmdPath returns the cmd path. +func findCmdPath(path string) string { + cmdDir := findCmdDir(path) + if filepathHasPrefix(cmdDir, string(os.PathSeparator)) { + return strings.TrimSuffix(path, cmdDir) + } + return strings.TrimSuffix(path, string(os.PathSeparator)+cmdDir) +} + // findCmdDir checks if base of absPath is cmd dir and returns it or // looks for existing cmd dir in absPath. func findCmdDir(absPath string) string {