-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanetu-sparql-cli
executable file
·107 lines (92 loc) · 3.77 KB
/
manetu-sparql-cli
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bb
(require '[babashka.deps :as deps])
(deps/add-deps '{:deps {medley/medley {:mvn/version "1.3.0"}
doric/doric {:mvn/version "0.9.0"}
environ/environ {:mvn/version "1.2.0"}}})
(require '[clojure.java.shell :refer [sh]])
(require '[medley.core :as m])
(require '[clojure.tools.cli :refer [parse-opts]])
(require '[clojure.string :as string])
(require '[taoensso.timbre :as log])
(require '[doric.core :refer [table]])
(require '[environ.core :refer [env]])
(log/set-level! :info)
(defn sparql-query [{:keys [show output update] :as options}]
(let [expr (slurp *in*)
escaped-expr (-> expr (string/replace #"\s+" " ") (string/trim))
gql (if (some? update)
[:update_provider_attributes {:label update :sparql_expr escaped-expr}]
[:query_provider_attributes {:sparql_expr escaped-expr} [:name :value]])
command (-> [(env :manetu-graphql-cli "manetu-graphql-cli")]
(cond-> (some? update) (conj "-m"))
(concat [:in (pr-str gql)]))]
(when show
(binding [*out* *err*]
(println "====================== SPARQL ========================")
(println expr)
(println "====================== RESULTS =======================")))
(log/debug "query:" gql)
(log/debug "command:" (pr-str command))
(let [{:keys [exit out err] :as r} (apply sh command)]
(log/debug "result:" r)
(if (zero? exit)
(let [result (json/parse-string out true)
errors (some-> result :errors)
data (mapv (fn [x] (reduce (fn [acc {:keys [name value]}] (assoc acc (string/replace name #"^\?" "") value)) {} x))
(some-> result :data :query_provider_attributes))]
(when (some? errors)
(log/error errors))
(log/debug "data:" data)
(println (case output
:json (json/generate-string data)
:table (table (-> data first keys) data))))
(do
(when (some? err)
(log/error err)
(println out)))))))
(defn exit [status msg & args]
(apply println msg args)
(System/exit status))
(defn prep-usage [msg] (->> msg flatten (string/join \newline)))
(defn usage [options-summary]
(prep-usage ["Usage: manetu-sparql-cli [options]"
""
"Options:"
options-summary]))
(def output-types #{:json :table})
(defn print-output-types []
(str "[" (string/join ", " (map name output-types)) "]"))
(def output-types-description
(str "Select the output-types from " (print-output-types)))
(def log-level-types '(:trace :debug :info :warn :error :fatal :report))
(defn print-log-level-types []
(str "[" (string/join ", " (map name log-level-types)) "]"))
(def output-log-level-types-description
(str "Select the output-types from " (print-log-level-types)))
(def options
[["-h" "--help"]
[nil "--update VAULTLABEL"]
["-s" "--show" "Display the SPARQL query"]
["-l" "--log-level LEVEL" output-log-level-types-description
:default :info
:parse-fn keyword
:validate [(set log-level-types) (str "Must be one of " (print-log-level-types))]]
["-o" "--output TYPE" output-types-description
:default :json
:parse-fn keyword
:validate [output-types (str "Must be one of " (print-output-types))]]])
(let [{{:keys [help] :as options} :options :keys [errors summary]} (parse-opts *command-line-args* options)]
(cond
help
(exit 0 (usage summary))
(not= errors nil)
(exit -1 "Error: " (string/join errors))
:else
(try
(log/set-level! (:log-level options))
(sparql-query options)
(catch Exception e
(exit -1 (ex-message e))))))
;; Local Variables:
;; eval: (clojure-mode)
;; End: