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.

90 lines
2.0 KiB

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