action

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README ¶

action

Inspired by Cobra. Cobra is a library for creating powerful modern CLI applications. "action" can be used to create a more fine-grained behavior of a command.

Test Status Codecov Release License

Getting Started

cmd := &cobra.Command{
    Use:   "example-cmd",
    Short: "A sample command.",
    RunE: func(cmd *cobra.Command, args []string) error {
        act := &action.Action{
            Name: "example-action",
            Run:  func(act *action.Action) error { return nil },
        }
        
        _ = act.AddAction(
            newSubAction1(),
            newSubAction2(),
        )

        act.Execute()
    },
}

func newSubAction1() *action.Action {
    return &action.Action{
        Name: "sub-action1", 
        Executable: func(act *action.Action) bool {
            // do something
            return true
        },
        Run:  func(act *action.Action) error { return nil },
    }
}

func newSubAction2() *action.Action {
    return &action.Action{
        Name: "sub-action2",
        Executable: func(act *action.Action) bool {
            // do something
            return false
        },
        Run:  func(act *action.Action) error { return nil },
    }
}
  • Executable: whether is an executable action.
  • action.Execute() will find the first executable action of the root action and execute it. If no executable action can be found, run the current action.

Documentation

You can find the docs at go docs.

🔋 JetBrains OS licenses

action had been being developed with GoLand under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.

JetBrains Logo (Main) logo.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Action ¶

type Action struct {
	// Name action's name
	Name string

	// The *Run functions are executed in the following order:
	//   * PersistentPreRun()
	//   * PreRun()
	//   * Run()
	//   * PostRun()
	//   * PersistentPostRun()
	// All functions get the same args.
	//
	// PersistentPreRun: children of this action will inherit and execute.
	PersistentPreRun func(act *Action) error
	// PreRun: children of this action will not inherit.
	PreRun func(act *Action) error
	// Run: Typically the actual work function. Most actions will only implement this.
	Run func(act *Action) error
	// PostRun: run after the Run action.
	PostRun func(act *Action) error
	// PersistentPostRun: children of this action will inherit and execute after PostRun.
	PersistentPostRun func(act *Action) error

	// Executable: whether is an executable action.
	Executable func(act *Action) bool
	// contains filtered or unexported fields
}

Action is just that, an action for your application.

func (*Action) Actions ¶

func (a *Action) Actions() []*Action

Actions returns a slice of child actions.

func (*Action) AddAction ¶

func (a *Action) AddAction(actions ...*Action) error

AddAction adds one or more actions to this parent action.

func (*Action) Context ¶

func (a *Action) Context() context.Context

Context returns underlying action context. If action wasn't executed with ExecuteContext Context returns Background context.

func (*Action) Execute ¶

func (a *Action) Execute() (err error)

Execute executes the action.

func (*Action) ExecuteContext ¶

func (a *Action) ExecuteContext(ctx context.Context) (err error)

ExecuteContext is the same as Execute(), but sets the ctx on the action.

func (*Action) Find ¶

func (a *Action) Find() *Action

Find the first executable action.

func (*Action) HasParent ¶

func (a *Action) HasParent() bool

HasParent determines if the action is a child action.

func (*Action) HasSubActions ¶

func (a *Action) HasSubActions() bool

HasSubActions determines if the Action has children actions.

func (*Action) Parent ¶

func (a *Action) Parent() *Action

Parent returns a actions parent action.

func (*Action) RemoveAction ¶

func (a *Action) RemoveAction(actions ...*Action)

RemoveAction removes one or more actions from a parent action.

func (*Action) Root ¶

func (a *Action) Root() *Action

Root finds root Action.

func (*Action) Runnable ¶

func (a *Action) Runnable() bool

Runnable determines if the action is itself runnable.

Jump to

Keyboard shortcuts

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