mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-21 21:27:13 +00:00
[BRANDING] parse FORGEJO__* in the container environment
(cherry picked from commitb075991747
) (cherry picked from commitda3f76228e
) (cherry picked from commit20d196e74f
) (cherry picked from commit0bf8b1824e
) (cherry picked from commit655bb770a7
) (cherry picked from commitd69d5c2c46
) (cherry picked from commit00b55e5a53
) (cherry picked from commit456121fd8a
) (cherry picked from commit9716a158e4
) (cherry picked from commit7d60a6f511
) (cherry picked from commitd32a6d9437
) (cherry picked from commitee1de38527
) (cherry picked from commit54e7799d13
) (cherry picked from commit4f04da7ab7
) (cherry picked from commit0d39a0a520
) (cherry picked from commit7d8ae8279f
) (cherry picked from commit76b6770b73
) (cherry picked from commit9bc0d96064
) Conflicts: contrib/environment-to-ini/environment-to-ini.go https://codeberg.org/forgejo/forgejo/pulls/1769 (cherry picked from commite21bf9b144
) (cherry picked from commit96e501c5f0
) (cherry picked from commit466a66a1f6
) (cherry picked from commit7814cf700a
) (cherry picked from commit4d12344871
) (cherry picked from commitfad4cf84c3
) (cherry picked from commit7ad89400ee
) (cherry picked from commitec91140447
) (cherry picked from commit295a7f4487
) (cherry picked from commit66163a5dcf
) (cherry picked from commit9e06f57269
)
This commit is contained in:
parent
9050c1742c
commit
fe9659a579
|
@ -16,15 +16,15 @@ func main() {
|
|||
app := cli.NewApp()
|
||||
app.Name = "environment-to-ini"
|
||||
app.Usage = "Use provided environment to update configuration ini"
|
||||
app.Description = `As a helper to allow docker users to update the gitea configuration
|
||||
app.Description = `As a helper to allow docker users to update the forgejo configuration
|
||||
through the environment, this command allows environment variables to
|
||||
be mapped to values in the ini.
|
||||
|
||||
Environment variables of the form "GITEA__SECTION_NAME__KEY_NAME"
|
||||
Environment variables of the form "FORGEJO__SECTION_NAME__KEY_NAME"
|
||||
will be mapped to the ini section "[section_name]" and the key
|
||||
"KEY_NAME" with the value as provided.
|
||||
|
||||
Environment variables of the form "GITEA__SECTION_NAME__KEY_NAME__FILE"
|
||||
Environment variables of the form "FORGEJO__SECTION_NAME__KEY_NAME__FILE"
|
||||
will be mapped to the ini section "[section_name]" and the key
|
||||
"KEY_NAME" with the value loaded from the specified file.
|
||||
|
||||
|
@ -42,8 +42,8 @@ func main() {
|
|||
...
|
||||
"""
|
||||
|
||||
You would set the environment variables: "GITEA__LOG_0x2E_CONSOLE__COLORIZE=false"
|
||||
and "GITEA__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found
|
||||
You would set the environment variables: "FORGEJO__LOG_0x2E_CONSOLE__COLORIZE=false"
|
||||
and "FORGEJO__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found
|
||||
on the configuration cheat sheet.`
|
||||
app.Flags = []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
|
@ -62,7 +62,7 @@ func main() {
|
|||
Name: "work-path",
|
||||
Aliases: []string{"w"},
|
||||
Value: setting.AppWorkPath,
|
||||
Usage: "Set the gitea working path",
|
||||
Usage: "Set the forgejo working path",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "out",
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
EnvConfigKeyPrefixGitea = "GITEA__"
|
||||
EnvConfigKeyPrefixGitea = "^(FORGEJO|GITEA)__"
|
||||
EnvConfigKeySuffixFile = "__FILE"
|
||||
)
|
||||
|
||||
|
@ -97,19 +97,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 {
|
||||
|
@ -119,7 +121,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
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ package setting
|
|||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -33,7 +34,7 @@ func TestDecodeEnvSectionKey(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestDecodeEnvironmentKey(t *testing.T) {
|
||||
prefix := "GITEA__"
|
||||
prefix := regexp.MustCompile(EnvConfigKeyPrefixGitea)
|
||||
suffix := "__FILE"
|
||||
|
||||
ok, section, key, file := decodeEnvironmentKey(prefix, suffix, "SEC__KEY")
|
||||
|
@ -60,6 +61,12 @@ func TestDecodeEnvironmentKey(t *testing.T) {
|
|||
assert.Equal(t, "KEY", key)
|
||||
assert.False(t, file)
|
||||
|
||||
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "FORGEJO__SEC__KEY")
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "sec", section)
|
||||
assert.Equal(t, "KEY", key)
|
||||
assert.False(t, file)
|
||||
|
||||
// with "__FILE" suffix, it doesn't support to write "[sec].FILE" to config (no such key FILE is used in Gitea)
|
||||
// but it could be fixed in the future by adding a new suffix like "__VALUE" (no such key VALUE is used in Gitea either)
|
||||
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC__FILE")
|
||||
|
|
Loading…
Reference in a new issue