install.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package commands
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/astaxie/beego/orm"
  7. "github.com/lifei6671/mindoc/conf"
  8. "github.com/lifei6671/mindoc/models"
  9. "github.com/lifei6671/mindoc/utils"
  10. )
  11. //系统安装.
  12. func Install() {
  13. fmt.Println("Initializing...")
  14. err := orm.RunSyncdb("default", false, true)
  15. if err == nil {
  16. initialization()
  17. } else {
  18. panic(err.Error())
  19. os.Exit(1)
  20. }
  21. fmt.Println("Install Successfully!")
  22. os.Exit(0)
  23. }
  24. func Version() {
  25. if len(os.Args) >= 2 && os.Args[1] == "version" {
  26. fmt.Println(conf.VERSION)
  27. os.Exit(0)
  28. }
  29. }
  30. //修改用户密码
  31. func ModifyPassword(account,password string) {
  32. if len(os.Args) < 2 {
  33. fmt.Println("Parameter error.")
  34. os.Exit(1)
  35. }
  36. if account == "" {
  37. fmt.Println("Account cannot be empty.")
  38. os.Exit(1)
  39. }
  40. if password == "" {
  41. fmt.Println("Password cannot be empty.")
  42. os.Exit(1)
  43. }
  44. member,err := models.NewMember().FindByAccount(account)
  45. if err != nil {
  46. fmt.Println("Failed to change password:",err)
  47. os.Exit(1)
  48. }
  49. pwd,err := utils.PasswordHash(password)
  50. if err != nil {
  51. fmt.Println("Failed to change password:",err)
  52. os.Exit(1)
  53. }
  54. member.Password = pwd
  55. err = member.Update("password")
  56. if err != nil {
  57. fmt.Println("Failed to change password:",err)
  58. os.Exit(1)
  59. }
  60. fmt.Println("Successfully modified.")
  61. os.Exit(0)
  62. }
  63. //初始化数据
  64. func initialization() {
  65. err := models.NewOption().Init()
  66. if err != nil {
  67. panic(err.Error())
  68. os.Exit(1)
  69. }
  70. member, err := models.NewMember().FindByFieldFirst("account", "admin")
  71. if err == orm.ErrNoRows {
  72. member.Account = "admin"
  73. member.Avatar = "/static/images/headimgurl.jpg"
  74. member.Password = "123456"
  75. member.AuthMethod = "local"
  76. member.Role = 0
  77. member.Email = "[email protected]"
  78. if err := member.Add(); err != nil {
  79. panic("Member.Add => " + err.Error())
  80. os.Exit(0)
  81. }
  82. book := models.NewBook()
  83. book.MemberId = member.MemberId
  84. book.BookName = "MinDoc演示项目"
  85. book.Status = 0
  86. book.Description = "这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。"
  87. book.CommentCount = 0
  88. book.PrivatelyOwned = 0
  89. book.CommentStatus = "closed"
  90. book.Identify = "mindoc"
  91. book.DocCount = 0
  92. book.CommentCount = 0
  93. book.Version = time.Now().Unix()
  94. book.Cover = conf.GetDefaultCover()
  95. book.Editor = "markdown"
  96. book.Theme = "default"
  97. if err := book.Insert(); err != nil {
  98. panic("Book.Insert => " + err.Error())
  99. os.Exit(0)
  100. }
  101. }
  102. group := models.NewMemberGroup()
  103. group.GroupId = 1
  104. group.GroupName = "管理员组"
  105. group.GroupNumber = 1
  106. group.CreateTime = time.Now()
  107. group.CreateAt = 1
  108. group.IsEnableDelete = false
  109. }