From 44033778f5c65ac800f22ebf9ceae0f59705efc8 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Fri, 12 Jun 2015 13:56:43 -0700 Subject: [PATCH] Ensure collectd uses defaults with partial config --- services/collectd/config.go | 25 +++++++++++++++++++++++++ services/collectd/service.go | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/services/collectd/config.go b/services/collectd/config.go index 3129c3378d..ed9d69ca39 100644 --- a/services/collectd/config.go +++ b/services/collectd/config.go @@ -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 +} diff --git a/services/collectd/service.go b/services/collectd/service.go index 71efa90b69..76ba371a8c 100644 --- a/services/collectd/service.go +++ b/services/collectd/service.go @@ -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), }