-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
103 lines (80 loc) · 1.93 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
package main
import (
"bytes"
"flag"
"log/slog"
"os"
"os/exec"
"github.com/buger/jsonparser"
"github.com/migueleliasweb/go-github-mock/src/gen"
)
func main() {
flag.Parse()
fetchAndWriteAPIDefinition()
}
// type helper just to ensure uniqueness of the generated output
type uniq map[string]struct{}
// has will add `s` to the map whe not found
func (u uniq) has(s string) bool {
_, has := u[s]
if !has {
u[s] = struct{}{}
}
return has
}
func fetchAndWriteAPIDefinition() {
buf := bytes.NewBuffer([]byte(gen.OUTPUT_FILE_HEADER))
u := make(uniq)
defs := [][]byte{
gen.FetchAPIDefinition(gen.GITHUB_OPENAPI_DEFINITION_LOCATION),
gen.FetchAPIDefinition(gen.GITHUB_OPENAPI_ENTERPRISE_DEFINITION_LOCATION),
}
for _, d := range defs {
jsonparser.ObjectEach(
d,
func(key, endpointDefinition []byte, _ jsonparser.ValueType, _ int) error {
endpointPattern := string(key)
httpMethods := []string{}
jsonparser.ObjectEach(
endpointDefinition,
func(key, _ []byte, _ jsonparser.ValueType, _ int) error {
httpMethods = append(httpMethods, string(key))
return nil
},
)
for _, httpMethod := range httpMethods {
code := gen.FormatToGolangVarNameAndValue(
gen.ScrapeResult{
HTTPMethod: httpMethod,
EndpointPattern: endpointPattern,
},
)
if !u.has(code) {
buf.WriteString(code)
}
}
return nil
},
"paths",
)
}
os.WriteFile(
gen.OUTPUT_FILEPATH,
buf.Bytes(),
0755,
)
errorsFound := false
// to catch possible format errors
if err := exec.Command("gofmt", "-w", gen.OUTPUT_FILEPATH).Run(); err != nil {
slog.Info("error executing gofmt", "err", err.Error())
errorsFound = true
}
// to catch everything else (hopefully)
if err := exec.Command("go", "vet", "./...").Run(); err != nil {
slog.Info("error executing go vet", "err", err.Error())
errorsFound = true
}
if errorsFound {
os.Exit(1)
}
}