This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cljs
69 lines (59 loc) · 1.74 KB
/
file.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns re-conf.spec.file
"Spec checks for files"
(:require-macros
[clojure.core.strint :refer (<<)])
(:require
[cljs.spec.alpha :as s]
[re-conf.resources.common :refer (run error? obj->clj)]
[clojure.string :refer (includes?)]
[cljs.core.async :as async :refer [<! go]]
[cljs-node-io.core :as io]
[cljs-node-io.fs :as io-fs]))
; specs
(s/def mode string?)
(s/def file string?)
; check functions
(defn check-file
"File check spec"
[d]
(go
(if-not (<! (io-fs/afile? d))
{:error (<< "file ~{d} is missing") :exists false}
{:ok (<< "file ~{d} exists") :exists true})))
(defn check-dir
"Dir check spec"
[d]
(go
(if-not (<! (io-fs/adir? d))
{:error (<< "directory ~{d} is missing") :exists false}
{:ok (<< "directory ~{d} exists") :exists true})))
(defn check-link
"link check function"
[src target]
(go
(if (<! (io-fs/asymlink? target))
(let [[_ actual] (<! (io-fs/areadlink target))]
(if (= actual src)
{:ok (<< "link ~{src} to ~{target} exists") :exists true}
{:error (<< "~{src} points to ~{actual} and not ~{target}") :exists true}))
{:error (<< "link missing") :exists false})))
(defn contains
"Check that a file contains string spec"
([f s]
(go
(if-let [e (error? (<! (check-file f)))]
{:error e}
(if (includes? (io/slurp f) s)
{:ok (<< "~{f} contains ~{s}") :present true}
{:error (<< "~{f} does not contain ~{s}") :present false}))))
([c f s]
(run c contains [f s])))
(defn stats
"Get file stats info"
[dest]
(go
(let [[err stat] (<! (io-fs/astat dest))
prms (io-fs/permissions stat)]
(if (nil? err)
{:ok (assoc (obj->clj stat) :mode prms)}
{:error err}))))