Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ServerEnvironmentLabel.ts

104 lines2.9 KB

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;
12​
13function normalizeLabel(value: string | null | undefined): string | null {
14 const trimmed = value?.trim();
15 return trimmed && trimmed.length > 0 ? trimmed : null;
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;
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 runProcess(command, args, {
56 allowNonZeroExit: true,
57 }),
58 ).pipe(Effect.orElseSucceed(() => null));
59​
60 if (!result || result.code !== 0) {
61 return null;
62 }
63​
64 return normalizeLabel(result.stdout);
65});
66​
67const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function* (
68 platform: NodeJS.Platform,
69) {
70 if (platform === "darwin") {
71 return yield* runFriendlyLabelCommand("scutil", ["--get", "ComputerName"]);
72 }
73​
74 if (platform === "linux") {
75 const machineInfo = normalizeLabel(yield* readLinuxMachineInfo());
76 if (machineInfo) {
77 const prettyHostname = parseMachineInfoValue(machineInfo, "PRETTY_HOSTNAME");
78 if (prettyHostname) {
79 return prettyHostname;
80 }
81 }
82​
83 return yield* runFriendlyLabelCommand("hostnamectl", ["--pretty"]);
84 }
85​
86 return null;
87});
88​
89export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* (
90 input: ResolveServerEnvironmentLabelInput,
91) {
92 const platform = input.platform ?? process.platform;
93 const friendlyHostLabel = yield* resolveFriendlyHostLabel(platform);
94 if (friendlyHostLabel) {
95 return friendlyHostLabel;
96 }
97​
98 const hostname = normalizeLabel(input.hostname ?? OS.hostname());
99 if (hostname) {
100 return hostname;
101 }
102​
103 return normalizeLabel(input.cwdBaseName) ?? "T3 environment";
104});