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

admin.go 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "github.com/go-gitea/gitea/models"
  9. "github.com/go-gitea/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: "", Usage: "User password",
  35. },
  36. cli.StringFlag{
  37. Name: "email",
  38. Value: "", Usage: "User email address",
  39. },
  40. cli.BoolFlag{
  41. Name: "admin",
  42. Usage: "User is an admin",
  43. },
  44. cli.StringFlag{
  45. Name: "config, c",
  46. Value: "custom/conf/app.ini",
  47. Usage: "Custom configuration file path",
  48. },
  49. },
  50. }
  51. )
  52. func runCreateUser(c *cli.Context) error {
  53. if !c.IsSet("name") {
  54. return fmt.Errorf("Username is not specified")
  55. } else if !c.IsSet("password") {
  56. return fmt.Errorf("Password is not specified")
  57. } else if !c.IsSet("email") {
  58. return fmt.Errorf("Email is not specified")
  59. }
  60. if c.IsSet("config") {
  61. setting.CustomConf = c.String("config")
  62. }
  63. setting.NewContext()
  64. models.LoadConfigs()
  65. models.SetEngine()
  66. if err := models.CreateUser(&models.User{
  67. Name: c.String("name"),
  68. Email: c.String("email"),
  69. Passwd: c.String("password"),
  70. IsActive: true,
  71. IsAdmin: c.Bool("admin"),
  72. }); err != nil {
  73. return fmt.Errorf("CreateUser: %v", err)
  74. }
  75. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  76. return nil
  77. }