forked from NYANDEV/forgejo
[BRANDING] parse FORGEJO__* in the container environment
(cherry picked from commit b0759917479ee17ccb11773176dbc8a75323c5cb) (cherry picked from commit da3f76228e24d2276784ad7e1a03a34d094750e0) (cherry picked from commit 20d196e74f9d7827cd812e3eb714eb87e888b6db) (cherry picked from commit 0bf8b1824eac416ddb40fd2e0d7605573ba087a3) (cherry picked from commit 655bb770a7cf65ada5d3e214ec23219e6cb19df0) (cherry picked from commit d69d5c2c46f00b041beeb67a6238c5193ce9710a) (cherry picked from commit 00b55e5a5396a49a6798a3ae2588e412637a77c6) (cherry picked from commit 456121fd8a4c0c30f7854bd04907f2a0a583d27f) (cherry picked from commit 9716a158e48ad6333f46fc0806a838603606e5ea) (cherry picked from commit 7d60a6f5116124b72927a52db0dd36af7a9c3621) (cherry picked from commit d32a6d9437a38ac35320ad7329eece1ab2bd54d1) (cherry picked from commit ee1de38527ea1fbd52c319cfbbca24deef951fc1) (cherry picked from commit 54e7799d13fa041be172d4a05d036cee43e390d4) (cherry picked from commit 4f04da7ab716ccb1f2816842cde62965e89a852a) (cherry picked from commit 0d39a0a520c80abdc955e93cf2470eeb01d52496) (cherry picked from commit 7d8ae8279f350763c5ca04b462b288331656f53b) (cherry picked from commit 76b6770b73026e5192cc118363d8154242a5c7fc) (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 commit 96e501c5f038eebd04f0e0e466b8b4ccca769310) (cherry picked from commit 466a66a1f6d22694a6fc9d99378f91ad8cf66d87) (cherry picked from commit 7814cf700af5f9812ab7294b03faeef00c23ef76) (cherry picked from commit 4d12344871d882c6dd2cfa5b34383d06ad77e562) (cherry picked from commit fad4cf84c3b6f4891006154549faedd603267bbd) (cherry picked from commit 7ad89400ee90b3c45072c175a40d9c77fbc0fb36) (cherry picked from commit ec911404471fc8092e6b733a7cee41a518a360a5)
This commit is contained in:
parent
e671021168
commit
295a7f4487
3 changed files with 23 additions and 14 deletions
|
@ -16,15 +16,15 @@ func main() {
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "environment-to-ini"
|
app.Name = "environment-to-ini"
|
||||||
app.Usage = "Use provided environment to update configuration 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
|
through the environment, this command allows environment variables to
|
||||||
be mapped to values in the ini.
|
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
|
will be mapped to the ini section "[section_name]" and the key
|
||||||
"KEY_NAME" with the value as provided.
|
"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
|
will be mapped to the ini section "[section_name]" and the key
|
||||||
"KEY_NAME" with the value loaded from the specified file.
|
"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"
|
You would set the environment variables: "FORGEJO__LOG_0x2E_CONSOLE__COLORIZE=false"
|
||||||
and "GITEA__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found
|
and "FORGEJO__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found
|
||||||
on the configuration cheat sheet.`
|
on the configuration cheat sheet.`
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
|
@ -62,7 +62,7 @@ func main() {
|
||||||
Name: "work-path",
|
Name: "work-path",
|
||||||
Aliases: []string{"w"},
|
Aliases: []string{"w"},
|
||||||
Value: setting.AppWorkPath,
|
Value: setting.AppWorkPath,
|
||||||
Usage: "Set the gitea working path",
|
Usage: "Set the forgejo working path",
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "out",
|
Name: "out",
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EnvConfigKeyPrefixGitea = "GITEA__"
|
EnvConfigKeyPrefixGitea = "^(FORGEJO|GITEA)__"
|
||||||
EnvConfigKeySuffixFile = "__FILE"
|
EnvConfigKeySuffixFile = "__FILE"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -97,19 +97,21 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
|
||||||
|
|
||||||
// decodeEnvironmentKey decode the environment key to section and key
|
// 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
|
// 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) {
|
func decodeEnvironmentKey(prefixRegexp *regexp.Regexp, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) {
|
||||||
if !strings.HasPrefix(envKey, prefixGitea) {
|
|
||||||
return false, "", "", false
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(envKey, suffixFile) {
|
if strings.HasSuffix(envKey, suffixFile) {
|
||||||
useFileValue = true
|
useFileValue = true
|
||||||
envKey = envKey[:len(envKey)-len(suffixFile)]
|
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
|
return ok, section, key, useFileValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
|
func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
|
||||||
|
prefixRegexp := regexp.MustCompile(EnvConfigKeyPrefixGitea)
|
||||||
for _, kv := range envs {
|
for _, kv := range envs {
|
||||||
idx := strings.IndexByte(kv, '=')
|
idx := strings.IndexByte(kv, '=')
|
||||||
if idx < 0 {
|
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
|
// parse the environment variable to config section name and key name
|
||||||
envKey := kv[:idx]
|
envKey := kv[:idx]
|
||||||
envValue := kv[idx+1:]
|
envValue := kv[idx+1:]
|
||||||
ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(EnvConfigKeyPrefixGitea, EnvConfigKeySuffixFile, envKey)
|
ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(prefixRegexp, EnvConfigKeySuffixFile, envKey)
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ package setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -33,7 +34,7 @@ func TestDecodeEnvSectionKey(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeEnvironmentKey(t *testing.T) {
|
func TestDecodeEnvironmentKey(t *testing.T) {
|
||||||
prefix := "GITEA__"
|
prefix := regexp.MustCompile(EnvConfigKeyPrefixGitea)
|
||||||
suffix := "__FILE"
|
suffix := "__FILE"
|
||||||
|
|
||||||
ok, section, key, file := decodeEnvironmentKey(prefix, suffix, "SEC__KEY")
|
ok, section, key, file := decodeEnvironmentKey(prefix, suffix, "SEC__KEY")
|
||||||
|
@ -60,6 +61,12 @@ func TestDecodeEnvironmentKey(t *testing.T) {
|
||||||
assert.Equal(t, "KEY", key)
|
assert.Equal(t, "KEY", key)
|
||||||
assert.False(t, file)
|
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)
|
// 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)
|
// 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")
|
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC__FILE")
|
||||||
|
|
Loading…
Reference in a new issue