Added spec validation to uberjar input maps

This commit is contained in:
Clemens Geibel 2021-03-19 12:41:31 +01:00
parent 79008746e2
commit d9a954f93a
2 changed files with 18 additions and 3 deletions

View file

@ -4,6 +4,7 @@
:license {:name "Apache License, Version 2.0" :license {:name "Apache License, Version 2.0"
:url "https://www.apache.org/licenses/LICENSE-2.0.html"} :url "https://www.apache.org/licenses/LICENSE-2.0.html"}
:dependencies [[org.clojure/clojure "1.10.3"] :dependencies [[org.clojure/clojure "1.10.3"]
[org.clojure/tools.reader "1.3.4"]
[aero "1.1.6"] [aero "1.1.6"]
[orchestra "2021.01.01-1"] [orchestra "2021.01.01-1"]
[expound "0.8.9"] [expound "0.8.9"]

View file

@ -3,6 +3,8 @@
(:require (:require
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.string :as cs] [clojure.string :as cs]
[clojure.tools.reader.edn :as edn]
[expound.alpha :as expound]
[clojure.java.io :as io] [clojure.java.io :as io]
[dda.k8s-mastodon-bot.core :as core])) [dda.k8s-mastodon-bot.core :as core]))
@ -19,6 +21,10 @@
(s/cat :config ::filename (s/cat :config ::filename
:auth ::filename)))) :auth ::filename))))
(defn expound-config
[config]
(expound/expound ::core/config config))
(defn invalid-args-msg [spec args] (defn invalid-args-msg [spec args]
(do (s/explain spec args) (do (s/explain spec args)
(println (str "Bad commandline arguments\n" usage)))) (println (str "Bad commandline arguments\n" usage))))
@ -28,10 +34,18 @@
(if (= ::s/invalid parsed-args-cmd) (if (= ::s/invalid parsed-args-cmd)
(invalid-args-msg ::cmd-args cmd-args) (invalid-args-msg ::cmd-args cmd-args)
(let [{:keys [options args]} parsed-args-cmd (let [{:keys [options args]} parsed-args-cmd
config-location (:config args) config (slurp (:config args))
auth-location (:auth args)] auth (slurp (:auth args))]
(cond (cond
(some #(= "-h" %) options) (some #(= "-h" %) options)
(println usage) (println usage)
:default :default
(println (core/generate (slurp config-location) (slurp auth-location)))))))) (let [config-edn (edn/read-string config)
auth-edn (edn/read-string auth)
config-valid? (= ::s/invalid (s/conform ::core/config config-edn))
auth-valid? (= ::s/invalid (s/conform ::core/config auth-edn))]
(if (and config-valid? auth-valid?)
(println (core/generate config auth))
(do
(when (not config-valid?) (expound-config config-edn))
(when (not auth-valid?) (expound-config auth-edn))))))))))