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.

admin.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. stringFlag("name", "", "Username"),
  28. stringFlag("password", "", "User password"),
  29. stringFlag("email", "", "User email address"),
  30. boolFlag("admin", "User is an admin"),
  31. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  32. },
  33. }
  34. )
  35. func runCreateUser(c *cli.Context) error {
  36. if !c.IsSet("name") {
  37. return fmt.Errorf("Username is not specified")
  38. } else if !c.IsSet("password") {
  39. return fmt.Errorf("Password is not specified")
  40. } else if !c.IsSet("email") {
  41. return fmt.Errorf("Email is not specified")
  42. }
  43. if c.IsSet("config") {
  44. setting.CustomConf = c.String("config")
  45. }
  46. setting.NewContext()
  47. models.LoadConfigs()
  48. models.SetEngine()
  49. if err := models.CreateUser(&models.User{
  50. Name: c.String("name"),
  51. Email: c.String("email"),
  52. Passwd: c.String("password"),
  53. IsActive: true,
  54. IsAdmin: c.Bool("admin"),
  55. }); err != nil {
  56. return fmt.Errorf("CreateUser: %v", err)
  57. }
  58. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  59. return nil
  60. }