From 8b9d23d6ca99a168a4886d0a5d20b32d98962461 Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Wed, 26 Sep 2018 18:07:44 +0100 Subject: [PATCH 1/3] add random-password flag --- cmd/admin.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index 047f3befccf28..2a35902a00970 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -6,6 +6,7 @@ package cmd import ( + "errors" "fmt" "os" "text/tabwriter" @@ -13,6 +14,7 @@ import ( "code.gitea.io/git" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth/oauth2" + "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -59,6 +61,10 @@ var ( Value: "custom/conf/app.ini", Usage: "Custom configuration file path", }, + cli.BoolFlag{ + Name: "random-password", + Usage: "Generate a random password for the user", + }, }, } @@ -273,10 +279,29 @@ func runChangePassword(c *cli.Context) error { } func runCreateUser(c *cli.Context) error { - if err := argsSet(c, "name", "password", "email"); err != nil { + if err := argsSet(c, "name", "email"); err != nil { return err } + if c.IsSet("password") && c.IsSet("random-password") { + return errors.New("cannot set both -random-password and -password flags") + } + + var password string + + if c.IsSet("password") { + password = c.String("password") + } else if c.IsSet("random-password") { + password, err := generate.GetRandomString(30) + if err != nil { + return err + } + + fmt.Printf("generated random password is '%s'\n", password) + } else { + return errors.New("must set either password or random-password flag") + } + if c.IsSet("config") { setting.CustomConf = c.String("config") } @@ -288,7 +313,7 @@ func runCreateUser(c *cli.Context) error { if err := models.CreateUser(&models.User{ Name: c.String("name"), Email: c.String("email"), - Passwd: c.String("password"), + Passwd: password, IsActive: true, IsAdmin: c.Bool("admin"), }); err != nil { From 799564f5ab8533d12f293d3fe287e72e5be461fc Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Tue, 23 Oct 2018 23:18:40 +0100 Subject: [PATCH 2/3] run make fmt --- cmd/admin.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index 02a7c1b7b4abc..baa6f0b7a5235 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -64,8 +64,8 @@ var ( cli.BoolFlag{ Name: "random-password", Usage: "Generate a random password for the user", - }, - cli.BoolFlag{ + }, + cli.BoolFlag{ Name: "must-change-password", Usage: "Force the user to change his/her password after initial login", }, @@ -322,11 +322,11 @@ func runCreateUser(c *cli.Context) error { } if err := models.CreateUser(&models.User{ - Name: c.String("name"), - Email: c.String("email"), - Passwd: password, - IsActive: true, - IsAdmin: c.Bool("admin"), + Name: c.String("name"), + Email: c.String("email"), + Passwd: password, + IsActive: true, + IsAdmin: c.Bool("admin"), MustChangePassword: changePassword, }); err != nil { return fmt.Errorf("CreateUser: %v", err) From e6d3d78782ed876ce42033627ba727847dba065d Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Sun, 28 Oct 2018 17:15:42 +0100 Subject: [PATCH 3/3] add length cli flag rather than use a default value --- cmd/admin.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/admin.go b/cmd/admin.go index baa6f0b7a5235..d8acce778881b 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -69,6 +69,11 @@ var ( Name: "must-change-password", Usage: "Force the user to change his/her password after initial login", }, + cli.IntFlag{ + Name: "random-password-length", + Usage: "Length of the random password to be generated", + Value: 12, + }, }, } @@ -296,7 +301,7 @@ func runCreateUser(c *cli.Context) error { if c.IsSet("password") { password = c.String("password") } else if c.IsSet("random-password") { - password, err := generate.GetRandomString(30) + password, err := generate.GetRandomString(c.Int("random-password-length")) if err != nil { return err }