package cfg

import (
	"flag"
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

//配置文件数据，全局变量
var (
	Debug            bool
	Prd              bool
	CurlDebug        bool
	SrvAddr          string
	RedisAddr        string
	RedisAddrSecond  *RedisAddrSeconds
	DB               *DBCfg
	MQ               *MQCfg
	ES               *ESCfg
	Log              *LogCfg
	ArkID            *ArkIDCfg
	Admin            *AdminCfg
	Official         *OfficialCfg
	WxappletFilepath *WxappletFilepathCfg
	H5Filepath       *H5FilepathCfg
	Local            bool
	AppComm          *AppCommCfg
	Zhimeng          *ZhimengCfg
	WebsiteBackend   *WebsiteBackendCfg
	Supply           *SupplyCfg
	ImBusinessRpc    *ImBusinessRpcCfg
	ZhiosOpen        *ZhiosOpenCfg
	ZhimengDB        *DBCfg
)

//初始化配置文件，将cfg.yml读入到内存
func InitCfg() {
	//用指定的名称、默认值、使用信息注册一个string类型flag。
	path := flag.String("c", "etc/cfg.yml", "config file")
	//解析命令行参数写入注册的flag里。
	//解析之后，flag的值可以直接使用。
	flag.Parse()
	var (
		c    []byte
		err  error
		conf *Config
	)
	if c, err = ioutil.ReadFile(*path); err != nil {
		panic(err)
	}
	//yaml.Unmarshal反序列化映射到Config
	if err = yaml.Unmarshal(c, &conf); err != nil {
		panic(err)
	}
	//数据读入内存
	Prd = conf.Prd
	Debug = conf.Debug
	Local = conf.Local
	CurlDebug = conf.CurlDebug
	DB = &conf.DB
	MQ = &conf.MQ
	Log = &conf.Log
	ArkID = &conf.ArkID
	RedisAddr = conf.RedisAddr
	RedisAddrSecond = &conf.RedisAddrSecond
	SrvAddr = conf.SrvAddr
	Admin = &conf.Admin
	Official = &conf.Official
	WxappletFilepath = &conf.WxappletFilepath
	AppComm = &conf.AppComm
	Zhimeng = &conf.Zhimeng
	Supply = &conf.Supply
	H5Filepath = &conf.H5Filepath
	WebsiteBackend = &conf.WebsiteBackend
	ImBusinessRpc = &conf.ImBusinessRpc
	ES = &conf.ES
	ZhiosOpen = &conf.ZhiosOpen
	ZhimengDB = &conf.ZhimengDB

}
