| | |
| | | package util |
| | | |
| | | import ( |
| | | "fmt" |
| | | "io/ioutil" |
| | | "os" |
| | | "strings" |
| | | |
| | | "github.com/spf13/viper" |
| | | "github.com/olebedev/config" |
| | | ) |
| | | |
| | | // FillParams 填充MapParams |
| | |
| | | func InitConfig() { |
| | | |
| | | const ( |
| | | configFilePath = "/opt/vasystem/config/" |
| | | configFileName = "pro" |
| | | configFileType = "yaml" |
| | | configFileName = "pro.yaml" |
| | | LOGBASEPATH = "/data/disk1/valog/" |
| | | ) |
| | | |
| | | viper.SetConfigType(configFileType) |
| | | viper.SetConfigName(configFileName) |
| | | viper.AddConfigPath(configFilePath) |
| | | viper.AddConfigPath("./") |
| | | configFilePaths := []string{ |
| | | "/opt/vasystem/config/", |
| | | "./", |
| | | } |
| | | |
| | | LogFile = "./log/analysis-" |
| | | |
| | | if err := viper.ReadInConfig(); err == nil { |
| | | var file []byte |
| | | var err error |
| | | var fileName string |
| | | for _, v := range configFilePaths { |
| | | fileName = v + configFileName |
| | | file, err = ioutil.ReadFile(fileName) |
| | | if err == nil { |
| | | break |
| | | } |
| | | } |
| | | if file == nil { |
| | | fmt.Println(`Read All Log Config Files Failed, If -logit Use Default, "./log/analysis-[type]"`) |
| | | return |
| | | } |
| | | yamlString := string(file) |
| | | |
| | | logPath := viper.GetString("LogBasePath") |
| | | cfg, err := config.ParseYaml(yamlString) |
| | | if err != nil { |
| | | fmt.Println("Config Parse File: ", fileName, " Error: ", err) |
| | | return |
| | | } |
| | | logPath, err := cfg.String("LogBasePath") |
| | | |
| | | if err == nil { |
| | | if len(logPath) > 0 && IsFileExist(logPath) { |
| | | if logPath[len(logPath)-1] == '/' { |
| | | LogFile = logPath + "analysis-" |
| | |
| | | } else if IsFileExist(LOGBASEPATH) { |
| | | LogFile = LOGBASEPATH + "analysis-" |
| | | } |
| | | |
| | | } |
| | | |
| | | // const ( |
| | | // configFilePath = "/opt/vasystem/config/" |
| | | // configFileName = "pro" |
| | | // configFileType = "yaml" |
| | | // LOGBASEPATH = "/data/disk1/valog/" |
| | | // ) |
| | | |
| | | // viper.SetConfigType(configFileType) |
| | | // viper.SetConfigName(configFileName) |
| | | // viper.AddConfigPath(configFilePath) |
| | | // viper.AddConfigPath("./") |
| | | |
| | | // LogFile = "./log/analysis-" |
| | | |
| | | // if err := viper.ReadInConfig(); err == nil { |
| | | |
| | | // logPath := viper.GetString("LogBasePath") |
| | | // if len(logPath) > 0 && IsFileExist(logPath) { |
| | | // if logPath[len(logPath)-1] == '/' { |
| | | // LogFile = logPath + "analysis-" |
| | | // } else { |
| | | // LogFile = logPath + "/analysis-" |
| | | // } |
| | | // } else if IsFileExist(LOGBASEPATH) { |
| | | // LogFile = LOGBASEPATH + "analysis-" |
| | | // } |
| | | // } else if IsFileExist(LOGBASEPATH) { |
| | | // LogFile = LOGBASEPATH + "analysis-" |
| | | // } |