|
| 1 | +// Copyright 2016 The Gogs Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/codegangsta/cli" |
| 11 | + |
| 12 | + "github.com/gogits/gogs/models" |
| 13 | + "github.com/gogits/gogs/modules/setting" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + CmdAdmin = cli.Command{ |
| 18 | + Name: "admin", |
| 19 | + Usage: "Preform admin operations on command line", |
| 20 | + Description: `Allow using internal logic of Gogs without hacking into the source code |
| 21 | +to make automatic initialization process more smoothly`, |
| 22 | + Subcommands: []cli.Command{ |
| 23 | + subcmdCreateUser, |
| 24 | + }, |
| 25 | + } |
| 26 | + |
| 27 | + subcmdCreateUser = cli.Command{ |
| 28 | + Name: "create-user", |
| 29 | + Usage: "Create a new user in database", |
| 30 | + Action: runCreateUser, |
| 31 | + Flags: []cli.Flag{ |
| 32 | + stringFlag("name", "", "Username"), |
| 33 | + stringFlag("password", "", "User password"), |
| 34 | + stringFlag("email", "", "User email address"), |
| 35 | + boolFlag("admin", "User is an admin"), |
| 36 | + stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"), |
| 37 | + }, |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +func runCreateUser(c *cli.Context) error { |
| 42 | + if !c.IsSet("name") { |
| 43 | + return fmt.Errorf("Username is not specified") |
| 44 | + } else if !c.IsSet("password") { |
| 45 | + return fmt.Errorf("Password is not specified") |
| 46 | + } else if !c.IsSet("email") { |
| 47 | + return fmt.Errorf("Email is not specified") |
| 48 | + } |
| 49 | + |
| 50 | + if c.IsSet("config") { |
| 51 | + setting.CustomConf = c.String("config") |
| 52 | + } |
| 53 | + |
| 54 | + setting.NewContext() |
| 55 | + models.LoadConfigs() |
| 56 | + models.SetEngine() |
| 57 | + |
| 58 | + if err := models.CreateUser(&models.User{ |
| 59 | + Name: c.String("name"), |
| 60 | + Email: c.String("email"), |
| 61 | + Passwd: c.String("password"), |
| 62 | + IsActive: true, |
| 63 | + IsAdmin: c.Bool("admin"), |
| 64 | + }); err != nil { |
| 65 | + return fmt.Errorf("CreateUser: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + fmt.Printf("New user '%s' has been successfully created!\n", c.String("name")) |
| 69 | + return nil |
| 70 | +} |
0 commit comments