Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 9d6641d

Browse files
author
David Chung
authored
Workaround for Cobra.Command handling of PersistentPreRunE not setting log level (#372)
Signed-off-by: David Chung <[email protected]>
1 parent 93f6892 commit 9d6641d

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

cmd/cli/flavor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
2828
name := cmd.PersistentFlags().String("name", "", "Name of plugin")
2929

3030
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
31+
if err := upTree(c, func(x *cobra.Command, argv []string) error {
32+
if x.PersistentPreRunE != nil {
33+
return x.PersistentPreRunE(x, argv)
34+
}
35+
return nil
36+
}); err != nil {
37+
return err
38+
}
3139

3240
endpoint, err := plugins().Find(plugin.Name(*name))
3341
if err != nil {

cmd/cli/group.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
3131
}
3232
name := cmd.PersistentFlags().String("name", DefaultGroupPluginName, "Name of plugin")
3333
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
34+
if err := upTree(c, func(x *cobra.Command, argv []string) error {
35+
if x.PersistentPreRunE != nil {
36+
return x.PersistentPreRunE(x, argv)
37+
}
38+
return nil
39+
}); err != nil {
40+
return err
41+
}
3442

3543
endpoint, err := plugins().Find(plugin.Name(*name))
3644
if err != nil {

cmd/cli/instance.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func instancePluginCommand(plugins func() discovery.Plugins) *cobra.Command {
2626
}
2727
name := cmd.PersistentFlags().String("name", "", "Name of plugin")
2828
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
29+
if err := upTree(c, func(x *cobra.Command, argv []string) error {
30+
if x.PersistentPreRunE != nil {
31+
return x.PersistentPreRunE(x, argv)
32+
}
33+
return nil
34+
}); err != nil {
35+
return err
36+
}
2937

3038
endpoint, err := plugins().Find(plugin.Name(*name))
3139
if err != nil {

cmd/cli/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ func main() {
1919
Short: "infrakit cli",
2020
}
2121
logLevel := cmd.PersistentFlags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
22-
cmd.PersistentPreRun = func(c *cobra.Command, args []string) {
22+
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
2323
cli.SetLogLevel(*logLevel)
24+
return nil
2425
}
2526

2627
// Don't print usage text for any error returned from a RunE function. Only print it when explicitly requested.
@@ -57,3 +58,14 @@ func assertNotNil(message string, f interface{}) {
5758
os.Exit(1)
5859
}
5960
}
61+
62+
// upTree traverses up the command tree and starts executing the do function in the order from top
63+
// of the command tree to the bottom. Cobra commands executes only one level of PersistentPreRunE
64+
// in reverse order. This breaks our model of setting log levels at the very top and have the log level
65+
// set throughout the entire hierarchy of command execution.
66+
func upTree(c *cobra.Command, do func(*cobra.Command, []string) error) error {
67+
if p := c.Parent(); p != nil {
68+
return upTree(p, do)
69+
}
70+
return do(c, c.Flags().Args())
71+
}

cmd/cli/manager.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ func managerCommand(plugins func() discovery.Plugins) *cobra.Command {
2727
Short: "Access the manager",
2828
}
2929
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
30+
if err := upTree(c, func(x *cobra.Command, argv []string) error {
31+
if x.PersistentPreRunE != nil {
32+
return x.PersistentPreRunE(x, argv)
33+
}
34+
return nil
35+
}); err != nil {
36+
return err
37+
}
3038

3139
// Scan for a manager
3240
pm, err := plugins().List()

0 commit comments

Comments
 (0)