-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (117 loc) · 3.76 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"semconvdiff/internal"
)
const (
FILE_BASE_URL = "https://raw.githubusercontent.com/open-telemetry/opentelemetry-collector/semconv/v0.109.0/semconv"
)
var (
singleLineConstPattern = regexp.MustCompile(`const\s+(\w+)\s+= \s+"([^"]+)"`)
blockConstPattern = regexp.MustCompile(`const\s*\((.*?)\)`)
extraConstPattern = regexp.MustCompile(`(\w+)\s*= \s*"([^"]+)"`)
)
func fetchFile(url string) string {
resp, err := http.Get(url)
if err != nil {
internal.ErrorPrintf("Failed to fetch %s: %v", url, err)
return ""
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
internal.ErrorPrintf("Failed to fetch %s with status code %d", url, resp.StatusCode)
return ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
internal.ErrorPrintf("Error reading response from %s: %v", url, err)
return ""
}
return string(body)
}
func extractSingleLineConstants(content string) map[string]string {
constants := make(map[string]string)
matches := singleLineConstPattern.FindAllStringSubmatch(content, -1)
for _, match := range matches {
constName := match[1]
constValue := match[2]
constants[constName] = constValue
}
return constants
}
func extractBlockConstants(content string) map[string]string {
constants := make(map[string]string)
blockMatches := blockConstPattern.FindAllStringSubmatch(content, -1)
for _, block := range blockMatches {
blockContent := block[1]
blockConstants := extraConstPattern.FindAllStringSubmatch(blockContent, -1)
for _, match := range blockConstants {
constName := match[1]
constValue := match[2]
constants[constName] = constValue
}
}
return constants
}
func fetchConstants(url string) map[string]string {
content := fetchFile(url)
constants := extractSingleLineConstants(content)
blockConstants := extractBlockConstants(content)
for constName, constValue := range blockConstants {
constants[constName] = constValue
}
extraConstants := extraConstPattern.FindAllStringSubmatch(content, -1)
for _, match := range extraConstants {
constName := match[1]
constValue := match[2]
if _, exists := constants[constName]; !exists {
constants[constName] = constValue
}
}
return constants
}
func fetchConstantsFromVersion(version string) map[string]string {
allConstants := make(map[string]string)
files, err := internal.FetchFileNames(version)
if err != nil {
internal.DebugPrintf("Failed to fetch files %s: %v", version, err)
return map[string]string{}
}
internal.InfoPrint(version, "found files:", files)
for _, file := range files {
url := fmt.Sprintf("%s/%s/%s", FILE_BASE_URL, version, file)
internal.InfoPrintf("Fetching constants from %s...", url)
constants := fetchConstants(url)
for k, v := range constants {
allConstants[k] = v
}
if len(constants) == 0 {
internal.DebugPrint(version, "no constant found on", file)
}
}
return allConstants
}
func compareConstants(constantsV1, constantsV2 map[string]string, v1, v2 string) {
fmt.Printf("%-30s | %-50s | %-50s\n", "Constant Name", v1, v2)
fmt.Println(strings.Repeat("=", 140))
for constName, valueV1 := range constantsV1 {
if valueV2, exists := constantsV2[constName]; exists && valueV1 != valueV2 {
fmt.Printf("%-30s | %-50s | %-50s\n", constName, valueV1, valueV2)
}
}
}
func main() {
version1 := flag.String("v1", "v1", "Version 1 to compare")
version2 := flag.String("v2", "v2", "Version 2 to compare")
flag.Parse()
constantsV1 := fetchConstantsFromVersion(*version1)
constantsV2 := fetchConstantsFromVersion(*version2)
internal.InfoPrint(*version1, "has", len(constantsV1), "constants")
internal.InfoPrint(*version2, "has", len(constantsV2), "constants")
compareConstants(constantsV1, constantsV2, *version1, *version2)
}