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

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

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

Loading…
Cancel
Save