Merge pull request #260 from ianwalter/code-cleanup
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixing golint warnings
authored by Eric Paris · committed by Eric Paris · parents ee150b4, a0bd6c1 · Browse files · 7 files·+39 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+39 added−47 removed
- other:+39 added−47 removed
Provenance
- Commit
b0d571e- Tree
7554965- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
ee150b4- Parent 2
a0bd6c1
7 files changed+39 added−47 removed
modified:cobra/cmd/helpers.go+1 added-1 removed
194 unmodified lines195 defer f.Close()196 if err != nil {197 return false, err198 }199- list, err := f.Readdir(-1)199+ list, _ := f.Readdir(-1)200 // f.Close() - see bug fix above201 return len(list) == 0, nil202 }203 return fi.Size() == 0, nil144 unmodified linesmodified:doc/man_docs_test.go+1 added-2 removed
147 unmodified lines148 if scanner.Scan() {149 line := scanner.Text()150 if line == expectedLine {151 return nil152- } else {153- return fmt.Errorf("AssertNextLineEquals: got %#v, not %#v", line, expectedLine)152 }153+ return fmt.Errorf("AssertNextLineEquals: got %#v, not %#v", line, expectedLine)154 }155 156 if err := scanner.Err(); err != nil {157 return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())4 unmodified linesmodified:bash_completions_test.go+2 added-2 removed
23 unmodified lines24 }25 26 // World worst custom function, just keep telling you to enter hello!27 const (28- bash_completion_func = `__custom_func() {28+ bashCompletionFunc = `__custom_func() {29 COMPREPLY=( "hello" )30 }31 `32 )3 unmodified lines36 cmdEcho.AddCommand(cmdTimes)37 c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated, cmdColon)38 39 // custom completion function40- c.BashCompletionFunction = bash_completion_func40+ c.BashCompletionFunction = bashCompletionFunc41 42 // required flag43 c.MarkFlagRequired("introot")44 58 unmodified linesmodified:cobra_test.go+5 added-5 removed
452 unmodified lines453 }454 455 func TestUsage(t *testing.T) {456 x := fullSetupTest("help")457- checkResultContains(t, x, cmdRootWithRun.Use + " [flags]")457+ checkResultContains(t, x, cmdRootWithRun.Use+" [flags]")458 x = fullSetupTest("help customflags")459 checkResultContains(t, x, cmdCustomFlags.Use)460- checkResultOmits(t, x, cmdCustomFlags.Use + " [flags]")460+ checkResultOmits(t, x, cmdCustomFlags.Use+" [flags]")461 }462 463 func TestFlagLong(t *testing.T) {464 noRRSetupTest("echo --intone=13 something -- here")286 unmodified lines751 }752 }753 754 func TestRunnableRootCommandNilInput(t *testing.T) {755- empty_arg := make([]string, 0)755+ var emptyArg []string756 c := initializeWithRootCmd()757 758 buf := new(bytes.Buffer)759 // Testing flag with invalid input760 c.SetOutput(buf)761 cmdEcho.AddCommand(cmdTimes)762 c.AddCommand(cmdPrint, cmdEcho)763- c.SetArgs(empty_arg)763+ c.SetArgs(emptyArg)764 765 err := c.Execute()766 if err != nil {767 t.Errorf("Execute() failed with %v", err)94 unmodified lines862 }863 }864 865 func TestNoNRunnableRootCommandNilInput(t *testing.T) {866- args := make([]string, 0)866+ var args []string867 c := initialize()868 869 buf := new(bytes.Buffer)870 // Testing flag with invalid input318 unmodified linesmodified:cobra.go+2 added-2 removed
24 unmodified lines25 "text/template"26 "unicode"27 )28 29-var templateFuncs template.FuncMap = template.FuncMap{29+var templateFuncs = template.FuncMap{30 "trim": strings.TrimSpace,31 "trimRightSpace": trimRightSpace,32 "appendIfNotPresent": appendIfNotPresent,33 "rpad": rpad,4 unmodified lines38 var initializers []func()39 40 // automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.41 // Set this to true to enable it42-var EnablePrefixMatching bool = false42+var EnablePrefixMatching = false43 44 //AddTemplateFunc adds a template function that's available to Usage and Help45 //template generation.46 func AddTemplateFunc(name string, tmplFunc interface{}) {125 unmodified linesmodified:command_notwin.go+1 added-1 removed
1 // +build !windows2 3 package cobra4 5-var preExecHookFn func(*Command) = nil5+var preExecHookFn func(*Command)modified:command.go+27 added-34 removed
130 unmodified lines131 }132 133 if c.HasParent() {134 return c.parent.Out()135- } else {136- return def135 }136+ return def137 }138 139 func (c *Command) Out() io.Writer {140 return c.getOut(os.Stderr)51 unmodified lines192 }193 194 if c.HasParent() {195 return c.parent.UsageFunc()197- } else {198- return func(c *Command) error {199- err := tmpl(c.Out(), c.UsageTemplate(), c)200- if err != nil {201- fmt.Print(err)202- }203- return err196+ }197+ return func(c *Command) error {198+ err := tmpl(c.Out(), c.UsageTemplate(), c)199+ if err != nil {200+ fmt.Print(err)201 }202+ return err203 }204 }205 206 // HelpFunc returns either the function set by SetHelpFunc for this command13 unmodified lines220 }221 }222 }223 226-var minUsagePadding int = 25224+var minUsagePadding = 25225 226 func (c *Command) UsagePadding() int {227 if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {228 return minUsagePadding231- } else {232- return c.parent.commandsMaxUseLen229 }230+ return c.parent.commandsMaxUseLen231 }232 236-var minCommandPathPadding int = 11233+var minCommandPathPadding = 11234 235 //236 func (c *Command) CommandPathPadding() int {237 if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {238 return minCommandPathPadding242- } else {243- return c.parent.commandsMaxCommandPathLen239 }240+ return c.parent.commandsMaxCommandPathLen241 }242 247-var minNamePadding int = 11243+var minNamePadding = 11244 245 func (c *Command) NamePadding() int {246 if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {247 return minNamePadding252- } else {253- return c.parent.commandsMaxNameLen248 }249+ return c.parent.commandsMaxNameLen250 }251 252 func (c *Command) UsageTemplate() string {253 if c.usageTemplate != "" {1 unmodified line255 }256 257 if c.HasParent() {258 return c.parent.UsageTemplate()264- } else {265- return `Usage:{{if .Runnable}}259+ }260+ return `Usage:{{if .Runnable}}261 {{if .HasFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasSubCommands}}262 {{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}263 264 Aliases:16 unmodified lines281 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}282 283 Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}284 `290- }285 }286 287 func (c *Command) HelpTemplate() string {288 if c.helpTemplate != "" {1 unmodified line290 }291 292 if c.HasParent() {293 return c.parent.HelpTemplate()300- } else {301- return `{{with or .Long .Short }}{{. | trim}}294+ }295+ return `{{with or .Long .Short }}{{. | trim}}296 297 {{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`304- }298 }299 300 // Really only used when casting a command to a commander301 func (c *Command) resetChildrensParents() {293 unmodified lines595 x := strings.Split(s, "\n")596 597 if len(x) > 0 {598 return x[0]606- } else {607- return ""599 }600+ return ""601 }602 603 // Call execute to use the args (os.Args[1:] by default)604 // and run through the command tree finding appropriate matches172 unmodified lines777 }778 }779 }780 789-// Convenience method to Print to the defined output781+// Print is a convenience method to Print to the defined output782 func (c *Command) Print(i ...interface{}) {783 fmt.Fprint(c.Out(), i...)784 }785 794-// Convenience method to Println to the defined output786+// Println is a convenience method to Println to the defined output787 func (c *Command) Println(i ...interface{}) {788 str := fmt.Sprintln(i...)789 c.Print(str)790 }791 800-// Convenience method to Printf to the defined output792+// Printf is a convenience method to Printf to the defined output793 func (c *Command) Printf(format string, i ...interface{}) {794 str := fmt.Sprintf(format, i...)795 c.Print(str)796 }102 unmodified lines899 }900 return name901 }902 911-// Determine if a given string is an alias of the command.903+// HasAlias determines if a given string is an alias of the command.904 func (c *Command) HasAlias(s string) bool {905 for _, a := range c.Aliases {906 if a == s {907 return true9 unmodified lines917 func (c *Command) HasExample() bool {918 return len(c.Example) > 0919 }920 929-// Determine if the command is itself runnable921+// Runnable determines if the command is itself runnable922 func (c *Command) Runnable() bool {923 return c.Run != nil || c.RunE != nil924 }925 934-// Determine if the command has children commands926+// HasSubCommands determines if the command has children commands927 func (c *Command) HasSubCommands() bool {928 return len(c.commands) > 0929 }930 183 unmodified lines1114 func (c *Command) HasInheritedFlags() bool {1115 return c.InheritedFlags().HasFlags()1116 }1117 1126-// Climbs up the command tree looking for matching flag1118+// Flag climbs up the command tree looking for matching flag1119 func (c *Command) Flag(name string) (flag *flag.Flag) {1120 flag = c.Flags().Lookup(name)1121 1122 if flag == nil {14 unmodified lines1137 }1138 return1139 }1140 1149-// Parses persistent flag tree & local flags1141+// ParseFlags parses persistent flag tree & local flags1142 func (c *Command) ParseFlags(args []string) (err error) {1143 c.mergePersistentFlags()1144 err = c.Flags().Parse(args)1145 return1146 }1147 1148+// Parent returns a commands parent command1149 func (c *Command) Parent() *Command {1150 return c.parent1151 }1152 38 unmodified lines