install.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package commands
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "time"
  7. "flag"
  8. "github.com/beego/beego/v2/client/orm"
  9. "github.com/beego/beego/v2/core/logs"
  10. "github.com/mindoc-org/mindoc/conf"
  11. "github.com/mindoc-org/mindoc/models"
  12. "github.com/mindoc-org/mindoc/utils"
  13. )
  14. //系统安装.
  15. func Install() {
  16. fmt.Println("Initializing...")
  17. err := orm.RunSyncdb("default", false, true)
  18. if err == nil {
  19. initialization()
  20. } else {
  21. panic(err.Error())
  22. }
  23. fmt.Println("Install Successfully!")
  24. os.Exit(0)
  25. }
  26. func Version() {
  27. if len(os.Args) >= 2 && os.Args[1] == "version" {
  28. fmt.Println(conf.VERSION)
  29. os.Exit(0)
  30. }
  31. }
  32. //修改用户密码
  33. func ModifyPassword() {
  34. var account, password string
  35. //账号和密码需要解析参数后才能获取
  36. if len(os.Args) >= 2 && os.Args[1] == "password" {
  37. flagSet := flag.NewFlagSet("MinDoc command: ", flag.ExitOnError)
  38. flagSet.StringVar(&account, "account", "", "用户账号.")
  39. flagSet.StringVar(&password, "password", "", "用户密码.")
  40. if err := flagSet.Parse(os.Args[2:]); err != nil {
  41. logs.Error("解析参数失败 -> ", err)
  42. os.Exit(1)
  43. }
  44. if len(os.Args) < 2 {
  45. fmt.Println("Parameter error.")
  46. os.Exit(1)
  47. }
  48. if account == "" {
  49. fmt.Println("Account cannot be empty.")
  50. os.Exit(1)
  51. }
  52. if password == "" {
  53. fmt.Println("Password cannot be empty.")
  54. os.Exit(1)
  55. }
  56. member, err := models.NewMember().FindByAccount(account)
  57. if err != nil {
  58. fmt.Println("Failed to change password:", err)
  59. os.Exit(1)
  60. }
  61. pwd, err := utils.PasswordHash(password)
  62. if err != nil {
  63. fmt.Println("Failed to change password:", err)
  64. os.Exit(1)
  65. }
  66. member.Password = pwd
  67. err = member.Update("password")
  68. if err != nil {
  69. fmt.Println("Failed to change password:", err)
  70. os.Exit(1)
  71. }
  72. fmt.Println("Successfully modified.")
  73. os.Exit(0)
  74. }
  75. }
  76. //初始化数据
  77. func initialization() {
  78. err := models.NewOption().Init()
  79. if err != nil {
  80. panic(err.Error())
  81. }
  82. member, err := models.NewMember().FindByFieldFirst("account", "admin")
  83. if errors.Is(err, orm.ErrNoRows) {
  84. // create admin user
  85. logs.Info("creating admin user")
  86. member.Account = "admin"
  87. member.Avatar = conf.URLForWithCdnImage("/static/images/headimgurl.jpg")
  88. member.Password = "123456"
  89. member.AuthMethod = "local"
  90. member.Role = conf.MemberSuperRole
  91. member.Email = "[email protected]"
  92. if err := member.Add(); err != nil {
  93. panic("Member.Add => " + err.Error())
  94. }
  95. // create demo book
  96. logs.Info("creating demo book")
  97. book := models.NewBook()
  98. book.MemberId = member.MemberId
  99. book.BookName = "MinDoc演示项目"
  100. book.Status = 0
  101. book.ItemId = 1
  102. book.Description = "这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。"
  103. book.CommentCount = 0
  104. book.PrivatelyOwned = 0
  105. book.CommentStatus = "closed"
  106. book.Identify = "mindoc"
  107. book.DocCount = 0
  108. book.CommentCount = 0
  109. book.Version = time.Now().Unix()
  110. book.Cover = conf.GetDefaultCover()
  111. book.Editor = "markdown"
  112. book.Theme = "default"
  113. if err := book.Insert(); err != nil {
  114. panic("初始化项目失败 -> " + err.Error())
  115. }
  116. } else if err != nil {
  117. panic(fmt.Errorf("occur errors when initialize: %s", err))
  118. }
  119. if !models.NewItemsets().Exist(1) {
  120. item := models.NewItemsets()
  121. item.ItemName = "默认项目空间"
  122. item.MemberId = 1
  123. if err := item.Save(); err != nil {
  124. panic("初始化项目空间失败 -> " + err.Error())
  125. }
  126. }
  127. }