From 7cf6ac3643d18b0c8d659071fec89e9cffab1ada Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 18 Jun 2025 17:44:23 +0800 Subject: [PATCH 1/6] fix --- cmd/admin_auth_smtp.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/admin_auth_smtp.go b/cmd/admin_auth_smtp.go index e9daf7180956c..93e0587fc3ca5 100644 --- a/cmd/admin_auth_smtp.go +++ b/cmd/admin_auth_smtp.go @@ -39,12 +39,10 @@ func smtpCLIFlags() []cli.Flag { &cli.BoolFlag{ Name: "force-smtps", Usage: "SMTPS is always used on port 465. Set this to force SMTPS on other ports.", - Value: true, }, &cli.BoolFlag{ Name: "skip-verify", Usage: "Skip TLS verify.", - Value: true, }, &cli.StringFlag{ Name: "helo-hostname", @@ -54,7 +52,6 @@ func smtpCLIFlags() []cli.Flag { &cli.BoolFlag{ Name: "disable-helo", Usage: "Disable SMTP helo.", - Value: true, }, &cli.StringFlag{ Name: "allowed-domains", @@ -64,7 +61,6 @@ func smtpCLIFlags() []cli.Flag { &cli.BoolFlag{ Name: "skip-local-2fa", Usage: "Skip 2FA to log on.", - Value: true, }, &cli.BoolFlag{ Name: "active", From c5b7ee4d9d2fe6f220ae26dc8a0d3d0d7704038a Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 18 Jun 2025 18:51:56 +0800 Subject: [PATCH 2/6] fix tests --- cmd/admin_auth_smtp_test.go | 58 ++++++++++++++----------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/cmd/admin_auth_smtp_test.go b/cmd/admin_auth_smtp_test.go index 9778ff87d2bbf..e54e01830c7a6 100644 --- a/cmd/admin_auth_smtp_test.go +++ b/cmd/admin_auth_smtp_test.go @@ -60,10 +60,8 @@ func TestAddSMTP(t *testing.T) { Auth: "PLAIN", Host: "localhost", Port: 25, - // ForceSMTPS: true, - // SkipVerify: true, }, - TwoFactorPolicy: "skip", + TwoFactorPolicy: "", }, }, { @@ -73,12 +71,12 @@ func TestAddSMTP(t *testing.T) { "--host", "localhost", "--port", "25", "--auth-type", "LOGIN", - "--force-smtps=false", - "--skip-verify=false", + "--force-smtps", + "--skip-verify", "--helo-hostname", "example.com", - "--disable-helo=false", + "--disable-helo=true", "--allowed-domains", "example.com,example.org", - "--skip-local-2fa=false", + "--skip-local-2fa", "--active=false", }, source: &auth_model.Source{ @@ -89,13 +87,13 @@ func TestAddSMTP(t *testing.T) { Auth: "LOGIN", Host: "localhost", Port: 25, - ForceSMTPS: false, - SkipVerify: false, + ForceSMTPS: true, + SkipVerify: true, HeloHostname: "example.com", - DisableHelo: false, + DisableHelo: true, AllowedDomains: "example.com,example.org", }, - TwoFactorPolicy: "", + TwoFactorPolicy: "skip", }, }, } @@ -157,13 +155,10 @@ func TestUpdateSMTP(t *testing.T) { Name: "old name", IsActive: true, Cfg: &smtp.Source{ - Auth: "PLAIN", - Host: "old host", - Port: 26, - ForceSMTPS: true, - SkipVerify: true, + Auth: "PLAIN", + Host: "old host", + Port: 26, }, - TwoFactorPolicy: "", }, args: []string{ "--id", "1", @@ -177,13 +172,10 @@ func TestUpdateSMTP(t *testing.T) { Name: "test", IsActive: true, Cfg: &smtp.Source{ - Auth: "PLAIN", - Host: "localhost", - Port: 25, - ForceSMTPS: true, - SkipVerify: true, + Auth: "PLAIN", + Host: "localhost", + Port: 25, }, - TwoFactorPolicy: "skip", }, }, { @@ -197,10 +189,7 @@ func TestUpdateSMTP(t *testing.T) { Auth: "PLAIN", Host: "old host", Port: 26, - ForceSMTPS: true, - SkipVerify: true, HeloHostname: "old.example.com", - DisableHelo: false, AllowedDomains: "old.example.com", }, TwoFactorPolicy: "", @@ -211,12 +200,12 @@ func TestUpdateSMTP(t *testing.T) { "--host", "localhost", "--port", "25", "--auth-type", "LOGIN", - "--force-smtps=false", - "--skip-verify=false", + "--force-smtps", + "--skip-verify", "--helo-hostname", "example.com", - "--disable-helo=true", + "--disable-helo", "--allowed-domains", "example.com,example.org", - "--skip-local-2fa=true", + "--skip-local-2fa", "--active=false", }, authSource: &auth_model.Source{ @@ -228,8 +217,8 @@ func TestUpdateSMTP(t *testing.T) { Auth: "LOGIN", Host: "localhost", Port: 25, - ForceSMTPS: false, - SkipVerify: false, + ForceSMTPS: true, + SkipVerify: true, HeloHostname: "example.com", DisableHelo: true, AllowedDomains: "example.com,example.org", @@ -252,11 +241,8 @@ func TestUpdateSMTP(t *testing.T) { Name: "test", IsActive: true, Cfg: &smtp.Source{ - Auth: "PLAIN", - SkipVerify: true, - ForceSMTPS: true, + Auth: "PLAIN", }, - TwoFactorPolicy: "skip", }, nil }, From 50fab9760cc5eece30e332a505125e0402264c50 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 18 Jun 2025 18:52:06 +0800 Subject: [PATCH 3/6] fix default command --- cmd/cmd.go | 10 ++++++++++ cmd/cmd_test.go | 37 +++++++++++++++++++++++++++++++++++++ cmd/main.go | 2 ++ cmd/web.go | 4 ++++ 4 files changed, 53 insertions(+) create mode 100644 cmd/cmd_test.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 7a4d5d0d89b97..5b96bcbf9a91a 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -132,3 +132,13 @@ func PrepareConsoleLoggerLevel(defaultLevel log.Level) func(context.Context, *cl return ctx, nil } } + +func isValidDefaultSubCommand(cmd *cli.Command) (string, bool) { + // Dirty patch for urfave/cli's strange design. + // "./gitea bad-cmd" should not start the web server. + rootArgs := cmd.Root().Args().Slice() + if len(rootArgs) != 0 && rootArgs[0] != cmd.Name { + return rootArgs[0], false + } + return "", true +} diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go new file mode 100644 index 0000000000000..69776827fc483 --- /dev/null +++ b/cmd/cmd_test.go @@ -0,0 +1,37 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package cmd + +import ( + "context" + "github.com/stretchr/testify/assert" + "github.com/urfave/cli/v3" + "testing" +) + +func TestDefaultCommand(t *testing.T) { + test := func(t *testing.T, args []string, expectedRetName string, expectedRetValid bool) { + called := false + cmd := &cli.Command{ + DefaultCommand: "test", + Commands: []*cli.Command{ + { + Name: "test", + Action: func(ctx context.Context, command *cli.Command) error { + retName, retValid := isValidDefaultSubCommand(command) + assert.Equal(t, expectedRetName, retName) + assert.Equal(t, expectedRetValid, retValid) + called = true + return nil + }, + }, + }, + } + assert.NoError(t, cmd.Run(t.Context(), args)) + assert.True(t, called) + } + test(t, []string{"app"}, "", true) + test(t, []string{"app", "test"}, "", true) + test(t, []string{"app", "other"}, "other", false) +} diff --git a/cmd/main.go b/cmd/main.go index 128b8776b4a5f..3b8a8a931162b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -152,6 +152,8 @@ func NewMainApp(appVer AppVersion) *cli.Command { CmdDocs, } + // TODO: we should eventually drop the default command, + // but not sure whether it would break Windows users who used to double-click the EXE to run. app.DefaultCommand = CmdWeb.Name app.Flags = append(app.Flags, cli.VersionFlag) diff --git a/cmd/web.go b/cmd/web.go index 39e336fe54685..61ee3cbc2031e 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -251,6 +251,10 @@ func runWeb(_ context.Context, cmd *cli.Command) error { } }() + if subCmdName, valid := isValidDefaultSubCommand(cmd); !valid { + return fmt.Errorf("unknown command: %s", subCmdName) + } + managerCtx, cancel := context.WithCancel(context.Background()) graceful.InitManager(managerCtx) defer cancel() From 2e39ea4dcb99d8fe3be2a9289945faa23682a0ec Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 18 Jun 2025 18:55:38 +0800 Subject: [PATCH 4/6] fix lint --- cmd/cmd_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index 69776827fc483..2de823975151c 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -5,9 +5,10 @@ package cmd import ( "context" + "testing" + "github.com/stretchr/testify/assert" "github.com/urfave/cli/v3" - "testing" ) func TestDefaultCommand(t *testing.T) { From ec1d6865db630c09a5347ff52496e9015d6a87ee Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 18 Jun 2025 19:02:35 +0800 Subject: [PATCH 5/6] revert more default "true" --- cmd/manager_logging.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmd/manager_logging.go b/cmd/manager_logging.go index c83073e9c6438..ac29e7d3e504e 100644 --- a/cmd/manager_logging.go +++ b/cmd/manager_logging.go @@ -119,7 +119,6 @@ var ( Name: "rotate", Aliases: []string{"r"}, Usage: "Rotate logs", - Value: true, }, &cli.Int64Flag{ Name: "max-size", @@ -130,7 +129,6 @@ var ( Name: "daily", Aliases: []string{"d"}, Usage: "Rotate logs daily", - Value: true, }, &cli.IntFlag{ Name: "max-days", @@ -141,7 +139,6 @@ var ( Name: "compress", Aliases: []string{"z"}, Usage: "Compress rotated logs", - Value: true, }, &cli.IntFlag{ Name: "compression-level", From 3283ca6b30972a2ce1c11d294bb9225a836bb990 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 18 Jun 2025 19:06:37 +0800 Subject: [PATCH 6/6] fine tune --- cmd/cmd_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index 2de823975151c..a36d05c76e07d 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -32,7 +32,7 @@ func TestDefaultCommand(t *testing.T) { assert.NoError(t, cmd.Run(t.Context(), args)) assert.True(t, called) } - test(t, []string{"app"}, "", true) - test(t, []string{"app", "test"}, "", true) - test(t, []string{"app", "other"}, "other", false) + test(t, []string{"./gitea"}, "", true) + test(t, []string{"./gitea", "test"}, "", true) + test(t, []string{"./gitea", "other"}, "other", false) }