-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_config.go
56 lines (48 loc) · 1.19 KB
/
parse_config.go
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
package main
import (
"io/ioutil"
"os"
"log"
"github.com/PGDream/monitorprocess/monitor"
"gopkg.in/yaml.v2"
)
type ParseConfig struct {
FileName string
}
func init() {
log.SetPrefix("【parse-config】")
}
func (parseConfig *ParseConfig) NewParseConfig(fileName string) *ParseConfig {
return &ParseConfig{
FileName: fileName,
}
}
func (parseConfig *ParseConfig) Parse() *monitor.ProcessConfigInfo {
contents := readConfig(parseConfig)
if contents == nil {
log.Println("config content is nil")
return nil
}
processConfigInfo := new(monitor.ProcessConfigInfo)
err := yaml.Unmarshal(contents, &processConfigInfo)
if err != nil {
log.Fatalln("parse config to object failed, expection info:", err)
return nil
}
return processConfigInfo
}
/**
return can`t "" charset
*/
func readConfig(parseConfig *ParseConfig) []byte {
_, err := os.Open(parseConfig.FileName)
if err != nil && os.IsNotExist(err) {
log.Fatalln("read config failed, file not exists or get config for fd failed, expection info:", err)
return nil
}
contents, err := ioutil.ReadFile(parseConfig.FileName)
if err != nil {
log.Fatalln("read config file content failed, expection info:", err)
}
return contents
}