Open History to see the last commit
1import * as OS from "node:os";
2
3import { Effect, FileSystem } from "effect";
4
5import { runProcess } from "../../processRunner.ts";
6
7interface ResolveServerEnvironmentLabelInput {
8 readonly cwdBaseName: string;
9 readonly platform?: NodeJS.Platform;
10 readonly hostname?: string | null;
11}
12
13function normalizeLabel(value: string | null | undefined): string | null {
14 const trimmed = value?.trim();
15 return trimmed && trimmed.length > 0 ? trimmed : null;
16}
17
18function parseMachineInfoValue(raw: string, key: string): string | null {
19 for (const line of raw.split(/\r?\n/g)) {
20 const trimmed = line.trim();
21 if (trimmed.length === 0 || trimmed.startsWith("#") || !trimmed.startsWith(`${key}=`)) {
22 continue;
23 }
24 const value = trimmed.slice(key.length + 1).trim();
25 if (
26 (value.startsWith('"') && value.endsWith('"')) ||
27 (value.startsWith("'") && value.endsWith("'"))
28 ) {
29 return normalizeLabel(value.slice(1, -1));
30 }
31 return normalizeLabel(value);
32 }
33 return null;
34}
35
36const readLinuxMachineInfo = Effect.fn("readLinuxMachineInfo")(function* () {
37 const fileSystem = yield* FileSystem.FileSystem;
38 const exists = yield* fileSystem
39 .exists("/etc/machine-info")
40 .pipe(Effect.orElseSucceed(() => false));
41 if (!exists) {
42 return null;
43 }
44
45 return yield* fileSystem
46 .readFileString("/etc/machine-info")
47 .pipe(Effect.orElseSucceed(() => null));
48});
49
50const runFriendlyLabelCommand = Effect.fn("runFriendlyLabelCommand")(function* (
51 command: string,
52 args: readonly string[],
53) {
54 const result = yield* Effect.tryPromise({
55 try: () =>
56 runProcess(command, args, {
57 allowNonZeroExit: true,
58 }),
59 catch: () => null,
60 }).pipe(Effect.orElseSucceed(() => null));
61
62 if (!result || result.code !== 0) {
63 return null;
64 }
65
66 return normalizeLabel(result.stdout);
67});
68
69const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function* (
70 platform: NodeJS.Platform,
71) {
72 if (platform === "darwin") {
73 return yield* runFriendlyLabelCommand("scutil", ["--get", "ComputerName"]);
74 }
75
76 if (platform === "linux") {
77 const machineInfo = normalizeLabel(yield* readLinuxMachineInfo());
78 if (machineInfo) {
79 const prettyHostname = parseMachineInfoValue(machineInfo, "PRETTY_HOSTNAME");
80 if (prettyHostname) {
81 return prettyHostname;
82 }
83 }
84
85 return yield* runFriendlyLabelCommand("hostnamectl", ["--pretty"]);
86 }
87
88 return null;
89});
90
91export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* (
92 input: ResolveServerEnvironmentLabelInput,
93) {
94 const platform = input.platform ?? process.platform;
95 const friendlyHostLabel = yield* resolveFriendlyHostLabel(platform);
96 if (friendlyHostLabel) {
97 return friendlyHostLabel;
98 }
99
100 const hostname = normalizeLabel(input.hostname ?? OS.hostname());
101 if (hostname) {
102 return hostname;
103 }
104
105 return normalizeLabel(input.cwdBaseName) ?? "Workbench environment";
106});