mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
parent
e9078fccb8
commit
4cdb38c072
3 changed files with 25 additions and 10 deletions
|
@ -3,6 +3,7 @@ package cmd
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -37,7 +38,7 @@ func NewProject(projectName string) *Project {
|
|||
}
|
||||
for _, srcPath := range srcPaths {
|
||||
goPath := filepath.Dir(srcPath)
|
||||
if strings.HasPrefix(wd, goPath) {
|
||||
if filepathHasPrefix(wd, goPath) {
|
||||
p.absPath = filepath.Join(srcPath, projectName)
|
||||
break
|
||||
}
|
||||
|
@ -172,7 +173,7 @@ func (p *Project) SrcPath() string {
|
|||
}
|
||||
|
||||
for _, srcPath := range srcPaths {
|
||||
if strings.HasPrefix(p.absPath, srcPath) {
|
||||
if filepathHasPrefix(p.absPath, srcPath) {
|
||||
p.srcPath = srcPath
|
||||
break
|
||||
}
|
||||
|
@ -180,3 +181,15 @@ func (p *Project) SrcPath() string {
|
|||
|
||||
return p.srcPath
|
||||
}
|
||||
|
||||
func filepathHasPrefix(path string, prefix string) bool {
|
||||
if len(path) <= len(prefix) {
|
||||
return false
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
// Paths in windows are case-insensitive.
|
||||
return strings.EqualFold(path[0:len(prefix)], prefix)
|
||||
}
|
||||
return path[0:len(prefix)] == prefix
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -17,7 +16,7 @@ func TestFindExistingPackage(t *testing.T) {
|
|||
|
||||
func hasGoPathPrefix(path string) bool {
|
||||
for _, srcPath := range srcPaths {
|
||||
if strings.HasPrefix(path, srcPath) {
|
||||
if filepathHasPrefix(path, srcPath) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,15 +21,18 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile, projectBase, userLicense string // are used for flags
|
||||
var (
|
||||
// Used for flags.
|
||||
cfgFile, projectBase, userLicense string
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "cobra",
|
||||
Short: "A generator for Cobra based Applications",
|
||||
Long: `Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
}
|
||||
)
|
||||
|
||||
// Execute executes the root command.
|
||||
func Execute() {
|
||||
|
|
Loading…
Reference in a new issue