code.gitea.io/gitea@v1.21.7/contrib/environment-to-ini/environment-to-ini.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package main 5 6 import ( 7 "os" 8 9 "code.gitea.io/gitea/modules/log" 10 "code.gitea.io/gitea/modules/setting" 11 12 "github.com/urfave/cli/v2" 13 ) 14 15 func main() { 16 app := cli.NewApp() 17 app.Name = "environment-to-ini" 18 app.Usage = "Use provided environment to update configuration ini" 19 app.Description = `As a helper to allow docker users to update the gitea configuration 20 through the environment, this command allows environment variables to 21 be mapped to values in the ini. 22 23 Environment variables of the form "GITEA__SECTION_NAME__KEY_NAME" 24 will be mapped to the ini section "[section_name]" and the key 25 "KEY_NAME" with the value as provided. 26 27 Environment variables of the form "GITEA__SECTION_NAME__KEY_NAME__FILE" 28 will be mapped to the ini section "[section_name]" and the key 29 "KEY_NAME" with the value loaded from the specified file. 30 31 Environment variables are usually restricted to a reduced character 32 set "0-9A-Z_" - in order to allow the setting of sections with 33 characters outside of that set, they should be escaped as following: 34 "_0X2E_" for ".". The entire section and key names can be escaped as 35 a UTF8 byte string if necessary. E.g. to configure: 36 37 """ 38 ... 39 [log.console] 40 COLORIZE=false 41 STDERR=true 42 ... 43 """ 44 45 You would set the environment variables: "GITEA__LOG_0x2E_CONSOLE__COLORIZE=false" 46 and "GITEA__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found 47 on the configuration cheat sheet.` 48 app.Flags = []cli.Flag{ 49 &cli.StringFlag{ 50 Name: "custom-path", 51 Aliases: []string{"C"}, 52 Value: setting.CustomPath, 53 Usage: "Custom path file path", 54 }, 55 &cli.StringFlag{ 56 Name: "config", 57 Aliases: []string{"c"}, 58 Value: setting.CustomConf, 59 Usage: "Custom configuration file path", 60 }, 61 &cli.StringFlag{ 62 Name: "work-path", 63 Aliases: []string{"w"}, 64 Value: setting.AppWorkPath, 65 Usage: "Set the gitea working path", 66 }, 67 &cli.StringFlag{ 68 Name: "out", 69 Aliases: []string{"o"}, 70 Value: "", 71 Usage: "Destination file to write to", 72 }, 73 } 74 app.Action = runEnvironmentToIni 75 err := app.Run(os.Args) 76 if err != nil { 77 log.Fatal("Failed to run app with %s: %v", os.Args, err) 78 } 79 } 80 81 func runEnvironmentToIni(c *cli.Context) error { 82 // the config system may change the environment variables, so get a copy first, to be used later 83 env := append([]string{}, os.Environ()...) 84 setting.InitWorkPathAndCfgProvider(os.Getenv, setting.ArgWorkPathAndCustomConf{ 85 WorkPath: c.String("work-path"), 86 CustomPath: c.String("custom-path"), 87 CustomConf: c.String("config"), 88 }) 89 90 cfg, err := setting.NewConfigProviderFromFile(setting.CustomConf) 91 if err != nil { 92 log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err) 93 } 94 95 changed := setting.EnvironmentToConfig(cfg, env) 96 97 // try to save the config file 98 destination := c.String("out") 99 if len(destination) == 0 { 100 destination = setting.CustomConf 101 } 102 if destination != setting.CustomConf || changed { 103 log.Info("Settings saved to: %q", destination) 104 err = cfg.SaveTo(destination) 105 if err != nil { 106 return err 107 } 108 } 109 110 return nil 111 }