zhangmeng
2019-12-23 5d4871d5e4252330c84170c24d9fbbfb4b36a80e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package util
 
import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"
 
    "github.com/olebedev/config"
)
 
// FillParams 填充MapParams
func FillParams(key, value string) {
    MapParames[key] = value
}
 
// FindStringInArray find
func FindStringInArray(str string, array []string) (string, bool) {
    found := false
    ret := ""
    for _, v := range array {
        if strings.EqualFold(str, v) {
            found = true
            ret = v
            break
        }
    }
    return ret, found
}
 
// IsFileExist file exist
func IsFileExist(path string) bool {
    _, err := os.Stat(path)
    if err != nil {
        if os.IsNotExist(err) {
            return false
        }
    }
    return true
}
 
// InitConfig 读取配置文件
func InitConfig() {
 
    const (
        configFileName = "pro.yaml"
        LOGBASEPATH    = "/data/disk1/valog/"
    )
 
    configFilePaths := []string{
        "/opt/vasystem/config/",
        "./",
    }
 
    LogFile = "./log/analysis-"
 
    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 Config Files Failed, Use Default, example LogFile ./log/analysis-'type'")
        return
    }
    yamlString := string(file)
 
    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 {
                LogFile = logPath + "/analysis-"
            }
        } else if IsFileExist(LOGBASEPATH) {
            LogFile = LOGBASEPATH + "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-"
// }