arg

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: 0

Documentation

Overview

This is an interceptor dedicated to push flags found to the final schema given by gautocloud. if flags is not a zero value it will override value from schema given by gautocloud. It use https://github.com/alexflint/go-arg to translate flags into a struct.

Example
// This is only to fake args from console
// Normally you don't have to set this option
argsOpt := Args([]string{
	"app",
	`--foo=bar`,
	"--bar",
})
argInterceptor := NewArg(argsOpt)
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{}, argInterceptor))
gautocloud.ReloadConnectors()
//////

var config MyConfig

err := gautocloud.Inject(&config)
if err != nil {
	panic(err)
}
// We can see that we have our config altered by flags found
fmt.Println(fmt.Sprintf("%#v", config))
Output:

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args

func Args(args []string) optSetter

Option to set args to be parsed as flags Default: os.Args

func Config

func Config(config arg.Config) optSetter

Option to set arg.Config from https://github.com/alexflint/go-arg Default: arg.Config{}

func Exit

func Exit(exit bool) optSetter

Option to exit program or not when --help or --version flags has been found Default: true

func Writer

func Writer(w io.Writer) optSetter

Option to set writer for output Default: os.Stdout

Types

type ArgInterceptor

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

func NewArg

func NewArg(setters ...optSetter) *ArgInterceptor

func (ArgInterceptor) Intercept

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

Jump to

Keyboard shortcuts

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