This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (86 loc) · 2.15 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
// haproxy-config-diff parset 2 HAProxy-Configs (grob),
// erstellt daraus jeweils eine Datenstruktur
// und vergleicht dann die 2 Datenstrukturen.
//
// Dadurch kann man inhaltliche Änderungen zwischen zwei Configs sehen,
// aber Änderungen an Formatierungen und (irrelevanter) Reihenfolge werden ignoriert.
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"github.com/google/go-cmp/cmp"
)
type config map[string]map[string]section
type section map[string][]string
func parseConfig(r io.Reader) (*config, error) {
cfg := make(config)
var currentSection section
scanner := bufio.NewScanner(r)
for scanner.Scan() {
s := strings.TrimSpace(scanner.Text())
if s == "" {
continue
}
if strings.HasPrefix(s, "#") {
continue
}
keyword, params, _ := strings.Cut(s, " ")
switch keyword {
case "global", "defaults", "listen", "frontend", "backend", "cache", "userlist", "peers":
// vorherige Section abschließen.
sort.Strings(currentSection["timeout"])
sort.Strings(currentSection["acl"])
sort.Strings(currentSection["bind"])
sort.Strings(currentSection["server"])
sort.Strings(currentSection["option"])
sort.Strings(currentSection["stats"])
currentSection = make(section)
if cfg[keyword] == nil {
cfg[keyword] = make(map[string]section)
}
cfg[keyword][params] = currentSection
default:
if currentSection == nil {
return nil, fmt.Errorf("keyword %q must be in a section", keyword)
}
currentSection[keyword] = append(currentSection[keyword], params)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return &cfg, nil
}
func parseConfigFile(fp string) (*config, error) {
f, err := os.Open(fp)
if err != nil {
return nil, err
}
defer f.Close()
cfg, err := parseConfig(f)
if err != nil {
return nil, fmt.Errorf("%s: %v", fp, err)
}
return cfg, nil
}
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: haproxy-config-diff FILE FILE")
os.Exit(1)
}
lhs, err := parseConfigFile(os.Args[1])
if err != nil {
log.Fatal(err)
}
rhs, err := parseConfigFile(os.Args[2])
if err != nil {
log.Fatal(err)
}
diff := cmp.Diff(lhs, rhs)
fmt.Println(diff)
}