admin.go 5.0 KB

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