feat(cli): add --json to pi2 pairing
The Neovim plugin needs the mint payload (URL, code, expiry, QR matrix, every
advertised base) to draw a scannable code, but the CLI only printed a human
report — so `:PiWebPair` spoke HTTP itself, duplicating the CLI token handling
and requiring `curl` in the editor.
`pi2 pairing --json` prints the normalised payload instead:
{ url, code, expiresAt?, qr: { version, ecc, mask, size, rows }, bases[] }
Same fields the human report renders, so the terminal QR and the float always
agree, and `--base` still pins the link.
This commit is contained in:
1 parent
f854ba8d4f
commit
8a39e65def
2 files changed
+69
-3
No files matched your search
+27
-3
@@ -82,7 +82,7 @@ Usage:
|
|||||||
pi2 open [--print] [--no-browser] open the web console signed in (local only)
|
pi2 open [--print] [--no-browser] open the web console signed in (local only)
|
||||||
pi2 config [--json] resolved configuration and sources
|
pi2 config [--json] resolved configuration and sources
|
||||||
pi2 doctor [--json] environment checks
|
pi2 doctor [--json] environment checks
|
||||||
pi2 pairing [--base <url>] create a pairing code (URL + code + QR)
|
pi2 pairing [--base <url>] [--json] create a pairing code (URL + code + QR)
|
||||||
pi2 install-service [--dry-run|--write] systemd user unit / LaunchAgent
|
pi2 install-service [--dry-run|--write] systemd user unit / LaunchAgent
|
||||||
pi2 uninstall-service [--dry-run|--write] remove the unit
|
pi2 uninstall-service [--dry-run|--write] remove the unit
|
||||||
pi2 service status|start|stop|enable|disable control that unit (status [--json])
|
pi2 service status|start|stop|enable|disable control that unit (status [--json])
|
||||||
@@ -93,7 +93,9 @@ does not exit in time; -t, --timeout <sec> configures the wait timeout (default
|
|||||||
\`pi2 open\` flags: --print prints the URL only; --no-browser prints it instead
|
\`pi2 open\` flags: --print prints the URL only; --no-browser prints it instead
|
||||||
of launching a browser (--print implies --no-browser).
|
of launching a browser (--print implies --no-browser).
|
||||||
\`pi2 pairing\` flags: --base <url> pins the link to one of the addresses the
|
\`pi2 pairing\` flags: --base <url> pins the link to one of the addresses the
|
||||||
daemon advertises (it prints the alternatives; anything else is rejected).
|
daemon advertises (it prints the alternatives; anything else is rejected);
|
||||||
|
--json prints the normalised mint payload ({url, code, expiresAt, qr, bases})
|
||||||
|
for tools and editor plugins instead of the human report.
|
||||||
The link prefers a phone-reachable address: the public URL when one is
|
The link prefers a phone-reachable address: the public URL when one is
|
||||||
configured, the primary LAN address, and loopback otherwise.
|
configured, the primary LAN address, and loopback otherwise.
|
||||||
|
|
||||||
@@ -389,6 +391,8 @@ export interface PairingOptions {
|
|||||||
print?: (line: string) => void;
|
print?: (line: string) => void;
|
||||||
/** `--base <url>`: pin the link to one of the addresses the daemon advertises. */
|
/** `--base <url>`: pin the link to one of the addresses the daemon advertises. */
|
||||||
base?: string | null;
|
base?: string | null;
|
||||||
|
/** `--json`: emit the mint payload (url/code/expiry/QR matrix/bases) instead of a report. */
|
||||||
|
json?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** One address the daemon says it is reachable on (`bases[]` in the mint response). */
|
/** One address the daemon says it is reachable on (`bases[]` in the mint response). */
|
||||||
@@ -508,6 +512,25 @@ export async function runPairing(options: PairingOptions): Promise<number> {
|
|||||||
const qr = pairingQrOf(response.body.qr);
|
const qr = pairingQrOf(response.body.qr);
|
||||||
const bases = pairingBasesOf(response.body.bases);
|
const bases = pairingBasesOf(response.body.bases);
|
||||||
|
|
||||||
|
if (options.json === true) {
|
||||||
|
// The same payload the HTTP mint returned, normalised: `:PiWebPair` in
|
||||||
|
// pi2.nvim renders this instead of talking HTTP itself.
|
||||||
|
print(
|
||||||
|
JSON.stringify(
|
||||||
|
{
|
||||||
|
url,
|
||||||
|
code,
|
||||||
|
...(expiresAt === undefined ? {} : { expiresAt }),
|
||||||
|
...(qr === undefined ? {} : { qr }),
|
||||||
|
bases,
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
print(`url ${url}`);
|
print(`url ${url}`);
|
||||||
print(`code ${code}`);
|
print(`code ${code}`);
|
||||||
if (expiresAt !== undefined) {
|
if (expiresAt !== undefined) {
|
||||||
@@ -837,7 +860,7 @@ export async function main(argv: readonly string[], options: Pi2Options = {}): P
|
|||||||
case "pair": {
|
case "pair": {
|
||||||
const parsed = parseArgs(rest, {
|
const parsed = parseArgs(rest, {
|
||||||
values: ["--base"],
|
values: ["--base"],
|
||||||
booleans: ["--help"],
|
booleans: ["--json", "--help"],
|
||||||
aliases: { "-h": "--help" },
|
aliases: { "-h": "--help" },
|
||||||
});
|
});
|
||||||
if (boolFlag(parsed.flags, "--help")) {
|
if (boolFlag(parsed.flags, "--help")) {
|
||||||
@@ -849,6 +872,7 @@ export async function main(argv: readonly string[], options: Pi2Options = {}): P
|
|||||||
client,
|
client,
|
||||||
print: out,
|
print: out,
|
||||||
base: stringFlag(parsed.flags, "--base"),
|
base: stringFlag(parsed.flags, "--base"),
|
||||||
|
json: boolFlag(parsed.flags, "--json"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
case "install-service":
|
case "install-service":
|
||||||
|
|||||||
@@ -398,6 +398,48 @@ describe("pi2 pairing", () => {
|
|||||||
expect(lines.some((line) => /[█▀▄]/.test(line))).toBe(false);
|
expect(lines.some((line) => /[█▀▄]/.test(line))).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("emits the normalised mint payload with --json (no terminal art)", async () => {
|
||||||
|
const lines: string[] = [];
|
||||||
|
const client = fakeClient({ pair: minted() });
|
||||||
|
|
||||||
|
const code = await runPairing({ client, print: (line) => lines.push(line), json: true });
|
||||||
|
|
||||||
|
expect(code).toBe(0);
|
||||||
|
const payload = JSON.parse(lines.join("\n")) as Record<string, unknown>;
|
||||||
|
expect(payload.url).toBe("http://192.168.2.10:7712/pair/ABCD2345");
|
||||||
|
expect(payload.code).toBe("ABCD2345");
|
||||||
|
expect(typeof payload.expiresAt).toBe("number");
|
||||||
|
expect(payload.qr).toMatchObject({ version: 2, size: 25 });
|
||||||
|
expect((payload.qr as { rows: string[] }).rows).toHaveLength(25);
|
||||||
|
expect(payload.bases).toEqual([
|
||||||
|
{ kind: "lan", name: "wlan0", address: "192.168.2.10", url: "http://192.168.2.10:7712" },
|
||||||
|
{ kind: "loopback", name: "loopback", address: "127.0.0.1", url: "http://127.0.0.1:7712" },
|
||||||
|
]);
|
||||||
|
expect(lines.some((line) => /[█▀▄]/.test(line))).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("omits qr/expiresAt from --json when the daemon sent none", async () => {
|
||||||
|
const lines: string[] = [];
|
||||||
|
const client = fakeClient({ pair: minted({ qr: null, expiresAt: undefined }) });
|
||||||
|
|
||||||
|
await runPairing({ client, print: (line) => lines.push(line), json: true });
|
||||||
|
|
||||||
|
const payload = JSON.parse(lines.join("\n")) as Record<string, unknown>;
|
||||||
|
expect(payload).not.toHaveProperty("qr");
|
||||||
|
expect(payload).not.toHaveProperty("expiresAt");
|
||||||
|
expect(payload.code).toBe("ABCD2345");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reaches --json through the dispatcher", async () => {
|
||||||
|
const lines: string[] = [];
|
||||||
|
const client = fakeClient({ pair: minted() });
|
||||||
|
|
||||||
|
const code = await main(["pairing", "--json"], { out: (line) => lines.push(line), client: () => client });
|
||||||
|
|
||||||
|
expect(code).toBe(0);
|
||||||
|
expect(JSON.parse(lines.join("\n"))).toMatchObject({ code: "ABCD2345" });
|
||||||
|
});
|
||||||
|
|
||||||
it("exits non-zero when the daemon rejects the request", async () => {
|
it("exits non-zero when the daemon rejects the request", async () => {
|
||||||
await expect(
|
await expect(
|
||||||
runPairing({ client: fakeClient({ postError: new CliError("pair failed: E_UNAUTHORIZED") }) }),
|
runPairing({ client: fakeClient({ postError: new CliError("pair failed: E_UNAUTHORIZED") }) }),
|
||||||
|
|||||||
Reference in new issue
Block a user