You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.2 KiB

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