Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/nvd/task.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
(ns nvd.task
"Clojure CLI tool entry points: `check`."
(:require
[nvd.task.check :refer [-main]]))
[nvd.task.check :as check]
[nvd.task.prepopulate-db :as prepopulate-db]))

(defn check
"Arguments: `:config-filename` (optional), `:classpath` (required)."
[{:keys [config-filename classpath]}]
(-main (or config-filename "") classpath))
(check/-main (or config-filename "") classpath))

(defn prepopulate-db
"Arguments: `:config-filename` (optional)."
[{:keys [config-filename]}]
(prepopulate-db/-main (or config-filename "")))
70 changes: 70 additions & 0 deletions src/nvd/task/prepopulate_db.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
;; The MIT License (MIT)
;;
;; Copyright (c) 2026- bevuta IT GmbH
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.

(ns nvd.task.prepopulate-db
(:require
[clansi :refer [style]]
[clojure.string :as s]
[nvd.config :refer [default-edn-config-filename with-config]]
[trptcolin.versioneer.core :refer [get-version]])
(:import
(org.owasp.dependencycheck Engine)
(org.owasp.dependencycheck.exception ExceptionCollection)))

(def version
(delay {:nvd-clojure (get-version "nvd-clojure" "nvd-clojure")
:dependency-check (.getImplementationVersion (.getPackage Engine))}))

(defn do-updates [project]
(let [^Engine engine (:engine project)]
(try
(.doUpdates engine)
(catch ExceptionCollection e
(println "Encountered errors while analyzing:" (.getMessage e))
(doseq [exc (.getExceptions e)]
(println exc))
(let [exception-info (ex-info (str `ExceptionCollection)
{:exceptions (.getExceptions e)})]
(throw exception-info))))
project))

(defn impl [config-filename]
(with-config [project config-filename]
(println "Prepopulating database for" (-> project
:title
(s/trim)
(str "...")
(style :bright :yellow)))
(println " using nvd-clojure:" (:nvd-clojure @version) "and dependency-check:" (:dependency-check @version))
(-> project
do-updates)
;; If we got here, it's all good. Otherwise, we'd be throwing an exception
(System/exit 0)))
Comment on lines +50 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Unconditionally calling System/exit 0 inside impl prevents programmatic usage of this task (e.g., from a REPL, tests, or other Clojure tools) and halts the JVM immediately. Furthermore, calling System/exit inside the with-config block prevents the finally block from executing, which means the engine's resources are not cleaned up properly.

We should introduce a conditional-exit helper that respects the :exit-after-check configuration option, matching the pattern used in nvd.task.check.

Additionally, there is a minor typo in the log message: Prepulating should be Prepopulating.

(defn conditional-exit [{:keys [exit-after-check] :as project}]
  (if exit-after-check
    (System/exit 0)
    project))

(defn impl [config-filename]
  (with-config [project config-filename]
    (println "Prepopulating database for" (-> project
                                             :title
                                             (s/trim)
                                             (str "...")
                                             (style :bright :yellow)))
    (println "  using nvd-clojure:" (:nvd-clojure @version) "and dependency-check:" (:dependency-check @version))
    (-> project
        do-updates
        conditional-exit)))


(defn -main [& [config-filename]]
;; specifically handle blank strings (in addition to nil)
;; so that CLI callers can skip the first argument by simply passing an empty string:
(let [config-filename (if (s/blank? config-filename)
default-edn-config-filename
config-filename)]
(impl config-filename)))