Skip to content
Myr

Merge pull request #102 from andronat/master

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

Fixed Persistent-Run function propagation

authored by Eric Paris · committed by Eric Paris · parents 3ee9552, 799a8ef · Browse files · 2 files+27 added−12 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+27 added−12 removed

  • other:+27 added−12 removed

2 files+27 added−12 removed

modified:cobra_test.go+17 added
867 unmodified lines
868 if echoPre != nil || echoPersPre != nil || timesPersPre != nil {869 t.Error("Wrong *Pre functions called!")870 }871}872873// Check if cmdEchoSub gets PersistentPreRun from rootCmd even if is added last874func TestPeristentPreRunPropagation(t *testing.T) {875 rootCmd := initialize()876877 // First add the cmdEchoSub to cmdPrint878 cmdPrint.AddCommand(cmdEchoSub)879 // Now add cmdPrint to rootCmd880 rootCmd.AddCommand(cmdPrint)881882 rootCmd.SetArgs(strings.Split("print echosub lala", " "))883 rootCmd.Execute()884885 if rootPersPre == nil || len(rootPersPre) == 0 || rootPersPre[0] != "lala" {886 t.Error("RootCmd PersistentPreRun not called but should have been")887 }888}
modified:command.go+10 added-12 removed
461 unmodified lines
462463 c.preRun()464 argWoFlags := c.Flags().Args()465466 for p := c; p != nil; p = p.Parent() {466 if c.PersistentPreRun != nil {467 c.PersistentPreRun(c, argWoFlags)467 if p.PersistentPreRun != nil {468 p.PersistentPreRun(c, argWoFlags)469 break470 }471 }472 if c.PreRun != nil {473 c.PreRun(c, argWoFlags)474 }
2 unmodified lines
477478 if c.PostRun != nil {479 c.PostRun(c, argWoFlags)480 }481 for p := c; p != nil; p = p.Parent() {478 if c.PersistentPostRun != nil {479 c.PersistentPostRun(c, argWoFlags)482 if p.PersistentPostRun != nil {483 p.PersistentPostRun(c, argWoFlags)484 break485 }486 }487488 return nil489}
116 unmodified lines
606 if nameLen > c.commandsMaxNameLen {607 c.commandsMaxNameLen = nameLen608 }609 c.commands = append(c.commands, x)604605 // Pass on peristent pre/post functions to children606 if x.PersistentPreRun == nil {607 x.PersistentPreRun = c.PersistentPreRun608 }609 if x.PersistentPostRun == nil {610 x.PersistentPostRun = c.PersistentPostRun611 }610 }611}612613// AddCommand removes one or more commands from a parent command.
390 unmodified lines