modulepackage
0.0.0-20240308113458-d8f1afbc0820
Repository: https://github.com/ungerik/go-dynconfig.git
Documentation: pkg.go.dev
# README
go-dynconfig
Dynamic reload of a watched config file
Example:
type Config struct {
A string
B bool
C int
}
// Load config of type *Config from "config.json" and watch for changes.
// config.Get() will return the latest configuration of type *Config
// independent of any errors during loading.
var config = dynconfig.MustLoadAndWatch(
"config.json",
dynconfig.LoadEnvJSON[*Config],
nil, // onLoad
nil, // onError: nil means that errors will panic
nil, // onInvalidate
)
// Use the callbacks to log what's happening
// and return a default configuration in case of an error.
var emailBlackist = dynconfig.MustLoadAndWatch(
"email-blacklist.txt",
dynconfig.LoadStringLineSetTrimSpace,
// onLoad
func(loaded map[string]struct{}) map[string]struct{} {
log.Printf("Loaded email blacklist with %d addresses", len(loaded))
return loaded
},
// onError
func(err error) map[string]struct{} {
log.Printf("Can't load email blacklist because: %s", err)
return map[string]struct{}{"[email protected]": {}} // default in case of an error
},
// onInvalidate
func() {
log.Print("Invalidated email blacklist")
},
)
func main() {
// Get will always return the latest configuration
// independent of any errors during loading
log.Printf("Loaded config: %#v", config.Get())
log.Printf("Loaded blacklist: %s", emailBlackist.Get())
}
# Packages
No description provided by the author
# Functions
LoadAndWatch returns a new Loader for the type T that watches the given file for changes.
LoadEnvJSON unmarshals the passed JSON file into a config of type T and then parses environment variables into the config by looking for struct fields with an `env` tag.
LoadXML unmarshals the passed XML file into a config of type T and then parses environment variables into the config by looking for struct fields with an `env` tag.
LoadJSON unmarshals the passed JSON file into a config of type T.
LoadString reads the passed file as a string.
LoadStringLines parses the passed file as a slice of strings by splitting the file content at newlines.
LoadStringLineSet parses the passed file as a unique set of strings by splitting the file content at newlines.
LoadStringLineSetT parses the passed file as a unique set of strings of type T by splitting the file content at newlines.
LoadStringLineSetTrimSpace parses the passed file as a unique set of strings by splitting the file content at newlines and trims leading and trailing whitespace from each line.
LoadStringLineSetTrimSpaceT parses the passed file as a unique set of strings of type T by splitting the file content at newlines and trims leading and trailing whitespace from each line.
LoadStringLinesT parses the passed file as a slice of strings of type T by splitting the file content at newlines.
LoadStringLinesTrimSpace parses the passed file as a slice of strings by splitting the file content at newlines and trims leading and trailing whitespace from each line.
LoadStringLinesTrimSpaceT parses the passed file as a slice of strings of type T by splitting the file content at newlines and trims leading and trailing whitespace from each line.
LoadStringT reads the passed file as a string type T.
LoadStringTrimSpace reads the passed file as a string and trims leading and trailing whitespace.
LoadStringTrimSpaceT reads the passed file as a string type T and trims leading and trailing whitespace.
LoadXML unmarshals the passed XML file into a config of type T.
MustLoadAndWatch calls LoadAndWatch and panics on any error that it returns.
NewLoader returns a new Loader for the type T without loading the configuration yet.
# Variables
ParseEnv parses environment variables into the passed struct pointer by looking for struct fields with an `env` tag.
SplitLines splits a string at newlines.