Skip to content
Myr

Add support for an InitializeConfig method as defined on Cobra by the user

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

authored by spf13 · committed by spf13 · parent 10a8494 · Browse files · 2 files+20 added−2 removed
This commit

State

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

Diff stats

2 files+20 added−2 removed

  • other:+20 added−2 removed

2 files+20 added−2 removed

modified:cobra.go+7 added
23 unmodified lines
24 "strings"25 "text/template"26)2728// Called after flags are parsed immediately before executing any Command29var InitializeConfig func()3031func init() {32 InitializeConfig = func() {}33}3435func Gt(a interface{}, b interface{}) bool {36 var left, right int6437 av := reflect.ValueOf(a)38
55 unmodified lines
modified:command.go+13 added-2 removed
348 unmodified lines
349 c.Help()350 return nil351 }352353 c.preRun()354 argWoFlags := c.Flags().Args()355 c.Run(c, argWoFlags)356 return nil357 }358}359360func (c *Command) preRun() {361 InitializeConfig()362}363364func (c *Command) errorMsgFromParse() string {365 s := c.flagErrorBuf.String()366367 x := strings.Split(s, "\n")
39 unmodified lines
407 }408409 // Now handle the case where the root is runnable and only flags are provided410 if err != nil && c.Runnable() {411 // This is pretty much a custom version of the *Command.execute method412 // with a few differences because it's the final command (no fall back)413 e := c.ParseFlags(args)414 if e != nil {415 // Flags parsing had an error.409 //fmt.Println(e)416 // If an error happens here, we have to report it to the user417 c.Println(c.errorMsgFromParse())418 c.Usage()419 return e420 } else {414421 // If help is called, regardless of other flags, we print that422 if c.helpFlagVal {423 c.Help()424 return nil425 }426427 argWoFlags := c.Flags().Args()428 if len(argWoFlags) > 0 {429 // If there are arguments (not flags) one of the earlier430 // cases should have caught it.. It means invalid usage431 // print the usage432 c.Usage()433 } else {434 // Only flags left... Call root.Run435 c.preRun()436 c.Run(c, argWoFlags)437 err = nil438 }439 }
296 unmodified lines