Skip to content
Myr

improving project path guessing

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by spf13 · committed by spf13 · parent bc980b8 · Browse files · 2 files+8 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+8 added−7 removed

  • other:+8 added−7 removed

2 files+8 added−7 removed

modified:cobra/cmd/helpers_test.go+1 added
33 unmodified lines
34 checkGuess(t, "/bar/foo", "baz", "/bar/foo/baz")35 checkGuess(t, "/bar/foo/cmd", "", "/bar/foo")36 checkGuess(t, "/bar/foo/command", "", "/bar/foo")37 checkGuess(t, "/bar/foo/commands", "", "/bar/foo")38 checkGuess(t, "github.com/spf13/hugo/../hugo", "", "github.com/spf13/hugo")39}
modified:cobra/cmd/helpers.go+7 added-7 removed
127 unmodified lines
128129 // if we are in the cmd directory.. back up130 for _, c := range cmdDirs {131 if base == c {132 projectPath = filepath.Dir(x)132 projectPath = filepath.Clean(filepath.Dir(x))133 return134 }135 }136137 if projectPath == "" {138 projectPath = x138 projectPath = filepath.Clean(x)139 return140 }141 }142
1 unmodified line
144 // if provided inspect for logical locations145 if strings.ContainsRune(inputPath, os.PathSeparator) {146 if filepath.IsAbs(inputPath) {147 // if Absolute, use it148 projectPath = inputPath148 projectPath = filepath.Clean(inputPath)149 return150 }151 // If not absolute but contains slashes.. assuming it means create it from $GOPATH152 count := strings.Count(inputPath, string(os.PathSeparator))153154 switch count {155 // If only one directory deep assume "github.com"156 case 1:157 projectPath = srcPath + "github.com" + string(os.PathSeparator) + inputPath157 projectPath = filepath.Clean(srcPath + "github.com" + string(os.PathSeparator) + inputPath)158 return159 case 2:160 projectPath = srcPath + inputPath160 projectPath = filepath.Clean(srcPath + inputPath)161 return162 default:163 er("Unknown directory")164 }
1 unmodified line
166 // hardest case.. just a word.167 if projectBase == "" {168 x, err := getWd()169 if err == nil {170 projectPath = x + string(os.PathSeparator) + inputPath170 projectPath = filepath.Clean(x + string(os.PathSeparator) + inputPath)171 return172 }173 er(err)174 } else {175 projectPath = srcPath + projectBase + string(os.PathSeparator) + inputPath175 projectPath = filepath.Clean(srcPath + projectBase + string(os.PathSeparator) + inputPath)176 return177 }178 }179}
163 unmodified lines