Validate config.

This commit is contained in:
John Wiseman 2020-01-21 20:29:18 -08:00
parent ea0bb782dc
commit fe17c489d9
3 changed files with 74 additions and 30 deletions

View File

@ -484,6 +484,27 @@
(util/deep-merge default-config config cli-config secrets))
(defn validate-config [config]
(let [required [[:lat]
[:lon]
[:radius-km]
[:basestation-sqb]
[:adsbx :url]
[:adsbx :api-key]
[:pelias :url]]
present (util/nested-keys config)
missing1 (set/difference (set required) (set present))
missing2 (when (get-in config [:twitter :enabled?])
(let [required (map (fn [key] [:twitter key])
[:consumer-key :consumer-secret
:access-token :access-token-secret])]
(set/difference (set required) (set present))))
missing (concat missing1 missing2)]
(when missing
(throw (js/Error. (str "Missing configuration values: "
(string/join ", " (sort-by str missing))))))))
(def default-config-path "config.yaml")
(defn main [& args]
@ -511,6 +532,7 @@
cli-config (build-config-from-commander commander)
secrets (util/read-config (.-secrets commander))
config (build-config base-config cli-config secrets)
_ (validate-config config)
db (read-history-db (:history-db-path config))
data (get-adsbexchange-live-data
{:url (get-in config [:adsbx :url])

View File

@ -70,3 +70,18 @@
(if (every? map? vs)
(apply merge-with deep-merge vs)
(last vs)))
;; From https://stackoverflow.com/a/21769626/122762
(defn nested-keys [m]
(if (map? m)
(vec
(mapcat (fn [[k v]]
(let [sub (nested-keys v)
nested (map #(into [k] %) (filter (comp not empty?) sub))]
(if (seq nested)
nested
[[k]])))
m))
[]))

View File

@ -13,3 +13,10 @@
{}
{:adsbx {:secret "123"}})
{:adsbx {:url "http://bar", :secret "123"}})))
(deftest nested-keys
(is (= (util/nested-keys {:a 1 :b 2})
[[:a] [:b]]))
(is (= (util/nested-keys {:a {:b {:c 1} :c {:d 2}} :b {:c 3} :c 4})
[[:a :b :c] [:a :c :d] [:b :c] [:c]])))