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