admin.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, 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 Initialize admin CLI
  18. Admin = cli.Command{
  19. Name: "admin",
  20. Usage: "Perform admin operations on command line",
  21. Description: "Perform admin operations on command line",
  22. Subcommands: []cli.Command{
  23. subcmdCreateUser,
  24. subcmdDeleteRepositoryArchives,
  25. subcmdDeleteMissingRepositories,
  26. subcmdGitGcRepos,
  27. subcmdRewriteAuthorizedKeys,
  28. subcmdSyncRepositoryHooks,
  29. subcmdReinitMissingRepositories,
  30. },
  31. }
  32. subcmdCreateUser = cli.Command{
  33. Name: "create-user",
  34. Usage: "Create a new user in database",
  35. Action: runCreateUser,
  36. Flags: []cli.Flag{
  37. stringFlag("name", "", "Username"),
  38. stringFlag("password", "", "User password"),
  39. stringFlag("email", "", "User email address"),
  40. boolFlag("admin", "User is an admin"),
  41. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  42. },
  43. }
  44. subcmdDeleteRepositoryArchives = cli.Command{
  45. Name: "delete-repository-archives",
  46. Usage: "Delete all repositories archives",
  47. Action: adminDashboardOperation(
  48. models.DeleteRepositoryArchives,
  49. "All repositories archives have been deleted successfully",
  50. ),
  51. Flags: []cli.Flag{
  52. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  53. },
  54. }
  55. subcmdDeleteMissingRepositories = cli.Command{
  56. Name: "delete-missing-repositories",
  57. Usage: "Delete all repository records that lost Git files",
  58. Action: adminDashboardOperation(
  59. models.DeleteMissingRepositories,
  60. "All repositories archives have been deleted successfully",
  61. ),
  62. Flags: []cli.Flag{
  63. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  64. },
  65. }
  66. subcmdGitGcRepos = cli.Command{
  67. Name: "collect-garbage",
  68. Usage: "Do garbage collection on repositories",
  69. Action: adminDashboardOperation(
  70. models.GitGcRepos,
  71. "All repositories have done garbage collection successfully",
  72. ),
  73. Flags: []cli.Flag{
  74. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  75. },
  76. }
  77. subcmdRewriteAuthorizedKeys = cli.Command{
  78. Name: "rewrite-authorized-keys",
  79. Usage: "Rewrite '.ssh/authorized_keys' file (caution: non-Gitote keys will be lost)",
  80. Action: adminDashboardOperation(
  81. models.RewriteAuthorizedKeys,
  82. "All public keys have been rewritten successfully",
  83. ),
  84. Flags: []cli.Flag{
  85. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  86. },
  87. }
  88. subcmdSyncRepositoryHooks = cli.Command{
  89. Name: "resync-hooks",
  90. Usage: "Resync pre-receive, update and post-receive hooks",
  91. Action: adminDashboardOperation(
  92. models.SyncRepositoryHooks,
  93. "All repositories' pre-receive, update and post-receive hooks have been resynced successfully",
  94. ),
  95. Flags: []cli.Flag{
  96. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  97. },
  98. }
  99. subcmdReinitMissingRepositories = cli.Command{
  100. Name: "reinit-missing-repositories",
  101. Usage: "Reinitialize all repository records that lost Git files",
  102. Action: adminDashboardOperation(
  103. models.ReinitMissingRepositories,
  104. "All repository records that lost Git files have been reinitialized successfully",
  105. ),
  106. Flags: []cli.Flag{
  107. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  108. },
  109. }
  110. )
  111. func runCreateUser(c *cli.Context) error {
  112. if !c.IsSet("name") {
  113. return fmt.Errorf("Username is not specified")
  114. } else if !c.IsSet("password") {
  115. return fmt.Errorf("Password is not specified")
  116. } else if !c.IsSet("email") {
  117. return fmt.Errorf("Email is not specified")
  118. }
  119. if c.IsSet("config") {
  120. setting.CustomConf = c.String("config")
  121. }
  122. setting.NewContext()
  123. models.LoadConfigs()
  124. models.SetEngine()
  125. if err := models.CreateUser(&models.User{
  126. Name: c.String("name"),
  127. Email: c.String("email"),
  128. Passwd: c.String("password"),
  129. ThemeColor: "#161616",
  130. IsActive: 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. }