forked from migueleliasweb/go-github-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (76 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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/buger/jsonparser"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/migueleliasweb/go-github-mock/src/gen"
)
var debug bool
func init() {
flag.BoolVar(&debug, "debug", false, "output debug information")
}
func main() {
flag.Parse()
var l log.Logger
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
l = log.With(l, "caller", log.DefaultCaller)
if debug {
l = level.NewFilter(l, level.AllowDebug())
level.Debug(l).Log("msg", "running in debug mode")
} else {
l = level.NewFilter(l, level.AllowInfo())
}
apiDefinition := gen.FetchAPIDefinition(l)
buf := bytes.NewBuffer([]byte(gen.OUTPUT_FILE_HEADER))
jsonparser.ObjectEach(
apiDefinition,
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(
l,
gen.ScrapeResult{
HTTPMethod: httpMethod,
EndpointPattern: endpointPattern,
},
)
buf.WriteString(code)
}
return nil
},
"paths",
)
ioutil.WriteFile(
gen.OUTPUT_FILEPATH,
buf.Bytes(),
0755,
)
errorsFound := false
// to catch possible format errors
if err := exec.Command("gofmt", "-w", "src/mock/endpointpattern.go").Run(); err != nil {
level.Error(l).Log("msg", fmt.Sprintf("error executing gofmt: %s", err.Error()))
errorsFound = true
}
// to catch everything else (hopefully)
if err := exec.Command("go", "vet", "./...").Run(); err != nil {
level.Error(l).Log("msg", fmt.Sprintf("error executing go vet: %s", err.Error()))
errorsFound = true
}
if errorsFound {
os.Exit(1)
}
}