cmd: Small correctives
This commit is contained in:
parent
84cba621a0
commit
d20925b932
@ -51,14 +51,19 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
|
|||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
project := NewProjectFromPath(wd)
|
project := NewProjectFromPath(wd)
|
||||||
|
|
||||||
cmdName := validateCmdName(args[0])
|
cmdName := validateCmdName(args[0])
|
||||||
createCmdFile(project, cmdName)
|
cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")
|
||||||
|
createCmdFile(project.License(), cmdPath, cmdName)
|
||||||
|
|
||||||
|
fmt.Fprintln(cmd.OutOrStdout(), cmdName, "created at", cmdPath)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateCmdName returns source without any dashes and underscore.
|
// validateCmdName returns source without any dashes and underscore.
|
||||||
// If there will be dash or underscore, next letter will be uppered.
|
// If there will be dash or underscore, next letter will be uppered.
|
||||||
// It supports only ASCII (1-byte character) strings.
|
// It supports only ASCII (1-byte character) strings.
|
||||||
|
// https://github.com/spf13/cobra/issues/269
|
||||||
func validateCmdName(source string) string {
|
func validateCmdName(source string) string {
|
||||||
i := 0
|
i := 0
|
||||||
l := len(source)
|
l := len(source)
|
||||||
@ -102,7 +107,7 @@ func validateCmdName(source string) string {
|
|||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCmdFile(project *Project, cmdName string) {
|
func createCmdFile(license License, path, cmdName string) {
|
||||||
template := `{{comment .copyright}}
|
template := `{{comment .copyright}}
|
||||||
{{comment .license}}
|
{{comment .license}}
|
||||||
|
|
||||||
@ -146,21 +151,17 @@ func init() {
|
|||||||
|
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
data["copyright"] = copyrightLine()
|
data["copyright"] = copyrightLine()
|
||||||
data["license"] = project.License().Header
|
data["license"] = license.Header
|
||||||
data["viper"] = viper.GetBool("useViper")
|
data["viper"] = viper.GetBool("useViper")
|
||||||
data["parentName"] = parentName
|
data["parentName"] = parentName
|
||||||
data["cmdName"] = cmdName
|
data["cmdName"] = cmdName
|
||||||
|
|
||||||
filePath := filepath.Join(project.CmdPath(), cmdName+".go")
|
|
||||||
|
|
||||||
cmdScript, err := executeTemplate(template, data)
|
cmdScript, err := executeTemplate(template, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
err = writeStringToFile(filePath, cmdScript)
|
err = writeStringToFile(path, cmdScript)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(cmdName, "created at", filePath)
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import (
|
|||||||
var projectPath string
|
var projectPath string
|
||||||
|
|
||||||
var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}
|
var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}
|
||||||
var goPaths, srcPaths []string
|
var srcPaths []string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Initialize goPaths and srcPaths
|
// Initialize goPaths and srcPaths
|
||||||
@ -35,7 +35,7 @@ func init() {
|
|||||||
er("$GOPATH is not set")
|
er("$GOPATH is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
goPaths = filepath.SplitList(envGoPath)
|
goPaths := filepath.SplitList(envGoPath)
|
||||||
srcPaths = make([]string, 0, len(goPaths))
|
srcPaths = make([]string, 0, len(goPaths))
|
||||||
for _, goPath := range goPaths {
|
for _, goPath := range goPaths {
|
||||||
srcPaths = append(srcPaths, filepath.Join(goPath, "src"))
|
srcPaths = append(srcPaths, filepath.Join(goPath, "src"))
|
||||||
|
@ -48,16 +48,27 @@ Init will not use an existing directory with contents.`,
|
|||||||
}
|
}
|
||||||
project = NewProjectFromPath(wd)
|
project = NewProjectFromPath(wd)
|
||||||
} else if len(args) == 1 {
|
} else if len(args) == 1 {
|
||||||
project = NewProject(args[0])
|
arg := args[0]
|
||||||
|
if filepath.IsAbs(arg) {
|
||||||
|
project = NewProjectFromPath(arg)
|
||||||
|
} else {
|
||||||
|
project = NewProject(arg)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
er("please enter the name")
|
er("please enter the name")
|
||||||
}
|
}
|
||||||
|
|
||||||
initializePath(project)
|
initializeProject(project)
|
||||||
|
|
||||||
|
fmt.Fprintln(cmd.OutOrStdout(), `Your Cobra application is ready at
|
||||||
|
`+project.AbsPath()+`.
|
||||||
|
|
||||||
|
Give it a try by going there and running `+"`go run main.go`."+`
|
||||||
|
Add commands to it by running `+"`cobra add [cmdname]`.")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializePath(project *Project) {
|
func initializeProject(project *Project) {
|
||||||
if !exists(project.AbsPath()) { // If path doesn't yet exist, create it
|
if !exists(project.AbsPath()) { // If path doesn't yet exist, create it
|
||||||
err := os.MkdirAll(project.AbsPath(), os.ModePerm)
|
err := os.MkdirAll(project.AbsPath(), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -216,9 +227,4 @@ func initConfig() {
|
|||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(`Your Cobra application is ready at
|
|
||||||
` + project.AbsPath() + `.
|
|
||||||
|
|
||||||
Give it a try by going there and running ` + "`go run main.go`." + `
|
|
||||||
Add commands to it by running ` + "`cobra add [cmdname]`.")
|
|
||||||
}
|
}
|
||||||
|
@ -29,21 +29,22 @@ func NewProject(projectName string) *Project {
|
|||||||
p.absPath = findPackage(projectName)
|
p.absPath = findPackage(projectName)
|
||||||
|
|
||||||
// 2. If there are no created project with this path, and user is in GOPATH,
|
// 2. If there are no created project with this path, and user is in GOPATH,
|
||||||
// then use GOPATH/src+projectName.
|
// then use GOPATH/src/projectName.
|
||||||
if p.absPath == "" {
|
if p.absPath == "" {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
for _, goPath := range goPaths {
|
for _, srcPath := range srcPaths {
|
||||||
|
goPath := filepath.Dir(srcPath)
|
||||||
if filepath.HasPrefix(wd, goPath) {
|
if filepath.HasPrefix(wd, goPath) {
|
||||||
p.absPath = filepath.Join(goPath, "src", projectName)
|
p.absPath = filepath.Join(srcPath, projectName)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. If user is not in GOPATH, then use (first GOPATH)+projectName.
|
// 3. If user is not in GOPATH, then use (first GOPATH)/src/projectName.
|
||||||
if p.absPath == "" {
|
if p.absPath == "" {
|
||||||
p.absPath = filepath.Join(srcPaths[0], projectName)
|
p.absPath = filepath.Join(srcPaths[0], projectName)
|
||||||
}
|
}
|
||||||
@ -130,25 +131,31 @@ func findCmdDir(absPath string) string {
|
|||||||
return "cmd"
|
return "cmd"
|
||||||
}
|
}
|
||||||
|
|
||||||
base := filepath.Base(absPath)
|
if isCmdDir(absPath) {
|
||||||
for _, cmdDir := range cmdDirs {
|
return filepath.Base(absPath)
|
||||||
if base == cmdDir {
|
|
||||||
return cmdDir
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
files, _ := filepath.Glob(filepath.Join(absPath, "c*"))
|
files, _ := filepath.Glob(filepath.Join(absPath, "c*"))
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
for _, cmdDir := range cmdDirs {
|
if isCmdDir(file) {
|
||||||
if file == cmdDir {
|
return file
|
||||||
return cmdDir
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "cmd"
|
return "cmd"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isCmdDir checks if base of name is one of cmdDir.
|
||||||
|
func isCmdDir(name string) bool {
|
||||||
|
name = filepath.Base(name)
|
||||||
|
for _, cmdDir := range cmdDirs {
|
||||||
|
if name == cmdDir {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// AbsPath returns absolute path of project.
|
// AbsPath returns absolute path of project.
|
||||||
func (p Project) AbsPath() string {
|
func (p Project) AbsPath() string {
|
||||||
return p.absPath
|
return p.absPath
|
||||||
|
@ -227,8 +227,7 @@ func TestFlagErrorFunc(t *testing.T) {
|
|||||||
|
|
||||||
// TestSortedFlags checks,
|
// TestSortedFlags checks,
|
||||||
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
|
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
|
||||||
//
|
// https://github.com/spf13/cobra/issues/404
|
||||||
// Source: https://github.com/spf13/cobra/issues/404
|
|
||||||
func TestSortedFlags(t *testing.T) {
|
func TestSortedFlags(t *testing.T) {
|
||||||
cmd := &Command{}
|
cmd := &Command{}
|
||||||
cmd.Flags().SortFlags = false
|
cmd.Flags().SortFlags = false
|
||||||
@ -264,8 +263,7 @@ func isStringInStringSlice(s string, ss []string) bool {
|
|||||||
// TestHelpFlagInHelp checks,
|
// TestHelpFlagInHelp checks,
|
||||||
// if '--help' flag is shown in help for child (executing `parent help child`),
|
// if '--help' flag is shown in help for child (executing `parent help child`),
|
||||||
// that has no other flags.
|
// that has no other flags.
|
||||||
//
|
// https://github.com/spf13/cobra/issues/302
|
||||||
// Source: https://github.com/spf13/cobra/issues/302
|
|
||||||
func TestHelpFlagInHelp(t *testing.T) {
|
func TestHelpFlagInHelp(t *testing.T) {
|
||||||
output := new(bytes.Buffer)
|
output := new(bytes.Buffer)
|
||||||
parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }}
|
parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }}
|
||||||
|
Loading…
Reference in New Issue
Block a user