Documentation
¶
Overview ¶
Package konfig is a minimal and unopinionated library for reading configuration values in Go applications based on The 12-Factor App (https://12factor.net/config).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Pick ¶
Pick reads values for exported fields of a struct from either command-line flags, environment variables, or configuration files. Default values can also be specified. You should pass the pointer to a struct for config; otherwise you will get an error.
Example ¶
package main
import (
"fmt"
"net/url"
"time"
"github.com/moorara/konfig"
)
func main() {
// First, you need to define a struct.
// Each field of the struct represents a configuration value.
// Create an object from the struct with default values for its fields.
var config = struct {
LogLevel string
Environment string
Region string
Timeout time.Duration
Replicas []url.URL
}{
LogLevel: "info", // default
Environment: "dev", // default
Region: "local", // default
Timeout: 10 * time.Second, // default
}
// Second, pass the pointer to the struct object to the Pick method.
// For each field, a value will be read either from flags, environment variables, or files.
_ = konfig.Pick(&config)
// Now, you can access the configuration values on the struct object.
fmt.Printf("%+v\n", config)
}
func Watch ¶ added in v0.3.0
Watch first reads values for exported fields of a struct from either command-line flags, environment variables, or configuration files. It then watches any change to those fields that their values are read from configuration files and notifies subscribers on a channel.
Example ¶
package main
import (
"sync"
"github.com/moorara/konfig"
)
func main() {
// When using the Watch method, your struct needs to implement the sync.Locker interface.
// You can simply achieve that by embedding the sync.Mutex type in your struct.
var config = struct {
sync.Mutex
LogLevel string
}{
LogLevel: "info", // default
}
// For using the Watch method, you need to define a channel for receiving updates.
// If a configuration value gets a new value (through files), you will get notified on this channel.
ch := make(chan konfig.Update, 1)
// In a separate goroutine, you can receive the new configuration values and re-configure your application accordingly.
go func() {
for update := range ch {
if update.Name == "LogLevel" {
config.Lock()
// logger.SetLevel(config.LogLevel)
config.Unlock()
}
}
}()
// You can now watch for configuration values.
close, _ := konfig.Watch(&config, []chan konfig.Update{ch})
defer close()
}
Types ¶
type Option ¶ added in v0.2.1
type Option func(*reader)
Option sets optional parameters for reader.
func Debug ¶ added in v0.3.0
Debug is the option for enabling logs for debugging purposes. verbosity is the verbosity level of logs. You can also enable this option by setting KONFIG_DEBUG environment variable to a verbosity level. You should not use this option in production.
func ListSep ¶ added in v0.3.2
ListSep is the option for specifying list separator for all fields with slice type. You can specify a list separator for each field using `sep` struct tag. Using `tag` struct tag for a field will override this option for that field.
func PrefixEnv ¶ added in v0.3.1
PrefixEnv is the option for prefixing all environment variable names with a given string. You can specify a custom name for environment variable for each field using `env` struct tag. Using `env` struct tag for a field will override this option for that field.
func PrefixFileEnv ¶ added in v0.3.1
PrefixFileEnv is the option for prefixing all file environment variable names with a given string. You can specify a custom name for file environment variable for each field using `fileenv` struct tag. Using `fileenv` struct tag for a field will override this option for that field.
func PrefixFlag ¶ added in v0.3.1
PrefixFlag is the option for prefixing all flag names with a given string. You can specify a custom name for command-line flag for each field using `flag` struct tag. Using `flag` struct tag for a field will override this option for that field.
func SkipEnv ¶ added in v0.3.2
func SkipEnv() Option
SkipEnv is the option for skipping environment variables as a source for all fields. You can skip environment variables as a source for each field by setting `env` struct tag to `-`.
func SkipFileEnv ¶ added in v0.3.2
func SkipFileEnv() Option
SkipFileEnv is the option for skipping file environment variables as a source for all fields. You can skip file environment variable as a source for each field by setting `fileenv` struct tag to `-`.
func SkipFlag ¶ added in v0.3.2
func SkipFlag() Option
SkipFlag is the option for skipping command-line flags as a source for all fields. You can skip command-line flag as a source for each field by setting `flag` struct tag to `-`.
func Telepresence ¶ added in v0.2.1
func Telepresence() Option
Telepresence is the option for reading files when running in a Telepresence shell. If the TELEPRESENCE_ROOT environment variable exist, files will be read from mounted volume. See https://telepresence.io/howto/volumes.html for details.