Skip to content

Commit f3554a3

Browse files
committed
feat: render approvals get as table
1 parent b30359e commit f3554a3

1 file changed

Lines changed: 94 additions & 3 deletions

File tree

‎src/cli/exec-approvals-cli.ts‎

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type { Command } from "commander";
55
import type { ExecApprovalsAgent, ExecApprovalsFile } from "../infra/exec-approvals.js";
66
import { defaultRuntime } from "../runtime.js";
77
import { formatDocsLink } from "../terminal/links.js";
8-
import { theme } from "../terminal/theme.js";
8+
import { isRich, theme } from "../terminal/theme.js";
9+
import { renderTable } from "../terminal/table.js";
910
import { callGatewayFromCli } from "./gateway-rpc.js";
1011
import { nodesCallOpts, resolveNodeId } from "./nodes-cli/rpc.js";
1112
import type { NodesRpcOpts } from "./nodes-cli/types.js";
@@ -24,6 +25,17 @@ type ExecApprovalsCliOpts = NodesRpcOpts & {
2425
agent?: string;
2526
};
2627

28+
function formatAge(msAgo: number) {
29+
const s = Math.max(0, Math.floor(msAgo / 1000));
30+
if (s < 60) return `${s}s`;
31+
const m = Math.floor(s / 60);
32+
if (m < 60) return `${m}m`;
33+
const h = Math.floor(m / 60);
34+
if (h < 24) return `${h}h`;
35+
const d = Math.floor(h / 24);
36+
return `${d}d`;
37+
}
38+
2739
async function readStdin(): Promise<string> {
2840
const chunks: Buffer[] = [];
2941
for await (const chunk of process.stdin) {
@@ -109,8 +121,87 @@ export function registerExecApprovalsCli(program: Command) {
109121
.action(async (opts: ExecApprovalsCliOpts) => {
110122
const nodeId = await resolveTargetNodeId(opts);
111123
const snapshot = await loadSnapshot(opts, nodeId);
112-
const payload = opts.json ? JSON.stringify(snapshot) : JSON.stringify(snapshot, null, 2);
113-
defaultRuntime.log(payload);
124+
if (opts.json) {
125+
defaultRuntime.log(JSON.stringify(snapshot));
126+
return;
127+
}
128+
129+
const rich = isRich();
130+
const heading = (text: string) => (rich ? theme.heading(text) : text);
131+
const muted = (text: string) => (rich ? theme.muted(text) : text);
132+
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
133+
134+
const file = snapshot.file ?? { version: 1 };
135+
const defaults = file.defaults ?? {};
136+
const defaultsParts = [
137+
defaults.security ? `security=${defaults.security}` : null,
138+
defaults.ask ? `ask=${defaults.ask}` : null,
139+
defaults.askFallback ? `askFallback=${defaults.askFallback}` : null,
140+
typeof defaults.autoAllowSkills === "boolean"
141+
? `autoAllowSkills=${defaults.autoAllowSkills ? "on" : "off"}`
142+
: null,
143+
].filter(Boolean) as string[];
144+
const agents = file.agents ?? {};
145+
const allowlistRows: Array<{ Agent: string; Pattern: string; LastUsed: string }> = [];
146+
const now = Date.now();
147+
for (const [agentId, agent] of Object.entries(agents)) {
148+
const allowlist = Array.isArray(agent.allowlist) ? agent.allowlist : [];
149+
for (const entry of allowlist) {
150+
const pattern = entry?.pattern?.trim() ?? "";
151+
if (!pattern) continue;
152+
const lastUsedAt = typeof entry.lastUsedAt === "number" ? entry.lastUsedAt : null;
153+
allowlistRows.push({
154+
Agent: agentId,
155+
Pattern: pattern,
156+
LastUsed: lastUsedAt
157+
? `${formatAge(Math.max(0, now - lastUsedAt))} ago`
158+
: muted("unknown"),
159+
});
160+
}
161+
}
162+
163+
const summaryRows = [
164+
{ Field: "Path", Value: snapshot.path },
165+
{ Field: "Exists", Value: snapshot.exists ? "yes" : "no" },
166+
{ Field: "Hash", Value: snapshot.hash },
167+
{ Field: "Version", Value: String(file.version ?? 1) },
168+
{ Field: "Socket", Value: file.socket?.path ?? "default" },
169+
{ Field: "Defaults", Value: defaultsParts.length > 0 ? defaultsParts.join(", ") : "none" },
170+
{ Field: "Agents", Value: String(Object.keys(agents).length) },
171+
{ Field: "Allowlist", Value: String(allowlistRows.length) },
172+
];
173+
174+
defaultRuntime.log(heading("Approvals"));
175+
defaultRuntime.log(
176+
renderTable({
177+
width: tableWidth,
178+
columns: [
179+
{ key: "Field", header: "Field", minWidth: 8 },
180+
{ key: "Value", header: "Value", minWidth: 24, flex: true },
181+
],
182+
rows: summaryRows,
183+
}).trimEnd(),
184+
);
185+
186+
if (allowlistRows.length === 0) {
187+
defaultRuntime.log("");
188+
defaultRuntime.log(muted("No allowlist entries."));
189+
return;
190+
}
191+
192+
defaultRuntime.log("");
193+
defaultRuntime.log(heading("Allowlist"));
194+
defaultRuntime.log(
195+
renderTable({
196+
width: tableWidth,
197+
columns: [
198+
{ key: "Agent", header: "Agent", minWidth: 8 },
199+
{ key: "Pattern", header: "Pattern", minWidth: 20, flex: true },
200+
{ key: "LastUsed", header: "Last Used", minWidth: 10 },
201+
],
202+
rows: allowlistRows,
203+
}).trimEnd(),
204+
);
114205
});
115206
nodesCallOpts(getCmd);
116207

0 commit comments

Comments
 (0)