Skip to content
Myr

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

7 files+39 added−47 removed

modified:cobra/cmd/helpers.go+1 added-1 removed
194 unmodified lines
195 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, nil
144 unmodified lines
modified:doc/man_docs_test.go+1 added-2 removed
147 unmodified lines
148 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 }155156 if err := scanner.Err(); err != nil {157 return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())
4 unmodified lines
modified:bash_completions_test.go+2 added-2 removed
23 unmodified lines
24}2526// World worst custom function, just keep telling you to enter hello!27const (28 bash_completion_func = `__custom_func() {28 bashCompletionFunc = `__custom_func() {29COMPREPLY=( "hello" )30}31`32)
3 unmodified lines
36 cmdEcho.AddCommand(cmdTimes)37 c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated, cmdColon)3839 // custom completion function40 c.BashCompletionFunction = bash_completion_func40 c.BashCompletionFunction = bashCompletionFunc4142 // required flag43 c.MarkFlagRequired("introot")44
58 unmodified lines
modified:cobra_test.go+5 added-5 removed
452 unmodified lines
453}454455func 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}462463func TestFlagLong(t *testing.T) {464 noRRSetupTest("echo --intone=13 something -- here")
286 unmodified lines
751 }752}753754func TestRunnableRootCommandNilInput(t *testing.T) {755 empty_arg := make([]string, 0)755 var emptyArg []string756 c := initializeWithRootCmd()757758 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)764765 err := c.Execute()766 if err != nil {767 t.Errorf("Execute() failed with %v", err)
94 unmodified lines
862 }863}864865func TestNoNRunnableRootCommandNilInput(t *testing.T) {866 args := make([]string, 0)866 var args []string867 c := initialize()868869 buf := new(bytes.Buffer)870 // Testing flag with invalid input
318 unmodified lines
modified:cobra.go+2 added-2 removed
24 unmodified lines
25 "text/template"26 "unicode"27)2829var templateFuncs template.FuncMap = template.FuncMap{29var templateFuncs = template.FuncMap{30 "trim": strings.TrimSpace,31 "trimRightSpace": trimRightSpace,32 "appendIfNotPresent": appendIfNotPresent,33 "rpad": rpad,
4 unmodified lines
38var initializers []func()3940// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.41// Set this to true to enable it42var EnablePrefixMatching bool = false42var EnablePrefixMatching = false4344//AddTemplateFunc adds a template function that's available to Usage and Help45//template generation.46func AddTemplateFunc(name string, tmplFunc interface{}) {
125 unmodified lines
modified:command_notwin.go+1 added-1 removed
1// +build !windows23package cobra45var preExecHookFn func(*Command) = nil5var preExecHookFn func(*Command)
modified:command.go+27 added-34 removed
130 unmodified lines
131 }132133 if c.HasParent() {134 return c.parent.Out()135 } else {136 return def135 }136 return def137}138139func (c *Command) Out() io.Writer {140 return c.getOut(os.Stderr)
51 unmodified lines
192 }193194 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}205206// HelpFunc returns either the function set by SetHelpFunc for this command
13 unmodified lines
220 }221 }222}223226var minUsagePadding int = 25224var minUsagePadding = 25225226func (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}232236var minCommandPathPadding int = 11233var minCommandPathPadding = 11234235//236func (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}242247var minNamePadding int = 11243var minNamePadding = 11244245func (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}251252func (c *Command) UsageTemplate() string {253 if c.usageTemplate != "" {
1 unmodified line
255 }256257 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}}263264Aliases:
16 unmodified lines
281 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}282283Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}284`290 }285}286287func (c *Command) HelpTemplate() string {288 if c.helpTemplate != "" {
1 unmodified line
290 }291292 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}}296297{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`304 }298}299300// Really only used when casting a command to a commander301func (c *Command) resetChildrensParents() {
293 unmodified lines
595 x := strings.Split(s, "\n")596597 if len(x) > 0 {598 return x[0]606 } else {607 return ""599 }600 return ""601}602603// Call execute to use the args (os.Args[1:] by default)604// and run through the command tree finding appropriate matches
172 unmodified lines
777 }778 }779}780789// Convenience method to Print to the defined output781// Print is a convenience method to Print to the defined output782func (c *Command) Print(i ...interface{}) {783 fmt.Fprint(c.Out(), i...)784}785794// Convenience method to Println to the defined output786// Println is a convenience method to Println to the defined output787func (c *Command) Println(i ...interface{}) {788 str := fmt.Sprintln(i...)789 c.Print(str)790}791800// Convenience method to Printf to the defined output792// Printf is a convenience method to Printf to the defined output793func (c *Command) Printf(format string, i ...interface{}) {794 str := fmt.Sprintf(format, i...)795 c.Print(str)796}
102 unmodified lines
899 }900 return name901}902911// Determine if a given string is an alias of the command.903// HasAlias determines if a given string is an alias of the command.904func (c *Command) HasAlias(s string) bool {905 for _, a := range c.Aliases {906 if a == s {907 return true
9 unmodified lines
917func (c *Command) HasExample() bool {918 return len(c.Example) > 0919}920929// Determine if the command is itself runnable921// Runnable determines if the command is itself runnable922func (c *Command) Runnable() bool {923 return c.Run != nil || c.RunE != nil924}925934// Determine if the command has children commands926// HasSubCommands determines if the command has children commands927func (c *Command) HasSubCommands() bool {928 return len(c.commands) > 0929}930
183 unmodified lines
1114func (c *Command) HasInheritedFlags() bool {1115 return c.InheritedFlags().HasFlags()1116}11171126// Climbs up the command tree looking for matching flag1118// Flag climbs up the command tree looking for matching flag1119func (c *Command) Flag(name string) (flag *flag.Flag) {1120 flag = c.Flags().Lookup(name)11211122 if flag == nil {
14 unmodified lines
1137 }1138 return1139}11401149// Parses persistent flag tree & local flags1141// ParseFlags parses persistent flag tree & local flags1142func (c *Command) ParseFlags(args []string) (err error) {1143 c.mergePersistentFlags()1144 err = c.Flags().Parse(args)1145 return1146}11471148// Parent returns a commands parent command1149func (c *Command) Parent() *Command {1150 return c.parent1151}1152
38 unmodified lines