-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (96 loc) · 2.31 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
package main
import (
"os"
"path"
"strings"
"io/ioutil"
"fmt"
"github.com/smallfish/simpleyaml"
"github.com/urfave/cli"
)
func extractKeys(fileData []byte, ymlPathArgs []string) []string {
keys := make([]string, 0, 50)
data := ""
if len(ymlPathArgs) > 0 {
y, _ := simpleyaml.NewYaml(fileData)
data, _ = y.GetPath(ymlPathArgs...).String()
} else {
data = string(fileData[:])
}
lines := strings.Split(data, "\n")
for _, line := range lines {
keyVals := strings.Split(line, "=")
if len(keyVals) > 0 {
if len(keyVals[0]) > 0 {
keys = append(keys, keyVals[0])
}
}
}
return keys
}
func ParseFileData(filenames []string, ymlPathArgs []string) [][]string {
envKeys := make([][]string, 0, 50)
for _, file := range filenames {
if _, err := os.Stat(file); os.IsNotExist(err) {
fmt.Println(file + " Does not exist")
os.Exit(1)
}
fileData, _ := ioutil.ReadFile(file)
envKeys = append(envKeys, extractKeys(fileData, ymlPathArgs))
}
return envKeys
}
func CompareEnvArrays(envKeys [][]string, filenames []string) bool {
for i := 0; i < len(envKeys); i++ {
for j := i + 1; j < len(envKeys); j++ {
if len(envKeys[i]) != len(envKeys[j]) {
fmt.Println("Unequal number of keys")
return false
}
for y := 0; y < len(envKeys[j]); y++ {
keyExists := false
for z := 0; z < len(envKeys[j]); z++ {
if envKeys[j][y] == envKeys[i][z] {
keyExists = true
}
}
if !keyExists {
fmt.Println("Key " + envKeys[j][y] + " doesn't exist in file parameter " + filenames[j])
return false
}
}
}
}
return true
}
func appendStringsToCWD(filenames []string) []string {
pwd, _ := os.Getwd()
for index, file := range filenames {
filenames[index] = path.Join(pwd, file)
}
return filenames
}
func main() {
app := cli.NewApp()
app.Name = "par"
app.Usage = "fight the loneliness!"
app.Action = func(c *cli.Context) error {
fileNames := strings.Split(c.Args().Get(0), ",")
ymlArgs := []string{}
cmdArgs := c.Args().Get(1)
if cmdArgs != "" {
ymlArgs = strings.Split(c.Args().Get(1), ",")
}
envKeys := ParseFileData(fileNames, ymlArgs)
result := CompareEnvArrays(envKeys, fileNames)
if result {
fmt.Println("All configs are matching")
os.Exit(0)
} else {
fmt.Println("Keys arent equal")
os.Exit(1)
}
return nil
}
app.Run(os.Args)
}