test-errcode
wangchuxiao 2 years ago
parent 5a77f91fa0
commit 757fd4bf0b

@ -18,9 +18,9 @@ var startCmd = &cobra.Command{
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetInt("port")
configPath, _ := cmd.Flags().GetString("config_path")
fmt.Printf("Starting server on port %s with config file at %s\n", port, configPath)
if err := run(port); err != nil {
configFolderPath, _ := cmd.Flags().GetString("config_folder_path")
fmt.Printf("Starting server on port %s with config file at %s\n", port, configFolderPath)
if err := run(configFolderPath, port); err != nil {
panic(err.Error())
}
},
@ -31,8 +31,8 @@ func init() {
startCmd.Flags().StringP("config_path", "c", "", "Path to config file folder")
}
func run(port int) error {
if err := config.InitConfig(); err != nil {
func run(configFolderPath string, port int) error {
if err := config.InitConfig(configFolderPath); err != nil {
return err
}
log.NewPrivateLog(constant.LogFileName)

@ -2,7 +2,6 @@ package config
import (
"OpenIM/pkg/discoveryregistry"
"flag"
"io/ioutil"
"os"
"path/filepath"
@ -21,7 +20,7 @@ const (
FileName = "config.yaml"
NotificationFileName = "notification.yaml"
ENV = "CONFIG_NAME"
DefaultPath = "../config/"
DefaultFolderPath = "../config/"
ConfKey = "conf"
)
@ -497,11 +496,12 @@ func (c *config) unmarshalConfig(config interface{}, configPath string) error {
return nil
}
func (c *config) initConfig(config interface{}, configName, configPath string) error {
if configPath == "" {
configPath = DefaultPath
func (c *config) initConfig(config interface{}, configName, configFolderPath string) error {
if configFolderPath == "" {
configFolderPath = DefaultFolderPath
}
_, err := os.Stat(filepath.Join(configPath, configName))
_, err := os.Stat(filepath.Join(configFolderPath, configName))
var configPath string
if os.IsNotExist(err) {
configPath = filepath.Join(Root, "config", configName)
}
@ -521,14 +521,12 @@ func (c *config) GetConfFromRegistry(registry discoveryregistry.SvcDiscoveryRegi
return registry.GetConfFromRegistry(ConfKey)
}
func InitConfig() error {
configPath := flag.String("config_path", os.Getenv(ENV), "folder for config")
flag.Parse()
err := Config.initConfig(&Config, FileName, *configPath)
func InitConfig(configFolderPath string) error {
err := Config.initConfig(&Config, FileName, configFolderPath)
if err != nil {
return err
}
err = Config.initConfig(&Config.Notification, NotificationFileName, *configPath)
err = Config.initConfig(&Config.Notification, NotificationFileName, configFolderPath)
if err != nil {
return err
}

Loading…
Cancel
Save