answer/internal/cli/install.go

120 lines
3.4 KiB
Go
Raw Normal View History

2022-09-28 16:24:33 +08:00
package cli
import (
"fmt"
"os"
2022-10-12 20:20:26 +08:00
"path/filepath"
2022-09-28 16:24:33 +08:00
"github.com/answerdev/answer/configs"
"github.com/answerdev/answer/i18n"
"github.com/answerdev/answer/pkg/dir"
"github.com/answerdev/answer/pkg/writer"
2022-09-28 16:24:33 +08:00
)
2022-10-12 11:12:04 +08:00
const (
DefaultConfigFileName = "config.yaml"
DefaultCacheFileName = "cache.db"
DefaultReservedUsernamesConfigFileName = "reserved-usernames.json"
)
var (
2022-11-09 15:23:08 +08:00
ConfigFileDir = "/conf/"
UploadFilePath = "/uploads/"
I18nPath = "/i18n/"
2022-11-04 18:36:33 +08:00
CacheDir = "/cache/"
2022-10-12 11:12:04 +08:00
)
2022-09-28 16:24:33 +08:00
2022-11-04 18:36:33 +08:00
// GetConfigFilePath get config file path
func GetConfigFilePath() string {
return filepath.Join(ConfigFileDir, DefaultConfigFileName)
}
func FormatAllPath(dataDirPath string) {
ConfigFileDir = filepath.Join(dataDirPath, ConfigFileDir)
UploadFilePath = filepath.Join(dataDirPath, UploadFilePath)
I18nPath = filepath.Join(dataDirPath, I18nPath)
2022-11-04 18:36:33 +08:00
CacheDir = filepath.Join(dataDirPath, CacheDir)
}
2022-11-04 18:36:33 +08:00
// InstallAllInitialEnvironment install all initial environment
func InstallAllInitialEnvironment(dataDirPath string) {
FormatAllPath(dataDirPath)
2022-10-12 11:12:04 +08:00
installUploadDir()
InstallI18nBundle(false)
2022-10-12 11:12:04 +08:00
fmt.Println("install all initial environment done")
}
2022-09-28 16:24:33 +08:00
2022-11-03 20:09:04 +08:00
func InstallConfigFile(configFilePath string) error {
if len(configFilePath) == 0 {
2022-11-04 18:36:33 +08:00
configFilePath = filepath.Join(ConfigFileDir, DefaultConfigFileName)
2022-11-03 20:09:04 +08:00
}
fmt.Println("[config-file] try to create at ", configFilePath)
2022-10-12 20:20:26 +08:00
// if config file already exists do nothing.
2022-11-03 20:09:04 +08:00
if CheckConfigFile(configFilePath) {
fmt.Printf("[config-file] %s already exists\n", configFilePath)
return nil
2022-09-28 16:24:33 +08:00
}
2022-10-12 20:20:26 +08:00
2022-11-04 18:36:33 +08:00
if err := dir.CreateDirIfNotExist(ConfigFileDir); err != nil {
2022-10-12 20:20:26 +08:00
fmt.Printf("[config-file] create directory fail %s\n", err.Error())
2022-11-03 20:09:04 +08:00
return fmt.Errorf("create directory fail %s", err.Error())
2022-10-12 20:20:26 +08:00
}
2022-11-03 20:09:04 +08:00
fmt.Printf("[config-file] create directory success, config file is %s\n", configFilePath)
2022-10-12 20:20:26 +08:00
if err := writer.WriteFile(configFilePath, string(configs.Config)); err != nil {
2022-10-12 11:12:04 +08:00
fmt.Printf("[config-file] install fail %s\n", err.Error())
2022-11-03 20:09:04 +08:00
return fmt.Errorf("write file failed %s", err)
2022-09-28 16:24:33 +08:00
}
2022-10-12 20:20:26 +08:00
fmt.Printf("[config-file] install success\n")
2022-11-03 20:09:04 +08:00
return nil
2022-10-12 11:12:04 +08:00
}
func installUploadDir() {
fmt.Println("[upload-dir] try to install...")
if err := dir.CreateDirIfNotExist(UploadFilePath); 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, upload directory is %s\n", UploadFilePath)
2022-10-12 11:12:04 +08:00
}
}
func InstallI18nBundle(replace bool) {
2022-10-12 11:12:04 +08:00
fmt.Println("[i18n] try to install i18n bundle...")
if err := dir.CreateDirIfNotExist(I18nPath); 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 {
path := filepath.Join(I18nPath, 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
}
exist := dir.CheckFileExist(path)
if exist && !replace {
continue
}
if exist {
fmt.Printf("[i18n] install %s file exist, try to replace it\n", item.Name())
if err = os.Remove(path); err != nil {
fmt.Println(err)
}
}
2022-10-12 11:12:04 +08:00
fmt.Printf("[i18n] install %s bundle...\n", item.Name())
err = writer.WriteFile(path, string(content))
2022-10-12 11:12:04 +08:00
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
}
}