diff --git a/cmd/admin.go b/cmd/admin.go index c38357625e..3e998a8989 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -70,9 +70,9 @@ var ( Action: runSendMail, Flags: []cli.Flag{ &cli.StringFlag{ - Name: "title", - Usage: `a title of a message`, - Value: "", + Name: "title", + Usage: "a title of a message", + Required: true, }, &cli.StringFlag{ Name: "content", diff --git a/cmd/admin_user_change_password.go b/cmd/admin_user_change_password.go index aef6b9f944..c27905b4db 100644 --- a/cmd/admin_user_change_password.go +++ b/cmd/admin_user_change_password.go @@ -24,16 +24,16 @@ func microcmdUserChangePassword() *cli.Command { Action: runChangePassword, Flags: []cli.Flag{ &cli.StringFlag{ - Name: "username", - Aliases: []string{"u"}, - Value: "", - Usage: "The user to change password for", + Name: "username", + Aliases: []string{"u"}, + Usage: "The user to change password for", + Required: true, }, &cli.StringFlag{ - Name: "password", - Aliases: []string{"p"}, - Value: "", - Usage: "New password to set for user", + Name: "password", + Aliases: []string{"p"}, + Usage: "New password to set for user", + Required: true, }, &cli.BoolFlag{ Name: "must-change-password", @@ -45,10 +45,6 @@ func microcmdUserChangePassword() *cli.Command { } func runChangePassword(ctx context.Context, c *cli.Command) error { - if err := argsSet(c, "username", "password"); err != nil { - return err - } - if !setting.IsInTesting { if err := initDB(ctx); err != nil { return err diff --git a/cmd/admin_user_change_password_test.go b/cmd/admin_user_change_password_test.go index a5aadd6200..55a59e2145 100644 --- a/cmd/admin_user_change_password_test.go +++ b/cmd/admin_user_change_password_test.go @@ -65,12 +65,12 @@ func TestChangePasswordCommand(t *testing.T) { { name: "missing username", args: []string{"change-password", "--password", "newpassword"}, - expectedErr: "username is not set", + expectedErr: `"username" not set`, }, { name: "missing password", args: []string{"change-password", "--username", "testuser"}, - expectedErr: "password is not set", + expectedErr: `"password" not set`, }, { name: "too short password", diff --git a/cmd/admin_user_create.go b/cmd/admin_user_create.go index 4f68c932e0..3520f77e14 100644 --- a/cmd/admin_user_create.go +++ b/cmd/admin_user_create.go @@ -52,8 +52,9 @@ func microcmdUserCreate() *cli.Command { Usage: "User password", }, &cli.StringFlag{ - Name: "email", - Usage: "User email address", + Name: "email", + Usage: "User email address", + Required: true, }, &cli.BoolFlag{ Name: "admin", @@ -104,10 +105,6 @@ func runCreateUser(ctx context.Context, c *cli.Command) error { // duplicate setting loading should be safe at the moment, but it should be refactored & improved in the future. setting.LoadSettings() - if err := argsSet(c, "email"); err != nil { - return err - } - userTypes := map[string]user_model.UserType{ "individual": user_model.UserTypeIndividual, "bot": user_model.UserTypeBot, diff --git a/cmd/cert.go b/cmd/cert.go index 329a9a10eb..fd68b2539c 100644 --- a/cmd/cert.go +++ b/cmd/cert.go @@ -35,9 +35,9 @@ Outputs to 'cert.pem' and 'key.pem' and will overwrite existing files.`, Action: runCert, Flags: []cli.Flag{ &cli.StringFlag{ - Name: "host", - Value: "", - Usage: "Comma-separated hostnames and IPs to generate a certificate for", + Name: "host", + Usage: "Comma-separated hostnames and IPs to generate a certificate for", + Required: true, }, &cli.StringFlag{ Name: "ecdsa-curve", @@ -104,10 +104,6 @@ func pemBlockForKey(priv any) *pem.Block { } func runCert(_ context.Context, c *cli.Command) error { - if err := argsSet(c, "host"); err != nil { - return err - } - var priv any var err error switch c.String("ecdsa-curve") { diff --git a/cmd/cert_test.go b/cmd/cert_test.go index 062662a28e..fbf5ca2073 100644 --- a/cmd/cert_test.go +++ b/cmd/cert_test.go @@ -102,7 +102,7 @@ func TestCertCommandFailures(t *testing.T) { args: []string{ "cert-test", }, - errMsg: "host is not set", + errMsg: `"host" not set`, }, } for _, c := range cases { diff --git a/cmd/cmd.go b/cmd/cmd.go index afd9b3124e..764cccccac 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -18,26 +18,10 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/util" "github.com/urfave/cli/v3" ) -// argsSet checks that all the required arguments are set. args is a list of -// arguments that must be set in the passed Context. -func argsSet(c *cli.Command, args ...string) error { - for _, a := range args { - if !c.IsSet(a) { - return errors.New(a + " is not set") - } - - if util.IsEmptyString(c.String(a)) { - return errors.New(a + " is required") - } - } - return nil -} - // confirm waits for user input which confirms an action func confirm() (bool, error) { var response string diff --git a/cmd/mailer.go b/cmd/mailer.go index e0bbd91ad6..72bd8e5601 100644 --- a/cmd/mailer.go +++ b/cmd/mailer.go @@ -16,10 +16,6 @@ import ( func runSendMail(ctx context.Context, c *cli.Command) error { setting.MustInstalled() - if err := argsSet(c, "title"); err != nil { - return err - } - subject := c.String("title") confirmSkiped := c.Bool("force") body := c.String("content")