From b032259089a0b5a0eff6592ce9c1038f15e6a551 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 12 Jan 2023 22:56:56 +0100 Subject: [PATCH] [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 (cherry picked from commit b4665b0796cf582d761797df2be45e3574839dda) --- .woodpecker/testing-amd64.yml | 8 ++++ .../environment-to-ini/environment-to-ini.go | 48 ++++++++++--------- .../environment-to-ini_test.go | 21 ++++++++ 3 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 contrib/environment-to-ini/environment-to-ini_test.go diff --git a/.woodpecker/testing-amd64.yml b/.woodpecker/testing-amd64.yml index 6c0ba08196..c321a4bd78 100644 --- a/.woodpecker/testing-amd64.yml +++ b/.woodpecker/testing-amd64.yml @@ -75,6 +75,14 @@ pipeline: commands: - ./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: image: *test_image environment: diff --git a/contrib/environment-to-ini/environment-to-ini.go b/contrib/environment-to-ini/environment-to-ini.go index ccda03fa92..0baba5902b 100644 --- a/contrib/environment-to-ini/environment-to-ini.go +++ b/contrib/environment-to-ini/environment-to-ini.go @@ -1,3 +1,4 @@ +// Copyright 2023 The Forgejo Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -19,17 +20,17 @@ import ( ) // EnvironmentPrefix environment variables prefixed with this represent ini values to write -const EnvironmentPrefix = "GITEA" +const prefixRegexpString = "^(FORGEJO|GITEA)" 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. @@ -47,9 +48,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 - on the configuration cheat sheet.` + You would set the environment variables: "FORGEJO__LOG_0x2E_CONSOLE__COLORIZE=false" + and "FORGEJO__LOG_0x2E_CONSOLE__STDERR=false".` app.Flags = []cli.Flag{ cli.StringFlag{ Name: "custom-path, C", @@ -77,7 +77,7 @@ func main() { }, cli.StringFlag{ Name: "prefix, p", - Value: EnvironmentPrefix, + Value: prefixRegexpString, Usage: "Environment prefix to look for - will be suffixed by __ (2 underscores)", }, } @@ -90,6 +90,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 { providedCustom := c.String("custom-path") providedConf := c.String("config") @@ -112,19 +125,13 @@ func runEnvironmentToIni(c *cli.Context) error { changed := false - prefix := c.String("prefix") + "__" + prefixRegexp := regexp.MustCompile(c.String("prefix") + "__") for _, kv := range os.Environ() { - idx := strings.IndexByte(kv, '=') - if idx < 0 { + eKey, value := splitEnvironmentVariable(prefixRegexp, kv) + if eKey == "" { continue } - eKey := kv[:idx] - value := kv[idx+1:] - if !strings.HasPrefix(eKey, prefix) { - continue - } - eKey = eKey[len(prefix):] sectionName, keyName := DecodeSectionKey(eKey) if len(keyName) == 0 { continue @@ -164,14 +171,11 @@ func runEnvironmentToIni(c *cli.Context) error { } if c.Bool("clear") { for _, kv := range os.Environ() { - idx := strings.IndexByte(kv, '=') - if idx < 0 { + eKey, _ := splitEnvironmentVariable(prefixRegexp, kv) + if eKey == "" { continue } - eKey := kv[:idx] - if strings.HasPrefix(eKey, prefix) { - _ = os.Unsetenv(eKey) - } + _ = os.Unsetenv(eKey) } } return nil diff --git a/contrib/environment-to-ini/environment-to-ini_test.go b/contrib/environment-to-ini/environment-to-ini_test.go new file mode 100644 index 0000000000..6abbb67eff --- /dev/null +++ b/contrib/environment-to-ini/environment-to-ini_test.go @@ -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, "") +}