admin.go 4.7 KB

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