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

ServerEnvironmentLabel.ts

109 lines3.0 KB

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