aboutsummaryrefslogtreecommitdiff
path: root/src/commands/hostid.js
blob: b36f70da283eb10ad40f6e39619529dd49cc05b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);