-
-
Save jeroenvandijk/09aec4f88fef113c937daf77c9d6560f to your computer and use it in GitHub Desktop.
DirectoryWatcher visitor utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import '[io.methvin.watcher.visitor FileTreeVisitor]) | |
;; See https://github.com/babashka/fs/blob/master/API.md#babashka.fs/walk-file-tree for options | |
(require '[babashka.fs :as fs]) | |
(defn walker-file-tree [opts] | |
(fn [path {:keys [on-file on-dir on-failure] | |
:or {on-file (fn [_ _]) | |
on-dir (fn [_ _]) | |
on-failure (fn [_ _])}}] | |
(fs/walk-file-tree path | |
(merge {:pre-visit-dir | |
(fn [path attrs] | |
(on-dir path attrs) | |
:continue) | |
:visit-file | |
(fn [path attrs] | |
(on-file path attrs) | |
:continue) | |
:post-visit-dir | |
(fn [path ex] | |
(when ex | |
(on-failure path ex)) | |
:continue) | |
:visit-file-failed | |
(fn [path ex] | |
(on-failure path ex) | |
:continue)} | |
opts)))) | |
(defn ->file-tree-visitor [file-tree-walker] | |
(reify FileTreeVisitor | |
(recursiveVisitFiles [_ root-path onDirectory onFile] | |
(file-tree-walker root-path | |
{:on-file (fn [path _] | |
(.call onFile path) | |
) | |
:on-dir (fn [path _] | |
(.call onDirectory path))})))) | |
(comment | |
(let [*files (atom []) | |
f (fn [path _] | |
(swap! *files conj (str path))) | |
;; play around with :max-depth and see the difference | |
walker (walker-file-tree {:max-depth 2})] | |
(walker "/usr/lib" | |
{:on-file f}) | |
(count (map str @*files))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment