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
2425func 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+
93106func 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
0 commit comments