diff --git a/config/configuration.go b/config/configuration.go index 45d2383..5917057 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -92,33 +92,24 @@ func (c EnvConfig) Split() []Config { for i, hostname := range c.PIHoleHostname { config := Config{ - PIHoleHostname: hostname, - PIHoleProtocol: c.PIHoleProtocol[i], + PIHoleHostname: strings.TrimSpace(hostname), PIHolePort: c.PIHolePort[i], } - if c.PIHoleApiToken != nil { - if len(c.PIHoleApiToken) == 1 { - if c.PIHoleApiToken[0] != "" { - config.PIHoleApiToken = c.PIHoleApiToken[0] - } - } else if len(c.PIHoleApiToken) > 1 { - if c.PIHoleApiToken[i] != "" { - config.PIHoleApiToken = c.PIHoleApiToken[i] - } - } + if hasData, data := extractConfig(c.PIHoleProtocol, i); hasData { + config.PIHoleProtocol = data } - if c.PIHolePassword != nil { - if len(c.PIHolePassword) == 1 { - if c.PIHolePassword[0] != "" { - config.PIHolePassword = c.PIHolePassword[0] - } - } else if len(c.PIHolePassword) > 1 { - if c.PIHolePassword[i] != "" { - config.PIHolePassword = c.PIHolePassword[i] - } - } + if hasData, data := extractConfig(c.PIHoleApiToken, i); hasData { + config.PIHoleApiToken = data + } + + if hasData, data := extractConfig(c.PIHoleApiToken, i); hasData { + config.PIHoleApiToken = data + } + + if hasData, data := extractConfig(c.PIHolePassword, i); hasData { + config.PIHolePassword = data } result = append(result, config) @@ -126,6 +117,22 @@ func (c EnvConfig) Split() []Config { return result } +func extractConfig(data []string, idx int) (bool, string) { + if len(data) == 1 { + v := strings.TrimSpace(data[0]) + if v != "" { + return true, v + } + } else if len(data) > 1 { + v := strings.TrimSpace(data[idx]) + if v != "" { + return true, v + } + } + + return false, "" +} + func (c Config) hostnameURL() string { s := fmt.Sprintf("%s://%s", c.PIHoleProtocol, c.PIHoleHostname) if c.PIHolePort != 0 {