mirror of
https://github.com/glanceapp/glance.git
synced 2025-06-21 18:31:24 +02:00
Change environment variables behavior
* Can now be inserted anywhere in the string * Can now insert multiple environment variables in a single string * Can now be escaped if prefixed with \
This commit is contained in:
parent
ea3b8124fc
commit
1fe7f61ec8
@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var HSLColorPattern = regexp.MustCompile(`^(?:hsla?\()?(\d{1,3})(?: |,)+(\d{1,3})%?(?: |,)+(\d{1,3})%?\)?$`)
|
var HSLColorPattern = regexp.MustCompile(`^(?:hsla?\()?(\d{1,3})(?: |,)+(\d{1,3})%?(?: |,)+(\d{1,3})%?\)?$`)
|
||||||
var EnvFieldPattern = regexp.MustCompile(`^\${([A-Z_]+)}$`)
|
var EnvFieldPattern = regexp.MustCompile(`(^|.)\$\{([A-Z_]+)\}`)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HSLHueMax = 360
|
HSLHueMax = 360
|
||||||
@ -133,21 +133,42 @@ func (f *OptionalEnvString) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
matches := EnvFieldPattern.FindStringSubmatch(value)
|
replaced := EnvFieldPattern.ReplaceAllStringFunc(value, func(whole string) string {
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
if len(matches) != 2 {
|
groups := EnvFieldPattern.FindStringSubmatch(whole)
|
||||||
*f = OptionalEnvString(value)
|
|
||||||
|
|
||||||
return nil
|
if len(groups) != 3 {
|
||||||
|
return whole
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix, key := groups[1], groups[2]
|
||||||
|
|
||||||
|
if prefix == `\` {
|
||||||
|
if len(whole) >= 2 {
|
||||||
|
return whole[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
|
||||||
}
|
}
|
||||||
|
|
||||||
value, found := os.LookupEnv(matches[1])
|
*f = OptionalEnvString(replaced)
|
||||||
|
|
||||||
if !found {
|
|
||||||
return fmt.Errorf("environment variable %s not found", matches[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
*f = OptionalEnvString(value)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user