Skip to content
peakPublic

About

Load config files and watch for changes

Resources

Stars

12 stars

Watchers

7 watching

Forks

Latest commit

 

History

34 Commits

Folders and files

Repository files navigation

Build Status Go Report Card

go-config

Offers a rich configuration file handler.

  • Read configuration files with ease
  • Bind CLI flags
  • Bind environment variables
  • Watch file (or files) and get notified if they change

Uses the following precedence order:

  • flag
  • env
  • toml
flag env toml result
☑ ☑ ☑ flag
☑ ☑ ☐ flag
☑ ☐ ☑ flag
☐ ☑ ☑ env
☑ ☐ ☐ flag
☐ ☑ ☐ env
☐ ☐ ☑ toml

If flag is set and not given, it will parse env or toml according to their precedence order (otherwise flag default).

Basic Example

Call the Load() method to load a config.

    type MyConfig struct {
        Key1    string   `toml:"key1"`
        Key2    string   `toml:"key2"`
        Port    int      `toml:"-" flag:"port"`
        Secret  string   `toml:"-" flag:"-" env:"secret"`
    }

    _ = flag.Int("port", 8080, "Port to listen on") // <- notice no variable
    flag.Parse()

    var cfg MyConfig
    err := config.Load("./config.toml", &cfg)

    fmt.Printf("Loaded config: %#v\n", cfg)
    // Port info is in cfg.Port, parsed from `-port` param
    // Secret info is in cfg.Secret, parsed from `secret` environment variable

File Watching

Call Watch() method, get a notification channel and listen...

    ch, err := config.Watch(context.Background(), "config.toml")

    for {
        select {
        case e := <-ch:
        	if e != nil {
        		fmt.Printf("Error occured watching file: %v", e)
        		continue
        	}

            fmt.Println("Changed, reloading...")
            var cfg MyConfig
            err := config.Load("config.toml", &cfg)
            fmt.Printf("Loaded: %v %#v\n", err, cfg)
            // Handle cfg...
        }
    }

About

Load config files and watch for changes

Resources

Stars

12 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages