Skip to content

Commit b032259

Browse files
earl-warrenLoïc Dachary
authored andcommitted
[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 commit b4665b0796cf582d761797df2be45e3574839dda)
1 parent 5c4cd14 commit b032259

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

.woodpecker/testing-amd64.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ pipeline:
7575
commands:
7676
- ./build/test-env-prepare.sh
7777

78+
environment-to-ini:
79+
image: *golang_image
80+
environment:
81+
GOPROXY_OVERRIDE: *goproxy_override
82+
commands:
83+
- *goproxy_setup
84+
- go test contrib/environment-to-ini/environment-to-ini.go contrib/environment-to-ini/environment-to-ini_test.go
85+
7886
build:
7987
image: *test_image
8088
environment:

contrib/environment-to-ini/environment-to-ini.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright 2023 The Forgejo Authors. All rights reserved.
12
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
@@ -19,17 +20,17 @@ import (
1920
)
2021

2122
// EnvironmentPrefix environment variables prefixed with this represent ini values to write
22-
const EnvironmentPrefix = "GITEA"
23+
const prefixRegexpString = "^(FORGEJO|GITEA)"
2324

2425
func main() {
2526
app := cli.NewApp()
2627
app.Name = "environment-to-ini"
2728
app.Usage = "Use provided environment to update configuration ini"
28-
app.Description = `As a helper to allow docker users to update the gitea configuration
29+
app.Description = `As a helper to allow docker users to update the forgejo configuration
2930
through the environment, this command allows environment variables to
3031
be mapped to values in the ini.
3132
32-
Environment variables of the form "GITEA__SECTION_NAME__KEY_NAME"
33+
Environment variables of the form "FORGEJO__SECTION_NAME__KEY_NAME"
3334
will be mapped to the ini section "[section_name]" and the key
3435
"KEY_NAME" with the value as provided.
3536
@@ -47,9 +48,8 @@ func main() {
4748
...
4849
"""
4950
50-
You would set the environment variables: "GITEA__LOG_0x2E_CONSOLE__COLORIZE=false"
51-
and "GITEA__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found
52-
on the configuration cheat sheet.`
51+
You would set the environment variables: "FORGEJO__LOG_0x2E_CONSOLE__COLORIZE=false"
52+
and "FORGEJO__LOG_0x2E_CONSOLE__STDERR=false".`
5353
app.Flags = []cli.Flag{
5454
cli.StringFlag{
5555
Name: "custom-path, C",
@@ -77,7 +77,7 @@ func main() {
7777
},
7878
cli.StringFlag{
7979
Name: "prefix, p",
80-
Value: EnvironmentPrefix,
80+
Value: prefixRegexpString,
8181
Usage: "Environment prefix to look for - will be suffixed by __ (2 underscores)",
8282
},
8383
}
@@ -90,6 +90,19 @@ func main() {
9090
}
9191
}
9292

93+
func splitEnvironmentVariable(prefixRegexp *regexp.Regexp, kv string) (string, string) {
94+
idx := strings.IndexByte(kv, '=')
95+
if idx < 0 {
96+
return "", ""
97+
}
98+
k := kv[:idx]
99+
loc := prefixRegexp.FindStringIndex(k)
100+
if loc == nil {
101+
return "", ""
102+
}
103+
return k[loc[1]:], kv[idx+1:]
104+
}
105+
93106
func runEnvironmentToIni(c *cli.Context) error {
94107
providedCustom := c.String("custom-path")
95108
providedConf := c.String("config")
@@ -112,19 +125,13 @@ func runEnvironmentToIni(c *cli.Context) error {
112125

113126
changed := false
114127

115-
prefix := c.String("prefix") + "__"
128+
prefixRegexp := regexp.MustCompile(c.String("prefix") + "__")
116129

117130
for _, kv := range os.Environ() {
118-
idx := strings.IndexByte(kv, '=')
119-
if idx < 0 {
131+
eKey, value := splitEnvironmentVariable(prefixRegexp, kv)
132+
if eKey == "" {
120133
continue
121134
}
122-
eKey := kv[:idx]
123-
value := kv[idx+1:]
124-
if !strings.HasPrefix(eKey, prefix) {
125-
continue
126-
}
127-
eKey = eKey[len(prefix):]
128135
sectionName, keyName := DecodeSectionKey(eKey)
129136
if len(keyName) == 0 {
130137
continue
@@ -164,14 +171,11 @@ func runEnvironmentToIni(c *cli.Context) error {
164171
}
165172
if c.Bool("clear") {
166173
for _, kv := range os.Environ() {
167-
idx := strings.IndexByte(kv, '=')
168-
if idx < 0 {
174+
eKey, _ := splitEnvironmentVariable(prefixRegexp, kv)
175+
if eKey == "" {
169176
continue
170177
}
171-
eKey := kv[:idx]
172-
if strings.HasPrefix(eKey, prefix) {
173-
_ = os.Unsetenv(eKey)
174-
}
178+
_ = os.Unsetenv(eKey)
175179
}
176180
}
177181
return nil
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2023 The Forgejo Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package main
5+
6+
import (
7+
"regexp"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func Test_splitEnvironmentVariable(t *testing.T) {
14+
prefixRegexp := regexp.MustCompile(prefixRegexpString + "__")
15+
k, v := splitEnvironmentVariable(prefixRegexp, "FORGEJO__KEY=VALUE")
16+
assert.Equal(t, k, "KEY")
17+
assert.Equal(t, v, "VALUE")
18+
k, v = splitEnvironmentVariable(prefixRegexp, "nothing=interesting")
19+
assert.Equal(t, k, "")
20+
assert.Equal(t, v, "")
21+
}

0 commit comments

Comments
 (0)