Documentation
¶
Overview ¶
Package goconfig loads configuration values into Go structs from three sources: JSON config files, environment variables and command-line flags.
Sources are applied in this order:
- JSON file
- Environment variables
- Command-line flags
This means command-line flags have the highest precedence.
Quick start:
type Config struct {
Port int `usage:"HTTP port"`
}
cfg := Config{Port: 8080}
if err := goconfig.Load(&cfg); err != nil {
log.Fatal(err)
}
Index ¶
- func FillArgs(c interface{}, args []string) error
- func FillEnvironments(c interface{}) (err error)
- func FillJson(c interface{}, filename string) error
- func Load(c interface{}, opts ...Option) error
- func Read(c interface{})
- func ReadWithError(c interface{}) error
- type Option
- func WithArgs(args []string) Option
- func WithConfigFile(filename string) Option
- func WithConfigFlagName(name string) Option
- func WithEnvLookup(lookup func(string) (string, bool)) Option
- func WithImplicitConfigFile(filename string) Option
- func WithProgramName(name string) Option
- func WithoutImplicitConfigFile() Option
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FillEnvironments ¶ added in v1.1.0
func FillEnvironments(c interface{}) (err error)
func Load ¶ added in v1.8.0
Load populates c from JSON config file, environment variables and command-line flags.
Priority order (highest to lowest): flags > environment > JSON file > defaults.
Example ¶
type Server struct {
Host string `usage:"Server host"`
Port int `usage:"Server port"`
}
type Config struct {
Timeout time.Duration `usage:"Request timeout"`
Server Server
}
cfg := Config{
Timeout: 5 * time.Second,
Server: Server{
Host: "127.0.0.1",
Port: 8080,
},
}
_ = Load(&cfg,
WithArgs([]string{"-server.port", "9090"}),
WithoutImplicitConfigFile(),
WithEnvLookup(func(string) (string, bool) { return "", false }),
)
Example (ApiService) ¶
type Config struct {
HTTPPort int `usage:"HTTP port"`
Timeout time.Duration `usage:"Request timeout"`
DB struct {
Host string `usage:"Database host"`
Port int `usage:"Database port"`
}
}
cfg := Config{
HTTPPort: 8080,
Timeout: 3 * time.Second,
}
_ = Load(&cfg)
Example (CliTool) ¶
type Config struct {
ConfigPath string `usage:"Path to config file"`
Verbose bool `usage:"Enable verbose output"`
}
cfg := Config{}
_ = Load(&cfg,
WithArgs([]string{"-verbose", "-configpath", "./dev.json"}),
WithoutImplicitConfigFile(),
)
Example (WorkerService) ¶
type Config struct {
Concurrency int `usage:"Worker concurrency"`
PollEvery time.Duration `usage:"Polling interval"`
Queues []string `usage:"Enabled queues"`
}
cfg := Config{
Concurrency: 4,
PollEvery: 2 * time.Second,
Queues: []string{"emails", "billing"},
}
_ = Load(&cfg)
func Read ¶
func Read(c interface{})
Read loads configuration and exits with status code 1 on error.
For library code, prefer Load so the caller can handle errors.
func ReadWithError ¶ added in v1.8.0
func ReadWithError(c interface{}) error
ReadWithError loads configuration and returns any error.
Types ¶
type Option ¶ added in v1.8.0
type Option func(*Options)
Option customizes Load behaviour.
func WithConfigFile ¶ added in v1.8.0
WithConfigFile sets the JSON file used by Load.
func WithConfigFlagName ¶ added in v1.8.0
WithConfigFlagName changes the command-line flag used to point to a config file.
func WithEnvLookup ¶ added in v1.8.0
WithEnvLookup sets a custom environment lookup function.
func WithImplicitConfigFile ¶ added in v1.8.0
WithImplicitConfigFile enables and sets the fallback JSON config filename.
func WithProgramName ¶ added in v1.8.0
WithProgramName sets the name used in generated help output.
func WithoutImplicitConfigFile ¶ added in v1.8.0
func WithoutImplicitConfigFile() Option
WithoutImplicitConfigFile disables automatic loading of config.json.
