Skip to content

Commit eaf2577

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

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/nl/jomco/apie/main.clj

Lines changed: 47 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,17 @@
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+
(codec/form-decode q)))))
34+
2335
(def cli-options
2436
[["-u" "--base-url BASE-URL" "Base URL of service to validate."
2537
:missing "BASE-URL is missing"]
@@ -58,6 +70,8 @@
5870
["-v" "--version"
5971
"Print version and exit."
6072
:id :print-version?]
73+
[nil "--help"
74+
"Print usage information and exit."]
6175
["-a" "--basic-auth 'USER:PASS'" "Send basic authentication header."
6276
:default nil
6377
:parse-fn (fn [s]
@@ -67,6 +81,29 @@
6781
{:user user
6882
:pass pass}))]])
6983

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

145182
(defn -main
146183
[& args]
147-
(let [{:keys [errors summary options]} (parse-opts args cli-options)]
184+
(let [{:keys [errors summary options arguments]} (parse-opts args cli-options)]
185+
(when (:help options)
186+
(println (usage summary))
187+
(System/exit 0))
148188
(when (:print-version? options)
149189
(println (version))
150190
(System/exit 0))
151191
(when (seq errors)
152192
(run! println errors)
153193
(println summary)
154194
(System/exit 1))
155-
(main options)))
195+
196+
(let [options (if (seq arguments)
197+
(assoc options :seeds (map #(parse-seed (:base-url options) %) arguments))
198+
options)]
199+
(main options))))
156200

157201
(comment
158202

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)