-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.clj
269 lines (245 loc) · 8.98 KB
/
main.clj
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
(ns main
(:require
[babashka.http-client :as http]
[clojure.java.io :as io]
[clojure.string :as s])
(:import
[io.swagger.parser OpenAPIParser]
[io.swagger.v3.oas.models Operation PathItem]
[io.swagger.v3.oas.models.parameters Parameter]
[io.swagger.v3.parser.core.models ParseOptions]
[java.util.concurrent Executors]))
(def sources
{:docker {:url "https://docs.docker.com/reference/api/engine/version/%s.yaml"
:doc-url "https://docs.docker.com/reference/api/engine/version/%s"
:versions ["v1.25"
"v1.26"
"v1.27"
"v1.28"
"v1.29"
"v1.30"
"v1.31"
"v1.32"
"v1.33"
"v1.34"
"v1.35"
"v1.36"
"v1.37"
"v1.38"
"v1.39"
"v1.40"
"v1.41"
"v1.42"
"v1.43"
"v1.44"
"v1.45"]}
:podman {:url "https://storage.googleapis.com/libpod-master-releases/swagger-%s.yaml"
:doc-url "https://docs.podman.io/en/%s/_static/api.html"
:namespaces #{"/libpod"}
:versions ["v3.1.0"
"v3.1.1"
"v3.1.2"
"v3.2.0"
"v3.2.1"
"v3.2.2"
"v3.2.3"
"v3.3.0"
"v3.3.1"
"v3.4.0"
"v3.4.1"
"v3.4.2"
"v3.4.3"
"v3.4.4"
"v4.0.0"
"v4.0.1"
"v4.0.2"
"v4.0.3"
"v4.1.0"
"v4.1.1"
"v4.2.0"
"v4.2.1"
"v4.3.0"
"v4.3.1"
"v4.4.0"
"v4.4.1"
"v4.4.2"
"v4.4.3"
"v4.4.4"
"v4.5.0"
"v4.5.1"
"v4.6.0"
"v4.6.1"
"v4.6.2"
"v4.7.0"
"v4.7.2"
"v4.8.0"
"v4.8.1"
"v4.8.2"
"v4.8.3"
"v4.9.0"
"v4.9.1"
"v4.9.2"
"v4.9.3"
"v4.9.4"
"v4.9.5"
"v5.0.0"
"v5.0.1"
"v5.0.2"
"v5.0.3"
"v5.1.0"
"v5.1.1"
"v5.2.0"
"v5.2.2"
"v5.2.3"
"v5.2.4"
"v5.2.5"
"v5.3.0"
"v5.3.1"
"v5.3.2"
"v5.4.0"]}})
(def resource-path "resources/contajners")
(defn find-first
[pred coll]
(some #(when (pred %) %) coll))
;; TODO: Better?
(defn ->category
"Given a path and a set of namespaces, returns the (namespaced)category.
path: /containers/json
namespaces: #{}
category: :containers
path: /libpod/containers/json
namespaces: #{\"/libpod\"}
category: :libpod/containers
path: /libpod/deeper/api/containers/json
namespaces: #{\"/libpod/deeper/api\"}
category: :libpod.deeper.api/containers
The category is the prefix of the path being passed. eg /containers, /images
The set of namespaces, if passed, determines if the category is to be namespaced. eg /libpod/containers and /containers
The namespace is useful to distinguish similarly named ops in APIs supporting compatibility with other engines."
[path namespaces]
(let [matched (find-first #(s/starts-with? path %) namespaces)
nspace (when matched
(-> matched
(subs 1)
(s/replace "/" ".")))
path (if matched
(s/replace-first path matched "")
path)
category (-> path
(subs 1)
(s/split #"/")
(first))]
(if nspace
(keyword nspace category)
(keyword category))))
;; TODO: Parse and validate the types someday
(defn ->params
"Given a io.swagger.v3.oas.models.parameters.Parameter, returns a map of necessary keys."
[^Parameter param]
{:name (.getName param)
:in (keyword (.getIn param))})
(defn ->operation
"Given a path, http method and an io.swagger.v3.oas.models.Operation, returns a map of operation id and necessary keys."
[path method ^Operation operation]
(let [op {:summary (.getSummary operation)
:method (-> method
str
s/lower-case
keyword)
:path path
:params (map ->params (.getParameters operation))}
request-body (.getRequestBody operation)]
{(keyword (.getOperationId operation)) (if request-body
(assoc op :request-body true)
op)}))
(defn ->operations
"Given a set of namespaces, path and a io.swagger.v3.oas.models.PathItem returns a list of maps of operations."
[namespaces path ^PathItem path-item]
(->> (.readOperationsMap path-item)
(map #(->operation path (key %) (val %)))
(map #(hash-map (->category path namespaces) %))))
(defn parse
"Given a set of namespaces and the OpenAPI 2.0 spec as a string, returns the spec in the following format:
{:category1 {:operation-id1 {:summary \"summary\"
:method :HTTP_METHOD
:path \"/path/to/api\"
:params [{:name \"..\"}]}}
:namespaced/category {...}}"
[^String spec namespaces]
(let [parse-options (doto (ParseOptions.)
(.setResolveFully true))]
(->> (.readContents (OpenAPIParser.) spec nil parse-options) ; specs are still Swagger 2.0 🙄
(.getOpenAPI)
(.getPaths)
(mapcat #(->operations namespaces (key %) (val %)))
(apply (partial merge-with into)))))
(defn fetch-spec
"Downloads the spec from the URL and version provided."
[url-template version]
(let [{:keys [status body]} (http/get (format url-template version)
{:throw false})]
(if (>= status 400)
(binding [*out* *err*]
(println (format "Error fetching version %s: %s" version body)))
body)))
(defn process-spec
"Processes the spec with the namespaces and adds metadata."
[spec doc-url namespaces]
(-> spec
(parse namespaces)
(assoc :contajners/doc-url doc-url)))
(defn write-spec
"Writes the spec as minified edn."
[spec engine version]
(let [path (format "%s/%s/%s.edn"
resource-path
(name engine)
version)]
(with-open [w (io/writer path)]
(binding [*print-length* false
*out* w]
(pr spec)))))
(defn ensure-engine-dirs
[engine]
(let [path (io/file (str resource-path "/" (name engine)))]
(when-not (.exists path)
(.mkdirs path))))
(defn run
"Driver fn, iterates over the sources, downloads, processes and saves as resources."
[& _]
(let [executor (Executors/newVirtualThreadPerTaskExecutor)
download-info (for [[engine {:keys [url doc-url namespaces versions]}] sources
version versions]
{:engine engine
:url url
:doc-url doc-url
:version version
:namespaces namespaces})
fetchers (map #(fn [] (fetch-spec (% :url) (% :version)))
download-info)
fetched (->> (.invokeAll executor fetchers)
(map deref)
(filter some?))
processed (pmap #(process-spec %1 (%2 :doc-url) (%2 :namespaces))
fetched
download-info)]
(run! ensure-engine-dirs (keys sources))
(->> (map #(fn [] (write-spec %1 (%2 :engine) (%2 :version)))
processed
download-info)
(.invokeAll executor)
(run! deref))))
(comment
(set! *warn-on-reflection* true)
(run)
(find-first #{3} [1 2 3 4 5])
(->category "/libpod/containers" #{"/libpod"})
(->category "/containers/json" #{})
(-> "https://storage.googleapis.com/libpod-master-releases/swagger-v3.2.2.yaml"
http/get
:body
(parse #{"/libpod"}))
(-> "https://docs.docker.com/engine/api/v1.41.yaml"
http/get
:body
(parse #{})))