6
6
package cmd
7
7
8
8
import (
9
+ "errors"
9
10
"fmt"
10
11
"os"
11
12
"text/tabwriter"
12
13
13
14
"code.gitea.io/git"
14
15
"code.gitea.io/gitea/models"
15
16
"code.gitea.io/gitea/modules/auth/oauth2"
17
+ "code.gitea.io/gitea/modules/generate"
16
18
"code.gitea.io/gitea/modules/log"
17
19
"code.gitea.io/gitea/modules/setting"
18
20
@@ -59,10 +61,19 @@ var (
59
61
Value : "custom/conf/app.ini" ,
60
62
Usage : "Custom configuration file path" ,
61
63
},
64
+ cli.BoolFlag {
65
+ Name : "random-password" ,
66
+ Usage : "Generate a random password for the user" ,
67
+ },
62
68
cli.BoolFlag {
63
69
Name : "must-change-password" ,
64
70
Usage : "Force the user to change his/her password after initial login" ,
65
71
},
72
+ cli.IntFlag {
73
+ Name : "random-password-length" ,
74
+ Usage : "Length of the random password to be generated" ,
75
+ Value : 12 ,
76
+ },
66
77
},
67
78
}
68
79
@@ -277,10 +288,29 @@ func runChangePassword(c *cli.Context) error {
277
288
}
278
289
279
290
func runCreateUser (c * cli.Context ) error {
280
- if err := argsSet (c , "name" , "password" , " email" ); err != nil {
291
+ if err := argsSet (c , "name" , "email" ); err != nil {
281
292
return err
282
293
}
283
294
295
+ if c .IsSet ("password" ) && c .IsSet ("random-password" ) {
296
+ return errors .New ("cannot set both -random-password and -password flags" )
297
+ }
298
+
299
+ var password string
300
+
301
+ if c .IsSet ("password" ) {
302
+ password = c .String ("password" )
303
+ } else if c .IsSet ("random-password" ) {
304
+ password , err := generate .GetRandomString (c .Int ("random-password-length" ))
305
+ if err != nil {
306
+ return err
307
+ }
308
+
309
+ fmt .Printf ("generated random password is '%s'\n " , password )
310
+ } else {
311
+ return errors .New ("must set either password or random-password flag" )
312
+ }
313
+
284
314
if c .IsSet ("config" ) {
285
315
setting .CustomConf = c .String ("config" )
286
316
}
@@ -299,7 +329,7 @@ func runCreateUser(c *cli.Context) error {
299
329
if err := models .CreateUser (& models.User {
300
330
Name : c .String ("name" ),
301
331
Email : c .String ("email" ),
302
- Passwd : c . String ( " password" ) ,
332
+ Passwd : password ,
303
333
IsActive : true ,
304
334
IsAdmin : c .Bool ("admin" ),
305
335
MustChangePassword : changePassword ,
0 commit comments