From dbcc13a5cf92813eb9ff444888341f0fbaba568e Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Mon, 16 Dec 2024 23:59:25 +0000 Subject: [PATCH] Allow inserting env variables anywhere in the config --- internal/glance/config-fields.go | 56 ------------------- internal/glance/config.go | 47 +++++++++++++++- internal/glance/templates/bookmarks.html | 2 +- .../glance/templates/monitor-compact.html | 2 +- internal/glance/templates/monitor.html | 2 +- internal/glance/widget-bookmarks.go | 10 ++-- internal/glance/widget-changedetection.go | 2 +- internal/glance/widget-custom-api.go | 18 +++--- internal/glance/widget-dns-stats.go | 14 ++--- internal/glance/widget-monitor.go | 12 ++-- internal/glance/widget-releases.go | 8 +-- internal/glance/widget-repository.go | 12 ++-- internal/glance/widget-rss.go | 6 +- 13 files changed, 90 insertions(+), 101 deletions(-) diff --git a/internal/glance/config-fields.go b/internal/glance/config-fields.go index 9a47820..6c6f0c5 100644 --- a/internal/glance/config-fields.go +++ b/internal/glance/config-fields.go @@ -2,7 +2,6 @@ package glance import ( "fmt" - "os" "regexp" "strconv" "strings" @@ -112,61 +111,6 @@ func (d *durationField) UnmarshalYAML(node *yaml.Node) error { return nil } -var optionalEnvFieldPattern = regexp.MustCompile(`(^|.)\$\{([A-Z0-9_]+)\}`) - -type optionalEnvField string - -func (f *optionalEnvField) UnmarshalYAML(node *yaml.Node) error { - var value string - - err := node.Decode(&value) - if err != nil { - return err - } - - replaced := optionalEnvFieldPattern.ReplaceAllStringFunc(value, func(match string) string { - if err != nil { - return "" - } - - groups := optionalEnvFieldPattern.FindStringSubmatch(match) - - if len(groups) != 3 { - return match - } - - prefix, key := groups[1], groups[2] - - if prefix == `\` { - if len(match) >= 2 { - return match[1:] - } else { - return "" - } - } - - value, found := os.LookupEnv(key) - if !found { - err = fmt.Errorf("environment variable %s not found", key) - return "" - } - - return prefix + value - }) - - if err != nil { - return err - } - - *f = optionalEnvField(replaced) - - return nil -} - -func (f *optionalEnvField) String() string { - return string(*f) -} - type customIconField struct { URL string IsFlatIcon bool diff --git a/internal/glance/config.go b/internal/glance/config.go index 0f6b259..0ab79af 100644 --- a/internal/glance/config.go +++ b/internal/glance/config.go @@ -69,10 +69,15 @@ type page struct { } func newConfigFromYAML(contents []byte) (*config, error) { + contents, err := parseConfigEnvVariables(contents) + if err != nil { + return nil, err + } + config := &config{} config.Server.Port = 8080 - err := yaml.Unmarshal(contents, config) + err = yaml.Unmarshal(contents, config) if err != nil { return nil, err } @@ -94,6 +99,46 @@ func newConfigFromYAML(contents []byte) (*config, error) { return config, nil } +var configEnvVariablePattern = regexp.MustCompile(`(^|.)\$\{([A-Z0-9_]+)\}`) + +func parseConfigEnvVariables(contents []byte) ([]byte, error) { + var err error + + replaced := configEnvVariablePattern.ReplaceAllFunc(contents, func(match []byte) []byte { + if err != nil { + return nil + } + + groups := configEnvVariablePattern.FindSubmatch(match) + if len(groups) != 3 { + return match + } + + prefix, key := string(groups[1]), string(groups[2]) + if prefix == `\` { + if len(match) >= 2 { + return match[1:] + } else { + return nil + } + } + + value, found := os.LookupEnv(key) + if !found { + err = fmt.Errorf("environment variable %s not found", key) + return nil + } + + return []byte(prefix + value) + }) + + if err != nil { + return nil, err + } + + return replaced, nil +} + func formatWidgetInitError(err error, w widget) error { return fmt.Errorf("%s widget: %v", w.GetType(), err) } diff --git a/internal/glance/templates/bookmarks.html b/internal/glance/templates/bookmarks.html index 711abb6..1e8bfc7 100644 --- a/internal/glance/templates/bookmarks.html +++ b/internal/glance/templates/bookmarks.html @@ -13,7 +13,7 @@ {{ end }} - {{ .Title }} + {{ .Title }} {{ end }} diff --git a/internal/glance/templates/monitor-compact.html b/internal/glance/templates/monitor-compact.html index e888c17..dca5683 100644 --- a/internal/glance/templates/monitor-compact.html +++ b/internal/glance/templates/monitor-compact.html @@ -21,7 +21,7 @@ {{ end }} {{ define "site" }} -{{ .Title }} +{{ .Title }} {{ if not .Status.TimedOut }}
{{ .Status.ResponseTime.Milliseconds | formatNumber }}ms
{{ end }} {{ if eq .StatusStyle "ok" }}
diff --git a/internal/glance/templates/monitor.html b/internal/glance/templates/monitor.html index 5c541a8..7e95b99 100644 --- a/internal/glance/templates/monitor.html +++ b/internal/glance/templates/monitor.html @@ -25,7 +25,7 @@ {{ end }}
- {{ .Title }} + {{ .Title }}