您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

environment-to-ini.go 3.1KB

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