urfave

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: JSON Imports: 6 Imported by: 2

Documentation

Overview

This is an interceptor dedicated to push flags from https://github.com/urfave/cli to the final schema given by gautocloud. if flags is not a zero value it will override value from schema given by gautocloud.

Example
cliInterceptor := NewCli()
type MyConfig struct {
	Foo  string
	Bar  bool
	Orig string
}

// Initialize a fake cloud env only for example, normally you should do this in init() function
os.Clearenv()
os.Setenv("DYNO", "true")
// Here we set a value for Orig field from MyConfig schema
os.Setenv("CONFIG_ORIG", "<injected by gautocloud>")
gautocloud.RegisterConnector(generic.NewConfigGenericConnector(MyConfig{}, cliInterceptor))
gautocloud.ReloadConnectors()
//////

// This is our urfave cli command action
action := func(c *cli.Context) error {
	config := MyConfig{}
	// We pass context to interceptor, this is mandatory
	cliInterceptor.SetContext(c)

	// We asking to retrieve a config schema through injection
	err := gautocloud.Inject(&config)
	if err != nil {
		panic(err)
	}
	// We can see that we have our config altered by flags found by urfave/cli
	fmt.Println(fmt.Sprintf("%#v", config))
	return nil
}

// Here we simply create a fake urfave/cli app
app := cli.NewApp()

app.Flags = []cli.Flag{
	cli.StringFlag{
		Name:  "foo, f",
		Value: "Foo",
		Usage: "foo",
	},
}
app.Commands = []cli.Command{
	{
		Name:        "doo",
		Aliases:     []string{"do"},
		Category:    "motion",
		Usage:       "do the doo",
		UsageText:   "doo - does the dooing",
		Description: "no really, there is a lot of dooing to be done",
		ArgsUsage:   "[arrgh]",
		Flags: []cli.Flag{
			cli.BoolFlag{Name: "bar, b"},
		},
		Action: action,
	},
}
app.Run([]string{"app", "--foo=bar", "doo", "--bar"})
Output:

urfave_test.MyConfig{Foo:"bar", Bar:true, Orig:"<injected by gautocloud>"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CliInterceptor

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

func NewCli

func NewCli() *CliInterceptor

func (CliInterceptor) Intercept

func (i CliInterceptor) Intercept(current, found interface{}) (interface{}, error)

func (*CliInterceptor) SetContext

func (i *CliInterceptor) SetContext(context *cli.Context)

Jump to

Keyboard shortcuts

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