admin.go 4.6 KB

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