Four things that each made keyman quietly less useful than it looked. **Clipboard.** `pbcopy` was spawned unconditionally, with a comment admitting it. Copy is now a list of commands per platform — pbcopy, clip, and wl-copy / xclip / xsel tried in order on everything else, because there is no single answer under Linux and trying them beats detecting the session type. Only an absent tool advances to the next candidate: one that ran and refused has an opinion. And if nothing is installed the key is printed, since "give me this public key" is answerable without a clipboard and used to be a dead end everywhere but macOS. Verified the round trip through real pbcopy/pbpaste. **Home directories.** `/home/<user>` was hardcoded — wrong on the platform this was written on. A named user is now looked for beside the current user's home first, which is right wherever homes live together whatever that directory is called, then in /home and /Users, and the failure names every path tried instead of feeding a nonexistent one to readdir. For the current user, `HOME` still wins, with `os.userInfo()` behind it: `process.env.HOME || ''` made an unset HOME fatal, which it is not in a cron job or a container. **Keys that are not named id_*.** A key called `deploy_ed25519` was absent from every menu with nothing said. It still is — the vault stores `<name minus id_>/id_<name>.age` and decrypt rebuilds the filename from the directory, so relaxing discovery means changing the on-disk layout, which the plan sizes as its largest single item and is not folded in here. What it does do is say so: any file whose first line carries a private key header and whose name lacks the prefix is now reported, per directory, with the reason. A bounded 64-byte read, because classifying a key is no reason to load one. **Plaintext hygiene.** A "Clear decrypted keys" entry, defaulting to no and listing what it would delete first, and a vault `.gitignore` written on first run covering the age identity and the tmp directory — which the README asked the user to do by hand. Never overwritten, and silent about a configured directory that sits outside the vault, since a .gitignore cannot speak for a path above itself and pretending otherwise reads as protection that is absent.
57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import { runTool, ToolNotFoundError } from './keyman.utils.js';
|
|
|
|
/** A clipboard command and the argv it wants, in the order they are tried. */
|
|
interface ClipboardTool {
|
|
binary: string;
|
|
args: string[];
|
|
}
|
|
|
|
/**
|
|
* The clipboard commands worth trying on a platform, best first.
|
|
*
|
|
* Linux is a list rather than a choice because there is no single answer:
|
|
* `wl-copy` under Wayland, `xclip`/`xsel` under X11, and a user may have any
|
|
* subset installed. Trying them in order and moving on from an absent one costs a
|
|
* failed spawn and removes the need to detect the session type.
|
|
*/
|
|
export function clipboardTools(platform: string = process.platform): ClipboardTool[] {
|
|
switch (platform) {
|
|
case 'darwin':
|
|
return [{ binary: 'pbcopy', args: [] }];
|
|
case 'win32':
|
|
return [{ binary: 'clip', args: [] }];
|
|
default:
|
|
return [
|
|
{ binary: 'wl-copy', args: [] },
|
|
{ binary: 'xclip', args: ['-selection', 'clipboard'] },
|
|
{ binary: 'xsel', args: ['--clipboard', '--input'] },
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Puts `text` on the system clipboard.
|
|
*
|
|
* keyman used to spawn `pbcopy` unconditionally, with a comment saying so — which
|
|
* made "copy public key" a dead end on every platform but macOS, and reported it
|
|
* as a clipboard failure rather than as a missing tool.
|
|
*
|
|
* @returns the command that took it, or null if none was available
|
|
*/
|
|
export async function copyToClipboard(text: string, platform?: string): Promise<string | null> {
|
|
for (const { binary, args } of clipboardTools(platform)) {
|
|
try {
|
|
await runTool(binary, args, { input: text });
|
|
return binary;
|
|
} catch (error) {
|
|
// Only an absent tool is worth trying the next candidate for. One that ran
|
|
// and refused has an opinion, and repeating the paste elsewhere is not it.
|
|
if (!(error instanceof ToolNotFoundError)) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|