-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (121 loc) · 2.36 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)
var verbose *bool
var caseSensitive *bool
type variableList struct {
path string
vars []string
}
func (l variableList) contains(name string) bool {
for _, v := range l.vars {
if *caseSensitive {
if v == name {
return true
}
} else {
if strings.ToUpper(v) == strings.ToUpper(name) {
return true
}
}
}
return false
}
func main() {
verbose = flag.Bool("v", false, "Print more verbose output")
caseSensitive = flag.Bool("cs", false, "Check should be case sensitive")
flag.Parse()
if len(flag.Args()) < 2 {
flag.Usage()
os.Exit(0)
}
lists := []variableList{}
for _, arg := range flag.Args() {
list, err := parseFile(arg)
if err != nil {
exit(err.Error())
}
lists = append(lists, *list)
}
for _, listA := range lists {
for _, listB := range lists {
if listA.path == listB.path {
continue
}
compare(listA, listB)
}
}
}
func compare(list1 variableList, list2 variableList) {
p1 := list1.path
if !*verbose {
p1 = path.Base(p1)
}
p2 := list2.path
if !*verbose {
p2 = path.Base(p2)
}
for _, v := range list1.vars {
if !list2.contains(v) {
fmt.Printf("%v \x1b[2mfrom %v missing in %v\x1b[0m\n", v, p1, p2)
}
}
}
func parseFile(path string) (*variableList, error) {
contentBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if string(contentBytes[0]) == "{" {
vars, err := parseJSONFile(contentBytes)
if err != nil {
return nil, err
}
return &variableList{
path: path,
vars: vars,
}, nil
}
return &variableList{
path: path,
vars: parseEnvFile(contentBytes),
}, nil
}
func parseJSONFile(content []byte) ([]string, error) {
var target map[string]interface{}
if err := json.Unmarshal(content, &target); err != nil {
return nil, err
}
names := []string{}
for k := range target {
names = append(names, k)
}
return names, nil
}
func parseEnvFile(content []byte) []string {
lines := strings.Split(string(content), "\n")
names := []string{}
for _, line := range lines {
line = strings.Trim(line, " ")
if len(line) == 0 {
continue
}
if line[0:1] == "#" {
continue
}
parts := strings.Split(line, "=")
names = append(names, parts[0])
}
return names
}
func exit(message string) {
fmt.Println("\x1b[31m" + message + "\x1b[0m")
os.Exit(1)
}