From d4a012d404f05b4d044222cc0dccc1fea787850c Mon Sep 17 00:00:00 2001 From: Galorhallen Date: Thu, 23 Dec 2021 12:05:26 +0100 Subject: [PATCH] Add trim space to env variables and fix protocol index bug --- config/configuration.go | 51 +++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 22 deletions(-) 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 {