2017-05-11 13:39:34 +08:00
|
|
|
|
package commands
|
|
|
|
|
|
|
|
|
|
import (
|
2021-03-25 15:32:44 +08:00
|
|
|
|
"errors"
|
2017-05-11 13:39:34 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
2018-07-10 19:13:33 +08:00
|
|
|
|
"flag"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
|
2021-03-25 15:32:44 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-24 00:28:13 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-04-21 22:00:03 +08:00
|
|
|
|
"github.com/beego/beego/v2/server/web"
|
|
|
|
|
"github.com/beego/i18n"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
|
|
|
|
"github.com/mindoc-org/mindoc/utils"
|
2017-05-11 13:39:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//系统安装.
|
|
|
|
|
func Install() {
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
|
|
|
|
fmt.Println("Initializing...")
|
|
|
|
|
|
|
|
|
|
err := orm.RunSyncdb("default", false, true)
|
|
|
|
|
if err == nil {
|
|
|
|
|
initialization()
|
|
|
|
|
} else {
|
|
|
|
|
panic(err.Error())
|
2017-05-11 13:39:34 +08:00
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
fmt.Println("Install Successfully!")
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-31 15:26:51 +08:00
|
|
|
|
func Version() {
|
2017-05-30 20:46:00 +08:00
|
|
|
|
if len(os.Args) >= 2 && os.Args[1] == "version" {
|
|
|
|
|
fmt.Println(conf.VERSION)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 19:13:33 +08:00
|
|
|
|
//修改用户密码
|
|
|
|
|
func ModifyPassword() {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
var account, password string
|
2018-07-10 19:13:33 +08:00
|
|
|
|
|
|
|
|
|
//账号和密码需要解析参数后才能获取
|
|
|
|
|
if len(os.Args) >= 2 && os.Args[1] == "password" {
|
|
|
|
|
flagSet := flag.NewFlagSet("MinDoc command: ", flag.ExitOnError)
|
|
|
|
|
|
|
|
|
|
flagSet.StringVar(&account, "account", "", "用户账号.")
|
|
|
|
|
flagSet.StringVar(&password, "password", "", "用户密码.")
|
|
|
|
|
|
|
|
|
|
if err := flagSet.Parse(os.Args[2:]); err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("解析参数失败 -> ", err)
|
2018-07-10 19:13:33 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
|
fmt.Println("Parameter error.")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if account == "" {
|
|
|
|
|
fmt.Println("Account cannot be empty.")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
if password == "" {
|
|
|
|
|
fmt.Println("Password cannot be empty.")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
member, err := models.NewMember().FindByAccount(account)
|
2018-07-10 19:13:33 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
fmt.Println("Failed to change password:", err)
|
2018-07-10 19:13:33 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
pwd, err := utils.PasswordHash(password)
|
2018-07-10 19:13:33 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
fmt.Println("Failed to change password:", err)
|
2018-07-10 19:13:33 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
member.Password = pwd
|
|
|
|
|
|
|
|
|
|
err = member.Update("password")
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
fmt.Println("Failed to change password:", err)
|
2018-07-10 19:13:33 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Successfully modified.")
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
|
//初始化数据
|
|
|
|
|
func initialization() {
|
|
|
|
|
|
2017-05-31 15:26:51 +08:00
|
|
|
|
err := models.NewOption().Init()
|
2017-05-11 13:39:34 +08:00
|
|
|
|
if err != nil {
|
2017-05-25 15:19:17 +08:00
|
|
|
|
panic(err.Error())
|
2017-05-11 13:39:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 22:00:03 +08:00
|
|
|
|
lang, _ := web.AppConfig.String("default_lang")
|
|
|
|
|
err = i18n.SetMessage(lang, "conf/lang/"+lang+".ini")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("initialize locale error: %s", err))
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
|
member, err := models.NewMember().FindByFieldFirst("account", "admin")
|
2021-03-25 15:32:44 +08:00
|
|
|
|
if errors.Is(err, orm.ErrNoRows) {
|
2017-05-11 13:39:34 +08:00
|
|
|
|
|
2021-03-25 11:36:06 +08:00
|
|
|
|
// create admin user
|
|
|
|
|
logs.Info("creating admin user")
|
2017-05-11 13:39:34 +08:00
|
|
|
|
member.Account = "admin"
|
2018-07-10 19:13:33 +08:00
|
|
|
|
member.Avatar = conf.URLForWithCdnImage("/static/images/headimgurl.jpg")
|
2017-05-11 13:39:34 +08:00
|
|
|
|
member.Password = "123456"
|
2017-05-24 20:31:58 +08:00
|
|
|
|
member.AuthMethod = "local"
|
2021-03-25 10:42:18 +08:00
|
|
|
|
member.Role = conf.MemberSuperRole
|
2017-05-11 13:39:34 +08:00
|
|
|
|
member.Email = "admin@iminho.me"
|
|
|
|
|
|
|
|
|
|
if err := member.Add(); err != nil {
|
|
|
|
|
panic("Member.Add => " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 11:36:06 +08:00
|
|
|
|
// create demo book
|
|
|
|
|
logs.Info("creating demo book")
|
2017-05-11 13:39:34 +08:00
|
|
|
|
book := models.NewBook()
|
|
|
|
|
|
|
|
|
|
book.MemberId = member.MemberId
|
2021-04-21 22:00:03 +08:00
|
|
|
|
book.BookName = i18n.Tr(lang, "init.default_proj_name") //"MinDoc演示项目"
|
2017-05-11 13:39:34 +08:00
|
|
|
|
book.Status = 0
|
2018-11-30 10:03:50 +08:00
|
|
|
|
book.ItemId = 1
|
2021-04-21 22:00:03 +08:00
|
|
|
|
book.Description = i18n.Tr(lang, "init.default_proj_desc") //"这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。"
|
2017-05-11 13:39:34 +08:00
|
|
|
|
book.CommentCount = 0
|
|
|
|
|
book.PrivatelyOwned = 0
|
|
|
|
|
book.CommentStatus = "closed"
|
|
|
|
|
book.Identify = "mindoc"
|
|
|
|
|
book.DocCount = 0
|
|
|
|
|
book.CommentCount = 0
|
|
|
|
|
book.Version = time.Now().Unix()
|
|
|
|
|
book.Cover = conf.GetDefaultCover()
|
|
|
|
|
book.Editor = "markdown"
|
|
|
|
|
book.Theme = "default"
|
|
|
|
|
|
2021-04-21 22:00:03 +08:00
|
|
|
|
if err := book.Insert(lang); err != nil {
|
2018-11-20 20:36:14 +08:00
|
|
|
|
panic("初始化项目失败 -> " + err.Error())
|
|
|
|
|
}
|
2021-03-25 15:32:44 +08:00
|
|
|
|
} else if err != nil {
|
|
|
|
|
panic(fmt.Errorf("occur errors when initialize: %s", err))
|
2018-11-20 20:36:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !models.NewItemsets().Exist(1) {
|
|
|
|
|
item := models.NewItemsets()
|
2021-04-21 22:00:03 +08:00
|
|
|
|
item.ItemName = i18n.Tr(lang, "init.default_proj_space") //"默认项目空间"
|
2018-11-20 20:36:14 +08:00
|
|
|
|
item.MemberId = 1
|
|
|
|
|
if err := item.Save(); err != nil {
|
2018-11-21 11:03:16 +08:00
|
|
|
|
panic("初始化项目空间失败 -> " + err.Error())
|
2017-05-11 13:39:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|