@postshiba/cli 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.
- package/LICENSE +21 -0
- package/README.md +149 -2
- package/bin/postshiba.js +11 -0
- package/package.json +41 -4
- package/skills/postshiba-cli/SKILL.md +146 -0
- package/src/cli.js +467 -0
- package/src/client.js +74 -0
- package/src/commands.js +300 -0
- package/src/config.js +91 -0
- package/src/doctor.js +124 -0
- package/src/domains.js +67 -0
- package/src/errors.js +33 -0
- package/src/help.js +91 -0
- package/src/login.js +107 -0
- package/src/menu.js +51 -0
- package/src/mode.js +16 -0
- package/src/pkg.js +9 -0
- package/src/prompts.js +118 -0
- package/src/render.js +177 -0
- package/src/send.js +283 -0
- package/src/skills.js +40 -0
package/src/help.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { actionUsage, actionsFor, commands, resources } from "./commands.js";
|
|
4
|
+
import { VERSION } from "./pkg.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} [topic]
|
|
8
|
+
*/
|
|
9
|
+
export function formatHelp(topic) {
|
|
10
|
+
if (!topic) return generalHelp();
|
|
11
|
+
if (topic === "send") return sendHelp();
|
|
12
|
+
if (topic === "login" || topic === "logout" || topic === "whoami") return generalHelp();
|
|
13
|
+
if (topic === "doctor") return "Usage: postshiba doctor [--json]";
|
|
14
|
+
if (topic === "skills") return "Usage: postshiba skills install [--global] [--target cursor|claude|agents]";
|
|
15
|
+
const rows = actionsFor(topic);
|
|
16
|
+
if (rows.length === 0) return "";
|
|
17
|
+
const lines = [`Usage: postshiba ${topic} <action> [ids]`, "", "Actions:"];
|
|
18
|
+
for (const row of rows) {
|
|
19
|
+
lines.push(` ${actionUsage(row)}`);
|
|
20
|
+
}
|
|
21
|
+
lines.push("", "Pass --data JSON, --data @file.json, or --data - for writes.");
|
|
22
|
+
if (rows.some((row) => row.query.length)) {
|
|
23
|
+
lines.push("Action flags such as --limit VALUE go on the query string. Bracketed flags are optional.");
|
|
24
|
+
}
|
|
25
|
+
if (rows.some((row) => row.cluster)) {
|
|
26
|
+
lines.push("--cluster ID (or POSTSHIBA_CLUSTER_ID) sets X-Capsule-Cluster-Id.");
|
|
27
|
+
}
|
|
28
|
+
return lines.join("\n");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function generalHelp() {
|
|
32
|
+
const width = Math.max(...resources().map((name) => name.length));
|
|
33
|
+
const lines = [
|
|
34
|
+
`postshiba ${VERSION}`,
|
|
35
|
+
"",
|
|
36
|
+
"Usage: postshiba <command>",
|
|
37
|
+
"",
|
|
38
|
+
"Commands:",
|
|
39
|
+
" login Sign in and write the local config file",
|
|
40
|
+
" logout Remove the local config file",
|
|
41
|
+
" whoami Print the signed-in user",
|
|
42
|
+
" send Send an email",
|
|
43
|
+
" doctor Check key, team, cluster, and domain",
|
|
44
|
+
" skills install Copy the agent skill into this project",
|
|
45
|
+
" help [resource] Show this help or help for a resource",
|
|
46
|
+
"",
|
|
47
|
+
"Resources:",
|
|
48
|
+
];
|
|
49
|
+
for (const name of resources()) {
|
|
50
|
+
const actions = commands.filter((row) => row.resource === name).map((row) => row.action);
|
|
51
|
+
lines.push(` ${name.padEnd(width)} ${actions.join(", ")}`);
|
|
52
|
+
}
|
|
53
|
+
lines.push(
|
|
54
|
+
"",
|
|
55
|
+
"Options:",
|
|
56
|
+
" --json Print API JSON. Never prompt.",
|
|
57
|
+
" --no-input Plain mode. Never prompt.",
|
|
58
|
+
" --yes Skip confirm for delete, suspend, release, unassign, cancel,",
|
|
59
|
+
" rotate-secret, flush-queue, remove-ip, detach, bulk-delete",
|
|
60
|
+
" --api-key KEY API key (POSTSHIBA_API_KEY)",
|
|
61
|
+
" --team ID Team id (POSTSHIBA_TEAM_ID)",
|
|
62
|
+
" --base-url URL API base URL (POSTSHIBA_BASE_URL)",
|
|
63
|
+
" -h, --help Show help",
|
|
64
|
+
" --version Print version",
|
|
65
|
+
);
|
|
66
|
+
return lines.join("\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function sendHelp() {
|
|
70
|
+
return [
|
|
71
|
+
"Usage: postshiba send [options]",
|
|
72
|
+
"",
|
|
73
|
+
" --from ADDR from",
|
|
74
|
+
" --to ADDR to (repeatable)",
|
|
75
|
+
" --cc ADDR --bcc ADDR cc / bcc (repeatable)",
|
|
76
|
+
" --reply-to ADDR reply_to",
|
|
77
|
+
" --subject TEXT subject",
|
|
78
|
+
" --text TEXT text",
|
|
79
|
+
" --html HTML html",
|
|
80
|
+
" --template ID template.id",
|
|
81
|
+
" --var KEY=VALUE template.variables (repeatable)",
|
|
82
|
+
" --header \"Name: value\" headers (repeatable)",
|
|
83
|
+
" --arg KEY=VALUE unique_args (repeatable)",
|
|
84
|
+
" --attach PATH attachments (repeatable)",
|
|
85
|
+
" --tenant NAME tenant",
|
|
86
|
+
" --cluster ID X-Capsule-Cluster-Id on POST /api/v1/emails",
|
|
87
|
+
" --sandbox POST /api/v1/teams/:teamId/clusters/:clusterId/sends (requires --cluster)",
|
|
88
|
+
" --idempotency-key KEY Idempotency-Key on the cluster send path (requires --cluster)",
|
|
89
|
+
" --data JSON base body; flags override keys",
|
|
90
|
+
].join("\n");
|
|
91
|
+
}
|
package/src/login.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { request } from "./client.js";
|
|
4
|
+
import { readConfig, removeConfig, writeConfig } from "./config.js";
|
|
5
|
+
import { UsageError } from "./errors.js";
|
|
6
|
+
import { spinning } from "./prompts.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {Record<string, any>} values
|
|
10
|
+
* @param {object} ctx
|
|
11
|
+
* @param {NodeJS.ProcessEnv} ctx.env
|
|
12
|
+
* @param {string} [ctx.configDir]
|
|
13
|
+
* @param {typeof fetch} ctx.fetch
|
|
14
|
+
* @param {{ write: (chunk: string) => unknown }} ctx.stdout
|
|
15
|
+
* @param {boolean} ctx.interactive
|
|
16
|
+
* @param {import("./prompts.js").Prompts} [ctx.prompts]
|
|
17
|
+
*/
|
|
18
|
+
export async function login(values, ctx) {
|
|
19
|
+
let apiKey = values["api-key"] || ctx.env.POSTSHIBA_API_KEY;
|
|
20
|
+
if (!apiKey && ctx.interactive && ctx.prompts) {
|
|
21
|
+
apiKey = await ctx.prompts.password({ message: "API key" });
|
|
22
|
+
}
|
|
23
|
+
if (!apiKey) {
|
|
24
|
+
throw new UsageError("Missing API key. Run postshiba login or set POSTSHIBA_API_KEY.");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let teamId = values.team || ctx.env.POSTSHIBA_TEAM_ID;
|
|
28
|
+
const baseUrl = (values["base-url"] || ctx.env.POSTSHIBA_BASE_URL || "https://app.postshiba.com").replace(/\/$/, "");
|
|
29
|
+
|
|
30
|
+
const me = /** @type {{ email?: string }} */ (
|
|
31
|
+
await spinning(
|
|
32
|
+
ctx.interactive ? ctx.prompts : null,
|
|
33
|
+
"Checking key",
|
|
34
|
+
() => request({ method: "GET", url: `${baseUrl}/api/v1/users/me`, apiKey, fetch: ctx.fetch }),
|
|
35
|
+
(user) => `Signed in as ${/** @type {{ email?: string }} */ (user).email ?? "unknown"}`,
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
if (!teamId && ctx.interactive && ctx.prompts) {
|
|
40
|
+
teamId = await ctx.prompts.text({
|
|
41
|
+
message: "Team id",
|
|
42
|
+
placeholder: "on the API keys page",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @type {Record<string, string>} */
|
|
47
|
+
const saved = { api_key: apiKey };
|
|
48
|
+
if (teamId) saved.team_id = teamId;
|
|
49
|
+
if (values["base-url"] || ctx.env.POSTSHIBA_BASE_URL) saved.base_url = baseUrl;
|
|
50
|
+
|
|
51
|
+
if (teamId && ctx.interactive && ctx.prompts) {
|
|
52
|
+
const clusters = await listClusters(baseUrl, apiKey, teamId, ctx.fetch);
|
|
53
|
+
if (clusters.length === 1) saved.cluster_id = String(clusters[0].id);
|
|
54
|
+
else if (clusters.length > 1) {
|
|
55
|
+
saved.cluster_id = await ctx.prompts.select({
|
|
56
|
+
message: "Default cluster",
|
|
57
|
+
options: clusters.map((item) => ({
|
|
58
|
+
value: String(item.id),
|
|
59
|
+
label: String(item.name ?? item.id),
|
|
60
|
+
})),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
await writeConfig(ctx.env, ctx.configDir, saved);
|
|
66
|
+
ctx.stdout.write(`Signed in as ${me.email}\n`);
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {object} ctx
|
|
72
|
+
* @param {NodeJS.ProcessEnv} ctx.env
|
|
73
|
+
* @param {string} [ctx.configDir]
|
|
74
|
+
* @param {boolean} ctx.interactive
|
|
75
|
+
* @param {import("./prompts.js").Prompts} [ctx.prompts]
|
|
76
|
+
*/
|
|
77
|
+
export async function logout(ctx) {
|
|
78
|
+
await removeConfig(ctx.env, ctx.configDir);
|
|
79
|
+
if (ctx.interactive && ctx.prompts) ctx.prompts.outro("Signed out.");
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {string} baseUrl
|
|
85
|
+
* @param {string} apiKey
|
|
86
|
+
* @param {string} teamId
|
|
87
|
+
* @param {typeof fetch} fetchImpl
|
|
88
|
+
*/
|
|
89
|
+
async function listClusters(baseUrl, apiKey, teamId, fetchImpl) {
|
|
90
|
+
const body = await request({
|
|
91
|
+
method: "GET",
|
|
92
|
+
url: `${baseUrl}/api/v1/teams/${teamId}/clusters`,
|
|
93
|
+
apiKey,
|
|
94
|
+
fetch: fetchImpl,
|
|
95
|
+
});
|
|
96
|
+
return Array.isArray(body) ? body.filter((item) => item && typeof item === "object") : [];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {object} ctx
|
|
101
|
+
* @param {NodeJS.ProcessEnv} ctx.env
|
|
102
|
+
* @param {string} [ctx.configDir]
|
|
103
|
+
*/
|
|
104
|
+
export async function hasSavedKey(ctx) {
|
|
105
|
+
const file = await readConfig(ctx.env, ctx.configDir);
|
|
106
|
+
return Boolean(file.api_key);
|
|
107
|
+
}
|
package/src/menu.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { actionsFor, resources } from "./commands.js";
|
|
4
|
+
import { hasSavedKey } from "./login.js";
|
|
5
|
+
import { colorsFor, wordmark } from "./render.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {object} ctx
|
|
9
|
+
* @param {import("./prompts.js").Prompts} ctx.prompts
|
|
10
|
+
* @param {boolean} ctx.interactive
|
|
11
|
+
* @param {{ write: (chunk: string) => unknown }} ctx.stdout
|
|
12
|
+
* @param {NodeJS.ProcessEnv} ctx.env
|
|
13
|
+
* @param {string} [ctx.configDir]
|
|
14
|
+
* @param {boolean} [ctx.isTTY]
|
|
15
|
+
* @param {(choice: string, action?: string) => Promise<number>} runChoice
|
|
16
|
+
*/
|
|
17
|
+
export async function mainMenu(ctx, runChoice) {
|
|
18
|
+
const c = colorsFor(ctx);
|
|
19
|
+
ctx.prompts.intro(wordmark(c));
|
|
20
|
+
const signedIn = await hasSavedKey(ctx);
|
|
21
|
+
const choice = await ctx.prompts.select({
|
|
22
|
+
message: "What do you want to do?",
|
|
23
|
+
options: [
|
|
24
|
+
{ value: "send", label: "Send an email" },
|
|
25
|
+
{ value: "sending-domains", label: "Add a sending domain" },
|
|
26
|
+
{ value: "clusters", label: "Clusters" },
|
|
27
|
+
{ value: "inboxes", label: "Inboxes" },
|
|
28
|
+
{ value: "templates", label: "Templates" },
|
|
29
|
+
{ value: "events", label: "Events" },
|
|
30
|
+
{ value: "webhooks", label: "Webhooks" },
|
|
31
|
+
{ value: "doctor", label: "Check my setup" },
|
|
32
|
+
{ value: signedIn ? "logout" : "login", label: signedIn ? "Sign out" : "Sign in" },
|
|
33
|
+
{ value: "skills", label: "Install the agent skill" },
|
|
34
|
+
{ value: "quit", label: "Quit" },
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
if (choice === "quit") {
|
|
38
|
+
ctx.prompts.outro("Later.");
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
if (choice === "sending-domains") return runChoice("sending-domains", "create");
|
|
42
|
+
if (resources().includes(choice)) {
|
|
43
|
+
const rows = actionsFor(choice);
|
|
44
|
+
const action = await ctx.prompts.select({
|
|
45
|
+
message: `${choice}`,
|
|
46
|
+
options: rows.map((row) => ({ value: row.action, label: row.action })),
|
|
47
|
+
});
|
|
48
|
+
return runChoice(choice, action);
|
|
49
|
+
}
|
|
50
|
+
return runChoice(choice);
|
|
51
|
+
}
|
package/src/mode.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One decision at the process boundary. Interactive only when both
|
|
5
|
+
* streams are TTYs, --json/--no-input are absent, and CI is unset.
|
|
6
|
+
*
|
|
7
|
+
* @param {{ json?: boolean, "no-input"?: boolean }} flags
|
|
8
|
+
* @param {{ env?: NodeJS.ProcessEnv, isTTY?: boolean, stdin?: { isTTY?: boolean }, stdout?: { isTTY?: boolean } }} io
|
|
9
|
+
*/
|
|
10
|
+
export function isInteractive(flags, io) {
|
|
11
|
+
if (flags.json || flags["no-input"]) return false;
|
|
12
|
+
if (io.env && io.env.CI) return false;
|
|
13
|
+
if (io.isTTY === false) return false;
|
|
14
|
+
if (io.isTTY === true) return true;
|
|
15
|
+
return Boolean(io.stdin && io.stdin.isTTY && io.stdout && io.stdout.isTTY);
|
|
16
|
+
}
|
package/src/pkg.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
export const PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
8
|
+
export const VERSION = JSON.parse(readFileSync(join(PACKAGE_ROOT, "package.json"), "utf8")).version;
|
|
9
|
+
export const SKILL_FILE = join(PACKAGE_ROOT, "skills/postshiba-cli/SKILL.md");
|
package/src/prompts.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import * as clack from "@clack/prompts";
|
|
4
|
+
import { CancelError } from "./errors.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {object} Prompts
|
|
8
|
+
* @property {(opts: { message: string, placeholder?: string, defaultValue?: string }) => Promise<string>} text
|
|
9
|
+
* @property {(opts: { message: string }) => Promise<string>} password
|
|
10
|
+
* @property {(opts: { message: string, options: { value: string, label: string, hint?: string }[] }) => Promise<string>} select
|
|
11
|
+
* @property {(opts: { message: string }) => Promise<boolean>} confirm
|
|
12
|
+
* @property {(message: string) => void} intro
|
|
13
|
+
* @property {(message: string) => void} outro
|
|
14
|
+
* @property {(message: string, title?: string) => void} note
|
|
15
|
+
* @property {() => { start: (message?: string) => void, stop: (message?: string) => void, error: (message?: string) => void }} spinner
|
|
16
|
+
* @property {(message: string) => void} [cancel]
|
|
17
|
+
* @property {{ success: (message: string) => void, error: (message: string) => void, info: (message: string) => void }} [log]
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {unknown} value
|
|
22
|
+
*/
|
|
23
|
+
function unwrap(value) {
|
|
24
|
+
if (clack.isCancel(value)) throw new CancelError();
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @template T
|
|
30
|
+
* @param {Prompts | null | undefined} prompts
|
|
31
|
+
* @param {string} label
|
|
32
|
+
* @param {() => Promise<T>} work
|
|
33
|
+
* @param {(result: T) => string} done
|
|
34
|
+
* @returns {Promise<T>}
|
|
35
|
+
*/
|
|
36
|
+
export async function spinning(prompts, label, work, done) {
|
|
37
|
+
if (!prompts) return work();
|
|
38
|
+
const spin = prompts.spinner();
|
|
39
|
+
spin.start(label);
|
|
40
|
+
try {
|
|
41
|
+
const result = await work();
|
|
42
|
+
spin.stop(done(result));
|
|
43
|
+
return result;
|
|
44
|
+
} catch (err) {
|
|
45
|
+
spin.error(`${label} failed`);
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @returns {Prompts} */
|
|
51
|
+
export function createClackPrompts() {
|
|
52
|
+
return {
|
|
53
|
+
async text(opts) {
|
|
54
|
+
return String(
|
|
55
|
+
unwrap(
|
|
56
|
+
await clack.text({
|
|
57
|
+
message: opts.message,
|
|
58
|
+
placeholder: opts.placeholder,
|
|
59
|
+
defaultValue: opts.defaultValue,
|
|
60
|
+
}),
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
async password(opts) {
|
|
65
|
+
return String(unwrap(await clack.password({ message: opts.message })));
|
|
66
|
+
},
|
|
67
|
+
async select(opts) {
|
|
68
|
+
return String(
|
|
69
|
+
unwrap(
|
|
70
|
+
await clack.select({
|
|
71
|
+
message: opts.message,
|
|
72
|
+
options: opts.options,
|
|
73
|
+
}),
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
},
|
|
77
|
+
async confirm(opts) {
|
|
78
|
+
return Boolean(unwrap(await clack.confirm({ message: opts.message })));
|
|
79
|
+
},
|
|
80
|
+
intro(message) {
|
|
81
|
+
clack.intro(message);
|
|
82
|
+
},
|
|
83
|
+
outro(message) {
|
|
84
|
+
clack.outro(message);
|
|
85
|
+
},
|
|
86
|
+
note(message, title) {
|
|
87
|
+
clack.note(message, title);
|
|
88
|
+
},
|
|
89
|
+
spinner() {
|
|
90
|
+
const spin = clack.spinner();
|
|
91
|
+
return {
|
|
92
|
+
start(message) {
|
|
93
|
+
spin.start(message);
|
|
94
|
+
},
|
|
95
|
+
stop(message) {
|
|
96
|
+
spin.stop(message);
|
|
97
|
+
},
|
|
98
|
+
error(message) {
|
|
99
|
+
spin.error(message);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
cancel(message) {
|
|
104
|
+
clack.cancel(message);
|
|
105
|
+
},
|
|
106
|
+
log: {
|
|
107
|
+
success(message) {
|
|
108
|
+
clack.log.success(message);
|
|
109
|
+
},
|
|
110
|
+
error(message) {
|
|
111
|
+
clack.log.error(message);
|
|
112
|
+
},
|
|
113
|
+
info(message) {
|
|
114
|
+
clack.log.info(message);
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
package/src/render.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { createColors } from "picocolors";
|
|
4
|
+
|
|
5
|
+
/** @typedef {ReturnType<typeof createColors>} Palette */
|
|
6
|
+
|
|
7
|
+
const SECRET_KEYS = new Set(["dkim_private_key", "provider_resources"]);
|
|
8
|
+
const STATUS_KEYS = new Set([
|
|
9
|
+
"status",
|
|
10
|
+
"desired_status",
|
|
11
|
+
"dkim_status",
|
|
12
|
+
"return_path_status",
|
|
13
|
+
"dmarc_status",
|
|
14
|
+
"mx_status",
|
|
15
|
+
"drain_status",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {{ env?: NodeJS.ProcessEnv, stdout?: { isTTY?: boolean, write?: (chunk: string) => unknown }, isTTY?: boolean, interactive?: boolean }} io
|
|
20
|
+
*/
|
|
21
|
+
export function colorsFor(io) {
|
|
22
|
+
const env = io.env ?? {};
|
|
23
|
+
if (env.FORCE_COLOR) return createColors(true);
|
|
24
|
+
if (env.NO_COLOR) return createColors(false);
|
|
25
|
+
return createColors(Boolean(io.interactive || io.isTTY || (io.stdout && io.stdout.isTTY)));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {unknown} value
|
|
30
|
+
* @returns {unknown}
|
|
31
|
+
*/
|
|
32
|
+
export function redact(value) {
|
|
33
|
+
if (Array.isArray(value)) return value.map(redact);
|
|
34
|
+
if (value && typeof value === "object") {
|
|
35
|
+
/** @type {Record<string, unknown>} */
|
|
36
|
+
const out = {};
|
|
37
|
+
for (const [key, item] of Object.entries(value)) {
|
|
38
|
+
if (SECRET_KEYS.has(key)) continue;
|
|
39
|
+
out[key] = redact(item);
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {{ write: (chunk: string) => unknown }} stdout
|
|
48
|
+
* @param {unknown} value
|
|
49
|
+
*/
|
|
50
|
+
export function writeJson(stdout, value) {
|
|
51
|
+
stdout.write(`${JSON.stringify(redact(value), null, 2)}\n`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {Palette} c
|
|
56
|
+
*/
|
|
57
|
+
export function wordmark(c) {
|
|
58
|
+
const letters = ["p", "o", "s", "t", "s", "h", "i", "b", "a"];
|
|
59
|
+
const paints = [c.cyan, c.magenta, c.yellow, c.green, c.blue, c.cyan, c.magenta, c.yellow, c.green];
|
|
60
|
+
const title = letters.map((ch, i) => paints[i % paints.length](c.bold(ch))).join(" ");
|
|
61
|
+
const line = c.dim("─".repeat(21));
|
|
62
|
+
return [` ${c.dim("╭")} ${title} ${c.dim("╮")}`, ` ${line}`, ` ${c.dim("mail from the terminal")}`].join("\n");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {unknown} value
|
|
67
|
+
* @param {string} key
|
|
68
|
+
* @param {Palette} c
|
|
69
|
+
*/
|
|
70
|
+
export function paintStatus(value, key, c) {
|
|
71
|
+
const text = value === null || value === undefined ? "" : String(value);
|
|
72
|
+
if (key === "sending_ready") return value ? c.green("ready") : c.red("not ready");
|
|
73
|
+
if (key === "enabled" || key === "published" || key === "primary") {
|
|
74
|
+
if (value === true) return c.green(text);
|
|
75
|
+
if (value === false) return c.dim(text);
|
|
76
|
+
}
|
|
77
|
+
if (key === "suspended") return value ? c.red("suspended") : c.green("active");
|
|
78
|
+
const lower = text.toLowerCase();
|
|
79
|
+
if (["ready", "verified", "active", "delivered", "pass"].includes(lower)) return c.green(text);
|
|
80
|
+
if (["pending", "not_required"].includes(lower)) return c.yellow(text);
|
|
81
|
+
if (["failed", "suspended", "bounce", "dropped"].includes(lower)) return c.red(text);
|
|
82
|
+
if (STATUS_KEYS.has(key)) return c.yellow(text);
|
|
83
|
+
return text;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {unknown} value
|
|
88
|
+
*/
|
|
89
|
+
function cell(value) {
|
|
90
|
+
if (value === null || value === undefined) return "";
|
|
91
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
92
|
+
return String(value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @param {Record<string, unknown>[]} rows
|
|
97
|
+
* @param {string[]} columns
|
|
98
|
+
* @param {Palette} c
|
|
99
|
+
*/
|
|
100
|
+
export function formatTable(rows, columns, c) {
|
|
101
|
+
const widths = columns.map((col) =>
|
|
102
|
+
Math.max(col.length, ...rows.map((item) => cell(item[col]).length)),
|
|
103
|
+
);
|
|
104
|
+
const header = columns.map((col, i) => c.dim(col.toUpperCase().padEnd(widths[i] ?? 0))).join(" ");
|
|
105
|
+
const lines = [header];
|
|
106
|
+
for (const item of rows) {
|
|
107
|
+
lines.push(
|
|
108
|
+
columns
|
|
109
|
+
.map((col, i) => {
|
|
110
|
+
const raw = cell(item[col]);
|
|
111
|
+
const painted = paintStatus(item[col], col, c);
|
|
112
|
+
const pad = (widths[i] ?? 0) - raw.length;
|
|
113
|
+
return painted + " ".repeat(Math.max(0, pad));
|
|
114
|
+
})
|
|
115
|
+
.join(" "),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return lines.join("\n");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {Record<string, unknown>} value
|
|
123
|
+
* @param {Palette} c
|
|
124
|
+
*/
|
|
125
|
+
export function formatSummary(value, c) {
|
|
126
|
+
const cleaned = /** @type {Record<string, unknown>} */ (redact(value));
|
|
127
|
+
const keys = Object.keys(cleaned);
|
|
128
|
+
const width = Math.max(0, ...keys.map((key) => key.length));
|
|
129
|
+
return keys
|
|
130
|
+
.map((key) => {
|
|
131
|
+
const raw = cleaned[key];
|
|
132
|
+
const display =
|
|
133
|
+
raw && typeof raw === "object" ? JSON.stringify(raw) : paintStatus(raw, key, c);
|
|
134
|
+
return ` ${c.dim(key.padEnd(width))} ${display}`;
|
|
135
|
+
})
|
|
136
|
+
.join("\n");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param {unknown} value
|
|
141
|
+
*/
|
|
142
|
+
export function asRecords(value) {
|
|
143
|
+
if (Array.isArray(value)) return value.filter((item) => item && typeof item === "object");
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {unknown} domain
|
|
149
|
+
*/
|
|
150
|
+
export function domainDnsRows(domain) {
|
|
151
|
+
if (!domain || typeof domain !== "object") return [];
|
|
152
|
+
const d = /** @type {Record<string, unknown>} */ (domain);
|
|
153
|
+
/** @type {{ type: string, name: string, value: string }[]} */
|
|
154
|
+
const rows = [];
|
|
155
|
+
if (d.dkim_cname_host && d.dkim_cname_target) {
|
|
156
|
+
rows.push({ type: "CNAME", name: String(d.dkim_cname_host), value: String(d.dkim_cname_target) });
|
|
157
|
+
}
|
|
158
|
+
if (d.dkim_manual && d.dkim_txt && d.dkim_cname_host) {
|
|
159
|
+
rows.push({ type: "TXT", name: String(d.dkim_cname_host), value: String(d.dkim_txt) });
|
|
160
|
+
}
|
|
161
|
+
if (d.return_path_host && d.return_path_target) {
|
|
162
|
+
rows.push({ type: "CNAME", name: String(d.return_path_host), value: String(d.return_path_target) });
|
|
163
|
+
}
|
|
164
|
+
if (d.dmarc_host && d.dmarc_record) {
|
|
165
|
+
rows.push({ type: "TXT", name: String(d.dmarc_host), value: String(d.dmarc_record) });
|
|
166
|
+
}
|
|
167
|
+
return rows;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @param {unknown} domain
|
|
172
|
+
*/
|
|
173
|
+
export function domainVerified(domain) {
|
|
174
|
+
if (!domain || typeof domain !== "object") return false;
|
|
175
|
+
const d = /** @type {Record<string, unknown>} */ (domain);
|
|
176
|
+
return d.dkim_status === "verified" && d.return_path_status === "verified";
|
|
177
|
+
}
|