Browse Source

Some refactoring

Stephen Starkey 5 months ago
parent
commit
966a19afde
2 changed files with 15 additions and 17 deletions
  1. 1
    1
      project.clj
  2. 14
    16
      src/ensure_mounted/core.clj

+ 1
- 1
project.clj View File

@@ -1,4 +1,4 @@
1
-(defproject ensure-mounted "0.1.0"
1
+(defproject ensure-mounted "0.1.1"
2 2
   :description "Ensure that directories are mounted"
3 3
   :url "https://git.calmabiding.me/scstarkey/ensure-mounted"
4 4
   :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"

+ 14
- 16
src/ensure_mounted/core.clj View File

@@ -5,30 +5,28 @@
5 5
   (:import (java.io BufferedReader FileInputStream InputStreamReader))
6 6
   (:gen-class))
7 7
 
8
+(defmacro def-> [n & body] `(defn ~n [top#] (-> top# ~@body)))
9
+(defmacro def->> [n & body] `(defn ~n [top#] (->> top# ~@body)))
10
+
8 11
 (def start-ensure? (partial = "#== ensure-mounted"))
9 12
 (def end-ensure? (partial = "#== ensure-mounted-end"))
10
-(defn mount-point [line] (-> line (str/split #" ") second))
13
+(def-> mount-point (str/split #" ") second)
11 14
 (def mount-points (comp (partial apply hash-set) (partial map mount-point)))
15
+(def mount! (partial sh/sh "mount"))
16
+(def-> reader FileInputStream. InputStreamReader. BufferedReader.)
12 17
 
13
-(defn lines [filename]
14
-  (with-open [f (FileInputStream. filename)
15
-              ir (InputStreamReader. f)
16
-              r (BufferedReader. ir)]
17
-    (doall (line-seq r))))
18
+(defn lines [filename] (with-open [r (reader filename)] (doall (line-seq r))))
18 19
 
19
-(defn assess-line [{:keys [block-active?] :as result} line]
20
+(defn assess-line [{:keys [active?] :as result} line]
20 21
   (cond
21
-    (start-ensure? line) (assoc result :block-active? true)
22
-    (end-ensure? line) (assoc result :block-active? false)
23
-    block-active? (update result :lines conj line)
22
+    (start-ensure? line) (assoc result :active? true)
23
+    (end-ensure? line) (assoc result :active? false)
24
+    active? (update result :lines conj line)
24 25
     :else result))
25 26
 
26
-(defn desired-block [lines]
27
-  (->> lines (reduce assess-line {:block-active? false, :lines []}) :lines))
28
-
29
-(defn mount! [path] (sh/sh "mount" path))
27
+(def->> desired-block (reduce assess-line {:active? false, :lines []}) :lines)
30 28
 
31
-(defn assess-result [results {:keys [exit err]}]
29
+(defn assess-mount-result [results {:keys [exit err]}]
32 30
   (if (= 0 exit)
33 31
     results
34 32
     (-> results (assoc :failed? true) (update :errs conj err))))
@@ -39,7 +37,7 @@
39 37
         necessary (set/difference desired existing)
40 38
         results (doall (map mount! necessary))]
41 39
     (if (seq desired)
42
-      (reduce assess-result {:failed? false :errs []} results)
40
+      (reduce assess-mount-result {:failed? false :errs []} results)
43 41
       {:failed? true
44 42
        :errs ["Unable to find any mount points. Is your fstab annotated?"]})))
45 43