gitea源码

environment-to-ini.go 3.2KB

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