Open History to see the last commit
1import type { EnvironmentId, SshDeviceHostConfig } from "@t3tools/contracts";
2
3export interface DeviceHostEnvironment {
4 readonly environmentId: EnvironmentId;
5 readonly label: string;
6 readonly hosts: ReadonlyArray<SshDeviceHostConfig>;
7}
8
9export interface DeviceHostGroup {
10 readonly host: SshDeviceHostConfig;
11 readonly members: Array<{
12 readonly environmentId: EnvironmentId;
13 readonly label: string;
14 readonly host: SshDeviceHostConfig;
15 }>;
16}
17
18/** Only combine matching configurations; ambiguous identities keep their machine's list. */
19export function groupDeviceHosts(
20 environments: ReadonlyArray<DeviceHostEnvironment>,
21): ReadonlyArray<DeviceHostGroup> | null {
22 const groups = new Map<string, DeviceHostGroup>();
23 const identities = new Map<string, string>();
24 const configurations = new Map<string, string>();
25 for (const environment of environments) {
26 const destinations = new Set<string>();
27 for (const host of environment.hosts) {
28 const destination = JSON.stringify([host.target, host.port, host.identityFile]);
29 const configuration = JSON.stringify([host.label, host.target, host.port, host.identityFile]);
30 const previousConfiguration = identities.get(host.id);
31 const targetConfiguration = configurations.get(host.target);
32 const group = groups.get(destination);
33 if (
34 destinations.has(destination) ||
35 (previousConfiguration !== undefined && previousConfiguration !== configuration) ||
36 (targetConfiguration !== undefined && targetConfiguration !== configuration)
37 ) {
38 return null;
39 }
40 destinations.add(destination);
41 identities.set(host.id, configuration);
42 configurations.set(host.target, configuration);
43 const member = {
44 environmentId: environment.environmentId,
45 label: environment.label,
46 host,
47 };
48 if (group) group.members.push(member);
49 else groups.set(destination, { host, members: [member] });
50 }
51 }
52 return [...groups.values()];
53}
54
55/** Apply one host change without replacing another environment's host list. */
56export function updateDeviceHosts(
57 hosts: ReadonlyArray<SshDeviceHostConfig>,
58 host: SshDeviceHostConfig,
59 remove: boolean,
60 original = host,
61): ReadonlyArray<SshDeviceHostConfig> {
62 const sameDestination = (candidate: SshDeviceHostConfig, other: SshDeviceHostConfig) =>
63 candidate.target === other.target &&
64 candidate.port === other.port &&
65 candidate.identityFile === other.identityFile;
66 const findDestination = (destination: SshDeviceHostConfig) => {
67 const matches = hosts.filter((candidate) => sameDestination(candidate, destination));
68 if (matches.length > 1) {
69 throw new Error(
70 "Multiple hosts match this SSH destination. Select the environment to edit its hosts.",
71 );
72 }
73 return matches[0];
74 };
75 // A retry can encounter the updated destination on an environment that
76 // already saved, including one with a different environment-local host ID.
77 const existing =
78 hosts.find((candidate) => candidate.id === original.id) ??
79 findDestination(original) ??
80 (remove ? undefined : findDestination(host));
81 if (remove) return hosts.filter((candidate) => candidate.id !== existing?.id);
82 return existing
83 ? hosts.map((candidate) =>
84 candidate.id === existing.id ? { ...host, id: existing.id } : candidate,
85 )
86 : [...hosts, host];
87}