mirror of https://gitee.com/answerdev/answer.git
Merge branch 'feat/assign-install-path' into 'main'
feat: support specify the installation directory See merge request opensource/answer!58
This commit is contained in:
commit
11054a36e3
|
@ -9,15 +9,20 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
// confFlag is the config flag.
|
||||
confFlag string
|
||||
// configFilePath is the config file path
|
||||
configFilePath string
|
||||
// dataDirPath save all answer application data in this directory. like config file, upload file...
|
||||
dataDirPath string
|
||||
// dumpDataPath dump data path
|
||||
dumpDataPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.Version = Version
|
||||
runCmd.Flags().StringVarP(&confFlag, "config", "c", "/data/conf/config.yaml", "config path, eg: -c config.yaml")
|
||||
|
||||
initCmd.Flags().StringVarP(&dataDirPath, "data-path", "C", "/data/", "data path, eg: -C ./data/")
|
||||
|
||||
runCmd.Flags().StringVarP(&configFilePath, "config", "c", "", "config path, eg: -c config.yaml")
|
||||
|
||||
dumpCmd.Flags().StringVarP(&dumpDataPath, "path", "p", "./", "dump data path, eg: -p ./dump/data/")
|
||||
|
||||
|
@ -55,7 +60,7 @@ To run answer, use:
|
|||
Short: "init answer application",
|
||||
Long: `init answer application`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cli.InstallAllInitialEnvironment()
|
||||
cli.InstallAllInitialEnvironment(dataDirPath)
|
||||
c, err := readConfig()
|
||||
if err != nil {
|
||||
fmt.Println("read config failed: ", err.Error())
|
||||
|
@ -108,7 +113,7 @@ To run answer, use:
|
|||
Long: `Check if the current environment meets the startup requirements`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Start checking the required environment...")
|
||||
if cli.CheckConfigFile(confFlag) {
|
||||
if cli.CheckConfigFile(configFilePath) {
|
||||
fmt.Println("config file exists [✔]")
|
||||
} else {
|
||||
fmt.Println("config file not exists [x]")
|
||||
|
|
|
@ -2,9 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/segmentfault/answer/internal/base/conf"
|
||||
"github.com/segmentfault/answer/internal/cli"
|
||||
"github.com/segmentfault/pacman"
|
||||
"github.com/segmentfault/pacman/contrib/conf/viper"
|
||||
"github.com/segmentfault/pacman/contrib/log/zap"
|
||||
|
@ -52,8 +54,11 @@ func runApp() {
|
|||
}
|
||||
|
||||
func readConfig() (c *conf.AllConfig, err error) {
|
||||
if len(configFilePath) == 0 {
|
||||
configFilePath = filepath.Join(cli.ConfigFilePath, cli.DefaultConfigFileName)
|
||||
}
|
||||
c = &conf.AllConfig{}
|
||||
config, err := viper.NewWithPath(confFlag)
|
||||
config, err := viper.NewWithPath(configFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -16,5 +16,5 @@ swaggerui:
|
|||
address: ':80'
|
||||
service_config:
|
||||
secret_key: "answer"
|
||||
web_host: "http://127.0.0.1"
|
||||
web_host: "http://127.0.0.1:9080"
|
||||
upload_path: "/data/upfiles"
|
||||
|
|
|
@ -11,7 +11,7 @@ services:
|
|||
links:
|
||||
- db
|
||||
volumes:
|
||||
- answer-data:/data
|
||||
- ./answer/data:/data
|
||||
db:
|
||||
image: mariadb:10.4.7
|
||||
ports:
|
||||
|
@ -25,4 +25,4 @@ services:
|
|||
timeout: 20s
|
||||
retries: 10
|
||||
volumes:
|
||||
answer-data:
|
||||
- ./answer/mysql:/var/lib/mysql
|
||||
|
|
|
@ -16,13 +16,21 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
defaultConfigFilePath = "/data/conf/"
|
||||
defaultUploadFilePath = "/data/upfiles/"
|
||||
defaultI18nPath = "/data/i18n/"
|
||||
DefaultConfigFileName = "config.yaml"
|
||||
)
|
||||
|
||||
var (
|
||||
ConfigFilePath = "/conf/"
|
||||
UploadFilePath = "/upfiles/"
|
||||
I18nPath = "/i18n/"
|
||||
)
|
||||
|
||||
// InstallAllInitialEnvironment install all initial environment
|
||||
func InstallAllInitialEnvironment() {
|
||||
func InstallAllInitialEnvironment(dataDirPath string) {
|
||||
ConfigFilePath = filepath.Join(dataDirPath, ConfigFilePath)
|
||||
UploadFilePath = filepath.Join(dataDirPath, UploadFilePath)
|
||||
I18nPath = filepath.Join(dataDirPath, I18nPath)
|
||||
|
||||
installConfigFile()
|
||||
installUploadDir()
|
||||
installI18nBundle()
|
||||
|
@ -32,21 +40,21 @@ func InstallAllInitialEnvironment() {
|
|||
|
||||
func installConfigFile() {
|
||||
fmt.Println("[config-file] try to install...")
|
||||
defaultConfigFile := filepath.Join(defaultConfigFilePath, "config.yaml")
|
||||
defaultConfigFile := filepath.Join(ConfigFilePath, DefaultConfigFileName)
|
||||
|
||||
// if config file already exists do nothing.
|
||||
if CheckConfigFile(defaultConfigFile) {
|
||||
fmt.Println("[config-file] already exists")
|
||||
fmt.Printf("[config-file] %s already exists\n", defaultConfigFile)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := dir.CreatePathIsNotExist(defaultConfigFilePath); err != nil {
|
||||
if _, err := dir.CreatePathIsNotExist(ConfigFilePath); err != nil {
|
||||
fmt.Printf("[config-file] create directory fail %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Printf("[config-file] create directory success\n")
|
||||
fmt.Printf("[config-file] create directory success, config file is %s\n", defaultConfigFile)
|
||||
|
||||
if err := WriterFile(defaultConfigFile, string(configs.Config)); err != nil {
|
||||
if err := writerFile(defaultConfigFile, string(configs.Config)); err != nil {
|
||||
fmt.Printf("[config-file] install fail %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
@ -55,16 +63,16 @@ func installConfigFile() {
|
|||
|
||||
func installUploadDir() {
|
||||
fmt.Println("[upload-dir] try to install...")
|
||||
if _, err := dir.CreatePathIsNotExist(defaultUploadFilePath); err != nil {
|
||||
if _, err := dir.CreatePathIsNotExist(UploadFilePath); err != nil {
|
||||
fmt.Printf("[upload-dir] install fail %s\n", err.Error())
|
||||
} else {
|
||||
fmt.Printf("[upload-dir] install success\n")
|
||||
fmt.Printf("[upload-dir] install success, upload directory is %s\n", UploadFilePath)
|
||||
}
|
||||
}
|
||||
|
||||
func installI18nBundle() {
|
||||
fmt.Println("[i18n] try to install i18n bundle...")
|
||||
if _, err := dir.CreatePathIsNotExist(defaultI18nPath); err != nil {
|
||||
if _, err := dir.CreatePathIsNotExist(I18nPath); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
@ -76,13 +84,13 @@ func installI18nBundle() {
|
|||
}
|
||||
fmt.Printf("[i18n] find i18n bundle %d\n", len(i18nList))
|
||||
for _, item := range i18nList {
|
||||
path := filepath.Join(defaultI18nPath, item.Name())
|
||||
path := filepath.Join(I18nPath, item.Name())
|
||||
content, err := i18n.I18n.ReadFile(item.Name())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("[i18n] install %s bundle...\n", item.Name())
|
||||
err = WriterFile(path, string(content))
|
||||
err = writerFile(path, string(content))
|
||||
if err != nil {
|
||||
fmt.Printf("[i18n] install %s bundle fail: %s\n", item.Name(), err.Error())
|
||||
} else {
|
||||
|
@ -91,7 +99,7 @@ func installI18nBundle() {
|
|||
}
|
||||
}
|
||||
|
||||
func WriterFile(filePath, content string) error {
|
||||
func writerFile(filePath, content string) error {
|
||||
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -10,7 +10,7 @@ func CheckConfigFile(configPath string) bool {
|
|||
}
|
||||
|
||||
func CheckUploadDir() bool {
|
||||
return dir.CheckPathExist(defaultUploadFilePath)
|
||||
return dir.CheckPathExist(UploadFilePath)
|
||||
}
|
||||
|
||||
func CheckDB(dataConf *data.Database) bool {
|
||||
|
|
Loading…
Reference in New Issue