@ -1,3 +1,4 @@
// Copyright 2023 The Forgejo Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
@ -18,17 +19,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 .
@ -46,9 +47,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" ,
@ -76,7 +76,7 @@ func main() {
} ,
cli . StringFlag {
Name : "prefix, p" ,
Value : EnvironmentPrefix ,
Value : prefixRegexpString ,
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 {
providedCustom := c . String ( "custom-path" )
providedConf := c . String ( "config" )
@ -111,19 +124,13 @@ func runEnvironmentToIni(c *cli.Context) error {
changed := false
prefix := c. String ( "prefix" ) + "__"
prefix Regexp := 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
@ -163,14 +170,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