File tree 2 files changed +16
-12
lines changed
2 files changed +16
-12
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ For a list of breaking changes, check [here](#breaking-changes).
10
10
- Allow JQ variables to be passed at execution time #38 (Thanks to @charles-dyfis-net )
11
11
- API to return a stream of JSON entities #41
12
12
- Transducers API #42
13
+ - CLI: pretty print output by default, flag ` -c ` for compact #44
13
14
14
15
## v1.2.1
15
16
Original file line number Diff line number Diff line change 3
3
(:require [clojure.java.io :as io]
4
4
[clojure.string :as str]
5
5
[clojure.tools.cli :as cli]
6
- [jq.api :as jq])
6
+ [jq.transducers :as jq])
7
7
(:import (java.io Reader BufferedReader)))
8
8
9
9
(def cli-options
10
- [[" -h" " --help" ]])
10
+ [[" -c" " --[no-]compact" " compact instead of pretty-printed output." :default false ]
11
+ [" -h" " --help" ]])
11
12
12
13
(defn handle-args [args]
13
14
(cli/parse-opts args cli-options))
23
24
(println " Supported options:" )
24
25
(println summary))
25
26
26
- (defn execute [jq-filter files _]
27
- (let [jq-processor (jq/stream-processor jq-filter)]
28
- (if (seq files)
29
- (doseq [f files
30
- item (jq-processor (jq/string->json-node (slurp f)))]
31
- (println (jq/json-node->string item)))
32
- (when (.ready ^Reader *in*)
33
- (doseq [^String line (line-seq (BufferedReader. *in*))
34
- item (jq-processor (jq/string->json-node line))]
35
- (println (jq/json-node->string item)))))))
27
+ (defn printer
28
+ ([_])
29
+ ([_ item] (println item)))
30
+ (defn execute [jq-expression files opts]
31
+ (let [xfs [(when (seq files) (map slurp))
32
+ (jq/parse )
33
+ (jq/execute jq-expression)
34
+ (if (:compact opts) (jq/serialize ) (jq/pretty-print ))]
35
+ xf (apply comp (remove nil? xfs))
36
+ values (or (seq files) (when (.ready ^Reader *in*)
37
+ (line-seq (BufferedReader. *in*))))]
38
+ (transduce xf printer nil values)))
36
39
37
40
(defn -main [& args]
38
41
(let [{:keys [options arguments errors summary]
You can’t perform that action at this time.
0 commit comments