|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/modules/setting" |
| 12 | + "code.gitea.io/gitea/modules/util" |
| 13 | + |
| 14 | + "github.com/urfave/cli/v3" |
| 15 | +) |
| 16 | + |
| 17 | +func cmdConfig() *cli.Command { |
| 18 | + subcmdConfigUpdateIni := &cli.Command{ |
| 19 | + Name: "update-ini", |
| 20 | + Usage: "Load an existing INI file, apply environment variables, keep specified keys, and output to a new INI file.", |
| 21 | + Description: ` |
| 22 | +Help users to update the gitea configuration INI file: |
| 23 | +* Keep specified keys to for the new INI file. |
| 24 | +* Map environment variables to values in the INI. |
| 25 | +
|
| 26 | +# Keep Specified Keys |
| 27 | +
|
| 28 | +If you need to re-create the configuration file with only a subset of keys, |
| 29 | +you can provide an INI template file and use the "--config-keep-template" flag. |
| 30 | +For example, if a helm chart needs to reset the settings and only keep SECRET_KEY, |
| 31 | +it can use a template file like: |
| 32 | +
|
| 33 | + [security] |
| 34 | + SECRET_KEY= |
| 35 | +
|
| 36 | +$ ./gitea config update-ini --config app-old.ini --config-keep-template app-template.ini --out app-new.ini |
| 37 | +
|
| 38 | +# Map Environment Variables to INI Configuration |
| 39 | +
|
| 40 | +Environment variables of the form "GITEA__section_name__KEY_NAME" |
| 41 | +will be mapped to the ini section "[section_name]" and the key |
| 42 | +"KEY_NAME" with the value as provided. |
| 43 | +
|
| 44 | +Environment variables of the form "GITEA__section_name__KEY_NAME__FILE" |
| 45 | +will be mapped to the ini section "[section_name]" and the key |
| 46 | +"KEY_NAME" with the value loaded from the specified file. |
| 47 | +
|
| 48 | +Environment variable keys can only contain characters "0-9A-Z_", |
| 49 | +if a section or key name contains dot ".", it needs to be escaped as _0x2E_. |
| 50 | +For example, to apply this config: |
| 51 | +
|
| 52 | + [git.config] |
| 53 | + foo.bar=val |
| 54 | +
|
| 55 | +$ export GITEA__git_0x2E_config__foo_0x2E_bar=val |
| 56 | +
|
| 57 | +# Put All Together |
| 58 | +
|
| 59 | +$ ./gitea config update-ini --config app.ini --config-keep-template app-template.ini --apply-env |
| 60 | +`, |
| 61 | + Flags: []cli.Flag{ |
| 62 | + // "--config" flag is provided by global flags, and this flag is also used by "environment-to-ini" script wrapper |
| 63 | + &cli.StringFlag{ |
| 64 | + Name: "config-keep-template", |
| 65 | + Usage: "An INI template file containing keys for updating. Only the keys defined in the INI template will be kept from old config. If not set, all keys will be kept.", |
| 66 | + }, |
| 67 | + &cli.BoolFlag{ |
| 68 | + Name: "apply-env", |
| 69 | + Usage: "Apply all GITEA__* variables from the environment to the config.", |
| 70 | + }, |
| 71 | + &cli.StringFlag{ |
| 72 | + Name: "out", |
| 73 | + Usage: "Destination config file to write to. If not set, will overwrite the source config file.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + Action: runConfigUpdateIni, |
| 77 | + } |
| 78 | + |
| 79 | + return &cli.Command{ |
| 80 | + Name: "config", |
| 81 | + Usage: "Manage Gitea configuration", |
| 82 | + Commands: []*cli.Command{ |
| 83 | + subcmdConfigUpdateIni, |
| 84 | + }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func runConfigUpdateIni(_ context.Context, c *cli.Command) error { |
| 89 | + // the config system may change the environment variables, so get a copy first, to be used later |
| 90 | + env := append([]string{}, os.Environ()...) |
| 91 | + if !c.IsSet("config") { |
| 92 | + return fmt.Errorf("flag is required but not set: --config") |
| 93 | + } |
| 94 | + |
| 95 | + configFileIn := c.String("config") |
| 96 | + cfgIn, err := setting.NewConfigProviderFromFile(configFileIn) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("failed to load config file %q: %v", configFileIn, err) |
| 99 | + } |
| 100 | + |
| 101 | + configFileOut := c.String("out") |
| 102 | + configFileOut = util.IfZero(configFileOut, configFileIn) |
| 103 | + needWriteOut := configFileOut != configFileIn |
| 104 | + |
| 105 | + cfgOut := cfgIn |
| 106 | + if c.IsSet("config-keep-template") { |
| 107 | + needWriteOut = true |
| 108 | + configKeepTemplate := c.String("config-keep-template") |
| 109 | + cfgOut, err = setting.NewConfigProviderFromFile(configKeepTemplate) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to load config keep template file %q: %v", configKeepTemplate, err) |
| 112 | + } |
| 113 | + |
| 114 | + for _, secOut := range cfgOut.Sections() { |
| 115 | + for _, keyOut := range secOut.Keys() { |
| 116 | + secIn := cfgIn.Section(secOut.Name()) |
| 117 | + keyIn := setting.ConfigSectionKey(secIn, keyOut.Name()) |
| 118 | + if keyIn != nil { |
| 119 | + keyOut.SetValue(keyIn.String()) |
| 120 | + } else { |
| 121 | + secOut.DeleteKey(keyOut.Name()) |
| 122 | + } |
| 123 | + } |
| 124 | + if len(secOut.Keys()) == 0 { |
| 125 | + cfgOut.DeleteSection(secOut.Name()) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if c.Bool("apply-env") { |
| 131 | + if setting.EnvironmentToConfig(cfgOut, env) { |
| 132 | + needWriteOut = true |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if needWriteOut { |
| 137 | + _, _ = fmt.Fprintf(c.Writer, "Saving config to: %q\n", configFileOut) |
| 138 | + err = cfgOut.SaveTo(configFileOut) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
0 commit comments