Skip to content

Commit 4135ac0

Browse files
committed
Add usage information and set seeds from CLI
1 parent 0317a16 commit 4135ac0

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/nl/jomco/apie/main.clj

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[clojure.tools.cli :refer [parse-opts]]
88
[nl.jomco.apie.report :as report]
99
[nl.jomco.apie.spider :as spider]
10-
[ring.util.codec :as codec]))
10+
[ring.util.codec :as codec])
11+
(:import java.net.URI))
1112

1213
(defn- parse-header
1314
[s]
@@ -20,6 +21,21 @@
2021
[headers [k v]]
2122
(update-in headers [k] spider/merge-header-values v))
2223

24+
(defn- parse-seed
25+
[base-url s]
26+
(let [s (string/replace s base-url "")
27+
u (URI. s)
28+
q (.getRawQuery u)]
29+
(cond-> {:method "get"
30+
:path (.getRawPath u)}
31+
q
32+
(assoc :query-params
33+
;; FIXME: this fails when param has no value: "/foo?bar&this=that"
34+
(into {}
35+
(map (fn [p]
36+
(mapv codec/percent-decode (string/split p #"=" 2)))
37+
(string/split q #"[&;]")))))))
38+
2339
(def cli-options
2440
[["-u" "--base-url BASE-URL" "Base URL of service to validate."
2541
:missing "BASE-URL is missing"]
@@ -58,6 +74,8 @@
5874
["-v" "--version"
5975
"Print version and exit."
6076
:id :print-version?]
77+
[nil "--help"
78+
"Print usage information and exit."]
6179
["-a" "--basic-auth 'USER:PASS'" "Send basic authentication header."
6280
:default nil
6381
:parse-fn (fn [s]
@@ -67,6 +85,27 @@
6785
{:user user
6886
:pass pass}))]])
6987

88+
(defn usage
89+
[summary]
90+
(->> ["Apie 🙈 Service Validator"
91+
""
92+
"A command-line tool to spider API endpoints and validate"
93+
"against OpenAPI v3 specs."
94+
""
95+
"Usage: apie OPTIONS SEEDS*"
96+
""
97+
"OPTIONS:"
98+
summary
99+
""
100+
"If SEEDS are not provided, uses seeds in profile."
101+
""
102+
"To validate all reachable paths from a service, use"
103+
"apie --profile=some/profile.edn --base-url=http://example.com"
104+
""
105+
"To validate one specific path, use"
106+
"apie -M1 --profile=some/profile.edn --base-url=http://example.com '/some/path?param=val'"]
107+
(string/join "\n")))
108+
70109
(defn version
71110
"Return app version"
72111
[]
@@ -144,15 +183,22 @@
144183

145184
(defn -main
146185
[& args]
147-
(let [{:keys [errors summary options]} (parse-opts args cli-options)]
186+
(let [{:keys [errors summary options arguments]} (parse-opts args cli-options)]
187+
(when (:help options)
188+
(println (usage summary))
189+
(System/exit 0))
148190
(when (:print-version? options)
149191
(println (version))
150192
(System/exit 0))
151193
(when (seq errors)
152194
(run! println errors)
153195
(println summary)
154196
(System/exit 1))
155-
(main options)))
197+
198+
(let [options (if (seq arguments)
199+
(assoc options :seeds (map #(parse-seed (:base-url options) %) arguments))
200+
options)]
201+
(main options))))
156202

157203
(comment
158204

src/nl/jomco/apie/spider.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@
191191

192192
(defn spider-and-validate
193193
[openapi-spec
194-
{:keys [rules seeds] :as _rules}
194+
{:keys [rules] :as _rules}
195195
{:keys [max-requests-per-operation max-total-requests] :as options}]
196-
(let [
196+
(let [seeds (or (:seeds options) (:seeds _rules))
197197
validate (-> (validator/validator-context openapi-spec {})
198198
(validator/interaction-validator))
199199
matcher (paths-matcher (keys (get openapi-spec "paths")))

0 commit comments

Comments
 (0)