From fe17c489d99c717390142532f90f219b091a5a0a Mon Sep 17 00:00:00 2001 From: John Wiseman Date: Tue, 21 Jan 2020 20:29:18 -0800 Subject: [PATCH] Validate config. --- src/main/lemondronor/circlebot.cljs | 82 ++++++++++++------- src/main/lemondronor/circlebot/util.cljs | 15 ++++ src/test/lemondronor/circlebot/util_test.cljs | 7 ++ 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/src/main/lemondronor/circlebot.cljs b/src/main/lemondronor/circlebot.cljs index 779d808..54bdca1 100644 --- a/src/main/lemondronor/circlebot.cljs +++ b/src/main/lemondronor/circlebot.cljs @@ -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] @@ -504,36 +525,37 @@ ;; file. Otherwise, only read config.yaml if it exists. (p/try (p/let [base-config (if-let [config-path (.-config commander)] - (util/read-config config-path) - (if (fs/existsSync default-config-path) - (util/read-config default-config-path) - {})) - cli-config (build-config-from-commander commander) - secrets (util/read-config (.-secrets commander)) - config (build-config base-config cli-config secrets) - db (read-history-db (:history-db-path config)) - data (get-adsbexchange-live-data - {:url (get-in config [:adsbx :url]) - :api-key (get-in config [:adsbx :api-key]) - :lat (:lat config) - :lon (:lon config) - :radius-nm (* (:radius-km config) 0.539957)}) - now (current-time) - [new-db potential-circles] (-> db - (update-history-db (:aircraft data) now config) - (detect-circles now config))] - (p/do - (when potential-circles - (doseq [ac potential-circles] - (log-warn "%s: New circle detected: %s" (:icao ac) (ac-desc ac))) - (process-potential-circles potential-circles config now)) - (write-history-db new-db (:history-db-path config)) - (let [end-time (current-time)] - (log-info - "Completed processing in %f seconds: tracking %s aircraft; %s potential circles" - (/ (- end-time start-time) 1000) - (count new-db) - (count potential-circles))))) + (util/read-config config-path) + (if (fs/existsSync default-config-path) + (util/read-config default-config-path) + {})) + 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]) + :api-key (get-in config [:adsbx :api-key]) + :lat (:lat config) + :lon (:lon config) + :radius-nm (* (:radius-km config) 0.539957)}) + now (current-time) + [new-db potential-circles] (-> db + (update-history-db (:aircraft data) now config) + (detect-circles now config))] + (p/do + (when potential-circles + (doseq [ac potential-circles] + (log-warn "%s: New circle detected: %s" (:icao ac) (ac-desc ac))) + (process-potential-circles potential-circles config now)) + (write-history-db new-db (:history-db-path config)) + (let [end-time (current-time)] + (log-info + "Completed processing in %f seconds: tracking %s aircraft; %s potential circles" + (/ (- end-time start-time) 1000) + (count new-db) + (count potential-circles))))) (p/catch :default e (log-error "%s" (.-stack e)) (.exit js/process 1))))) diff --git a/src/main/lemondronor/circlebot/util.cljs b/src/main/lemondronor/circlebot/util.cljs index 1c4aa0a..b82d334 100644 --- a/src/main/lemondronor/circlebot/util.cljs +++ b/src/main/lemondronor/circlebot/util.cljs @@ -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)) + [])) diff --git a/src/test/lemondronor/circlebot/util_test.cljs b/src/test/lemondronor/circlebot/util_test.cljs index b60fb8b..9fb8549 100644 --- a/src/test/lemondronor/circlebot/util_test.cljs +++ b/src/test/lemondronor/circlebot/util_test.cljs @@ -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]])))