Repository navigation
Expand file tree
/
Copy pathServerEnvironmentLabel.ts
More file actions
196 lines (178 loc) · 5.6 KB
/
Copy pathServerEnvironmentLabel.ts
File metadata and controls
196 lines (178 loc) · 5.6 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as ProcessRunner from "../processRunner.ts";
interface ResolveServerEnvironmentLabelInput {
readonly cwdBaseName: string;
}
const ServerEnvironmentLabelCommandProbe = Schema.Literals([
"macos-computer-name",
"linux-pretty-hostname",
]);
type ServerEnvironmentLabelCommandProbe = typeof ServerEnvironmentLabelCommandProbe.Type;
export class ServerEnvironmentLabelFileError extends Schema.TaggedError<ServerEnvironmentLabelFileError>()(
"ServerEnvironmentLabelFileError",
{
operation: Schema.Literals(["inspect", "read"]),
path: Schema.String,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Failed to ${this.operation} environment-label file at ${this.path}.`;
}
}
export class ServerEnvironmentLabelCommandError extends Schema.TaggedError<ServerEnvironmentLabelCommandError>()(
"ServerEnvironmentLabelCommandError",
{
probe: ServerEnvironmentLabelCommandProbe,
executable: Schema.String,
argumentCount: Schema.Number,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Failed to run environment-label probe '${this.probe}' with ${this.executable}.`;
}
}
function normalizeLabel(value: string | null | undefined): string | null {
const trimmed = value?.trim();
return trimmed && trimmed.length > 0 ? trimmed : null;
}
function parseMachineInfoValue(raw: string, key: string): string | null {
for (const line of raw.split(/\r?\n/g)) {
const trimmed = line.trim();
if (trimmed.length === 0 || trimmed.startsWith("#") || !trimmed.startsWith(`${key}=`)) {
continue;
}
const value = trimmed.slice(key.length + 1).trim();
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
return normalizeLabel(value.slice(1, -1));
}
return normalizeLabel(value);
}
return null;
}
const readLinuxMachineInfo = Effect.fn("readLinuxMachineInfo")(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const machineInfoPath = "/etc/machine-info";
return yield* fileSystem.exists(machineInfoPath).pipe(
Effect.mapError(
(cause) =>
new ServerEnvironmentLabelFileError({
operation: "inspect",
path: machineInfoPath,
cause,
}),
),
Effect.flatMap((exists) =>
exists
? fileSystem.readFileString(machineInfoPath).pipe(
Effect.mapError(
(cause) =>
new ServerEnvironmentLabelFileError({
operation: "read",
path: machineInfoPath,
cause,
}),
),
)
: Effect.succeed(null),
),
Effect.catchTags({
ServerEnvironmentLabelFileError: (error) =>
Effect.logDebug(error.message).pipe(
Effect.annotateLogs({
operation: error.operation,
path: error.path,
cause: error,
}),
Effect.as(null),
),
}),
);
});
const runFriendlyLabelCommand = Effect.fn("runFriendlyLabelCommand")(function* (input: {
readonly probe: ServerEnvironmentLabelCommandProbe;
readonly command: string;
readonly args: readonly string[];
}) {
const processRunner = yield* ProcessRunner.ProcessRunner;
const result = yield* processRunner
.run({
command: input.command,
args: input.args,
timeoutBehavior: "timedOutResult",
})
.pipe(
Effect.mapError(
(cause) =>
new ServerEnvironmentLabelCommandError({
probe: input.probe,
executable: input.command,
argumentCount: input.args.length,
cause,
}),
),
Effect.asSome,
Effect.catchTags({
ServerEnvironmentLabelCommandError: (error) =>
Effect.logDebug(error.message).pipe(
Effect.annotateLogs({
probe: error.probe,
executable: error.executable,
argumentCount: error.argumentCount,
cause: error,
}),
Effect.as(Option.none()),
),
}),
);
if (Option.isNone(result) || result.value.code !== 0) {
return null;
}
return normalizeLabel(result.value.stdout);
});
const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function* () {
const platform = yield* HostProcess.Platform;
if (platform === "darwin") {
return yield* runFriendlyLabelCommand({
probe: "macos-computer-name",
command: "scutil",
args: ["--get", "ComputerName"],
});
}
if (platform === "linux") {
const machineInfo = normalizeLabel(yield* readLinuxMachineInfo());
if (machineInfo) {
const prettyHostname = parseMachineInfoValue(machineInfo, "PRETTY_HOSTNAME");
if (prettyHostname) {
return prettyHostname;
}
}
return yield* runFriendlyLabelCommand({
probe: "linux-pretty-hostname",
command: "hostnamectl",
args: ["--pretty"],
});
}
return null;
});
export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* (
input: ResolveServerEnvironmentLabelInput,
) {
const friendlyHostLabel = yield* resolveFriendlyHostLabel();
if (friendlyHostLabel) {
return friendlyHostLabel;
}
const hostname = normalizeLabel(yield* HostProcess.Hostname);
if (hostname) {
return hostname;
}
return normalizeLabel(input.cwdBaseName) ?? "T3 environment";
});