-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_browser_links.clj
94 lines (80 loc) · 2.14 KB
/
manage_browser_links.clj
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(require '[clojure.edn :as edn])
(require '[clojure.set :as set])
(require '[clojure.string :as str])
(require '[clojure.pprint :as pprint])
(def browser-links-path (str (System/getenv "SHAREPATH") "/ref/"))
(def browser-links (str browser-links-path "browser-links.edn"))
(def browser-links-backup (str browser-links-path "browser-links-bak.edn"))
(defn entry->name->entry
[{n :name :as entry}]
[n entry])
(defn back-up-links [links]
(spit browser-links-backup links))
(defn append-link
"append a link to `browser-links`"
[_ link-name url tags]
(let [links (-> browser-links slurp edn/read-string)
entry
{:name link-name
:url url
:tags
(->> (str/split tags #" ")
(into #{} (map keyword)))}]
(back-up-links links)
(->> entry
(conj links)
(sort-by :name)
(into [])
pprint/pprint
with-out-str
(spit browser-links))))
(defn list-tags
"list link tags"
[selected]
(let [selected
(if (seq selected)
(->> (str/split selected #" ")
(into #{} (map keyword)))
#{})]
(->> browser-links
slurp
edn/read-string
(filter #(set/subset? selected (into #{} (:tags %))))
(mapcat :tags)
(into #{})
sort
(into [] (map name)))))
(defn list-all
"list all links"
[]
(->> browser-links
slurp
edn/read-string
(into {} (map entry->name->entry))))
(defn list-tagged
"list links by tag"
[tags]
(let [tags (->> (str/split tags #" ")
(into #{} (map keyword)))]
(->> browser-links
slurp
edn/read-string
(into {}
(comp
(filter #(set/subset? tags (into #{} (:tags %))))
(map entry->name->entry))))))
;;; TODO: implement nils
(prn
(case (first *command-line-args*)
"append-link"
(apply append-link *command-line-args*)
"list-all"
(list-all)
"list-tags"
(list-tags (second *command-line-args*))
"list-tagged"
(list-tagged (second *command-line-args*))
"remove-link"
nil
"tag-link"
nil))