|
| 1 | +;; The MIT License (MIT) |
| 2 | +;; |
| 3 | +;; Copyright (c) 2026- bevuta IT GmbH |
| 4 | +;; |
| 5 | +;; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +;; of this software and associated documentation files (the "Software"), to deal |
| 7 | +;; in the Software without restriction, including without limitation the rights |
| 8 | +;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +;; copies of the Software, and to permit persons to whom the Software is |
| 10 | +;; furnished to do so, subject to the following conditions: |
| 11 | +;; |
| 12 | +;; The above copyright notice and this permission notice shall be included in all |
| 13 | +;; copies or substantial portions of the Software. |
| 14 | +;; |
| 15 | +;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +;; SOFTWARE. |
| 22 | + |
| 23 | +(ns nvd.task.prepopulate-db |
| 24 | + (:require |
| 25 | + [clansi :refer [style]] |
| 26 | + [clojure.string :as s] |
| 27 | + [nvd.config :refer [default-edn-config-filename with-config]] |
| 28 | + [trptcolin.versioneer.core :refer [get-version]]) |
| 29 | + (:import |
| 30 | + (org.owasp.dependencycheck Engine) |
| 31 | + (org.owasp.dependencycheck.exception ExceptionCollection))) |
| 32 | + |
| 33 | +(def version |
| 34 | + (delay {:nvd-clojure (get-version "nvd-clojure" "nvd-clojure") |
| 35 | + :dependency-check (.getImplementationVersion (.getPackage Engine))})) |
| 36 | + |
| 37 | +(defn do-updates [project] |
| 38 | + (let [^Engine engine (:engine project)] |
| 39 | + (try |
| 40 | + (.doUpdates engine) |
| 41 | + (catch ExceptionCollection e |
| 42 | + (println "Encountered errors while analyzing:" (.getMessage e)) |
| 43 | + (doseq [exc (.getExceptions e)] |
| 44 | + (println exc)) |
| 45 | + (let [exception-info (ex-info (str `ExceptionCollection) |
| 46 | + {:exceptions (.getExceptions e)})] |
| 47 | + (throw exception-info)))) |
| 48 | + project)) |
| 49 | + |
| 50 | +(defn impl [config-filename] |
| 51 | + (with-config [project config-filename] |
| 52 | + (println "Prepulating database for" (-> project |
| 53 | + :title |
| 54 | + (s/trim) |
| 55 | + (str "...") |
| 56 | + (style :bright :yellow))) |
| 57 | + (println " using nvd-clojure:" (:nvd-clojure @version) "and dependency-check:" (:dependency-check @version)) |
| 58 | + (-> project |
| 59 | + do-updates) |
| 60 | + ;; If we got here, it's all good. Otherwise, we'd be throwing an exception |
| 61 | + (System/exit 0))) |
| 62 | + |
| 63 | +(defn -main [& [config-filename]] |
| 64 | + ;; specifically handle blank strings (in addition to nil) |
| 65 | + ;; so that CLI callers can skip the first argument by simply passing an empty string: |
| 66 | + (let [config-filename (if (s/blank? config-filename) |
| 67 | + default-edn-config-filename |
| 68 | + config-filename)] |
| 69 | + (impl config-filename))) |
| 70 | + |
0 commit comments