0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-15 10:43:21 +02:00

drop argsSet in favor of required

This commit is contained in:
TheFox0x7 2025-05-25 21:14:25 +02:00
parent 02497c0153
commit 8e2333f46d
No known key found for this signature in database
GPG Key ID: 6CA33903484AF7C2
8 changed files with 20 additions and 51 deletions

View File

@ -70,9 +70,9 @@ var (
Action: runSendMail, Action: runSendMail,
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "title", Name: "title",
Usage: `a title of a message`, Usage: "a title of a message",
Value: "", Required: true,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "content", Name: "content",

View File

@ -24,16 +24,16 @@ func microcmdUserChangePassword() *cli.Command {
Action: runChangePassword, Action: runChangePassword,
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "username", Name: "username",
Aliases: []string{"u"}, Aliases: []string{"u"},
Value: "", Usage: "The user to change password for",
Usage: "The user to change password for", Required: true,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "password", Name: "password",
Aliases: []string{"p"}, Aliases: []string{"p"},
Value: "", Usage: "New password to set for user",
Usage: "New password to set for user", Required: true,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "must-change-password", Name: "must-change-password",
@ -45,10 +45,6 @@ func microcmdUserChangePassword() *cli.Command {
} }
func runChangePassword(ctx context.Context, c *cli.Command) error { func runChangePassword(ctx context.Context, c *cli.Command) error {
if err := argsSet(c, "username", "password"); err != nil {
return err
}
if !setting.IsInTesting { if !setting.IsInTesting {
if err := initDB(ctx); err != nil { if err := initDB(ctx); err != nil {
return err return err

View File

@ -65,12 +65,12 @@ func TestChangePasswordCommand(t *testing.T) {
{ {
name: "missing username", name: "missing username",
args: []string{"change-password", "--password", "newpassword"}, args: []string{"change-password", "--password", "newpassword"},
expectedErr: "username is not set", expectedErr: `"username" not set`,
}, },
{ {
name: "missing password", name: "missing password",
args: []string{"change-password", "--username", "testuser"}, args: []string{"change-password", "--username", "testuser"},
expectedErr: "password is not set", expectedErr: `"password" not set`,
}, },
{ {
name: "too short password", name: "too short password",

View File

@ -52,8 +52,9 @@ func microcmdUserCreate() *cli.Command {
Usage: "User password", Usage: "User password",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "email", Name: "email",
Usage: "User email address", Usage: "User email address",
Required: true,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "admin", 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. // duplicate setting loading should be safe at the moment, but it should be refactored & improved in the future.
setting.LoadSettings() setting.LoadSettings()
if err := argsSet(c, "email"); err != nil {
return err
}
userTypes := map[string]user_model.UserType{ userTypes := map[string]user_model.UserType{
"individual": user_model.UserTypeIndividual, "individual": user_model.UserTypeIndividual,
"bot": user_model.UserTypeBot, "bot": user_model.UserTypeBot,

View File

@ -35,9 +35,9 @@ Outputs to 'cert.pem' and 'key.pem' and will overwrite existing files.`,
Action: runCert, Action: runCert,
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "host", Name: "host",
Value: "", Usage: "Comma-separated hostnames and IPs to generate a certificate for",
Usage: "Comma-separated hostnames and IPs to generate a certificate for", Required: true,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "ecdsa-curve", Name: "ecdsa-curve",
@ -104,10 +104,6 @@ func pemBlockForKey(priv any) *pem.Block {
} }
func runCert(_ context.Context, c *cli.Command) error { func runCert(_ context.Context, c *cli.Command) error {
if err := argsSet(c, "host"); err != nil {
return err
}
var priv any var priv any
var err error var err error
switch c.String("ecdsa-curve") { switch c.String("ecdsa-curve") {

View File

@ -102,7 +102,7 @@ func TestCertCommandFailures(t *testing.T) {
args: []string{ args: []string{
"cert-test", "cert-test",
}, },
errMsg: "host is not set", errMsg: `"host" not set`,
}, },
} }
for _, c := range cases { for _, c := range cases {

View File

@ -18,26 +18,10 @@ import (
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/urfave/cli/v3" "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 // confirm waits for user input which confirms an action
func confirm() (bool, error) { func confirm() (bool, error) {
var response string var response string

View File

@ -16,10 +16,6 @@ import (
func runSendMail(ctx context.Context, c *cli.Command) error { func runSendMail(ctx context.Context, c *cli.Command) error {
setting.MustInstalled() setting.MustInstalled()
if err := argsSet(c, "title"); err != nil {
return err
}
subject := c.String("title") subject := c.String("title")
confirmSkiped := c.Bool("force") confirmSkiped := c.Bool("force")
body := c.String("content") body := c.String("content")