@future-unknown/known 0.0.0-stage → 0.1.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,91 @@
1
+ require("@colors/colors")
2
+ const config = require("../config")
3
+ const project = require("../project")
4
+ const output = require("../output")
5
+ const { accountLabel } = require("../helpers")
6
+
7
+ // Mask a token to its last 4 chars.
8
+ function redact(t) {
9
+ if (!t) return null
10
+ return "•••" + String(t).slice(-4)
11
+ }
12
+
13
+ function head(label, value) {
14
+ const COL = 14
15
+ const pad = label.length >= COL ? " " : " ".repeat(COL - label.length)
16
+ console.log(" " + label.grey + pad + String(value))
17
+ }
18
+
19
+ function sub(label, value) {
20
+ const COL = 16
21
+ const pad = label.length >= COL ? " " : " ".repeat(COL - label.length)
22
+ console.log(" " + label.grey + pad + String(value))
23
+ }
24
+
25
+ // The project binding in effect here, as { ref, from } or { error }.
26
+ function bindingHere() {
27
+ try {
28
+ const bound = project.binding()
29
+ return bound ? { ref: bound.ref, from: bound.from } : null
30
+ } catch (e) {
31
+ return { error: e.message }
32
+ }
33
+ }
34
+
35
+ // Show the resolved config: where sessions live, what each one holds, and which
36
+ // org this directory is bound to. There is no config file and no selected
37
+ // environment — the store is only session files, each carrying the endpoint that
38
+ // issued its token; the binding is a KNOWN_ORG file in the project (or $KNOWN_ORG).
39
+ module.exports = function (argv) {
40
+ const envs = config.listEnvironments()
41
+ const bound = bindingHere()
42
+
43
+ if (output.isJson()) {
44
+ output.data({
45
+ store: config.configDir(),
46
+ project: bound,
47
+ environments: envs.map((env) => ({
48
+ endpoint: env.endpoint,
49
+ alias: env.alias,
50
+ sessions: env.sessions.map((s) => ({
51
+ label: s.label,
52
+ org: s.key,
53
+ org_id: (s.org && s.org.id) || null,
54
+ account: accountLabel(s.account),
55
+ role: s.role || null,
56
+ file: config.sessionFile(env.endpoint, s.key),
57
+ access_token: redact(s.access_token),
58
+ refresh_token: s.refresh_token ? "present" : null,
59
+ scope: s.scope || null,
60
+ created_at: s.created_at || null,
61
+ expires_at: s.expires_at || null
62
+ }))
63
+ }))
64
+ })
65
+ return process.exit(0)
66
+ }
67
+
68
+ console.log()
69
+ head("store", config.configDir() + "/<env>/<org>.json")
70
+ head("sessions", String(envs.reduce((n, e) => n + e.sessions.length, 0)))
71
+ head("project", !bound
72
+ ? "—".grey + (" no " + project.FILE + " file here or above").grey
73
+ : bound.error ? bound.error.red : bound.ref + (" " + bound.from).grey)
74
+
75
+ for (const env of envs) {
76
+ console.log()
77
+ console.log((" " + env.endpoint + (env.alias ? " (" + env.alias + ")" : "")).green)
78
+ for (const s of env.sessions) {
79
+ console.log(" " + s.label.cyan + (" " + ((s.org && s.org.id) || "")).grey)
80
+ sub("account", accountLabel(s.account) || "—".grey)
81
+ sub("role", s.role || "—".grey)
82
+ sub("token", redact(s.access_token) || "—".grey)
83
+ sub("refresh", s.refresh_token ? "present" : "—".grey)
84
+ if (s.scope) sub("scopes", s.scope)
85
+ if (s.expires_at) sub("expires", new Date(s.expires_at).toLocaleString())
86
+ }
87
+ }
88
+
89
+ console.log()
90
+ process.exit(0)
91
+ }
@@ -0,0 +1,58 @@
1
+ require("@colors/colors")
2
+ const config = require("../config")
3
+ const auth = require("../auth")
4
+ const output = require("../output")
5
+ const { run } = require("../helpers")
6
+
7
+ // `known endpoints` list the named environments and where you're signed in
8
+ // `known endpoints <url>` check that a URL is a Known API
9
+ //
10
+ // This is read-only on purpose. Selecting an environment is not a thing you can
11
+ // do: an endpoint is recorded with the token it issued, so you choose one when
12
+ // you sign in (`known login --endpoint staging`) and `--org` carries it from
13
+ // then on.
14
+ module.exports = function (argv) {
15
+ const input = argv._[1]
16
+
17
+ if (input) {
18
+ return run(async () => {
19
+ const url = config.expandEndpoint(input)
20
+ try {
21
+ await auth.fetchWellKnown(url)
22
+ } catch (e) {
23
+ throw new Error("not a Known API: " + url)
24
+ }
25
+ if (output.isJson()) return output.data({ endpoint: url, ok: true })
26
+ output.status("reachable", url, "sign in with `known login --endpoint " + input + "`")
27
+ })
28
+ }
29
+
30
+ const signedIn = new Map()
31
+ for (const s of config.allSessions()) {
32
+ signedIn.set(s.endpoint, (signedIn.get(s.endpoint) || []).concat(s.key))
33
+ }
34
+
35
+ if (output.isJson()) {
36
+ return output.data(
37
+ Object.keys(config.ENDPOINTS).map((name) => ({
38
+ name,
39
+ endpoint: config.ENDPOINTS[name],
40
+ default: config.ENDPOINTS[name] === config.DEFAULT_ENDPOINT,
41
+ orgs: signedIn.get(config.ENDPOINTS[name]) || []
42
+ }))
43
+ )
44
+ }
45
+
46
+ output.commands(
47
+ Object.keys(config.ENDPOINTS).map((name) => {
48
+ const url = config.ENDPOINTS[name]
49
+ const orgs = signedIn.get(url) || []
50
+ const tags = []
51
+ if (url === config.DEFAULT_ENDPOINT) tags.push("default")
52
+ if (orgs.length) tags.push("signed in: " + orgs.join(", "))
53
+ return [name, url + (tags.length ? " (" + tags.join(" · ") + ")" : "")]
54
+ })
55
+ )
56
+ console.log()
57
+ process.exit(0)
58
+ }
@@ -0,0 +1,44 @@
1
+ require("@colors/colors")
2
+ const output = require("../output")
3
+ const { getClient, sessionFor, run, unwrap } = require("../helpers")
4
+
5
+ /**
6
+ * `known <org> feedback <kind> <message…>` — tell Known's team what got in the
7
+ * way. For agents working through the CLI, and for people:
8
+ *
9
+ * known acme feedback missing "no way to rename a table"
10
+ * known acme feedback bug "wiki edit refused an exact match" --about "wiki edit" --ref 3f2c…
11
+ *
12
+ * The server mails it to the team with what it knows of the call (who, which
13
+ * org, from the CLI); nothing about the sender is the client's to assert.
14
+ * The kinds are checked here too, so a typo is said before signing in.
15
+ */
16
+ const KINDS = ["bug", "missing", "confusing", "idea"]
17
+ const USAGE = "usage: known <org> feedback <" + KINDS.join("|") + "> \"<message>\" [--about <tool or command>] [--ref <reference>]"
18
+
19
+ module.exports = function (argv, ref, rest = []) {
20
+ return run(async () => {
21
+ const [kind, ...words] = rest
22
+ if (!kind) throw friendly(USAGE)
23
+ if (!KINDS.includes(kind)) throw friendly("kind must be one of " + KINDS.join(", ") + " — " + USAGE)
24
+ const message = words.join(" ").trim()
25
+ if (!message) throw friendly("nothing to send — say what got in the way. " + USAGE)
26
+
27
+ const session = sessionFor(argv, ref)
28
+ const res = unwrap(await getClient(argv, { session }).call("feedback.send", {
29
+ kind,
30
+ message,
31
+ ...(argv.about ? { about: String(argv.about) } : {}),
32
+ ...(argv.ref ? { ref: String(argv.ref) } : {})
33
+ }))
34
+
35
+ if (output.isJson()) return output.data({ ...res, session: session.label })
36
+ output.status("sent", "feedback", [res.id, "to Known's team"].join(" · "))
37
+ })
38
+ }
39
+
40
+ function friendly(message) {
41
+ const e = new Error(message)
42
+ e.friendly = true
43
+ return e
44
+ }
@@ -0,0 +1,70 @@
1
+ require("@colors/colors")
2
+ const pkg = require("../../package.json")
3
+ const config = require("../config")
4
+ const project = require("../project")
5
+ const output = require("../output")
6
+
7
+ function group(rows, width) {
8
+ console.log()
9
+ output.commands(rows, width)
10
+ }
11
+
12
+ module.exports = function (argv) {
13
+ const sessionRows = [
14
+ ["known login [--endpoint <name>]", "sign in to an org (pick it in the browser)"],
15
+ ["known sessions", "every session on this machine"],
16
+ ["known whoami", "who you are, in every session"],
17
+ ["known logout", "sign out of every session"],
18
+ ["known config", "show resolved config (tokens redacted)"],
19
+ ["known endpoints [<url>]", "list environments / check a URL"]
20
+ ]
21
+
22
+ const orgRows = [
23
+ ["known <org>", "commands available for that org"],
24
+ ["known <org> ping", "prove the session works (token, membership, policy)"],
25
+ ["known <org> post --text \"…\"", "post a note (also --file, or pipe stdin)"],
26
+ ["known <org> wiki [<cmd>]", "the org's wikis: pages (read, write, edit, …) and records (get, set, …)"],
27
+ ["known <org> code [<cmd>]", "source trees held in Known: files (read, write, edit, …), packages, changes, checks"],
28
+ ["known <org> sites [<cmd>]", "sites that sign the org's people in with Known"],
29
+ ["known <org> teams [<cmd>]", "the org's teams: people grouped to be granted wikis together"],
30
+ ["known <org> members [<cmd>]", "the org's people: owners and members, invitations, requests to join"],
31
+ ["known <org> audit", "the org's audit log: who changed people, teams, access and settings, and when (org owners)"],
32
+ ["known <org> feedback <kind> \"…\"", "tell Known's team what got in the way (bug, missing, confusing, idea)"],
33
+ ["known <org> whoami", "who you are in that org"],
34
+ ["known <org> logout", "sign out of that org"],
35
+ ["known <org> rpc [<method>]", "call a JSON-RPC method as that org; alone, list every method"]
36
+ ]
37
+
38
+ const bareRows = [
39
+ ["known ping | post | wiki | code | teams | members | audit | rpc", "the same, as this project's org"],
40
+ ["echo acme > KNOWN_ORG", "bind a directory (and everything under it) to an org"]
41
+ ]
42
+
43
+ const width = output.commandWidth(sessionRows, orgRows, bareRows)
44
+
45
+ console.log()
46
+ console.log(" known".cyan + " " + ("(v" + pkg.version + ")").grey + " " + "~".grey + " " + "the Known CLI".brightYellow)
47
+
48
+ group(sessionRows, width)
49
+ group(orgRows, width)
50
+ group(bareRows, width)
51
+
52
+ // Show what's actually addressable rather than describing it abstractly, and
53
+ // qualify only the orgs that need it.
54
+ const held = config.allSessions()
55
+ const names = held.map((s) => (held.filter((o) => o.key === s.key).length > 1 ? s.label : s.key))
56
+ let bound = null
57
+ try { bound = project.binding() } catch (e) {}
58
+ console.log()
59
+ output.commands([
60
+ [
61
+ " <org>",
62
+ names.length
63
+ ? "signed in: " + [...new Set(names)].join(", ")
64
+ : "an org you've signed into (run `known login` first)"
65
+ ],
66
+ ...(bound ? [[" here", bound.ref + " (" + bound.from + ")"]] : [])
67
+ ], width)
68
+ console.log()
69
+ process.exit(0)
70
+ }
@@ -0,0 +1,50 @@
1
+ const config = require("../config")
2
+ const auth = require("../auth")
3
+ const output = require("../output")
4
+ const { clientFor, accountLabel, run, unwrap } = require("../helpers")
5
+ const sessions = require("./_sessions")
6
+
7
+ // `known login [--endpoint <name|url>]` — sign in to one organization.
8
+ //
9
+ // The endpoint is an argument of signing in, not a mode: it is recorded with the
10
+ // token it produces and never consulted again from anywhere else. There is no
11
+ // --org either — the browser's consent screen IS the org selector, and the CLI
12
+ // learns which org (and which account) was granted from the token itself.
13
+ //
14
+ // Run it again to add another org, another environment, or another account; each
15
+ // sign-in is filed alongside the others.
16
+ module.exports = function (argv) {
17
+ return run(async () => {
18
+ const endpoint = config.expandEndpoint(argv.endpoint) || config.DEFAULT_ENDPOINT
19
+ const bundle = await auth.authenticate(endpoint)
20
+
21
+ // The token carries an org id; whoami turns it into the slug the session is
22
+ // filed under, plus the account and role to label it with.
23
+ const who = unwrap(await clientFor(endpoint, bundle.access_token).auth.whoami())
24
+ const org = who && who.org
25
+ const key = org && (org.slug || org.id)
26
+ if (!key) {
27
+ throw new Error("sign-in returned no organization — known needs an org-scoped token")
28
+ }
29
+
30
+ const replaced = config.matchSessions(key + "@" + config.envLabel(endpoint)).length > 0
31
+ const session = config.writeSession(endpoint, key, bundle, {
32
+ org,
33
+ account: who.user,
34
+ role: who.role
35
+ })
36
+
37
+ if (output.isJson()) {
38
+ const { access_token, refresh_token, ...safe } = session
39
+ return output.data(safe)
40
+ }
41
+
42
+ const account = accountLabel(who.user)
43
+ output.status(
44
+ replaced ? "signed in again to" : "signed in to",
45
+ session.label,
46
+ [account ? "as " + account : null, who.role].filter(Boolean).join(" · ")
47
+ )
48
+ sessions.render()
49
+ })
50
+ }
@@ -0,0 +1,40 @@
1
+ const config = require("../config")
2
+ const output = require("../output")
3
+ const { sessionFor, confirm, run } = require("../helpers")
4
+ const sessions = require("./_sessions")
5
+
6
+ // `known <org> logout` sign out of one session
7
+ // `known logout` sign out of everything (confirms)
8
+ //
9
+ // Sessions are separate sign-ins — possibly different accounts — so signing out
10
+ // of one project leaves the others untouched, and dropping them all asks first.
11
+ module.exports = function (argv, ref) {
12
+ return run(async () => {
13
+ if (ref) {
14
+ const session = sessionFor(argv, ref)
15
+ const removed = config.removeSession(session)
16
+ if (!removed) throw Object.assign(new Error("could not remove " + session.label), { friendly: true })
17
+ if (!output.isJson()) output.status("signed out of", removed.label, null, "muted")
18
+ return sessions.render()
19
+ }
20
+
21
+ const held = config.allSessions()
22
+ if (!held.length) {
23
+ if (!output.isJson()) output.info("not signed in".grey)
24
+ return
25
+ }
26
+
27
+ const ok = await confirm(
28
+ "Sign out of " + held.length + " session" + (held.length === 1 ? "" : "s") +
29
+ " (" + held.map((s) => s.label).join(", ") + ")?",
30
+ argv
31
+ )
32
+ if (!ok) return output.info("aborted".grey)
33
+
34
+ const removed = held.map((s) => config.removeSession(s)).filter(Boolean)
35
+ if (!output.isJson()) {
36
+ output.status("signed out of", removed.map((s) => s.label).join(", "), null, "muted")
37
+ }
38
+ sessions.render()
39
+ })
40
+ }