-
Notifications
You must be signed in to change notification settings - Fork 45
/
chart_parser.go
104 lines (87 loc) · 2.05 KB
/
chart_parser.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
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
package main
import (
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/zieckey/goini"
)
const DataPrefix = "Data|"
const emptyRunes = " \r\t\v"
type ChartIf interface {
Parse(ini *goini.INI, file string) (map[string]string, error)
Template() string
}
type TemplateArgs struct {
args map[string]string
tmpl string
}
func Parse(file string) (tt TemplateArgs, err error) {
ini := goini.New()
err = ini.ParseFile(file)
if err != nil {
return tt, err
}
t, _ := ini.Get("ChartType")
if f, ok := ChartHandlers[t]; ok {
tt.args, err = f.Parse(ini, file)
tt.tmpl = f.Template()
}
return tt, err
}
func LookupChartFiles(dir string) ([]string, error) {
var files []string = make([]string, 0, 5)
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
return nil
}
if ok, err := filepath.Match("*.chart", f.Name()); err != nil {
return err
} else if ok {
log.Printf("Add chart file %v", path)
files = append(files, path)
}
return nil
})
if len(files) == 0 {
return files, errors.New("Not found any *.chart files")
}
return files, err
}
/**
* 读取配置文件获取有序map
*
* @return order map
* @param {[type]} ini string [description]
*/
func LoadConfGetOrderMap(configFile string) ([]string, map[string]string, error) {
stream, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, nil, errors.New("cannot load config file")
}
content := string(stream)
confMap := make(map[string]string)
mapkeys := make([]string, 0)
lines := strings.Split(content, "\n")
for _, line := range lines {
line = strings.Trim(line, emptyRunes)
//过滤注释 //过滤非DataPrefix
if line == "" || line[0] == '#' || !strings.HasPrefix(line, DataPrefix) {
continue
}
//过滤
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
key := strings.Trim(parts[0], emptyRunes)
value := strings.Trim(parts[1], emptyRunes)
mapkeys = append(mapkeys, key)
confMap[key] = value
}
}
return mapkeys, confMap, nil
}