Skip to content

Commit 6b4c6b2

Browse files
committed
added tools.cli to parse command line params
1 parent bc2a2a4 commit 6b4c6b2

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

project.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
55
:url "https://www.eclipse.org/legal/epl-2.0/"}
66
:dependencies [[org.clojure/clojure "1.10.1"]
7-
[lambdaisland/deep-diff2 "2.0.108"]
8-
[org.clojure/data.json "1.0.0"]
97
[org.clojure/core.async "1.3.610"]
8+
[org.clojure/data.json "1.0.0"]
9+
[org.clojure/tools.cli "1.0.194"]
10+
[lambdaisland/deep-diff2 "2.0.108"]
1011
[juji/editscript "0.4.6"]]
1112
:main ^:skip-aot json-diff.core
1213
:target-path "target/%s"

src/json_diff/core.clj

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,54 @@
22
(:require [clojure.data.json :as json]
33
[clojure.core.async :refer [>! <!! go chan]]
44
[lambdaisland.deep-diff2 :as ddiff]
5-
[clojure.java.io :as io])
5+
[clojure.java.io :as io]
6+
[editscript.core :as edcore]
7+
[editscript.edit :as edit]
8+
[clojure.tools.cli :refer [parse-opts]])
69
(:gen-class))
710

11+
(def cli-options
12+
[["-m" "--mode MODE" "operation mode: 'visual' or 'patch'"
13+
:default :patch
14+
:parse-fn #(keyword %)
15+
:validate [#(% #{:visual :patch}) "possible modes are 'visual' or 'patch'"]]])
816

9-
(defn open-parse
17+
(defn parse-file
1018
"open JSON file and parses it"
1119
[fn]
1220
(with-open [f (io/reader fn)]
1321
(json/read f)))
1422

23+
(defn parse-files
24+
"parses multiple file asynchronously"
25+
[& files]
26+
())
27+
28+
(defn patch
29+
"returns compact patch between objects"
30+
[json1 json2]
31+
)
32+
1533
(defn diff
16-
"returns diff between JSON files"
34+
"returns visual diff between JSON files"
1735
[fn1 fn2]
1836
(let [c1 (chan)
1937
c2 (chan)]
20-
(go (>! c1 (open-parse fn1)))
21-
(go (>! c2 (open-parse fn2)))
22-
(ddiff/diff (<!! c1) (<!! c2))))
38+
(go (>! c1 (parse-file fn1)))
39+
(go (>! c2 (parse-file fn2)))
40+
(ddiff/pretty-print
41+
(ddiff/diff (<!! c1) (<!! c2)))))
2342

2443
(defn -main
2544
"main function"
2645
[& args]
27-
(if (> 2 (count args))
28-
(println "Not enough parameters!")
29-
(ddiff/pretty-print
30-
(diff
31-
(first args)
32-
(second args)))))
46+
(let [{files :arguments
47+
errors :errors
48+
{:keys [mode]} :options} (parse-opts args cli-options)]
49+
(cond
50+
errors (do (apply println errors)
51+
(System/exit 1))
52+
(not= 2 (count files)) (do (println "you need to pass two JSON files!")
53+
(System/exit 1))
54+
(= :visual mode) (apply diff files)
55+
(= :patch mode) (apply patch files))))

0 commit comments

Comments
 (0)