/
cfgchecker.go
177 lines (159 loc) · 3.46 KB
/
cfgchecker.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"flag"
"fmt"
"github.com/clbanning/mxj"
"launchpad.net/xmlpath"
"os"
"path/filepath"
"reflect"
)
var myStack = &stack{}
var root *xmlpath.Node
var report *os.File
var matched = make([]string, 50)
var skipped = make([]string, 50)
var unmatched = make([]string, 50)
func main() {
sourceFilePath := flag.String("source-file-path", "", "source file path")
targetDirPath := flag.String("target-dir-path", "", "target dir path")
verbose := flag.Bool("verbose", false, "verbose")
flag.Parse()
if *sourceFilePath == "" || *targetDirPath == "" {
flag.PrintDefaults()
os.Exit(2)
}
var err error
report, err = os.Create("report.txt")
if err != nil {
panic(err)
}
defer report.Close()
f, err := os.Open(*sourceFilePath)
if err != nil {
panic(err)
}
defer f.Close()
root, err = xmlpath.Parse(f)
if err != nil {
panic(err)
}
filepath.Walk(*targetDirPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Base(*sourceFilePath) == filepath.Base(path) {
fmt.Printf("CHECKING... %s\n", path)
fmt.Fprintf(report, "\n\nSOURCE : %s\n", *sourceFilePath)
fmt.Fprintf(report, "TARGET : %s\n", path)
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
fmt.Fprintln(report, err)
return nil
}
defer f.Close()
m, err := mxj.NewMapXmlReader(f)
if err != nil {
fmt.Println(err)
fmt.Fprintln(report, err)
return nil
}
matched = matched[:0]
skipped = skipped[:0]
unmatched = unmatched[:0]
lookupMap(m)
if len(unmatched) != 0 {
fmt.Fprintf(report, "\n...UNMATCHED...\n")
for _, s := range unmatched {
fmt.Fprintf(report, "%s\n", s)
}
}
if len(skipped) != 0 {
fmt.Fprintf(report, "\n...skipped...\n")
for _, s := range skipped {
fmt.Fprintf(report, "%s\n", s)
}
}
if *verbose && len(matched) != 0 {
fmt.Fprintf(report, "\n...matched...\n")
for _, s := range matched {
fmt.Fprintf(report, "%s\n", s)
}
}
}
return nil
})
fmt.Println("DONE... report.txt")
}
func lookupMap(m map[string]interface{}) {
for n, v := range m {
myStack.push(n)
switch v2 := v.(type) {
case string:
xpath := xmlpath.MustCompile(myStack.String())
if xpath.Exists(root) {
itr := xpath.Iter(root)
isMatched := false
var srcStr string
for itr.Next() {
node := itr.Node()
srcStr = node.String()
if v2 == srcStr {
s := fmt.Sprintf("%s\t[%s]", myStack, v2)
matched = append(matched, s)
isMatched = true
break
}
}
if !isMatched {
s := fmt.Sprintf("%s, source: [%s], target: [%s]", myStack, srcStr, v2)
unmatched = append(unmatched, s)
}
} else {
s := fmt.Sprintf("%s\t[%s]", myStack, v2)
skipped = append(skipped, s)
}
case map[string]interface{}:
lookupMap(v2)
case []interface{}:
lookupSlice(v2)
default:
panic(reflect.TypeOf(v))
}
myStack.pop()
}
}
func lookupSlice(l []interface{}) {
for _, v := range l {
switch v2 := v.(type) {
case string:
panic(v2)
case map[string]interface{}:
lookupMap(v2)
default:
panic(reflect.TypeOf(v))
}
}
}
type stack struct {
nodes []string
count int
}
func (s *stack) push(n string) {
s.nodes = append(s.nodes[:s.count], n)
s.count++
}
func (s *stack) pop() string {
if s.count == 0 {
return ""
}
s.count--
return s.nodes[s.count]
}
func (s *stack) String() (ret string) {
for _, n := range s.nodes {
ret += "/" + n
}
return
}