install.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package commands
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "flag"
  7. "github.com/astaxie/beego"
  8. "github.com/astaxie/beego/orm"
  9. "github.com/mindoc-org/mindoc/conf"
  10. "github.com/mindoc-org/mindoc/models"
  11. "github.com/mindoc-org/mindoc/utils"
  12. )
  13. //系统安装.
  14. func Install() {
  15. fmt.Println("Initializing...")
  16. err := orm.RunSyncdb("default", false, true)
  17. if err == nil {
  18. initialization()
  19. } else {
  20. panic(err.Error())
  21. os.Exit(1)
  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. beego.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. os.Exit(1)
  82. }
  83. member, err := models.NewMember().FindByFieldFirst("account", "admin")
  84. if err == orm.ErrNoRows {
  85. member.Account = "admin"
  86. member.Avatar = conf.URLForWithCdnImage("/static/images/headimgurl.jpg")
  87. member.Password = "123456"
  88. member.AuthMethod = "local"
  89. member.Role = 0
  90. member.Email = "[email protected]"
  91. if err := member.Add(); err != nil {
  92. panic("Member.Add => " + err.Error())
  93. os.Exit(0)
  94. }
  95. book := models.NewBook()
  96. book.MemberId = member.MemberId
  97. book.BookName = "MinDoc演示项目"
  98. book.Status = 0
  99. book.ItemId = 1
  100. book.Description = "这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。"
  101. book.CommentCount = 0
  102. book.PrivatelyOwned = 0
  103. book.CommentStatus = "closed"
  104. book.Identify = "mindoc"
  105. book.DocCount = 0
  106. book.CommentCount = 0
  107. book.Version = time.Now().Unix()
  108. book.Cover = conf.GetDefaultCover()
  109. book.Editor = "markdown"
  110. book.Theme = "default"
  111. if err := book.Insert(); err != nil {
  112. panic("初始化项目失败 -> " + err.Error())
  113. os.Exit(1)
  114. }
  115. }
  116. if !models.NewItemsets().Exist(1) {
  117. item := models.NewItemsets()
  118. item.ItemName = "默认项目空间"
  119. item.MemberId = 1
  120. if err := item.Save(); err != nil {
  121. panic("初始化项目空间失败 -> " + err.Error())
  122. os.Exit(1)
  123. }
  124. }
  125. }