Add trim space to env variables and fix protocol index bug

This commit is contained in:
Galorhallen 2021-12-23 12:05:26 +01:00
parent 3cf4ad6781
commit d4a012d404

View File

@ -92,33 +92,24 @@ func (c EnvConfig) Split() []Config {
for i, hostname := range c.PIHoleHostname { for i, hostname := range c.PIHoleHostname {
config := Config{ config := Config{
PIHoleHostname: hostname, PIHoleHostname: strings.TrimSpace(hostname),
PIHoleProtocol: c.PIHoleProtocol[i],
PIHolePort: c.PIHolePort[i], PIHolePort: c.PIHolePort[i],
} }
if c.PIHoleApiToken != nil { if hasData, data := extractConfig(c.PIHoleProtocol, i); hasData {
if len(c.PIHoleApiToken) == 1 { config.PIHoleProtocol = data
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 c.PIHolePassword != nil { if hasData, data := extractConfig(c.PIHoleApiToken, i); hasData {
if len(c.PIHolePassword) == 1 { config.PIHoleApiToken = data
if c.PIHolePassword[0] != "" { }
config.PIHolePassword = c.PIHolePassword[0]
} if hasData, data := extractConfig(c.PIHoleApiToken, i); hasData {
} else if len(c.PIHolePassword) > 1 { config.PIHoleApiToken = data
if c.PIHolePassword[i] != "" { }
config.PIHolePassword = c.PIHolePassword[i]
} if hasData, data := extractConfig(c.PIHolePassword, i); hasData {
} config.PIHolePassword = data
} }
result = append(result, config) result = append(result, config)
@ -126,6 +117,22 @@ func (c EnvConfig) Split() []Config {
return result 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 { func (c Config) hostnameURL() string {
s := fmt.Sprintf("%s://%s", c.PIHoleProtocol, c.PIHoleHostname) s := fmt.Sprintf("%s://%s", c.PIHoleProtocol, c.PIHoleHostname)
if c.PIHolePort != 0 { if c.PIHolePort != 0 {