[BRANDING] parse FORGEJO__* in the container environment

(cherry picked from commit b0759917479ee17ccb11773176dbc8a75323c5cb)
(cherry picked from commit ac8503150a9a338ccaadf5b690adcdc600a2bf38)
This commit is contained in:
Earl Warren 2023-07-10 23:17:39 +02:00
parent 6730fd099d
commit 266948e08a
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 23 additions and 14 deletions

View file

@ -13,7 +13,7 @@ import (
)
const (
EnvConfigKeyPrefixGitea = "GITEA__"
EnvConfigKeyPrefixGitea = "^(FORGEJO|GITEA)__"
EnvConfigKeySuffixFile = "__FILE"
)
@ -96,19 +96,21 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
// decodeEnvironmentKey decode the environment key to section and key
// The environment key is in the form of GITEA__SECTION__KEY or GITEA__SECTION__KEY__FILE
func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) {
if !strings.HasPrefix(envKey, prefixGitea) {
return false, "", "", false
}
func decodeEnvironmentKey(prefixRegexp *regexp.Regexp, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) {
if strings.HasSuffix(envKey, suffixFile) {
useFileValue = true
envKey = envKey[:len(envKey)-len(suffixFile)]
}
ok, section, key = decodeEnvSectionKey(envKey[len(prefixGitea):])
loc := prefixRegexp.FindStringIndex(envKey)
if loc == nil {
return false, "", "", false
}
ok, section, key = decodeEnvSectionKey(envKey[loc[1]:])
return ok, section, key, useFileValue
}
func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
prefixRegexp := regexp.MustCompile(EnvConfigKeyPrefixGitea)
for _, kv := range envs {
idx := strings.IndexByte(kv, '=')
if idx < 0 {
@ -118,7 +120,7 @@ func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
// parse the environment variable to config section name and key name
envKey := kv[:idx]
envValue := kv[idx+1:]
ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(EnvConfigKeyPrefixGitea, EnvConfigKeySuffixFile, envKey)
ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(prefixRegexp, EnvConfigKeySuffixFile, envKey)
if !ok {
continue
}