aboutsummaryrefslogtreecommitdiff
path: root/src/commands/hostid.js
diff options
context:
space:
mode:
authorLily <[email protected]>2026-07-28 16:12:21 +1000
committerGitHub <[email protected]>2026-07-28 16:12:21 +1000
commitc11e17573334dfb487a8e1cc25fa84fa9e138f5c (patch)
treee91dbd19b524f9432da6bcb86b1fc193e0d02e26 /src/commands/hostid.js
parent6a2f098e72e71437154ec00506e8e736aa8e1026 (diff)
downloadbnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.tar.gz
bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.zip
Refactor coreutils into single-call command modules (#1)
* Refactor to single call
Diffstat (limited to 'src/commands/hostid.js')
-rw-r--r--src/commands/hostid.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/commands/hostid.js b/src/commands/hostid.js
new file mode 100644
index 0000000..b36f70d
--- /dev/null
+++ b/src/commands/hostid.js
@@ -0,0 +1,29 @@
+#!/usr/bin/env bun
+
+import { readFile } from "node:fs/promises";
+import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions } from "../shared/common.js";
+import { UsageError, stdout } from "../shared/diagnostics.js";
+import { defineCommand, runAsMain } from "../shared/command.js";
+
+export async function hostid(args) {
+ args = normalizeHelpVersionOnlyLongOptions(args);
+ const { operands } = parseOptions(args, { long: { help: false, version: false } });
+ if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true);
+ stdout(`${(await hostidValue()).toString(16).padStart(8, "0")}\n`);
+ return 0;
+}
+
+export async function hostidValue() {
+ try {
+ const bytes = await readFile("/etc/hostid");
+ if (bytes.length >= 4) return bytes.readUInt32LE(0);
+ } catch {
+ // GNU/Linux gethostid returns zero when no host id has been configured.
+ }
+ return 0;
+}
+
+const singleCall = defineCommand("hostid", hostid, helpVersionOnlyMetaOption);
+export default singleCall;
+
+if (import.meta.main) await runAsMain(singleCall);