Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/cli/flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
name := cmd.PersistentFlags().String("name", "", "Name of plugin")

cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
if err := upTree(c, func(x *cobra.Command, argv []string) error {
if x.PersistentPreRunE != nil {
return x.PersistentPreRunE(x, argv)
}
return nil
}); err != nil {
return err
}

endpoint, err := plugins().Find(plugin.Name(*name))
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cmd/cli/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
}
name := cmd.PersistentFlags().String("name", DefaultGroupPluginName, "Name of plugin")
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
if err := upTree(c, func(x *cobra.Command, argv []string) error {
if x.PersistentPreRunE != nil {
return x.PersistentPreRunE(x, argv)
}
return nil
}); err != nil {
return err
}

endpoint, err := plugins().Find(plugin.Name(*name))
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cmd/cli/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func instancePluginCommand(plugins func() discovery.Plugins) *cobra.Command {
}
name := cmd.PersistentFlags().String("name", "", "Name of plugin")
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
if err := upTree(c, func(x *cobra.Command, argv []string) error {
if x.PersistentPreRunE != nil {
return x.PersistentPreRunE(x, argv)
}
return nil
}); err != nil {
return err
}

endpoint, err := plugins().Find(plugin.Name(*name))
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func main() {
Short: "infrakit cli",
}
logLevel := cmd.PersistentFlags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
cmd.PersistentPreRun = func(c *cobra.Command, args []string) {
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
cli.SetLogLevel(*logLevel)
return nil
}

// Don't print usage text for any error returned from a RunE function. Only print it when explicitly requested.
Expand Down Expand Up @@ -57,3 +58,14 @@ func assertNotNil(message string, f interface{}) {
os.Exit(1)
}
}

// upTree traverses up the command tree and starts executing the do function in the order from top
// of the command tree to the bottom. Cobra commands executes only one level of PersistentPreRunE
// in reverse order. This breaks our model of setting log levels at the very top and have the log level
// set throughout the entire hierarchy of command execution.
func upTree(c *cobra.Command, do func(*cobra.Command, []string) error) error {
if p := c.Parent(); p != nil {
return upTree(p, do)
}
return do(c, c.Flags().Args())
}
8 changes: 8 additions & 0 deletions cmd/cli/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func managerCommand(plugins func() discovery.Plugins) *cobra.Command {
Short: "Access the manager",
}
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
if err := upTree(c, func(x *cobra.Command, argv []string) error {
if x.PersistentPreRunE != nil {
return x.PersistentPreRunE(x, argv)
}
return nil
}); err != nil {
return err
}

// Scan for a manager
pm, err := plugins().List()
Expand Down