-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanetu-graphql-cli
executable file
·122 lines (101 loc) · 3.91 KB
/
manetu-graphql-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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bb
(require '[babashka.deps :as deps])
(deps/add-deps '{:deps {borkdude/spartan.spec {:git/url "https://github.com/borkdude/spartan.spec"
:sha "12947185b4f8b8ff8ee3bc0f19c98dbde54d4c90"}
district0x/graphql-query {:mvn/version "1.0.6"}
environ {:mvn/version "1.2.0"}}})
;; Loading spartan.spec will create a namespace clojure.spec.alpha for compatibility:
(require 'spartan.spec)
(alias 's 'clojure.spec.alpha)
(require '[graphql-query.core :refer [graphql-query]])
(require '[babashka.curl :as curl])
(require '[clojure.edn :as edn])
(require '[environ.core :refer [env]])
(require '[clojure.tools.cli :refer [parse-opts]])
(require '[clojure.string :as string])
(require '[taoensso.timbre :as log])
(log/set-level! :info)
(defn with-creds [{:keys [token-type]} params]
(if-let [token (env :manetu-token)]
(assoc params :basic-auth [nil token])
(throw (ex-info "MANETU_TOKEN must be set" {}))))
(defn quote-non-option
"return a quoted argument if it is not an option (i.e. does not begin with a '-')"
[option]
(if (= \- (get option 0))
option
(str "'" option "'" ) ))
(defn trace-or-debug?
[]
(contains? #{:debug :trace} (:min-level log/*config*)))
(defn stringify-command
"Turn the curl command list into a string"
[command-list]
(if (nil? command-list)
command-list
(str "curl " (string/join " " (map quote-non-option (rest command-list))))))
(defn json-post [options query]
(let [params (with-creds options
{:headers {"Accept" "application/json"
"Content-Type" "application/json"}
:body (json/generate-string query)
:debug (trace-or-debug?)})
url (or (env :manetu-graphql-url)
(throw (ex-info "MANETU_GRAPHQL_URL must be set" {})))]
(log/debug "post" params "to" url)
(try
(let [{:keys [body] :as r} (curl/post url params)]
(log/debug "command:" (stringify-command (:command r)))
(log/debug "result:" r)
(println body))
(catch Exception e
(let [{:keys [body]} (ex-data e)]
(if (some? body)
(println body)
(log/error e)))))))
(defn graphql! [{:keys [mutation] :as options} req]
(let [q (-> (cond
(map? req) req
(and (vector? req) (-> req first keyword?)) {:queries [req]}
:default {:queries req})
graphql-query)]
(log/debug "query:" q)
(json-post options {:query (cond->> q mutation (str "mutation "))})))
(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: graphcli [options]"
""
"Options:"
options-summary]))
(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"]
["-m" "--mutation"]
["-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))]]])
(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))
(log/trace "Log config " log/*config*)
(graphql! options (edn/read *in*))
(exit 0 "")
(catch Exception e
(exit -1 (ex-message e))))))
;; Local Variables:
;; eval: (clojure-mode)
;; End: