admin.go 4.9 KB

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