Files
ansiblings/packages/keyman/src/keyman.home.ts
T
Benjamin Diedrichsen 0993a4d3bb [keyman] portability, and stop keys from being silently invisible
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.
2026-07-30 15:42:35 +02:00

71 lines
2.3 KiB
TypeScript

import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
/** The answer the USER prompt defaults to: whoever is running keyman. */
export const CURRENT_USER = '@current';
/**
* The home directory of the current user.
*
* `HOME` first, because a user who set it meant it, and `os.userInfo()` after,
* which reads the passwd database and so still answers when `HOME` is unset — a
* cron job, a `su` without `-l`, a container entrypoint. `process.env.HOME || ''`
* treated all of those as a fatal error.
*/
function currentHome(): string | null {
if (process.env.HOME) {
return process.env.HOME;
}
try {
return os.userInfo().homedir || null;
} catch {
// uv_os_get_passwd can fail outright when there is no passwd entry for the uid.
return null;
}
}
/**
* Where another user's home directory is, without asking the system.
*
* The sibling of the current user's home comes first because it is right wherever
* homes live together, whatever that directory is called — `/Users` on macOS,
* `/home` on Linux, `/export/home` on the odd installation. keyman previously
* hardcoded `/home/<user>`, which is wrong on the one platform it was written on.
*
* The candidates are checked for existence rather than guessed at, so a wrong one
* produces an error naming what was tried instead of an empty `readdir`.
*/
function candidateHomes(user: string): string[] {
const home = currentHome();
const siblings = home ? [path.join(path.dirname(home), user)] : [];
return [...new Set([...siblings, path.join('/home', user), path.join('/Users', user)])];
}
/**
* Resolves the home directory for an answer to the USER prompt.
*
* @returns the directory, or null with the reason already reported
*/
export function resolveHomeDir(user: string): string | null {
if (user === CURRENT_USER) {
const home = currentHome();
if (!home) {
console.error('❌ Unable to determine HOME directory for the current user.');
return null;
}
return home;
}
const candidates = candidateHomes(user);
const found = candidates.find((candidate) => fs.existsSync(candidate));
if (!found) {
console.error(`❌ No home directory found for ${user}. Tried: ${candidates.join(', ')}`);
return null;
}
return found;
}