aboutsummaryrefslogtreecommitdiff
path: root/scripts/utils.clj
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xscripts/utils.clj43
1 files changed, 43 insertions, 0 deletions
diff --git a/scripts/utils.clj b/scripts/utils.clj
new file mode 100755
index 0000000..ab1cf70
--- /dev/null
+++ b/scripts/utils.clj
@@ -0,0 +1,43 @@
+#!/usr/bin/env bb
+
+(require '[clojure.java.io :as io])
+
+(defn dirname [path] (.getParent (io/file path)))
+
+(defn dir-has-file? [dir filenames]
+ (some
+ (fn [file] (.exists (io/file (str dir "/" file))))
+ filenames))
+
+(defn find-closest [dir filenames]
+ (cond
+ (= dir "/") nil
+ (= dir ".") nil
+ (= dir "") nil
+ (dir-has-file? dir filenames) dir
+ :else (find-closest (dirname dir) filenames)))
+
+(defn normalize-dir-path [dir]
+ (.getAbsolutePath (io/file (if (empty? dir) "." dir))))
+
+(defn cmd-find-closest [[dir & filenames]]
+ (let [init-dir (normalize-dir-path dir)]
+ (when-let [found-dir (find-closest init-dir filenames)]
+ (println found-dir))))
+
+(defn cmd-find-closest-or-dir [[dir & filenames]]
+ (let [init-dir (normalize-dir-path dir)]
+ (if-let [found-dir (find-closest init-dir filenames)]
+ (println found-dir)
+ (println init-dir))))
+
+(def commands
+ {"find-closest" cmd-find-closest
+ "find-closest-or-dir" cmd-find-closest-or-dir})
+
+(let [[cmd & args] *command-line-args*]
+ (if-let [command-fn (commands cmd)]
+ (command-fn args)
+ (binding [*out* *err*]
+ (println (str "invalid command: " (or cmd "")))
+ (println (str "Valid commands: " (keys commands))))))