From 6f5241afb5bd1d6a1a8724bf06cc6f205c279402 Mon Sep 17 00:00:00 2001 From: Soccera1 <129828376+Soccera1@users.noreply.github.com> Date: Sat, 18 Jul 2026 09:27:17 +1000 Subject: Refactor to single call --- src/commands/hostid.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/commands/hostid.js (limited to 'src/commands/hostid.js') 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); -- cgit v1.3