|
7 | 7 | [clojure.tools.cli :refer [parse-opts]]
|
8 | 8 | [nl.jomco.apie.report :as report]
|
9 | 9 | [nl.jomco.apie.spider :as spider]
|
10 |
| - [ring.util.codec :as codec])) |
| 10 | + [ring.util.codec :as codec]) |
| 11 | + (:import java.net.URI)) |
11 | 12 |
|
12 | 13 | (defn- parse-header
|
13 | 14 | [s]
|
|
20 | 21 | [headers [k v]]
|
21 | 22 | (update-in headers [k] spider/merge-header-values v))
|
22 | 23 |
|
| 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 | + |
23 | 35 | (def cli-options
|
24 | 36 | [["-u" "--base-url BASE-URL" "Base URL of service to validate."
|
25 | 37 | :missing "BASE-URL is missing"]
|
|
58 | 70 | ["-v" "--version"
|
59 | 71 | "Print version and exit."
|
60 | 72 | :id :print-version?]
|
| 73 | + [nil "--help" |
| 74 | + "Print usage information and exit."] |
61 | 75 | ["-a" "--basic-auth 'USER:PASS'" "Send basic authentication header."
|
62 | 76 | :default nil
|
63 | 77 | :parse-fn (fn [s]
|
|
67 | 81 | {:user user
|
68 | 82 | :pass pass}))]])
|
69 | 83 |
|
| 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 | + |
70 | 107 | (defn version
|
71 | 108 | "Return app version"
|
72 | 109 | []
|
|
144 | 181 |
|
145 | 182 | (defn -main
|
146 | 183 | [& 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)) |
148 | 188 | (when (:print-version? options)
|
149 | 189 | (println (version))
|
150 | 190 | (System/exit 0))
|
151 | 191 | (when (seq errors)
|
152 | 192 | (run! println errors)
|
153 | 193 | (println summary)
|
154 | 194 | (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)))) |
156 | 200 |
|
157 | 201 | (comment
|
158 | 202 |
|
|
0 commit comments