mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-25 21:32:45 +01:00
[BRANDING] parse FORGEJO__* in the container environment
Add the FORGEJO__ prefix as equivalent to GITEA__ when interpreted by environment-to-ini. It is used when running the Forgejo container like so: docker run --name forgejo -e FORGEJO__security__INSTALL_LOCK=true \ -d codeberg.org/forgejo/forgejo:1.18 Signed-off-by: Earl Warren <contact@earl-warren.org> (cherry picked from commit6cd61e2ab7
) (cherry picked from commit62cae8cc6a
) (cherry picked from commitaee1afc509
) (cherry picked from commit6ba563cd9b
) (cherry picked from commit6429b20f4a
) (cherry picked from commitdd545aa077
)
This commit is contained in:
parent
3df1d21371
commit
63a00e573e
3 changed files with 55 additions and 22 deletions
|
@ -75,6 +75,14 @@ pipeline:
|
||||||
commands:
|
commands:
|
||||||
- ./build/test-env-prepare.sh
|
- ./build/test-env-prepare.sh
|
||||||
|
|
||||||
|
environment-to-ini:
|
||||||
|
image: *golang_image
|
||||||
|
environment:
|
||||||
|
GOPROXY_OVERRIDE: *goproxy_override
|
||||||
|
commands:
|
||||||
|
- *goproxy_setup
|
||||||
|
- go test contrib/environment-to-ini/environment-to-ini.go contrib/environment-to-ini/environment-to-ini_test.go
|
||||||
|
|
||||||
build:
|
build:
|
||||||
image: *test_image
|
image: *test_image
|
||||||
environment:
|
environment:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
@ -18,17 +19,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnvironmentPrefix environment variables prefixed with this represent ini values to write
|
// EnvironmentPrefix environment variables prefixed with this represent ini values to write
|
||||||
const EnvironmentPrefix = "GITEA"
|
const prefixRegexpString = "^(FORGEJO|GITEA)"
|
||||||
|
|
||||||
func main() {
|
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.
|
||||||
|
|
||||||
|
@ -46,9 +47,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".`
|
||||||
on the configuration cheat sheet.`
|
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "custom-path, C",
|
Name: "custom-path, C",
|
||||||
|
@ -76,7 +76,7 @@ func main() {
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "prefix, p",
|
Name: "prefix, p",
|
||||||
Value: EnvironmentPrefix,
|
Value: prefixRegexpString,
|
||||||
Usage: "Environment prefix to look for - will be suffixed by __ (2 underscores)",
|
Usage: "Environment prefix to look for - will be suffixed by __ (2 underscores)",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,19 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func splitEnvironmentVariable(prefixRegexp *regexp.Regexp, kv string) (string, string) {
|
||||||
|
idx := strings.IndexByte(kv, '=')
|
||||||
|
if idx < 0 {
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
k := kv[:idx]
|
||||||
|
loc := prefixRegexp.FindStringIndex(k)
|
||||||
|
if loc == nil {
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
return k[loc[1]:], kv[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
func runEnvironmentToIni(c *cli.Context) error {
|
func runEnvironmentToIni(c *cli.Context) error {
|
||||||
providedCustom := c.String("custom-path")
|
providedCustom := c.String("custom-path")
|
||||||
providedConf := c.String("config")
|
providedConf := c.String("config")
|
||||||
|
@ -111,19 +124,13 @@ func runEnvironmentToIni(c *cli.Context) error {
|
||||||
|
|
||||||
changed := false
|
changed := false
|
||||||
|
|
||||||
prefix := c.String("prefix") + "__"
|
prefixRegexp := regexp.MustCompile(c.String("prefix") + "__")
|
||||||
|
|
||||||
for _, kv := range os.Environ() {
|
for _, kv := range os.Environ() {
|
||||||
idx := strings.IndexByte(kv, '=')
|
eKey, value := splitEnvironmentVariable(prefixRegexp, kv)
|
||||||
if idx < 0 {
|
if eKey == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
eKey := kv[:idx]
|
|
||||||
value := kv[idx+1:]
|
|
||||||
if !strings.HasPrefix(eKey, prefix) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
eKey = eKey[len(prefix):]
|
|
||||||
sectionName, keyName := DecodeSectionKey(eKey)
|
sectionName, keyName := DecodeSectionKey(eKey)
|
||||||
if len(keyName) == 0 {
|
if len(keyName) == 0 {
|
||||||
continue
|
continue
|
||||||
|
@ -163,14 +170,11 @@ func runEnvironmentToIni(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
if c.Bool("clear") {
|
if c.Bool("clear") {
|
||||||
for _, kv := range os.Environ() {
|
for _, kv := range os.Environ() {
|
||||||
idx := strings.IndexByte(kv, '=')
|
eKey, _ := splitEnvironmentVariable(prefixRegexp, kv)
|
||||||
if idx < 0 {
|
if eKey == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
eKey := kv[:idx]
|
_ = os.Unsetenv(eKey)
|
||||||
if strings.HasPrefix(eKey, prefix) {
|
|
||||||
_ = os.Unsetenv(eKey)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
21
contrib/environment-to-ini/environment-to-ini_test.go
Normal file
21
contrib/environment-to-ini/environment-to-ini_test.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_splitEnvironmentVariable(t *testing.T) {
|
||||||
|
prefixRegexp := regexp.MustCompile(prefixRegexpString + "__")
|
||||||
|
k, v := splitEnvironmentVariable(prefixRegexp, "FORGEJO__KEY=VALUE")
|
||||||
|
assert.Equal(t, k, "KEY")
|
||||||
|
assert.Equal(t, v, "VALUE")
|
||||||
|
k, v = splitEnvironmentVariable(prefixRegexp, "nothing=interesting")
|
||||||
|
assert.Equal(t, k, "")
|
||||||
|
assert.Equal(t, v, "")
|
||||||
|
}
|
Loading…
Reference in a new issue