Skip to content

Commit 7954f25

Browse files
authored
Fix incorrect cli default values and default command (#34765)
1 parent 416ff1f commit 7954f25

File tree

7 files changed

+76
-43
lines changed

7 files changed

+76
-43
lines changed

cmd/admin_auth_smtp.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ func smtpCLIFlags() []cli.Flag {
3939
&cli.BoolFlag{
4040
Name: "force-smtps",
4141
Usage: "SMTPS is always used on port 465. Set this to force SMTPS on other ports.",
42-
Value: true,
4342
},
4443
&cli.BoolFlag{
4544
Name: "skip-verify",
4645
Usage: "Skip TLS verify.",
47-
Value: true,
4846
},
4947
&cli.StringFlag{
5048
Name: "helo-hostname",
@@ -54,7 +52,6 @@ func smtpCLIFlags() []cli.Flag {
5452
&cli.BoolFlag{
5553
Name: "disable-helo",
5654
Usage: "Disable SMTP helo.",
57-
Value: true,
5855
},
5956
&cli.StringFlag{
6057
Name: "allowed-domains",
@@ -64,7 +61,6 @@ func smtpCLIFlags() []cli.Flag {
6461
&cli.BoolFlag{
6562
Name: "skip-local-2fa",
6663
Usage: "Skip 2FA to log on.",
67-
Value: true,
6864
},
6965
&cli.BoolFlag{
7066
Name: "active",

cmd/admin_auth_smtp_test.go

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ func TestAddSMTP(t *testing.T) {
6060
Auth: "PLAIN",
6161
Host: "localhost",
6262
Port: 25,
63-
// ForceSMTPS: true,
64-
// SkipVerify: true,
6563
},
66-
TwoFactorPolicy: "skip",
64+
TwoFactorPolicy: "",
6765
},
6866
},
6967
{
@@ -73,12 +71,12 @@ func TestAddSMTP(t *testing.T) {
7371
"--host", "localhost",
7472
"--port", "25",
7573
"--auth-type", "LOGIN",
76-
"--force-smtps=false",
77-
"--skip-verify=false",
74+
"--force-smtps",
75+
"--skip-verify",
7876
"--helo-hostname", "example.com",
79-
"--disable-helo=false",
77+
"--disable-helo=true",
8078
"--allowed-domains", "example.com,example.org",
81-
"--skip-local-2fa=false",
79+
"--skip-local-2fa",
8280
"--active=false",
8381
},
8482
source: &auth_model.Source{
@@ -89,13 +87,13 @@ func TestAddSMTP(t *testing.T) {
8987
Auth: "LOGIN",
9088
Host: "localhost",
9189
Port: 25,
92-
ForceSMTPS: false,
93-
SkipVerify: false,
90+
ForceSMTPS: true,
91+
SkipVerify: true,
9492
HeloHostname: "example.com",
95-
DisableHelo: false,
93+
DisableHelo: true,
9694
AllowedDomains: "example.com,example.org",
9795
},
98-
TwoFactorPolicy: "",
96+
TwoFactorPolicy: "skip",
9997
},
10098
},
10199
}
@@ -157,13 +155,10 @@ func TestUpdateSMTP(t *testing.T) {
157155
Name: "old name",
158156
IsActive: true,
159157
Cfg: &smtp.Source{
160-
Auth: "PLAIN",
161-
Host: "old host",
162-
Port: 26,
163-
ForceSMTPS: true,
164-
SkipVerify: true,
158+
Auth: "PLAIN",
159+
Host: "old host",
160+
Port: 26,
165161
},
166-
TwoFactorPolicy: "",
167162
},
168163
args: []string{
169164
"--id", "1",
@@ -177,13 +172,10 @@ func TestUpdateSMTP(t *testing.T) {
177172
Name: "test",
178173
IsActive: true,
179174
Cfg: &smtp.Source{
180-
Auth: "PLAIN",
181-
Host: "localhost",
182-
Port: 25,
183-
ForceSMTPS: true,
184-
SkipVerify: true,
175+
Auth: "PLAIN",
176+
Host: "localhost",
177+
Port: 25,
185178
},
186-
TwoFactorPolicy: "skip",
187179
},
188180
},
189181
{
@@ -197,10 +189,7 @@ func TestUpdateSMTP(t *testing.T) {
197189
Auth: "PLAIN",
198190
Host: "old host",
199191
Port: 26,
200-
ForceSMTPS: true,
201-
SkipVerify: true,
202192
HeloHostname: "old.example.com",
203-
DisableHelo: false,
204193
AllowedDomains: "old.example.com",
205194
},
206195
TwoFactorPolicy: "",
@@ -211,12 +200,12 @@ func TestUpdateSMTP(t *testing.T) {
211200
"--host", "localhost",
212201
"--port", "25",
213202
"--auth-type", "LOGIN",
214-
"--force-smtps=false",
215-
"--skip-verify=false",
203+
"--force-smtps",
204+
"--skip-verify",
216205
"--helo-hostname", "example.com",
217-
"--disable-helo=true",
206+
"--disable-helo",
218207
"--allowed-domains", "example.com,example.org",
219-
"--skip-local-2fa=true",
208+
"--skip-local-2fa",
220209
"--active=false",
221210
},
222211
authSource: &auth_model.Source{
@@ -228,8 +217,8 @@ func TestUpdateSMTP(t *testing.T) {
228217
Auth: "LOGIN",
229218
Host: "localhost",
230219
Port: 25,
231-
ForceSMTPS: false,
232-
SkipVerify: false,
220+
ForceSMTPS: true,
221+
SkipVerify: true,
233222
HeloHostname: "example.com",
234223
DisableHelo: true,
235224
AllowedDomains: "example.com,example.org",
@@ -252,11 +241,8 @@ func TestUpdateSMTP(t *testing.T) {
252241
Name: "test",
253242
IsActive: true,
254243
Cfg: &smtp.Source{
255-
Auth: "PLAIN",
256-
SkipVerify: true,
257-
ForceSMTPS: true,
244+
Auth: "PLAIN",
258245
},
259-
TwoFactorPolicy: "skip",
260246
}, nil
261247
},
262248

cmd/cmd.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,13 @@ func PrepareConsoleLoggerLevel(defaultLevel log.Level) func(context.Context, *cl
132132
return ctx, nil
133133
}
134134
}
135+
136+
func isValidDefaultSubCommand(cmd *cli.Command) (string, bool) {
137+
// Dirty patch for urfave/cli's strange design.
138+
// "./gitea bad-cmd" should not start the web server.
139+
rootArgs := cmd.Root().Args().Slice()
140+
if len(rootArgs) != 0 && rootArgs[0] != cmd.Name {
141+
return rootArgs[0], false
142+
}
143+
return "", true
144+
}

cmd/cmd_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cmd
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/urfave/cli/v3"
12+
)
13+
14+
func TestDefaultCommand(t *testing.T) {
15+
test := func(t *testing.T, args []string, expectedRetName string, expectedRetValid bool) {
16+
called := false
17+
cmd := &cli.Command{
18+
DefaultCommand: "test",
19+
Commands: []*cli.Command{
20+
{
21+
Name: "test",
22+
Action: func(ctx context.Context, command *cli.Command) error {
23+
retName, retValid := isValidDefaultSubCommand(command)
24+
assert.Equal(t, expectedRetName, retName)
25+
assert.Equal(t, expectedRetValid, retValid)
26+
called = true
27+
return nil
28+
},
29+
},
30+
},
31+
}
32+
assert.NoError(t, cmd.Run(t.Context(), args))
33+
assert.True(t, called)
34+
}
35+
test(t, []string{"./gitea"}, "", true)
36+
test(t, []string{"./gitea", "test"}, "", true)
37+
test(t, []string{"./gitea", "other"}, "other", false)
38+
}

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func NewMainApp(appVer AppVersion) *cli.Command {
152152
CmdDocs,
153153
}
154154

155+
// TODO: we should eventually drop the default command,
156+
// but not sure whether it would break Windows users who used to double-click the EXE to run.
155157
app.DefaultCommand = CmdWeb.Name
156158

157159
app.Flags = append(app.Flags, cli.VersionFlag)

cmd/manager_logging.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ var (
119119
Name: "rotate",
120120
Aliases: []string{"r"},
121121
Usage: "Rotate logs",
122-
Value: true,
123122
},
124123
&cli.Int64Flag{
125124
Name: "max-size",
@@ -130,7 +129,6 @@ var (
130129
Name: "daily",
131130
Aliases: []string{"d"},
132131
Usage: "Rotate logs daily",
133-
Value: true,
134132
},
135133
&cli.IntFlag{
136134
Name: "max-days",
@@ -141,7 +139,6 @@ var (
141139
Name: "compress",
142140
Aliases: []string{"z"},
143141
Usage: "Compress rotated logs",
144-
Value: true,
145142
},
146143
&cli.IntFlag{
147144
Name: "compression-level",

cmd/web.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ func runWeb(_ context.Context, cmd *cli.Command) error {
251251
}
252252
}()
253253

254+
if subCmdName, valid := isValidDefaultSubCommand(cmd); !valid {
255+
return fmt.Errorf("unknown command: %s", subCmdName)
256+
}
257+
254258
managerCtx, cancel := context.WithCancel(context.Background())
255259
graceful.InitManager(managerCtx)
256260
defer cancel()

0 commit comments

Comments
 (0)