-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
install.go
162 lines (134 loc) · 3.72 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package commands
import (
"errors"
"fmt"
"os"
"time"
"flag"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/beego/i18n"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/models"
"github.com/mindoc-org/mindoc/utils"
)
//系统安装.
func Install() {
fmt.Println("Initializing...")
err := orm.RunSyncdb("default", false, true)
if err == nil {
initialization()
} else {
panic(err.Error())
}
fmt.Println("Install Successfully!")
os.Exit(0)
}
func Version() {
if len(os.Args) >= 2 && os.Args[1] == "version" {
fmt.Println(conf.VERSION)
os.Exit(0)
}
}
//修改用户密码
func ModifyPassword() {
var account, password string
//账号和密码需要解析参数后才能获取
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 {
logs.Error("解析参数失败 -> ", err)
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)
}
member, err := models.NewMember().FindByAccount(account)
if err != nil {
fmt.Println("Failed to change password:", err)
os.Exit(1)
}
pwd, err := utils.PasswordHash(password)
if err != nil {
fmt.Println("Failed to change password:", err)
os.Exit(1)
}
member.Password = pwd
err = member.Update("password")
if err != nil {
fmt.Println("Failed to change password:", err)
os.Exit(1)
}
fmt.Println("Successfully modified.")
os.Exit(0)
}
}
//初始化数据
func initialization() {
err := models.NewOption().Init()
if err != nil {
panic(err.Error())
}
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))
}
member, err := models.NewMember().FindByFieldFirst("account", "admin")
if errors.Is(err, orm.ErrNoRows) {
// create admin user
logs.Info("creating admin user")
member.Account = "admin"
member.Avatar = conf.URLForWithCdnImage("/static/images/headimgurl.jpg")
member.Password = "123456"
member.AuthMethod = "local"
member.Role = conf.MemberSuperRole
member.Email = "admin@iminho.me"
if err := member.Add(); err != nil {
panic("Member.Add => " + err.Error())
}
// create demo book
logs.Info("creating demo book")
book := models.NewBook()
book.MemberId = member.MemberId
book.BookName = i18n.Tr(lang, "init.default_proj_name") //"MinDoc演示项目"
book.Status = 0
book.ItemId = 1
book.Description = i18n.Tr(lang, "init.default_proj_desc") //"这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。"
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"
if err := book.Insert(lang); err != nil {
panic("初始化项目失败 -> " + err.Error())
}
} else if err != nil {
panic(fmt.Errorf("occur errors when initialize: %s", err))
}
if !models.NewItemsets().Exist(1) {
item := models.NewItemsets()
item.ItemName = i18n.Tr(lang, "init.default_proj_space") //"默认项目空间"
item.MemberId = 1
if err := item.Save(); err != nil {
panic("初始化项目空间失败 -> " + err.Error())
}
}
}