diff --git a/cmd/answer/command.go b/cmd/answer/command.go index cb80fe47..b5d83f9f 100644 --- a/cmd/answer/command.go +++ b/cmd/answer/command.go @@ -3,7 +3,6 @@ package main import ( "fmt" "os" - "path/filepath" "github.com/answerdev/answer/internal/base/conf" "github.com/answerdev/answer/internal/cli" @@ -13,8 +12,6 @@ import ( ) var ( - // 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 @@ -24,9 +21,7 @@ var ( func init() { 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(&configFilePath, "config", "c", "", "config path, eg: -c config.yaml") + rootCmd.PersistentFlags().StringVarP(&dataDirPath, "data-path", "C", "/data/", "data path, eg: -C ./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", Long: `Run the application`, Run: func(_ *cobra.Command, _ []string) { + cli.FormatAllPath(dataDirPath) + fmt.Println("config file path: ", cli.GetConfigFilePath()) fmt.Println("Answer is string..........................") runApp() }, @@ -65,15 +62,11 @@ To run answer, use: Run: func(_ *cobra.Command, _ []string) { // check config file and database. if config file exists and database is already created, init done 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 { fmt.Println("config file exists, try to read the config...") - c, err := conf.ReadConfig(configFilePath) + c, err := conf.ReadConfig(cli.GetConfigFilePath()) if err != nil { fmt.Println("read config failed: ", err.Error()) return @@ -87,7 +80,7 @@ To run answer, use: } // start installation server to install - install.Run(configFilePath) + install.Run(cli.GetConfigFilePath()) }, } @@ -97,7 +90,8 @@ To run answer, use: Short: "upgrade Answer version", Long: `upgrade Answer version`, Run: func(_ *cobra.Command, _ []string) { - c, err := conf.ReadConfig(configFilePath) + cli.FormatAllPath(dataDirPath) + c, err := conf.ReadConfig(cli.GetConfigFilePath()) if err != nil { fmt.Println("read config failed: ", err.Error()) return @@ -117,7 +111,8 @@ To run answer, use: Long: `back up data`, Run: func(_ *cobra.Command, _ []string) { fmt.Println("Answer is backing up data") - c, err := conf.ReadConfig(configFilePath) + cli.FormatAllPath(dataDirPath) + c, err := conf.ReadConfig(cli.GetConfigFilePath()) if err != nil { fmt.Println("read config failed: ", err.Error()) return @@ -137,8 +132,9 @@ To run answer, use: Short: "checking the required environment", Long: `Check if the current environment meets the startup requirements`, Run: func(_ *cobra.Command, _ []string) { + cli.FormatAllPath(dataDirPath) fmt.Println("Start checking the required environment...") - if cli.CheckConfigFile(configFilePath) { + if cli.CheckConfigFile(cli.GetConfigFilePath()) { fmt.Println("config file exists [✔]") } else { fmt.Println("config file not exists [x]") @@ -150,7 +146,7 @@ To run answer, use: fmt.Println("upload directory not exists [x]") } - c, err := conf.ReadConfig(configFilePath) + c, err := conf.ReadConfig(cli.GetConfigFilePath()) if err != nil { fmt.Println("read config failed: ", err.Error()) return diff --git a/cmd/answer/main.go b/cmd/answer/main.go index 0f1c7d94..37daf478 100644 --- a/cmd/answer/main.go +++ b/cmd/answer/main.go @@ -4,6 +4,7 @@ import ( "os" "github.com/answerdev/answer/internal/base/conf" + "github.com/answerdev/answer/internal/cli" "github.com/gin-gonic/gin" "github.com/segmentfault/pacman" "github.com/segmentfault/pacman/contrib/log/zap" @@ -38,7 +39,7 @@ func runApp() { log.SetLogger(zap.NewLogger( 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 { panic(err) } diff --git a/internal/base/conf/conf.go b/internal/base/conf/conf.go index 61e4f122..18a841d9 100644 --- a/internal/base/conf/conf.go +++ b/internal/base/conf/conf.go @@ -39,7 +39,7 @@ type Data struct { // ReadConfig read config func ReadConfig(configFilePath string) (c *AllConfig, err error) { if len(configFilePath) == 0 { - configFilePath = filepath.Join(cli.ConfigFilePath, cli.DefaultConfigFileName) + configFilePath = filepath.Join(cli.ConfigFileDir, cli.DefaultConfigFileName) } c = &AllConfig{} config, err := viper.NewWithPath(configFilePath) diff --git a/internal/cli/install.go b/internal/cli/install.go index ae56b1d1..3746118d 100644 --- a/internal/cli/install.go +++ b/internal/cli/install.go @@ -16,19 +16,27 @@ const ( ) var ( - ConfigFilePath = "/conf/" + ConfigFileDir = "/conf/" UploadFilePath = "/upfiles/" 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 func InstallAllInitialEnvironment(dataDirPath string) { - ConfigFilePath = filepath.Join(dataDirPath, ConfigFilePath) - UploadFilePath = filepath.Join(dataDirPath, UploadFilePath) - I18nPath = filepath.Join(dataDirPath, I18nPath) - CachePath = filepath.Join(dataDirPath, CachePath) - + FormatAllPath(dataDirPath) installUploadDir() installI18nBundle() fmt.Println("install all initial environment done") @@ -36,7 +44,7 @@ func InstallAllInitialEnvironment(dataDirPath string) { func InstallConfigFile(configFilePath string) error { if len(configFilePath) == 0 { - configFilePath = filepath.Join(ConfigFilePath, DefaultConfigFileName) + configFilePath = filepath.Join(ConfigFileDir, DefaultConfigFileName) } fmt.Println("[config-file] try to create at ", configFilePath) @@ -46,7 +54,7 @@ func InstallConfigFile(configFilePath string) error { 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()) return fmt.Errorf("create directory fail %s", err.Error()) } diff --git a/internal/install/install_controller.go b/internal/install/install_controller.go index 854147f9..c732f562 100644 --- a/internal/install/install_controller.go +++ b/internal/install/install_controller.go @@ -117,7 +117,7 @@ func InitEnvironment(ctx *gin.Context) { } c.Data.Database.Driver = req.DbType 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.ServiceConfig.UploadPath = cli.UploadFilePath diff --git a/script/entrypoint.sh b/script/entrypoint.sh index 9c09ec3c..41ce1c7a 100755 --- a/script/entrypoint.sh +++ b/script/entrypoint.sh @@ -1,4 +1,4 @@ #!/bin/bash /usr/bin/answer init /usr/bin/answer upgrade -/usr/bin/answer run -c /data/conf/config.yaml +/usr/bin/answer run -C /data/