Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileDecoder ¶
type FileOption ¶
type FileOption func(s *fileSource)
func FileDecoders ¶
func FileDecoders(decoder ...FileDecoder) FileOption
func FileFlagSet ¶
func FileFlagSet(set FlagSet) FileOption
type FlagOption ¶
type FlagOption func(*flagSource)
func FlagSplitter ¶
func FlagSplitter(splitter string) FlagOption
func FlagWithFlagSet ¶
func FlagWithFlagSet(set FlagSet) FlagOption
type FlagSet ¶
type FlagSet interface { BoolVar(p *bool, name string, value bool, usage string) StringVar(p *string, name string, value string, usage string) DurationVar(p *time.Duration, name string, value time.Duration, usage string) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string) Float64Var(p *float64, name string, value float64, usage string) Uint64Var(p *uint64, name string, value uint64, usage string) UintVar(p *uint, name string, value uint, usage string) Int64Var(p *int64, name string, value int64, usage string) IntVar(p *int, name string, value int, usage string) Var(value flag.Value, name string, usage string) }
type Parser ¶
type Parser[T any] struct { // contains filtered or unexported fields }
Example ¶
An example to load `Config` from sources orderly: - from a config file - from env variables - from flags
The same field in the flag overwrites the env variable, and the same file in the env variable overwrites the value in the config file.
package main import ( "bytes" "context" "flag" "fmt" "os" "strings" "github.com/googollee/go-cfg" ) type Config struct { Str string `cfg:"str,,string value from file"` I int `cfg:"int,,int value from file"` FromEnv struct { Str string `cfg:"str,none,string value from env"` } `cfg:"from_env"` FromFlag struct { Str string `cfg:"str,none,string value from flags"` } `cfg:"from_flag"` WithDefault struct { Str string `cfg:"str,default_value,string value with default"` } `cfg:"with_default"` } // An example to load `Config` from sources orderly: // - from a config file // - from env variables // - from flags // // The same field in the flag overwrites the env variable, and the same file in the env variable overwrites the value in the config file. func main() { os.Setenv("DEMO_FROM_ENV_STR", "string_from_env") set := flag.NewFlagSet("demo", flag.PanicOnError) parser := cfg.Parse[Config]( cfg.FromFile("config", "./testdata/config.json", "config file", cfg.FileDecoders(cfg.JSON{}), cfg.FileFlagSet(set)), cfg.FromEnv("DEMO", cfg.EnvSplitter("_")), cfg.FromFlag("demo", cfg.FlagSplitter("."), cfg.FlagWithFlagSet(set)), ) if err := set.Parse([]string{ "--config", "./testdata/config.json", "--demo.from_flag.str", "string_from_flag", }); err != nil { fmt.Println("flag error:", err) return } var buf bytes.Buffer set.SetOutput(&buf) set.Usage() fmt.Println(strings.ReplaceAll(buf.String(), "\t", " ")) var config Config if err := parser.Parse(context.Background(), &config); err != nil { fmt.Println("load config error:", err) return } fmt.Println("config:", config) }
Output: Usage of demo: -config string config file (default "./testdata/config.json") -demo.from_env.str value string value from env (default none) -demo.from_flag.str value string value from flags (default none) -demo.int value int value from file -demo.str value string value from file -demo.with_default.str value string value with default (default default_value) config: {string 10 {string_from_env} {string_from_flag} {default_value}}
Source Files ¶
Click to show internal directories.
Click to hide internal directories.