Skip to content
Myr

cmd: Small correctives

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

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 84cba62 · Browse files · 5 files+47 added−35 removed
This commit

State

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

Diff stats

5 files+47 added−35 removed

  • other:+47 added−35 removed

5 files+47 added−35 removed

modified:cobra/cmd/add.go+9 added-8 removed
49 unmodified lines
50 if err != nil {51 er(err)52 }53 project := NewProjectFromPath(wd)5455 cmdName := validateCmdName(args[0])56 cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")55 createCmdFile(project, cmdName)57 createCmdFile(project.License(), cmdPath, cmdName)5859 fmt.Fprintln(cmd.OutOrStdout(), cmdName, "created at", cmdPath)60 },61}6263// validateCmdName returns source without any dashes and underscore.64// If there will be dash or underscore, next letter will be uppered.65// It supports only ASCII (1-byte character) strings.66// https://github.com/spf13/cobra/issues/26967func validateCmdName(source string) string {68 i := 069 l := len(source)70 // The output is initialized on demand, then first dash or underscore
35 unmodified lines
106 }107 return output108}109105func createCmdFile(project *Project, cmdName string) {110func createCmdFile(license License, path, cmdName string) {111 template := `{{comment .copyright}}112{{comment .license}}113114package cmd
35 unmodified lines
150`151152 data := make(map[string]interface{})153 data["copyright"] = copyrightLine()149 data["license"] = project.License().Header154 data["license"] = license.Header155 data["viper"] = viper.GetBool("useViper")156 data["parentName"] = parentName157 data["cmdName"] = cmdName158154 filePath := filepath.Join(project.CmdPath(), cmdName+".go")155159 cmdScript, err := executeTemplate(template, data)160 if err != nil {161 er(err)162 }160 err = writeStringToFile(filePath, cmdScript)163 err = writeStringToFile(path, cmdScript)164 if err != nil {165 er(err)166 }164165 fmt.Println(cmdName, "created at", filePath)167}
modified:cobra/cmd/helpers.go+2 added-2 removed
24 unmodified lines
2526var projectPath string2728var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}29var goPaths, srcPaths []string29var srcPaths []string3031func init() {32 // Initialize goPaths and srcPaths33 envGoPath := os.Getenv("GOPATH")34 if envGoPath == "" {35 er("$GOPATH is not set")36 }3738 goPaths = filepath.SplitList(envGoPath)38 goPaths := filepath.SplitList(envGoPath)39 srcPaths = make([]string, 0, len(goPaths))40 for _, goPath := range goPaths {41 srcPaths = append(srcPaths, filepath.Join(goPath, "src"))42 }
104 unmodified lines
modified:cobra/cmd/init.go+14 added-8 removed
46 unmodified lines
47 er(err)48 }49 project = NewProjectFromPath(wd)50 } else if len(args) == 1 {51 arg := args[0]52 if filepath.IsAbs(arg) {53 project = NewProjectFromPath(arg)54 } else {51 project = NewProject(args[0])55 project = NewProject(arg)56 }57 } else {58 er("please enter the name")59 }6056 initializePath(project)61 initializeProject(project)6263 fmt.Fprintln(cmd.OutOrStdout(), `Your Cobra application is ready at64`+project.AbsPath()+`.6566Give it a try by going there and running `+"`go run main.go`."+`67Add commands to it by running `+"`cobra add [cmdname]`.")68 },69}7060func initializePath(project *Project) {71func initializeProject(project *Project) {72 if !exists(project.AbsPath()) { // If path doesn't yet exist, create it73 err := os.MkdirAll(project.AbsPath(), os.ModePerm)74 if err != nil {75 er(err)
150 unmodified lines
226 if err != nil {227 er(err)228 }229219 fmt.Println(`Your Cobra application is ready at220` + project.AbsPath() + `.221222Give it a try by going there and running ` + "`go run main.go`." + `223Add commands to it by running ` + "`cobra add [cmdname]`.")230}
modified:cobra/cmd/project.go+20 added-13 removed
27 unmodified lines
28 // 1. Find already created protect.29 p.absPath = findPackage(projectName)3031 // 2. If there are no created project with this path, and user is in GOPATH,32 // then use GOPATH/src+projectName.32 // then use GOPATH/src/projectName.33 if p.absPath == "" {34 wd, err := os.Getwd()35 if err != nil {36 er(err)37 }38 for _, goPath := range goPaths {38 for _, srcPath := range srcPaths {39 goPath := filepath.Dir(srcPath)40 if filepath.HasPrefix(wd, goPath) {40 p.absPath = filepath.Join(goPath, "src", projectName)41 p.absPath = filepath.Join(srcPath, projectName)42 break43 }44 }45 }4646 // 3. If user is not in GOPATH, then use (first GOPATH)+projectName.47 // 3. If user is not in GOPATH, then use (first GOPATH)/src/projectName.48 if p.absPath == "" {49 p.absPath = filepath.Join(srcPaths[0], projectName)50 }51
78 unmodified lines
130 if !exists(absPath) || isEmpty(absPath) {131 return "cmd"132 }133133 base := filepath.Base(absPath)134 for _, cmdDir := range cmdDirs {135 if base == cmdDir {136 return cmdDir137 }134 if isCmdDir(absPath) {135 return filepath.Base(absPath)136 }137138 files, _ := filepath.Glob(filepath.Join(absPath, "c*"))139 for _, file := range files {142 for _, cmdDir := range cmdDirs {143 if file == cmdDir {144 return cmdDir145 }140 if isCmdDir(file) {141 return file142 }143 }144145 return "cmd"146}147148// isCmdDir checks if base of name is one of cmdDir.149func isCmdDir(name string) bool {150 name = filepath.Base(name)151 for _, cmdDir := range cmdDirs {152 if name == cmdDir {153 return true154 }155 }156 return false157}158159// AbsPath returns absolute path of project.160func (p Project) AbsPath() string {161 return p.absPath162}
20 unmodified lines
modified:command_test.go+2 added-4 removed
225 unmodified lines
226}227228// TestSortedFlags checks,229// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.230//231// Source: https://github.com/spf13/cobra/issues/404230// https://github.com/spf13/cobra/issues/404231func TestSortedFlags(t *testing.T) {232 cmd := &Command{}233 cmd.Flags().SortFlags = false234 names := []string{"C", "B", "A", "D"}
27 unmodified lines
262263// TestHelpFlagInHelp checks,264// if '--help' flag is shown in help for child (executing `parent help child`),265// that has no other flags.267//268// Source: https://github.com/spf13/cobra/issues/302266// https://github.com/spf13/cobra/issues/302267func TestHelpFlagInHelp(t *testing.T) {268 output := new(bytes.Buffer)269 parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }}270 parent.SetOutput(output)
14 unmodified lines