Ensure collectd uses defaults with partial config

pull/2973/head
Philip O'Toole 2015-06-12 13:56:43 -07:00
parent f28362d161
commit 44033778f5
2 changed files with 26 additions and 1 deletions

View File

@ -42,3 +42,28 @@ func NewConfig() Config {
TypesDB: DefaultTypesDB,
}
}
// WithDefaults takes the given config and returns a new config with any required
// default values set.
func (c *Config) WithDefaults() *Config {
d := *c
if d.BindAddress == "" {
d.BindAddress = DefaultBindAddress
}
if d.Database == "" {
d.Database = DefaultDatabase
}
if d.RetentionPolicy == "" {
d.RetentionPolicy = DefaultRetentionPolicy
}
if d.BatchSize == 0 {
d.BatchSize = DefaultBatchSize
}
if d.BatchDuration == 0 {
d.BatchDuration = DefaultBatchDuration
}
if d.TypesDB == "" {
d.TypesDB = DefaultTypesDB
}
return &d
}

View File

@ -47,7 +47,7 @@ type Service struct {
// NewService returns a new instance of the collectd service.
func NewService(c Config) *Service {
s := &Service{
Config: &c,
Config: c.WithDefaults(),
Logger: log.New(os.Stderr, "[collectd] ", log.LstdFlags),
err: make(chan error),
}