1
0

admin.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "runtime"
  7. "github.com/pkg/errors"
  8. "github.com/urfave/cli"
  9. "gogs.io/gogs/internal/conf"
  10. "gogs.io/gogs/internal/database"
  11. )
  12. var (
  13. Admin = cli.Command{
  14. Name: "admin",
  15. Usage: "Perform 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. subcmdDeleteInactivateUsers,
  21. subcmdDeleteRepositoryArchives,
  22. subcmdDeleteMissingRepositories,
  23. subcmdGitGcRepos,
  24. subcmdRewriteAuthorizedKeys,
  25. subcmdSyncRepositoryHooks,
  26. subcmdReinitMissingRepositories,
  27. },
  28. }
  29. subcmdCreateUser = cli.Command{
  30. Name: "create-user",
  31. Usage: "Create a new user in database",
  32. Action: runCreateUser,
  33. Flags: []cli.Flag{
  34. stringFlag("name", "", "Username"),
  35. stringFlag("password", "", "User password"),
  36. stringFlag("email", "", "User email address"),
  37. boolFlag("admin", "User is an admin"),
  38. stringFlag("config, c", "", "Custom configuration file path"),
  39. },
  40. }
  41. subcmdDeleteInactivateUsers = cli.Command{
  42. Name: "delete-inactive-users",
  43. Usage: "Delete all inactive accounts",
  44. Action: adminDashboardOperation(
  45. func() error { return database.Handle.Users().DeleteInactivated() },
  46. "All inactivated accounts have been deleted successfully",
  47. ),
  48. Flags: []cli.Flag{
  49. stringFlag("config, c", "", "Custom configuration file path"),
  50. },
  51. }
  52. subcmdDeleteRepositoryArchives = cli.Command{
  53. Name: "delete-repository-archives",
  54. Usage: "Delete all repositories archives",
  55. Action: adminDashboardOperation(
  56. database.DeleteRepositoryArchives,
  57. "All repositories archives have been deleted successfully",
  58. ),
  59. Flags: []cli.Flag{
  60. stringFlag("config, c", "", "Custom configuration file path"),
  61. },
  62. }
  63. subcmdDeleteMissingRepositories = cli.Command{
  64. Name: "delete-missing-repositories",
  65. Usage: "Delete all repository records that lost Git files",
  66. Action: adminDashboardOperation(
  67. database.DeleteMissingRepositories,
  68. "All repositories archives have been deleted successfully",
  69. ),
  70. Flags: []cli.Flag{
  71. stringFlag("config, c", "", "Custom configuration file path"),
  72. },
  73. }
  74. subcmdGitGcRepos = cli.Command{
  75. Name: "collect-garbage",
  76. Usage: "Do garbage collection on repositories",
  77. Action: adminDashboardOperation(
  78. database.GitGcRepos,
  79. "All repositories have done garbage collection successfully",
  80. ),
  81. Flags: []cli.Flag{
  82. stringFlag("config, c", "", "Custom configuration file path"),
  83. },
  84. }
  85. subcmdRewriteAuthorizedKeys = cli.Command{
  86. Name: "rewrite-authorized-keys",
  87. Usage: "Rewrite '.ssh/authorized_keys' file (caution: non-Gogs keys will be lost)",
  88. Action: adminDashboardOperation(
  89. database.RewriteAuthorizedKeys,
  90. "All public keys have been rewritten successfully",
  91. ),
  92. Flags: []cli.Flag{
  93. stringFlag("config, c", "", "Custom configuration file path"),
  94. },
  95. }
  96. subcmdSyncRepositoryHooks = cli.Command{
  97. Name: "resync-hooks",
  98. Usage: "Resync pre-receive, update and post-receive hooks",
  99. Action: adminDashboardOperation(
  100. database.SyncRepositoryHooks,
  101. "All repositories' pre-receive, update and post-receive hooks have been resynced successfully",
  102. ),
  103. Flags: []cli.Flag{
  104. stringFlag("config, c", "", "Custom configuration file path"),
  105. },
  106. }
  107. subcmdReinitMissingRepositories = cli.Command{
  108. Name: "reinit-missing-repositories",
  109. Usage: "Reinitialize all repository records that lost Git files",
  110. Action: adminDashboardOperation(
  111. database.ReinitMissingRepositories,
  112. "All repository records that lost Git files have been reinitialized successfully",
  113. ),
  114. Flags: []cli.Flag{
  115. stringFlag("config, c", "", "Custom configuration file path"),
  116. },
  117. }
  118. )
  119. func runCreateUser(c *cli.Context) error {
  120. if !c.IsSet("name") {
  121. return errors.New("Username is not specified")
  122. } else if !c.IsSet("password") {
  123. return errors.New("Password is not specified")
  124. } else if !c.IsSet("email") {
  125. return errors.New("Email is not specified")
  126. }
  127. err := conf.Init(c.String("config"))
  128. if err != nil {
  129. return errors.Wrap(err, "init configuration")
  130. }
  131. conf.InitLogging(true)
  132. if _, err = database.SetEngine(); err != nil {
  133. return errors.Wrap(err, "set engine")
  134. }
  135. user, err := database.Handle.Users().Create(
  136. context.Background(),
  137. c.String("name"),
  138. c.String("email"),
  139. database.CreateUserOptions{
  140. Password: c.String("password"),
  141. Activated: true,
  142. Admin: c.Bool("admin"),
  143. },
  144. )
  145. if err != nil {
  146. return errors.Wrap(err, "create user")
  147. }
  148. fmt.Printf("New user %q has been successfully created!\n", user.Name)
  149. return nil
  150. }
  151. func adminDashboardOperation(operation func() error, successMessage string) func(*cli.Context) error {
  152. return func(c *cli.Context) error {
  153. err := conf.Init(c.String("config"))
  154. if err != nil {
  155. return errors.Wrap(err, "init configuration")
  156. }
  157. conf.InitLogging(true)
  158. if _, err = database.SetEngine(); err != nil {
  159. return errors.Wrap(err, "set engine")
  160. }
  161. if err := operation(); err != nil {
  162. functionName := runtime.FuncForPC(reflect.ValueOf(operation).Pointer()).Name()
  163. return fmt.Errorf("%s: %v", functionName, err)
  164. }
  165. fmt.Printf("%s\n", successMessage)
  166. return nil
  167. }
  168. }