arg

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: JSON Imports: 7 Imported by: 0

Documentation

Overview

Package arg 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 uses 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

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

func Config

func Config(config arg.Config) optSetter

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

func Exit

func Exit(exit bool) optSetter

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

func Writer

func Writer(w io.Writer) optSetter

Writer 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