-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghe.cljs
64 lines (52 loc) · 2.3 KB
/
ghe.cljs
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
#!/usr/bin/env nbb
;; Copyright (c) 2024, Viasat, Inc
;; Licensed under EPL 2.0
(ns ghe
(:require [clojure.string :as S]
[promesa.core :as P]
[viasat.util :refer [parse-opts Eprintln Epprint]]
[viasat.schema-print :refer [schema-print]]
[viasat.apis.github :as github]
["dotenv$default" :as dotenv]))
(def usage "
Usage:
ghe [options] <endpoint> <action> <args>...
Options:
--debug Show debug/trace output (stderr) [env: DEBUG]
--base-url URL Github API base URL [env: GITHUB_BASE_URL]
[default: 'https://api.github.com']
--github-token TOKEN Github Personal Access Token [env: GITHUB_TOKEN]
--owner OWNER Github Owner [env: GITHUB_OWNER]
--repo REPO Github Repository [env: GITHUB_REPO]
--table Print results as a table
--fields FIELDS Fields to print (comma or space separated)
--sort FIELD Sort using FIELD
--rsort FIELD Reverse sort using FIELD
--field-max FIELD_MAX Maximum field width (when not --verbose) [default: 100]
--no-headers Skip printing headers
--json Print full results as JSON (default)
--edn Print full results as EDN
--pretty Pretty print --json or --edn")
(def TIME-REGEX #"-time$")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(dotenv/config #js {:path ".secrets"})
(P/let
[cfg (parse-opts usage *command-line-args*)
{:keys [debug base-url github-token owner repo table
endpoint action args]} cfg
_ (when debug (Eprintln "Settings:") (Epprint cfg))
schema {:time-regex TIME-REGEX}
client (github/ghe (merge {:auth github-token :baseUrl base-url}
(when debug {:log js/console})))
cmd-opts (into {} (map #(S/split % #"[:=]") args))
resp (github/REST client endpoint action
(merge (when owner {:owner owner})
(when repo {:repo repo})
cmd-opts))
data (:data resp)
result (if (and (map? data) (contains? data :total_count))
(first (vals (dissoc data :total_count)))
data)]
(if table
(schema-print result schema cfg)
(schema-print result schema (merge {:json true} cfg))))