Skip to content

Commit 6802be3

Browse files
committed
refactor: "edit-ini --in-place --config-keep-keys ..."
1 parent c6d1837 commit 6802be3

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

cmd/config_update.go renamed to cmd/config.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ import (
1010
"os"
1111

1212
"code.gitea.io/gitea/modules/setting"
13-
"code.gitea.io/gitea/modules/util"
14-
1513
"github.com/urfave/cli/v3"
1614
)
1715

1816
func cmdConfig() *cli.Command {
19-
subcmdConfigUpdateIni := &cli.Command{
20-
Name: "update-ini",
17+
subcmdConfigEditIni := &cli.Command{
18+
Name: "edit-ini",
2119
Usage: "Load an existing INI file, apply environment variables, keep specified keys, and output to a new INI file.",
2220
Description: `
2321
Help users to update the gitea configuration INI file:
@@ -27,14 +25,14 @@ Help users to update the gitea configuration INI file:
2725
# Keep Specified Keys
2826
2927
If you need to re-create the configuration file with only a subset of keys,
30-
you can provide an INI template file and use the "--config-key-template" flag.
28+
you can provide an INI template file and use the "--config-keep-keys" flag.
3129
For example, if a helm chart needs to reset the settings and only keep SECRET_KEY,
32-
it can use a template file like:
30+
it can use a template file (only keys take effect, values are ignored):
3331
3432
[security]
3533
SECRET_KEY=
3634
37-
$ ./gitea config update-ini --config app-old.ini --config-key-template app-template.ini --out app-new.ini
35+
$ ./gitea config edit-ini --config app-old.ini --config-keep-keys app-template.ini --out app-new.ini
3836
3937
# Map Environment Variables to INI Configuration
4038
@@ -57,18 +55,22 @@ $ export GITEA__git_0x2E_config__foo_0x2E_bar=val
5755
5856
# Put All Together
5957
60-
$ ./gitea config update-ini --config app.ini --config-key-template app-template.ini --apply-env
58+
$ ./gitea config edit-ini --config app.ini --config-keep-keys app-template.ini --apply-env
6159
`,
6260
Flags: []cli.Flag{
6361
// "--config" flag is provided by global flags, and this flag is also used by "environment-to-ini" script wrapper
6462
&cli.StringFlag{
65-
Name: "config-key-template",
63+
Name: "config-keep-keys",
6664
Usage: "An INI template file containing keys for keeping. Only the keys defined in the INI template will be kept from old config. If not set, all keys will be kept.",
6765
},
6866
&cli.BoolFlag{
6967
Name: "apply-env",
7068
Usage: "Apply all GITEA__* variables from the environment to the config.",
7169
},
70+
&cli.BoolFlag{
71+
Name: "in-place",
72+
Usage: "Output to the same config file as input.",
73+
},
7274
&cli.StringFlag{
7375
Name: "out",
7476
Usage: "Destination config file to write to. If not set, will overwrite the source config file.",
@@ -81,7 +83,7 @@ $ ./gitea config update-ini --config app.ini --config-key-template app-template.
8183
Name: "config",
8284
Usage: "Manage Gitea configuration",
8385
Commands: []*cli.Command{
84-
subcmdConfigUpdateIni,
86+
subcmdConfigEditIni,
8587
},
8688
}
8789
}
@@ -99,14 +101,25 @@ func runConfigUpdateIni(_ context.Context, c *cli.Command) error {
99101
return fmt.Errorf("failed to load config file %q: %v", configFileIn, err)
100102
}
101103

104+
inPlace := c.Bool("in-place")
102105
configFileOut := c.String("out")
103-
configFileOut = util.IfZero(configFileOut, configFileIn)
106+
if inPlace {
107+
if configFileOut != "" {
108+
return errors.New("cannot use --in-place and --out together")
109+
}
110+
configFileOut = configFileIn
111+
} else {
112+
if configFileOut == "" {
113+
return errors.New("either --in-place or --out must be specified")
114+
}
115+
}
116+
104117
needWriteOut := configFileOut != configFileIn
105118

106119
cfgOut := cfgIn
107-
if c.IsSet("config-key-template") {
120+
if c.IsSet("config-keep-keys") {
108121
needWriteOut = true
109-
configKeepTemplate := c.String("config-key-template")
122+
configKeepTemplate := c.String("config-keep-keys")
110123
cfgOut, err = setting.NewConfigProviderFromFile(configKeepTemplate)
111124
if err != nil {
112125
return fmt.Errorf("failed to load config keep template file %q: %v", configKeepTemplate, err)

cmd/config_update_test.go renamed to cmd/config_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/require"
1111
)
1212

13-
func TestConfigUpdate(t *testing.T) {
13+
func TestConfigEdit(t *testing.T) {
1414
tmpDir := t.TempDir()
1515
configOld := tmpDir + "/app-old.ini"
1616
configTemplate := tmpDir + "/app-template.ini"
@@ -33,9 +33,10 @@ k3=v3
3333
t.Run("OutputToNewWithEnv", func(t *testing.T) {
3434
configNew := tmpDir + "/app-new.ini"
3535
err := NewMainApp(AppVersion{}).Run(t.Context(), []string{
36-
"./gitea", "--config", configOld, "config", "update-ini",
36+
"./gitea", "--config", configOld,
37+
"config", "edit-ini",
3738
"--apply-env",
38-
"--config-key-template", configTemplate,
39+
"--config-keep-keys", configTemplate,
3940
"--out", configNew,
4041
})
4142
require.NoError(t, err)
@@ -54,8 +55,18 @@ KeY = val
5455
})
5556

5657
t.Run("OutputToExisting(environment-to-ini)", func(t *testing.T) {
58+
// the legacy "environment-to-ini" (now a wrapper script) behavior:
59+
// if no "--out", then "--in-place" must be used to overwrite the existing "--config" file
5760
err := NewMainApp(AppVersion{}).Run(t.Context(), []string{
58-
"./gitea", "config", "update-ini",
61+
"./gitea", "config", "edit-ini",
62+
"--apply-env",
63+
"--config", configOld,
64+
})
65+
require.ErrorContains(t, err, "either --in-place or --out must be specified")
66+
67+
err = NewMainApp(AppVersion{}).Run(t.Context(), []string{
68+
"./gitea", "config", "edit-ini",
69+
"--in-place",
5970
"--apply-env",
6071
"--config", configOld,
6172
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
exec /app/gitea/gitea config update-ini --apply-env "$@"
2+
exec /app/gitea/gitea config edit-ini --in-place --apply-env "$@"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
exec /app/gitea/gitea config update-ini --apply-env "$@"
2+
exec /app/gitea/gitea config edit-ini --in-place --apply-env "$@"

0 commit comments

Comments
 (0)