2022-09-28 16:24:33 +08:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-10-12 11:12:04 +08:00
|
|
|
"bytes"
|
2022-09-28 16:24:33 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-10-12 20:20:26 +08:00
|
|
|
"path/filepath"
|
2022-09-28 16:24:33 +08:00
|
|
|
|
2022-10-12 11:12:04 +08:00
|
|
|
"github.com/segmentfault/answer/assets"
|
2022-09-28 16:24:33 +08:00
|
|
|
"github.com/segmentfault/answer/configs"
|
2022-09-28 16:41:51 +08:00
|
|
|
"github.com/segmentfault/answer/i18n"
|
2022-10-12 11:12:04 +08:00
|
|
|
"github.com/segmentfault/answer/internal/base/data"
|
|
|
|
"github.com/segmentfault/answer/internal/entity"
|
2022-09-28 16:24:33 +08:00
|
|
|
"github.com/segmentfault/answer/pkg/dir"
|
|
|
|
)
|
|
|
|
|
2022-10-12 11:12:04 +08:00
|
|
|
const (
|
2022-10-12 20:20:26 +08:00
|
|
|
defaultConfigFilePath = "/data/conf/"
|
|
|
|
defaultUploadFilePath = "/data/upfiles/"
|
|
|
|
defaultI18nPath = "/data/i18n/"
|
2022-10-12 11:12:04 +08:00
|
|
|
)
|
2022-09-28 16:24:33 +08:00
|
|
|
|
2022-10-12 11:12:04 +08:00
|
|
|
// InstallAllInitialEnvironment install all initial environment
|
|
|
|
func InstallAllInitialEnvironment() {
|
|
|
|
installConfigFile()
|
|
|
|
installUploadDir()
|
|
|
|
installI18nBundle()
|
|
|
|
fmt.Println("install all initial environment done")
|
|
|
|
return
|
|
|
|
}
|
2022-09-28 16:24:33 +08:00
|
|
|
|
2022-10-12 11:12:04 +08:00
|
|
|
func installConfigFile() {
|
|
|
|
fmt.Println("[config-file] try to install...")
|
2022-10-12 20:20:26 +08:00
|
|
|
defaultConfigFile := filepath.Join(defaultConfigFilePath, "config.yaml")
|
|
|
|
|
|
|
|
// if config file already exists do nothing.
|
|
|
|
if CheckConfigFile(defaultConfigFile) {
|
2022-10-12 11:12:04 +08:00
|
|
|
fmt.Println("[config-file] already exists")
|
|
|
|
return
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|
2022-10-12 20:20:26 +08:00
|
|
|
|
|
|
|
if _, err := dir.CreatePathIsNotExist(defaultConfigFilePath); err != nil {
|
|
|
|
fmt.Printf("[config-file] create directory fail %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("[config-file] create directory success\n")
|
|
|
|
|
|
|
|
if err := WriterFile(defaultConfigFile, string(configs.Config)); err != nil {
|
2022-10-12 11:12:04 +08:00
|
|
|
fmt.Printf("[config-file] install fail %s\n", err.Error())
|
2022-10-12 20:20:26 +08:00
|
|
|
return
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|
2022-10-12 20:20:26 +08:00
|
|
|
fmt.Printf("[config-file] install success\n")
|
2022-10-12 11:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func installUploadDir() {
|
|
|
|
fmt.Println("[upload-dir] try to install...")
|
2022-10-12 11:49:28 +08:00
|
|
|
if _, err := dir.CreatePathIsNotExist(defaultUploadFilePath); err != nil {
|
2022-10-12 11:12:04 +08:00
|
|
|
fmt.Printf("[upload-dir] install fail %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf("[upload-dir] install success\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func installI18nBundle() {
|
|
|
|
fmt.Println("[i18n] try to install i18n bundle...")
|
2022-10-12 11:49:28 +08:00
|
|
|
if _, err := dir.CreatePathIsNotExist(defaultI18nPath); err != nil {
|
2022-09-28 16:24:33 +08:00
|
|
|
fmt.Println(err.Error())
|
2022-10-12 11:12:04 +08:00
|
|
|
return
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
|
2022-09-28 16:41:51 +08:00
|
|
|
i18nList, err := i18n.I18n.ReadDir(".")
|
2022-09-28 16:24:33 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2022-10-12 11:12:04 +08:00
|
|
|
return
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
fmt.Printf("[i18n] find i18n bundle %d\n", len(i18nList))
|
2022-09-28 16:24:33 +08:00
|
|
|
for _, item := range i18nList {
|
2022-10-12 20:20:26 +08:00
|
|
|
path := filepath.Join(defaultI18nPath, item.Name())
|
2022-09-28 16:41:51 +08:00
|
|
|
content, err := i18n.I18n.ReadFile(item.Name())
|
2022-09-28 16:24:33 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
fmt.Printf("[i18n] install %s bundle...\n", item.Name())
|
|
|
|
err = WriterFile(path, string(content))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[i18n] install %s bundle fail: %s\n", item.Name(), err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf("[i18n] install %s bundle success\n", item.Name())
|
|
|
|
}
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriterFile(filePath, content string) error {
|
|
|
|
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-12 11:49:28 +08:00
|
|
|
defer func() {
|
|
|
|
_ = file.Close()
|
|
|
|
}()
|
2022-10-12 11:12:04 +08:00
|
|
|
writer := bufio.NewWriter(file)
|
|
|
|
if _, err := writer.WriteString(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writer.Flush(); err != nil {
|
2022-09-28 16:24:33 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-12 11:12:04 +08:00
|
|
|
// InitDB init db
|
|
|
|
func InitDB(dataConf *data.Database) (err error) {
|
|
|
|
fmt.Println("[database] try to initialize database")
|
|
|
|
db, err := data.NewDB(false, dataConf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// check db connection
|
|
|
|
if err = db.Ping(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("[database] connect success")
|
|
|
|
|
|
|
|
exist, err := db.IsTableExist(&entity.User{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
if exist {
|
|
|
|
fmt.Println("[database] already exists")
|
|
|
|
return nil
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
|
|
|
|
// create table if not exist
|
|
|
|
s := &bytes.Buffer{}
|
|
|
|
s.Write(assets.AnswerSql)
|
|
|
|
_, err = db.Import(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("[database] execute sql successfully")
|
|
|
|
return nil
|
2022-09-28 16:24:33 +08:00
|
|
|
}
|