Documentation ¶
Index ¶
- func DefaultPreviewTemplateFuncs(keys *[][2]string) template.FuncMap
- func DefaultTemplateFuncs() template.FuncMap
- func ExtractEnvKeysFromConfigFile(file string) ([][2]string, error)
- func FlagSetStruct(name string, errHandling flag.ErrorHandling, out any) *flag.FlagSet
- func ParseConfigFile(file string) error
- func ParseEnvFile(file string) ([][2]string, error)
- func PreviewConfig(configPath string, data string) (string, error)
- func PreviewConfigFile(file string) (string, error)
- func ReadConfig(configPath string, data string) ([]string, error)
- func ReadConfigFile(file string) ([]string, error)
- func Reset(f flag.Value)
- func SourceEnvFile(file string) error
- func StructVar(v any, fs *flag.FlagSet)
- type FieldTextMarshaler
- type FloatSlice
- type Int64Slice
- type StringSlice
- type TemplateConfigRenderer
- type Uint64Slice
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultTemplateFuncs ¶
func ExtractEnvKeysFromConfigFile ¶
ExtractEnvKeysFromConfigFile returns the environment keys that are read from a given config file.
func FlagSetStruct ¶
FlagSetStruct makes a new flagset based on an output string to set to
func ParseConfigFile ¶
ParseConfigFile reads a given filepath and applies command line parsing to it. This is a quick and easy way to provide file-based configuration, a la pip.
If you want to only read the file, use ReadConfig instead.
Comments are lines that start with a '#' (and not in the middle).
The configuration file format assumes:
- any line that starts with a '#' is a comment and ignored (ignoring leading whitespace) - all lines are replaced with spaces - then input is passed to shlex.Split to split by shell parsing rules
This is also templated, so the following variables are available:
- configDir: the directory of the config file
The following functions are available:
- env(key): returns the value of the environment variable - envOr(key, def): returns the value of the environment variable or the default value - envOrError(key, msg): returns the value of the environment variable or errors with msg
Example:
-load {{.configDir}}/file.txt -secret {{env "SECRET"}} -port {{envOr "PORT" "8080"}}
func ParseEnvFile ¶
ParseEnvFile reads a file like an enviroment file.
File format:
- "#" are to-end-of-line comments
- each line is in KEY=VALUE format
- any line without an equal sign is ignored
func PreviewConfig ¶
PreviewConfig returns the contents of data by passing that is ready to pass to flag.FlagSet.Parse(...)
func PreviewConfigFile ¶
PreviewConfigFile returns the contents of file by passing that is ready to pass to flag.FlagSet.Parse(...)
func ReadConfig ¶
ReadConfigFile returns args to parse from a given file path
Comments are lines that start with a '#' (and not in the middle).
The configuration file format assumes:
- any line that starts with a '#' is a comment and ignored (ignoring leading whitespace) - all lines are replaced with spaces - then input is passed to shlex.Split to split by shell parsing rules
This is also templated, so the following variables are available:
- configDir: the directory of the config file
The following functions are available:
- env(key): returns the value of the environment variable - envOr(key, def): returns the value of the environment variable or the default value - envOrError(key, msg): returns the value of the environment variable or errors with msg
Example:
-load {{.configDir}}/file.txt -secret {{env "SECRET"}} -port {{envOr "PORT" "8080"}}
func ReadConfigFile ¶
ReadConfigFile returns args to parse from a given file. If you have already opened the file, use ReadConfig instead.
Comments are lines that start with a '#' (and not in the middle).
The configuration file format assumes:
- any line that starts with a '#' is a comment and ignored (ignoring leading whitespace) - all lines are replaced with spaces - then input is passed to shlex.Split to split by shell parsing rules
This is also templated, so the following variables are available:
- configDir: the directory of the config file
The following functions are available:
- env(key): returns the value of the environment variable - envOr(key, def): returns the value of the environment variable or the default value
Example:
-load {{.configDir}}/file.txt -secret {{env "SECRET"}} -port {{envOr "PORT" "8080"}}
func Reset ¶
Reset zeros out the flag.Value given.
If the flag.Value has a Reset() method, that is called instead. Otherwise, defaults to calling value.Set("").
Implementers of Reset() should take case to not mutate the original value, in case it's used in other parts of the code base (post flag parsing).
Example:
var args flage.StringSlice flag.Var(&args, "arg", "additional arguments to pass. Can be used multiple times") // ... flag.Parse() fmt.Printf("args are: %s", strings.Join(args, ", ")) flage.Reset(&args) fmt.Printf("args are: %s", strings.Join(args, ", ")) // will be empty
func SourceEnvFile ¶
SourceEnvFile parses a env file via ParseEnvFile and sets the process' environment variables
func StructVar ¶
StructVar performs like flag.Var(...) but using a struct. Can optionally be annotated using tags. If fs is nil, then the global functions in the flag package are used instead.
Tags use the "flag" key with the following values: "<flagName>,<defaultValue>,<description>" If <flagName> is empty, then the lowercase of the fieldname is used. If <defaultValue> is empty, then the zero value is used. If <description> is empty, then the empty string is used.
As per flag package, the following types are supported:
- string
- float64
- uint / uint64
- int / int64
- bool
- flag.Value
- encoding.TextUnmarshaler | encoding.TextMarshaler
If encoding.TextUnmarshler is used then a method on the struct must be used to provide defaults to encoding.TextUnmarshaler:
func (m *myOptions) MarshalFlagField(field string) encoding.TextMarshaler { if field == "myTextUnmarshalerField" { return big.NewInt(123) } return nil // panics will happen if we return nil }
Example:
type Flag struct { Install bool `flag:"install,,enables installation"` ConfigFile string `flag:"config,,optional config file to load"` } var f Flag StructVar(&f, nil) flag.Parse()
Types ¶
type FieldTextMarshaler ¶
type FieldTextMarshaler interface {
MarshalFlagField(field string) encoding.TextMarshaler
}
FieldTextMarshaler is the interface a struct needs to implement if it has fields of type encoding.TextUnmarshaler to return the appropriate default value.
type FloatSlice ¶
type FloatSlice []float64
FloatSlice is a slice where mutliple of the flag appends to the slice Use ResetValues() to clear the slice (for multi-stage flag parsing)
func (*FloatSlice) Reset ¶
func (i *FloatSlice) Reset()
func (*FloatSlice) Set ¶
func (i *FloatSlice) Set(value string) error
Set appends an int64 or returns error if it is an invalid float64. Use Reset() to reset the string slice to an empty slice.
func (*FloatSlice) String ¶
func (i *FloatSlice) String() string
String returns a string with ", " joined between each element
type Int64Slice ¶
type Int64Slice []int64
Int64Slice is a slice where mutliple of the flag appends to the slice Use ResetValues() to clear the slice (for multi-stage flag parsing)
func (*Int64Slice) Set ¶
func (i *Int64Slice) Set(value string) error
Set appends an int64 or returns error if it is an invalid int. Use Reset() to reset the string slice to an empty slice.
func (*Int64Slice) String ¶
func (i *Int64Slice) String() string
String returns a string with ", " joined between each element
type StringSlice ¶
type StringSlice []string
StringSlice is a slice where mutliple of the flag appends to the slice Use ResetValues() to clear the slice (for multi-stage flag parsing)
func (*StringSlice) Set ¶
func (i *StringSlice) Set(value string) error
Set appends to the string slice. Use Reset() to reset the string slice to an empty slice.
func (*StringSlice) String ¶
func (i *StringSlice) String() string
String returns a string with ", " joined between each element
type TemplateConfigRenderer ¶
TemplateConfigRenderer manages rendering of config files that is used by other helper functions:
- ParseConfigFile - ReadConfigFile - ReadConfig - ExtractEnvKeysFromConfigFile - PreviewConfigFile - PreviewConfig
This allows you to set your own template variables and functions. Use DefaultTemplateFuncs to get default template functions used.
type Uint64Slice ¶
type Uint64Slice []uint64
Uint64Slice is a slice where mutliple of the flag appends to the slice Use ResetValues() to clear the slice (for multi-stage flag parsing)
func (*Uint64Slice) Set ¶
func (i *Uint64Slice) Set(value string) error
Set appends an int64 or returns error if it is an invalid uint. Use Reset() to reset the string slice to an empty slice.
func (*Uint64Slice) String ¶
func (i *Uint64Slice) String() string
String returns a string with ", " joined between each element