cobrahooks

package module
v0.0.0-...-fd24596 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2023 License: BSD-3-Clause Imports: 1 Imported by: 1

README

Cobrahooks

A more versatile hooks layer for Cobra. It allows to register multiple hooks for cobra commands and ensures persistent hooks are persistently executed.

parentCmd := &Command{
    &cobra.Command{
        Use: "parent",
    },
}

childCmd := &cobra.Command{
    Use: "child",
}

parentCmd.AddCommand(childCmd)

parentCmd.OnPersistentPreRun(func(_ *cobra.Command, args []string) error {
    fmt.Println("Your external behavior")
    return nil
})

Please note that this package makes use of Cobra's (Persistent)(Pre/Post)RunE Command fields. Defining your own hooks using these fields can interfere with the hooks registered through this package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OnHelp

func OnHelp(c *cobra.Command, h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnHelp registers a hook for when help is invoked.

func OnPersistentPostRun

func OnPersistentPostRun(c *cobra.Command, h func(cmd *cobra.Command, args []string) error)

OnPersistentPostRun registers a PostRun hook on the command and all of its childs

func OnPersistentPreRun

func OnPersistentPreRun(c *cobra.Command, h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnPersistentPostRun registers a PreRun hook on the command and all of its childs

func OnPostRun

func OnPostRun(c *cobra.Command, h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnPostRun registers a PostRun hook on the command.

func OnPreRun

func OnPreRun(c *cobra.Command, h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnPreRun registers a PreRun hook on the command.

func OnRun

func OnRun(c *cobra.Command, h func(cmd *cobra.Command, args []string) error)

OnRun registers a Run hook onto the command.

func Persistent

func Persistent(o *HookOptions)

func RunOnHelp

func RunOnHelp(o *HookOptions)

Types

type Command

type Command struct {
	*cobra.Command
}

func NewCommand

func NewCommand(c *cobra.Command) *Command

func (*Command) OnHelp

func (c *Command) OnHelp(h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnHelp registers a hook when help is invoked for the command

func (*Command) OnPersistentPostRun

func (c *Command) OnPersistentPostRun(h func(cmd *cobra.Command, args []string) error)

OnPersistentPostRun registers a PostRun hook on the command and all of its childs

func (*Command) OnPersistentPreRun

func (c *Command) OnPersistentPreRun(h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnPersistentPostRun registers a PreRun hook on the command and all of its childs

func (*Command) OnPostRun

func (c *Command) OnPostRun(h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnPostRun registers a PostRun hook on the command.

func (*Command) OnPreRun

func (c *Command) OnPreRun(h func(cmd *cobra.Command, args []string) error, options ...func(*HookOptions))

OnPreRun registers a PreRun hook on the command.

func (*Command) OnRun

func (c *Command) OnRun(h func(cmd *cobra.Command, args []string) error)

OnRun registers a Run hook onto the command.

type HookOptions

type HookOptions struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL