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
Provenance
- Commit
d20925b- Tree
0cab77b- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
84cba62
5 files changed+47 added−35 removed
modified:cobra/cmd/add.go+9 added-8 removed
49 unmodified lines50 if err != nil {51 er(err)52 }53 project := NewProjectFromPath(wd)54+55 cmdName := validateCmdName(args[0])56+ cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")55- createCmdFile(project, cmdName)57+ createCmdFile(project.License(), cmdPath, cmdName)58+59+ fmt.Fprintln(cmd.OutOrStdout(), cmdName, "created at", cmdPath)60 },61 }62 63 // 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/26967 func validateCmdName(source string) string {68 i := 069 l := len(source)70 // The output is initialized on demand, then first dash or underscore35 unmodified lines106 }107 return output108 }109 105-func createCmdFile(project *Project, cmdName string) {110+func createCmdFile(license License, path, cmdName string) {111 template := `{{comment .copyright}}112 {{comment .license}}113 114 package cmd35 unmodified lines150 `151 152 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"] = cmdName158 154- filePath := filepath.Join(project.CmdPath(), cmdName+".go")155-159 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 }164-165- fmt.Println(cmdName, "created at", filePath)167 }modified:cobra/cmd/helpers.go+2 added-2 removed
24 unmodified lines25 26 var projectPath string27 28 var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}29-var goPaths, srcPaths []string29+var srcPaths []string30 31 func init() {32 // Initialize goPaths and srcPaths33 envGoPath := os.Getenv("GOPATH")34 if envGoPath == "" {35 er("$GOPATH is not set")36 }37 38- 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 linesmodified:cobra/cmd/init.go+14 added-8 removed
46 unmodified lines47 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 }60 56- initializePath(project)61+ initializeProject(project)62+63+ fmt.Fprintln(cmd.OutOrStdout(), `Your Cobra application is ready at64+`+project.AbsPath()+`.65+66+Give it a try by going there and running `+"`go run main.go`."+`67+Add commands to it by running `+"`cobra add [cmdname]`.")68 },69 }70 60-func initializePath(project *Project) {71+func 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 lines226 if err != nil {227 er(err)228 }229 219- fmt.Println(`Your Cobra application is ready at220-` + project.AbsPath() + `.221-222-Give it a try by going there and running ` + "`go run main.go`." + `223-Add commands to it by running ` + "`cobra add [cmdname]`.")230 }modified:cobra/cmd/project.go+20 added-13 removed
27 unmodified lines28 // 1. Find already created protect.29 p.absPath = findPackage(projectName)30 31 // 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 }46 46- // 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 lines130 if !exists(absPath) || isEmpty(absPath) {131 return "cmd"132 }133 133- 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 }137 138 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 }144 145 return "cmd"146 }147 148+// isCmdDir checks if base of name is one of cmdDir.149+func 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+}158+159 // AbsPath returns absolute path of project.160 func (p Project) AbsPath() string {161 return p.absPath162 }20 unmodified linesmodified:command_test.go+2 added-4 removed
225 unmodified lines226 }227 228 // 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/404231 func TestSortedFlags(t *testing.T) {232 cmd := &Command{}233 cmd.Flags().SortFlags = false234 names := []string{"C", "B", "A", "D"}27 unmodified lines262 263 // 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/302267 func 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