Repository navigation
Expand file tree
/
Copy pathdiff.clj
More file actions
executable file
·76 lines (66 loc) · 2.28 KB
/
Copy pathdiff.clj
File metadata and controls
executable file
·76 lines (66 loc) · 2.28 KB
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
#!/usr/bin/env bb
(ns op.diff
(:require [cheshire.core :as json]
[clojure.string :as str]
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[clojure.tools.cli :as cli]))
(defn- prepare-oapi-path [path]
(str/replace path #"\{\w+\}" "{}"))
(defn openapi-endpoints
"Create a hashmap with unified-path:original-path pairs."
[openapi-file]
(let [paths (-> openapi-file
slurp
json/decode
(get "paths")
keys)]
(zipmap (map prepare-oapi-path paths)
paths)))
(defn prepare-pm-path [path]
(-> path
(str/replace #"\{\{Url}\}" "")
(str/replace #"\{\{\w+\}\}" "{}")))
(defn pm-item->path [{:keys [name request]}]
(let [{:keys [url]} request
{:keys [raw]} url
path (prepare-pm-path raw)]
[path {:name name
:raw raw}]))
(defn postman-paths [postman-file]
(let [items (-> postman-file
slurp
(json/decode true)
(get-in [:item]))]
(->> items
(map pm-item->path)
(into {}))))
(defn path->row [path pm-paths os-paths]
(let [{:keys [name raw]} (get pm-paths path)
oapi-path (get os-paths path)]
{:postman-name (or name "")
:postman-url (or raw "")
:openapi-url (or oapi-path "")}))
(defn write-csv [path row-data]
(let [columns [:openapi-url :postman-name :postman-url]
headers (map name columns)
rows (mapv #(mapv % columns) row-data)]
(with-open [file (io/writer path)]
(csv/write-csv file (cons headers rows)))))
(def cli-options
;; An option with a required argument
[["-o" "--openapi PATH" "OpenAPI JSON path"]
["-p" "--postman PATH" "Postman JSON path"]
["-h" "--help"]])
(let [{:keys [options summary]} (cli/parse-opts *command-line-args* cli-options)
{:keys [openapi postman help]} options]
(if (or help (and (str/blank? openapi)
(str/blank? postman)))
(println (str summary))
(let [pm-paths (postman-paths postman)
os-paths (openapi-endpoints openapi)
out (->> (concat (keys os-paths) (keys pm-paths))
distinct
sort
(map #(path->row % pm-paths os-paths)))]
(write-csv "diff.csv" out))))