mirror of https://gitee.com/answerdev/answer.git
feat: remove config file path
This commit is contained in:
parent
3eb193dffd
commit
84106a99f5
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/conf"
|
"github.com/answerdev/answer/internal/base/conf"
|
||||||
"github.com/answerdev/answer/internal/cli"
|
"github.com/answerdev/answer/internal/cli"
|
||||||
|
@ -13,8 +12,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// configFilePath is the config file path
|
|
||||||
configFilePath string
|
|
||||||
// dataDirPath save all answer application data in this directory. like config file, upload file...
|
// dataDirPath save all answer application data in this directory. like config file, upload file...
|
||||||
dataDirPath string
|
dataDirPath string
|
||||||
// dumpDataPath dump data path
|
// dumpDataPath dump data path
|
||||||
|
@ -24,9 +21,7 @@ var (
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.Version = fmt.Sprintf("%s\nrevision: %s\nbuild time: %s", Version, Revision, Time)
|
rootCmd.Version = fmt.Sprintf("%s\nrevision: %s\nbuild time: %s", Version, Revision, Time)
|
||||||
|
|
||||||
initCmd.Flags().StringVarP(&dataDirPath, "data-path", "C", "/data/", "data path, eg: -C ./data/")
|
rootCmd.PersistentFlags().StringVarP(&dataDirPath, "data-path", "C", "/data/", "data path, eg: -C ./data/")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&configFilePath, "config", "c", "", "config path, eg: -c config.yaml")
|
|
||||||
|
|
||||||
dumpCmd.Flags().StringVarP(&dumpDataPath, "path", "p", "./", "dump data path, eg: -p ./dump/data/")
|
dumpCmd.Flags().StringVarP(&dumpDataPath, "path", "p", "./", "dump data path, eg: -p ./dump/data/")
|
||||||
|
|
||||||
|
@ -52,6 +47,8 @@ To run answer, use:
|
||||||
Short: "Run the application",
|
Short: "Run the application",
|
||||||
Long: `Run the application`,
|
Long: `Run the application`,
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
Run: func(_ *cobra.Command, _ []string) {
|
||||||
|
cli.FormatAllPath(dataDirPath)
|
||||||
|
fmt.Println("config file path: ", cli.GetConfigFilePath())
|
||||||
fmt.Println("Answer is string..........................")
|
fmt.Println("Answer is string..........................")
|
||||||
runApp()
|
runApp()
|
||||||
},
|
},
|
||||||
|
@ -65,15 +62,11 @@ To run answer, use:
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
Run: func(_ *cobra.Command, _ []string) {
|
||||||
// check config file and database. if config file exists and database is already created, init done
|
// check config file and database. if config file exists and database is already created, init done
|
||||||
cli.InstallAllInitialEnvironment(dataDirPath)
|
cli.InstallAllInitialEnvironment(dataDirPath)
|
||||||
// set default config file path
|
|
||||||
if len(configFilePath) == 0 {
|
|
||||||
configFilePath = filepath.Join(cli.ConfigFilePath, cli.DefaultConfigFileName)
|
|
||||||
}
|
|
||||||
|
|
||||||
configFileExist := cli.CheckConfigFile(configFilePath)
|
configFileExist := cli.CheckConfigFile(cli.GetConfigFilePath())
|
||||||
if configFileExist {
|
if configFileExist {
|
||||||
fmt.Println("config file exists, try to read the config...")
|
fmt.Println("config file exists, try to read the config...")
|
||||||
c, err := conf.ReadConfig(configFilePath)
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("read config failed: ", err.Error())
|
fmt.Println("read config failed: ", err.Error())
|
||||||
return
|
return
|
||||||
|
@ -87,7 +80,7 @@ To run answer, use:
|
||||||
}
|
}
|
||||||
|
|
||||||
// start installation server to install
|
// start installation server to install
|
||||||
install.Run(configFilePath)
|
install.Run(cli.GetConfigFilePath())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +90,8 @@ To run answer, use:
|
||||||
Short: "upgrade Answer version",
|
Short: "upgrade Answer version",
|
||||||
Long: `upgrade Answer version`,
|
Long: `upgrade Answer version`,
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
Run: func(_ *cobra.Command, _ []string) {
|
||||||
c, err := conf.ReadConfig(configFilePath)
|
cli.FormatAllPath(dataDirPath)
|
||||||
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("read config failed: ", err.Error())
|
fmt.Println("read config failed: ", err.Error())
|
||||||
return
|
return
|
||||||
|
@ -117,7 +111,8 @@ To run answer, use:
|
||||||
Long: `back up data`,
|
Long: `back up data`,
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
Run: func(_ *cobra.Command, _ []string) {
|
||||||
fmt.Println("Answer is backing up data")
|
fmt.Println("Answer is backing up data")
|
||||||
c, err := conf.ReadConfig(configFilePath)
|
cli.FormatAllPath(dataDirPath)
|
||||||
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("read config failed: ", err.Error())
|
fmt.Println("read config failed: ", err.Error())
|
||||||
return
|
return
|
||||||
|
@ -137,8 +132,9 @@ To run answer, use:
|
||||||
Short: "checking the required environment",
|
Short: "checking the required environment",
|
||||||
Long: `Check if the current environment meets the startup requirements`,
|
Long: `Check if the current environment meets the startup requirements`,
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
Run: func(_ *cobra.Command, _ []string) {
|
||||||
|
cli.FormatAllPath(dataDirPath)
|
||||||
fmt.Println("Start checking the required environment...")
|
fmt.Println("Start checking the required environment...")
|
||||||
if cli.CheckConfigFile(configFilePath) {
|
if cli.CheckConfigFile(cli.GetConfigFilePath()) {
|
||||||
fmt.Println("config file exists [✔]")
|
fmt.Println("config file exists [✔]")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("config file not exists [x]")
|
fmt.Println("config file not exists [x]")
|
||||||
|
@ -150,7 +146,7 @@ To run answer, use:
|
||||||
fmt.Println("upload directory not exists [x]")
|
fmt.Println("upload directory not exists [x]")
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := conf.ReadConfig(configFilePath)
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("read config failed: ", err.Error())
|
fmt.Println("read config failed: ", err.Error())
|
||||||
return
|
return
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/conf"
|
"github.com/answerdev/answer/internal/base/conf"
|
||||||
|
"github.com/answerdev/answer/internal/cli"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/segmentfault/pacman"
|
"github.com/segmentfault/pacman"
|
||||||
"github.com/segmentfault/pacman/contrib/log/zap"
|
"github.com/segmentfault/pacman/contrib/log/zap"
|
||||||
|
@ -38,7 +39,7 @@ func runApp() {
|
||||||
log.SetLogger(zap.NewLogger(
|
log.SetLogger(zap.NewLogger(
|
||||||
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
|
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
|
||||||
|
|
||||||
c, err := conf.ReadConfig(configFilePath)
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ type Data struct {
|
||||||
// ReadConfig read config
|
// ReadConfig read config
|
||||||
func ReadConfig(configFilePath string) (c *AllConfig, err error) {
|
func ReadConfig(configFilePath string) (c *AllConfig, err error) {
|
||||||
if len(configFilePath) == 0 {
|
if len(configFilePath) == 0 {
|
||||||
configFilePath = filepath.Join(cli.ConfigFilePath, cli.DefaultConfigFileName)
|
configFilePath = filepath.Join(cli.ConfigFileDir, cli.DefaultConfigFileName)
|
||||||
}
|
}
|
||||||
c = &AllConfig{}
|
c = &AllConfig{}
|
||||||
config, err := viper.NewWithPath(configFilePath)
|
config, err := viper.NewWithPath(configFilePath)
|
||||||
|
|
|
@ -16,19 +16,27 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ConfigFilePath = "/conf/"
|
ConfigFileDir = "/conf/"
|
||||||
UploadFilePath = "/upfiles/"
|
UploadFilePath = "/upfiles/"
|
||||||
I18nPath = "/i18n/"
|
I18nPath = "/i18n/"
|
||||||
CachePath = "/cache/"
|
CacheDir = "/cache/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
CacheDir = filepath.Join(dataDirPath, CacheDir)
|
||||||
|
}
|
||||||
|
|
||||||
// InstallAllInitialEnvironment install all initial environment
|
// InstallAllInitialEnvironment install all initial environment
|
||||||
func InstallAllInitialEnvironment(dataDirPath string) {
|
func InstallAllInitialEnvironment(dataDirPath string) {
|
||||||
ConfigFilePath = filepath.Join(dataDirPath, ConfigFilePath)
|
FormatAllPath(dataDirPath)
|
||||||
UploadFilePath = filepath.Join(dataDirPath, UploadFilePath)
|
|
||||||
I18nPath = filepath.Join(dataDirPath, I18nPath)
|
|
||||||
CachePath = filepath.Join(dataDirPath, CachePath)
|
|
||||||
|
|
||||||
installUploadDir()
|
installUploadDir()
|
||||||
installI18nBundle()
|
installI18nBundle()
|
||||||
fmt.Println("install all initial environment done")
|
fmt.Println("install all initial environment done")
|
||||||
|
@ -36,7 +44,7 @@ func InstallAllInitialEnvironment(dataDirPath string) {
|
||||||
|
|
||||||
func InstallConfigFile(configFilePath string) error {
|
func InstallConfigFile(configFilePath string) error {
|
||||||
if len(configFilePath) == 0 {
|
if len(configFilePath) == 0 {
|
||||||
configFilePath = filepath.Join(ConfigFilePath, DefaultConfigFileName)
|
configFilePath = filepath.Join(ConfigFileDir, DefaultConfigFileName)
|
||||||
}
|
}
|
||||||
fmt.Println("[config-file] try to create at ", configFilePath)
|
fmt.Println("[config-file] try to create at ", configFilePath)
|
||||||
|
|
||||||
|
@ -46,7 +54,7 @@ func InstallConfigFile(configFilePath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dir.CreateDirIfNotExist(ConfigFilePath); err != nil {
|
if err := dir.CreateDirIfNotExist(ConfigFileDir); err != nil {
|
||||||
fmt.Printf("[config-file] create directory fail %s\n", err.Error())
|
fmt.Printf("[config-file] create directory fail %s\n", err.Error())
|
||||||
return fmt.Errorf("create directory fail %s", err.Error())
|
return fmt.Errorf("create directory fail %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,7 +117,7 @@ func InitEnvironment(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
c.Data.Database.Driver = req.DbType
|
c.Data.Database.Driver = req.DbType
|
||||||
c.Data.Database.Connection = req.GetConnection()
|
c.Data.Database.Connection = req.GetConnection()
|
||||||
c.Data.Cache.FilePath = filepath.Join(cli.CachePath, cli.DefaultCacheFileName)
|
c.Data.Cache.FilePath = filepath.Join(cli.CacheDir, cli.DefaultCacheFileName)
|
||||||
c.I18n.BundleDir = cli.I18nPath
|
c.I18n.BundleDir = cli.I18nPath
|
||||||
c.ServiceConfig.UploadPath = cli.UploadFilePath
|
c.ServiceConfig.UploadPath = cli.UploadFilePath
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
/usr/bin/answer init
|
/usr/bin/answer init
|
||||||
/usr/bin/answer upgrade
|
/usr/bin/answer upgrade
|
||||||
/usr/bin/answer run -c /data/conf/config.yaml
|
/usr/bin/answer run -C /data/
|
||||||
|
|
Loading…
Reference in New Issue