@future-unknown/known 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ const config = require("../config")
2
+ const project = require("../project")
3
+ const output = require("../output")
4
+ const { getClient, sessionFor, accountLabel, run, unwrap } = require("../helpers")
5
+
6
+ // `known whoami` who you are here: the project's org when a KNOWN_ORG file
7
+ // or $KNOWN_ORG binds one, else every session on this machine
8
+ // `known acme whoami` just that one
9
+ //
10
+ // Unbound, this reports on every session rather than erroring: with one session
11
+ // per org — and possibly a different account per org — "who am I" is genuinely
12
+ // a list.
13
+ module.exports = function (argv, ref) {
14
+ return run(async () => {
15
+ const single = !!(ref || project.binding())
16
+ const targets = single ? [sessionFor(argv, ref)] : config.allSessions()
17
+
18
+ if (!targets.length) {
19
+ const e = new Error("not signed in — run `known login`")
20
+ e.friendly = true
21
+ throw e
22
+ }
23
+
24
+ const results = []
25
+ for (const session of targets) {
26
+ try {
27
+ const client = getClient(argv, { session })
28
+ const who = unwrap(await client.auth.whoami())
29
+ results.push({ session, who: { ...who, handle: await handleOf(client) } })
30
+ } catch (e) {
31
+ results.push({ session, error: e.message })
32
+ }
33
+ }
34
+
35
+ if (output.isJson()) {
36
+ const shaped = results.map((r) =>
37
+ r.error
38
+ ? { session: r.session.label, error: r.error }
39
+ : { session: r.session.label, from: r.session.source || null, endpoint: r.session.endpoint, ...r.who }
40
+ )
41
+ return output.data(single ? shaped[0] : shaped)
42
+ }
43
+
44
+ for (const result of results) {
45
+ if (result.error) {
46
+ output.record([["session", result.session.label], ["error", result.error]])
47
+ continue
48
+ }
49
+ const who = result.who
50
+ const org = who.org || {}
51
+ output.record([
52
+ ["session", result.session.label],
53
+ ...(result.session.source ? [["from", result.session.source]] : []),
54
+ ["endpoint", result.session.endpoint],
55
+ ["org", (org.name || result.session.key) + (org.slug ? " (" + org.slug + ")" : "")],
56
+ ["account", accountLabel(who.user)],
57
+ ["handle", who.handle ? "@" + who.handle : "none yet — choose one in Known"],
58
+ ["role", who.role],
59
+ ["permissions", Array.isArray(who.permissions) ? who.permissions.length + " actions" : null]
60
+ ])
61
+ }
62
+
63
+ // A non-zero exit when nothing could be verified keeps scripts honest.
64
+ if (results.every((r) => r.error)) throw new Error(results[0].error)
65
+ })
66
+ }
67
+
68
+ // The person's handle, or null when they have
69
+ // none or the server is older than handles.
70
+ async function handleOf(client) {
71
+ try {
72
+ return unwrap(await client.handle.get()).handle || null
73
+ } catch {
74
+ return null
75
+ }
76
+ }