This commit is contained in:
Guilherme Saldanha 2025-10-19 16:05:18 +00:00 committed by GitHub
commit ecc7564e74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 32 deletions

View file

@ -38,36 +38,45 @@ func read(path string) (config models.Conf) {
viper.AutomaticEnv() // Get ENVIRONMENT variables viper.AutomaticEnv() // Get ENVIRONMENT variables
config.Host = viper.Get("HOST").(string) config.Host = stripQuotes(viper.GetString("HOST"))
config.Port = viper.Get("PORT").(string) config.Port = stripQuotes(viper.GetString("PORT"))
config.Theme = viper.Get("THEME").(string) config.Theme = stripQuotes(viper.GetString("THEME"))
config.Color = viper.Get("COLOR").(string) config.Color = stripQuotes(viper.GetString("COLOR"))
config.NodePath = viper.Get("NODEPATH").(string) config.NodePath = stripQuotes(viper.GetString("NODEPATH"))
config.LogLevel = viper.Get("LOG_LEVEL").(string) config.LogLevel = stripQuotes(viper.GetString("LOG_LEVEL"))
config.ArpArgs = viper.Get("ARP_ARGS").(string) config.ArpArgs = stripQuotes(viper.GetString("ARP_ARGS"))
config.ArpStrs = viper.GetStringSlice("ARP_STRS") config.ArpStrs = viper.GetStringSlice("ARP_STRS")
config.Ifaces = viper.Get("IFACES").(string) config.Ifaces = stripQuotes(viper.GetString("IFACES"))
config.Timeout = viper.GetInt("TIMEOUT") config.Timeout = viper.GetInt("TIMEOUT")
config.TrimHist = viper.GetInt("TRIM_HIST") config.TrimHist = viper.GetInt("TRIM_HIST")
config.ShoutURL = viper.Get("SHOUTRRR_URL").(string) config.ShoutURL = stripQuotes(viper.GetString("SHOUTRRR_URL"))
config.UseDB = viper.Get("USE_DB").(string) config.UseDB = stripQuotes(viper.GetString("USE_DB"))
config.PGConnect = viper.Get("PG_CONNECT").(string) config.PGConnect = stripQuotes(viper.GetString("PG_CONNECT"))
config.InfluxEnable = viper.GetBool("INFLUX_ENABLE") config.InfluxEnable = viper.GetBool("INFLUX_ENABLE")
config.InfluxSkipTLS = viper.GetBool("INFLUX_SKIP_TLS") config.InfluxSkipTLS = viper.GetBool("INFLUX_SKIP_TLS")
config.InfluxAddr, _ = viper.Get("INFLUX_ADDR").(string) config.InfluxAddr = stripQuotes(viper.GetString("INFLUX_ADDR"))
config.InfluxToken, _ = viper.Get("INFLUX_TOKEN").(string) config.InfluxToken = stripQuotes(viper.GetString("INFLUX_TOKEN"))
config.InfluxOrg, _ = viper.Get("INFLUX_ORG").(string) config.InfluxOrg = stripQuotes(viper.GetString("INFLUX_ORG"))
config.InfluxBucket, _ = viper.Get("INFLUX_BUCKET").(string) config.InfluxBucket = stripQuotes(viper.GetString("INFLUX_BUCKET"))
config.PrometheusEnable = viper.GetBool("PROMETHEUS_ENABLE") config.PrometheusEnable = viper.GetBool("PROMETHEUS_ENABLE")
joined := viper.Get("ARP_STRS_JOINED").(string) joined := stripQuotes(viper.GetString("ARP_STRS_JOINED"))
// slog.Info("ARP_STRS_JOINED: " + joined) // slog.Info("ARP_STRS_JOINED: " + joined)
if joined != "" { if joined != "" {
config.ArpStrs = strings.Split(joined, ",") splitStrs := strings.Split(joined, ",")
config.ArpStrs = make([]string, len(splitStrs))
for i, str := range splitStrs {
config.ArpStrs[i] = stripQuotes(strings.TrimSpace(str))
}
}
// Also clean quotes from ArpStrs if they were set directly
for i, str := range config.ArpStrs {
config.ArpStrs[i] = stripQuotes(str)
} }
return config return config

View file

@ -0,0 +1,14 @@
package conf
import "strings"
// stripQuotes removes surrounding quotes from a string value
func stripQuotes(value string) string {
if len(value) >= 2 {
if (strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`)) ||
(strings.HasPrefix(value, `'`) && strings.HasSuffix(value, `'`)) {
return value[1 : len(value)-1]
}
}
return value
}

View file

@ -17,29 +17,29 @@ func Write(config models.Conf) {
viper.SetConfigFile(config.ConfPath) viper.SetConfigFile(config.ConfPath)
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.Set("HOST", config.Host) viper.Set("HOST", stripQuotes(config.Host))
viper.Set("PORT", config.Port) viper.Set("PORT", stripQuotes(config.Port))
viper.Set("THEME", config.Theme) viper.Set("THEME", stripQuotes(config.Theme))
viper.Set("COLOR", config.Color) viper.Set("COLOR", stripQuotes(config.Color))
viper.Set("NODEPATH", config.NodePath) viper.Set("NODEPATH", stripQuotes(config.NodePath))
viper.Set("LOG_LEVEL", config.LogLevel) viper.Set("LOG_LEVEL", stripQuotes(config.LogLevel))
viper.Set("ARP_ARGS", config.ArpArgs) viper.Set("ARP_ARGS", stripQuotes(config.ArpArgs))
viper.Set("ARP_STRS", config.ArpStrs) viper.Set("ARP_STRS", config.ArpStrs)
viper.Set("ARP_STRS_JOINED", "") // Can be set only with ENV viper.Set("ARP_STRS_JOINED", "") // Can be set only with ENV
viper.Set("IFACES", config.Ifaces) viper.Set("IFACES", stripQuotes(config.Ifaces))
viper.Set("TIMEOUT", config.Timeout) viper.Set("TIMEOUT", config.Timeout)
viper.Set("TRIM_HIST", config.TrimHist) viper.Set("TRIM_HIST", config.TrimHist)
viper.Set("SHOUTRRR_URL", config.ShoutURL) viper.Set("SHOUTRRR_URL", stripQuotes(config.ShoutURL))
viper.Set("USE_DB", config.UseDB) viper.Set("USE_DB", stripQuotes(config.UseDB))
viper.Set("PG_CONNECT", config.PGConnect) viper.Set("PG_CONNECT", stripQuotes(config.PGConnect))
viper.Set("influx_enable", config.InfluxEnable) viper.Set("influx_enable", config.InfluxEnable)
viper.Set("influx_skip_tls", config.InfluxSkipTLS) viper.Set("influx_skip_tls", config.InfluxSkipTLS)
viper.Set("influx_addr", config.InfluxAddr) viper.Set("influx_addr", stripQuotes(config.InfluxAddr))
viper.Set("influx_token", config.InfluxToken) viper.Set("influx_token", stripQuotes(config.InfluxToken))
viper.Set("influx_org", config.InfluxOrg) viper.Set("influx_org", stripQuotes(config.InfluxOrg))
viper.Set("influx_bucket", config.InfluxBucket) viper.Set("influx_bucket", stripQuotes(config.InfluxBucket))
viper.Set("PROMETHEUS_ENABLE", config.PrometheusEnable) viper.Set("PROMETHEUS_ENABLE", config.PrometheusEnable)