admin.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "reflect"
  8. "runtime"
  9. "github.com/urfave/cli"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/pkg/setting"
  12. )
  13. var (
  14. Admin = cli.Command{
  15. Name: "admin",
  16. Usage: "Perform admin operations on command line",
  17. Description: `Allow using internal logic of Gogs without hacking into the source code
  18. to make automatic initialization process more smoothly`,
  19. Subcommands: []cli.Command{
  20. subcmdCreateUser,
  21. subcmdDeleteInactivateUsers,
  22. subcmdDeleteRepositoryArchives,
  23. subcmdDeleteMissingRepositories,
  24. subcmdGitGcRepos,
  25. subcmdRewriteAuthorizedKeys,
  26. subcmdSyncRepositoryHooks,
  27. subcmdReinitMissingRepositories,
  28. },
  29. }
  30. subcmdCreateUser = cli.Command{
  31. Name: "create-user",
  32. Usage: "Create a new user in database",
  33. Action: runCreateUser,
  34. Flags: []cli.Flag{
  35. stringFlag("name", "", "Username"),
  36. stringFlag("password", "", "User password"),
  37. stringFlag("email", "", "User email address"),
  38. boolFlag("admin", "User is an admin"),
  39. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  40. },
  41. }
  42. subcmdDeleteInactivateUsers = cli.Command{
  43. Name: "delete-inactive-users",
  44. Usage: "Delete all inactive accounts",
  45. Action: adminDashboardOperation(
  46. models.DeleteInactivateUsers,
  47. "All inactivate accounts have been deleted successfully",
  48. ),
  49. Flags: []cli.Flag{
  50. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  51. },
  52. }
  53. subcmdDeleteRepositoryArchives = cli.Command{
  54. Name: "delete-repository-archives",
  55. Usage: "Delete all repositories archives",
  56. Action: adminDashboardOperation(
  57. models.DeleteRepositoryArchives,
  58. "All repositories archives have been deleted successfully",
  59. ),
  60. Flags: []cli.Flag{
  61. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  62. },
  63. }
  64. subcmdDeleteMissingRepositories = cli.Command{
  65. Name: "delete-missing-repositories",
  66. Usage: "Delete all repository records that lost Git files",
  67. Action: adminDashboardOperation(
  68. models.DeleteMissingRepositories,
  69. "All repositories archives have been deleted successfully",
  70. ),
  71. Flags: []cli.Flag{
  72. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  73. },
  74. }
  75. subcmdGitGcRepos = cli.Command{
  76. Name: "collect-garbage",
  77. Usage: "Do garbage collection on repositories",
  78. Action: adminDashboardOperation(
  79. models.GitGcRepos,
  80. "All repositories have done garbage collection successfully",
  81. ),
  82. Flags: []cli.Flag{
  83. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  84. },
  85. }
  86. subcmdRewriteAuthorizedKeys = cli.Command{
  87. Name: "rewrite-authorized-keys",
  88. Usage: "Rewrite '.ssh/authorized_keys' file (caution: non-Gogs keys will be lost)",
  89. Action: adminDashboardOperation(
  90. models.RewriteAuthorizedKeys,
  91. "All public keys have been rewritten successfully",
  92. ),
  93. Flags: []cli.Flag{
  94. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  95. },
  96. }
  97. subcmdSyncRepositoryHooks = cli.Command{
  98. Name: "resync-hooks",
  99. Usage: "Resync pre-receive, update and post-receive hooks",
  100. Action: adminDashboardOperation(
  101. models.SyncRepositoryHooks,
  102. "All repositories' pre-receive, update and post-receive hooks have been resynced successfully",
  103. ),
  104. Flags: []cli.Flag{
  105. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  106. },
  107. }
  108. subcmdReinitMissingRepositories = cli.Command{
  109. Name: "reinit-missing-repositories",
  110. Usage: "Reinitialize all repository records that lost Git files",
  111. Action: adminDashboardOperation(
  112. models.ReinitMissingRepositories,
  113. "All repository records that lost Git files have been reinitialized successfully",
  114. ),
  115. Flags: []cli.Flag{
  116. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  117. },
  118. }
  119. )
  120. func runCreateUser(c *cli.Context) error {
  121. if !c.IsSet("name") {
  122. return fmt.Errorf("Username is not specified")
  123. } else if !c.IsSet("password") {
  124. return fmt.Errorf("Password is not specified")
  125. } else if !c.IsSet("email") {
  126. return fmt.Errorf("Email is not specified")
  127. }
  128. if c.IsSet("config") {
  129. setting.CustomConf = c.String("config")
  130. }
  131. setting.NewContext()
  132. models.LoadConfigs()
  133. models.SetEngine()
  134. if err := models.CreateUser(&models.User{
  135. Name: c.String("name"),
  136. Email: c.String("email"),
  137. Passwd: c.String("password"),
  138. IsActive: true,
  139. IsAdmin: c.Bool("admin"),
  140. }); err != nil {
  141. return fmt.Errorf("CreateUser: %v", err)
  142. }
  143. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  144. return nil
  145. }
  146. func adminDashboardOperation(operation func() error, successMessage string) func(*cli.Context) error {
  147. return func(c *cli.Context) error {
  148. if c.IsSet("config") {
  149. setting.CustomConf = c.String("config")
  150. }
  151. setting.NewContext()
  152. models.LoadConfigs()
  153. models.SetEngine()
  154. if err := operation(); err != nil {
  155. functionName := runtime.FuncForPC(reflect.ValueOf(operation).Pointer()).Name()
  156. return fmt.Errorf("%s: %v", functionName, err)
  157. }
  158. fmt.Printf("%s\n", successMessage)
  159. return nil
  160. }
  161. }