Documentation ¶
Index ¶
- Constants
- func GetTaintPosition(app *cli.App) string
- func Inject(app *cli.App, instrumentation InstrumentFunc, opts ...InstrumentOptions) (restore func())
- func InjectAndRun(app *cli.App, arguments []string, instrumentation InstrumentFunc, ...) error
- type ActionFunc
- type AfterFunc
- type BeforeFunc
- type CommandNotFoundFunc
- type ExitErrHandlerFunc
- type FlagEnvHintFunc
- type FlagFileHintFunc
- type FlagNamePrefixFunc
- type FlagStringFunc
- type HelpPrinter
- type HelpPrinterCustom
- type InstrumentAfterFunc
- type InstrumentBeforeFunc
- type InstrumentCommandNotFoundFunc
- type InstrumentExitErrHandlerFunc
- type InstrumentFlagEnvHintFunc
- type InstrumentFlagFileHintFunc
- type InstrumentFlagNamePrefixFunc
- type InstrumentFlagStringFunc
- type InstrumentFunc
- type InstrumentHelpPrinter
- type InstrumentHelpPrinterCustom
- type InstrumentOnUsageErrorFunc
- type InstrumentOptions
- func WithAfterInstrumentation(proxyAfter InstrumentAfterFunc) InstrumentOptions
- func WithBeforeAfterTagging(tags []string) InstrumentOptions
- func WithBeforeInstrumentation(proxyBefore InstrumentBeforeFunc) InstrumentOptions
- func WithCommandNotFoundInstrumentation(proxy InstrumentCommandNotFoundFunc) InstrumentOptions
- func WithExitErrHandlerInstrumentation(proxy InstrumentExitErrHandlerFunc) InstrumentOptions
- func WithFlagEnvHintInstrumentation(proxy InstrumentFlagEnvHintFunc) InstrumentOptions
- func WithFlagFileHintInstrumentation(proxy InstrumentFlagFileHintFunc) InstrumentOptions
- func WithFlagNamePrefixInstrumentation(proxy InstrumentFlagNamePrefixFunc) InstrumentOptions
- func WithFlagStringInstrumentation(proxy InstrumentFlagStringFunc) InstrumentOptions
- func WithHelpCustomInstrumentation(proxy InstrumentHelpPrinterCustom) InstrumentOptions
- func WithHelpInstrumentation(proxy InstrumentHelpPrinter) InstrumentOptions
- func WithOnUsageErrorInstrumentation(proxy InstrumentOnUsageErrorFunc) InstrumentOptions
- type OnUsageErrorFunc
Examples ¶
Constants ¶
const ( AfterTag = "$after" BeforeTag = "$before" CurrentTag = "$current" TaintPositionKey = "$taint" )
Variables ¶
This section is empty.
Functions ¶
func GetTaintPosition ¶
GetTaintPosition returns the position tag of the current execution. The tag is set by WithBeforeAfterTagging.
func Inject ¶
func Inject(app *cli.App, instrumentation InstrumentFunc, opts ...InstrumentOptions) (restore func())
Inject instrument the app. The returned function restores the original state of the app.
func InjectAndRun ¶
func InjectAndRun(app *cli.App, arguments []string, instrumentation InstrumentFunc, opts ...InstrumentOptions) error
InjectAndRun instrument the app and runs it. The returned error is the same as the one returned by app.Run, i.e. https://pkg.golang.ir/github.com/urfave/cli/v3#App.Run.
Example ¶
ExampleInjectAndRun is an example of how to use InjectAndRun to inject a BeforeHook and AfterHook into a cli.App and run the app.
err := InjectAndRun(&cli.App{ Commands: []cli.Command{ { Name: "exec", Action: func(ctx *cli.Context) error { fmt.Printf(" Execute(%v)\n", ctx.Command.Name) return nil }, }, }, }, []string{"awesome-app", "exec"}, func(ctx *cli.Context, next ActionFunc) error { fmt.Printf("clisan.BeforeHook(%v)\n", ctx.Command.Name) err := next(ctx) fmt.Printf("clisan.AfterHook(%v)\n", ctx.Command.Name) return err }) if err != nil { fmt.Printf("error %s", err.Error()) }
Output: clisan.BeforeHook(exec) Execute(exec) clisan.AfterHook(exec)
Types ¶
type ActionFunc ¶
ActionFunc is the action to execute when no subcommands are specified
type AfterFunc ¶
AfterFunc is an action to execute after any subcommands are run, but after the subcommand has finished it is run even if Action() panics
type BeforeFunc ¶
BeforeFunc is an action to execute before any subcommands are run, but after the context is ready if a non-nil error is returned, no subcommands are run
type CommandNotFoundFunc ¶
CommandNotFoundFunc is executed if the proper command cannot be found
type ExitErrHandlerFunc ¶
ExitErrHandlerFunc is executed if provided in order to handle ExitError values returned by Actions and Before/After functions.
type FlagEnvHintFunc ¶
FlagEnvHintFunc is used by the default FlagStringFunc to annotate flag help with the environment variable details.
type FlagFileHintFunc ¶
FlagFileHintFunc is used by the default FlagStringFunc to annotate flag help with the file details.
type FlagNamePrefixFunc ¶
FlagNamePrefixFunc is used by the default FlagStringFunc to create prefix text for a flag's full name.
type FlagStringFunc ¶
FlagStringFunc is used by the help generation to display a flag, which is expected to be a single line.
type HelpPrinter ¶
Prints help for the App or Command
type HelpPrinterCustom ¶
type HelpPrinterCustom = func(w io.Writer, templ string, data interface{}, customFunc map[string]interface{})
Prints help for the App or Command with custom template function.
type InstrumentBeforeFunc ¶
type InstrumentBeforeFunc = func(ctx *cli.Context, next BeforeFunc) error
type InstrumentCommandNotFoundFunc ¶
type InstrumentCommandNotFoundFunc = func(ctx *cli.Context, command string, next CommandNotFoundFunc)
type InstrumentExitErrHandlerFunc ¶
type InstrumentExitErrHandlerFunc = func(ctx *cli.Context, err error, next ExitErrHandlerFunc)
type InstrumentFlagEnvHintFunc ¶
type InstrumentFlagEnvHintFunc = func(envVar, str string, next FlagEnvHintFunc) string
type InstrumentFlagFileHintFunc ¶
type InstrumentFlagFileHintFunc = func(filePath, str string, next FlagFileHintFunc) string
type InstrumentFlagNamePrefixFunc ¶
type InstrumentFlagNamePrefixFunc = func(fullName, placeholder string, next FlagNamePrefixFunc) string
type InstrumentFlagStringFunc ¶
type InstrumentFlagStringFunc = func(flag cli.Flag, next FlagStringFunc) string
type InstrumentFunc ¶
type InstrumentFunc = func(ctx *cli.Context, next ActionFunc) error
type InstrumentHelpPrinter ¶
type InstrumentHelpPrinter = func(w io.Writer, templ string, data interface{}, next HelpPrinter)
type InstrumentHelpPrinterCustom ¶
type InstrumentHelpPrinterCustom = func(w io.Writer, templ string, data interface{}, customFunc map[string]interface{}, next HelpPrinterCustom)
type InstrumentOptions ¶
type InstrumentOptions func(opts *instrumentState)
func WithAfterInstrumentation ¶
func WithAfterInstrumentation(proxyAfter InstrumentAfterFunc) InstrumentOptions
func WithBeforeAfterTagging ¶
func WithBeforeAfterTagging(tags []string) InstrumentOptions
Example ¶
ExampleWithBeforeAfterTagging is an example of how to use WithBeforeAfterTagging to inject a BeforeHook and AfterHook into a cli.App and run the app.
err := InjectAndRun(&cli.App{ Commands: []cli.Command{ { Name: "exec", Before: func(ctx *cli.Context) error { fmt.Printf(" Before(%v)\n", ctx.Command.Name) return nil }, Action: func(ctx *cli.Context) error { fmt.Printf(" Execute(%v)\n", ctx.Command.Name) return nil }, After: func(ctx *cli.Context) error { fmt.Printf(" After(%v)\n", ctx.Command.Name) return nil }, }, }, }, []string{"awesome-app", "exec"}, func(ctx *cli.Context, next ActionFunc) error { fmt.Printf("clisan.BeforeHook%s(%v)\n", GetTaintPosition(ctx.App), ctx.Command.Name) err := next(ctx) fmt.Printf("clisan.AfterHook%s(%v)\n", GetTaintPosition(ctx.App), ctx.Command.Name) return err }, WithBeforeAfterTagging([]string{BeforeTag, AfterTag})) if err != nil { fmt.Printf("error %s", err.Error()) }
Output: clisan.BeforeHook$before(exec) Before(exec) clisan.AfterHook$before(exec) clisan.BeforeHook$current(exec) Execute(exec) clisan.AfterHook$current(exec) clisan.BeforeHook$after(exec) After(exec) clisan.AfterHook$after(exec)
func WithBeforeInstrumentation ¶
func WithBeforeInstrumentation(proxyBefore InstrumentBeforeFunc) InstrumentOptions
func WithCommandNotFoundInstrumentation ¶
func WithCommandNotFoundInstrumentation(proxy InstrumentCommandNotFoundFunc) InstrumentOptions
func WithExitErrHandlerInstrumentation ¶
func WithExitErrHandlerInstrumentation(proxy InstrumentExitErrHandlerFunc) InstrumentOptions
func WithFlagEnvHintInstrumentation ¶
func WithFlagEnvHintInstrumentation(proxy InstrumentFlagEnvHintFunc) InstrumentOptions
func WithFlagFileHintInstrumentation ¶
func WithFlagFileHintInstrumentation(proxy InstrumentFlagFileHintFunc) InstrumentOptions
func WithFlagNamePrefixInstrumentation ¶
func WithFlagNamePrefixInstrumentation(proxy InstrumentFlagNamePrefixFunc) InstrumentOptions
func WithFlagStringInstrumentation ¶
func WithFlagStringInstrumentation(proxy InstrumentFlagStringFunc) InstrumentOptions
func WithHelpCustomInstrumentation ¶
func WithHelpCustomInstrumentation(proxy InstrumentHelpPrinterCustom) InstrumentOptions
func WithHelpInstrumentation ¶
func WithHelpInstrumentation(proxy InstrumentHelpPrinter) InstrumentOptions
func WithOnUsageErrorInstrumentation ¶
func WithOnUsageErrorInstrumentation(proxy InstrumentOnUsageErrorFunc) InstrumentOptions
type OnUsageErrorFunc ¶
OnUsageErrorFunc is executed if an usage error occurs. This is useful for displaying customized usage error messages. This function is able to replace the original error messages. If this function is not set, the "Incorrect usage" is displayed and the execution is interrupted.