/
generate.go
199 lines (158 loc) · 3.96 KB
/
generate.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/fingerprintjs/fingerprint-pro-server-api-go-sdk/v3/config"
)
var files = []string{"README.md", "docs", ".swagger-codegen"}
var pathPrefix = "sdk"
func main() {
bumpConfigVersion()
generateSwagger()
moveFiles()
getExamples()
fixFingerPrintApiMdFile()
formatCode()
}
func getVersion() string {
var version string
envVersion := os.Getenv("VERSION")
if envVersion != "" {
version = envVersion
} else {
configFile := config.ReadConfig("./config.json")
version = configFile.PackageVersion
}
return version
}
func bumpConfigVersion() {
version := getVersion()
configFile := config.ReadConfig("./config.json")
if configFile.PackageVersion == version {
log.Println("Version is up to date")
return
}
configFile.PackageVersion = version
configContents, err := json.MarshalIndent(configFile, "", " ")
if err != nil {
log.Fatal(err)
}
if err = os.WriteFile("./config.json", configContents, 0644); err != nil {
log.Fatal(err)
}
}
func removeFileOrDirIfExists(path string) {
if stat, err := os.Stat(path); err == nil {
var err error
if stat.IsDir() {
err = os.RemoveAll(path)
} else {
err = os.Remove(path)
}
if err != nil {
log.Fatal(err)
}
}
}
func cleanupOldFiles() {
for _, filePath := range files {
removeFileOrDirIfExists(filePath)
}
}
func moveFiles() {
cleanupOldFiles()
for _, file := range files {
filePath := fmt.Sprintf("%s/%s", pathPrefix, file)
newFilePath := fmt.Sprintf("./%s", file)
err := os.Rename(filePath, newFilePath)
if err != nil {
log.Fatal(err)
}
}
}
func generateSwagger() {
cmd := exec.Command(
"java",
"-jar",
"./bin/swagger-codegen-cli.jar",
"generate",
"-t",
"./template",
"-l",
"go",
"-i",
"res/fingerprint-server-api.yaml",
"-o",
"./sdk",
"-c",
"config.json")
out, cmdErr := cmd.Output()
if cmdErr != nil {
log.Fatal(cmdErr)
}
fmt.Println(string(out))
}
func getExamples() {
list := []string{
"visits_limit_1.json",
"visits_limit_500.json",
"webhook.json",
"get_event.json",
"get_event_all_errors.json",
"get_event_extra_fields.json",
"get_event_403_error.json",
"get_event_404_error.json",
"get_event_botd_failed_error.json",
"get_event_botd_too_many_requests_error.json",
"get_event_identification_failed_error.json",
"get_event_identification_too_many_requests_error.json",
"visits_too_many_requests_error.json",
}
for _, file := range list {
cmd := exec.Command("curl", "-o", fmt.Sprintf("./test/mocks/%s", file), fmt.Sprintf("https://fingerprintjs.github.io/fingerprint-pro-server-api-openapi/examples/%s", file))
_, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
}
}
/**
* Fixes a bug with generated file in "docs/FingerprintApi.md" which contains invalid title generated by swagger
*/
func fixFingerPrintApiMdFile() {
token := "{{classname}}"
target := "FingerprintApi"
targetsToRemove := []string{"**optional** | ***FingerprintApiGetVisitsOpts** | optional parameters | nil if no parameters"}
filePath := "docs/FingerprintApi.md"
fileContents, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
fileContents = []byte(strings.Replace(string(fileContents), token, target, -1))
fileContentsArray := strings.Split(string(fileContents), "\n")
var fileContentsArrayResult []string
// Fixes markdown table for optional parameters, by default swagger-codegen applies new line there which breaks the table.
for _, line := range fileContentsArray {
for _, targetToRemove := range targetsToRemove {
if line != targetToRemove {
fileContentsArrayResult = append(fileContentsArrayResult, strings.Replace(line, targetToRemove, "", -1))
}
}
}
err = os.WriteFile(filePath, []byte(strings.Join(fileContentsArrayResult, "\n")), 0644)
if err != nil {
log.Fatal(err)
}
}
func formatCode() {
cmd := exec.Command("go", "fmt")
cmd.Dir = "./sdk"
_, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
}