127 lines
4.2 KiB
TypeScript
127 lines
4.2 KiB
TypeScript
import { validationError } from "../errors.js";
|
|
import { assertCorpActive } from "./active.js";
|
|
import { CorpConfig, GlobalConfig } from "./types.js";
|
|
|
|
export type CorpResolveOptions = Pick<GlobalConfig, "stateful" | "defaultCorpId"> & {
|
|
corpId?: string;
|
|
/** Only the tenant status-management commands (`corps enable/disable`) may resolve a disabled corp. */
|
|
allowDisabled?: boolean;
|
|
};
|
|
|
|
export function resolveCorpConfig(
|
|
corps: CorpConfig[],
|
|
options: CorpResolveOptions,
|
|
candidates: string[] = []
|
|
): CorpConfig {
|
|
// Every successful lookup passes through the same gate. This is the single
|
|
// choke point for CLI commands, MCP gateway commands, replay source/target
|
|
// resolution and background workers — a disabled tenant is never executable.
|
|
const accept = (found: CorpConfig): CorpConfig => {
|
|
if (!options.allowDisabled) assertCorpActive(found);
|
|
return found;
|
|
};
|
|
|
|
const requestedCorpId = options.corpId?.trim() || undefined;
|
|
const configuredCorpId = options.stateful
|
|
? options.defaultCorpId?.trim() || undefined
|
|
: undefined;
|
|
const selectedCorpId = requestedCorpId ?? configuredCorpId;
|
|
|
|
if (selectedCorpId) {
|
|
const exact = corps.find((item) => item.id === selectedCorpId);
|
|
if (exact) return accept(exact);
|
|
const caseInsensitive = corps.find((item) => item.id.toLowerCase() === selectedCorpId.toLowerCase());
|
|
if (caseInsensitive) return accept(caseInsensitive);
|
|
const byName = corps.find((item) => item.name.toLowerCase() === selectedCorpId.toLowerCase());
|
|
if (byName) return accept(byName);
|
|
throw validationError(`No enterprise config found for ${selectedCorpId}`);
|
|
}
|
|
|
|
if (!options.stateful) {
|
|
throw validationError("Stateless mode is enabled. Every enterprise command requires --corp <corpId>. " +
|
|
"Use `seal-use config mode stateful` to enable a default enterprise.");
|
|
}
|
|
|
|
const firstCorp = corps[0];
|
|
if (firstCorp) return accept(firstCorp);
|
|
|
|
throw validationError("No enterprise config found. Add a non-example JSON file under one of: " +
|
|
(candidates.length ? candidates.join(", ") : "the configured enterprise directories"));
|
|
}
|
|
|
|
export type CorpContextOptions = Pick<GlobalConfig, "stateful" | "defaultCorpId">;
|
|
|
|
export class CorpContext {
|
|
private current: CorpConfig | null = null;
|
|
private corps = new Map<string, CorpConfig>();
|
|
private stateful = false;
|
|
private defaultCorpId: string | undefined;
|
|
|
|
constructor(corps: CorpConfig[], options: CorpContextOptions = { stateful: false }) {
|
|
this.stateful = options.stateful;
|
|
this.defaultCorpId = options.defaultCorpId;
|
|
this.replaceCorps(corps);
|
|
this.selectCurrent(corps[0]);
|
|
}
|
|
|
|
refresh(corps: CorpConfig[], options: CorpContextOptions = {
|
|
stateful: this.stateful,
|
|
defaultCorpId: this.defaultCorpId
|
|
}): void {
|
|
const currentId = this.current?.id;
|
|
this.stateful = options.stateful;
|
|
this.defaultCorpId = options.defaultCorpId;
|
|
this.replaceCorps(corps);
|
|
this.selectCurrent(currentId ? this.corps.get(currentId) ?? corps[0] : corps[0]);
|
|
}
|
|
|
|
private replaceCorps(corps: CorpConfig[]): void {
|
|
this.corps = new Map();
|
|
for (const corp of corps) {
|
|
this.corps.set(corp.id, corp);
|
|
}
|
|
}
|
|
|
|
getCurrent(): CorpConfig | null {
|
|
return this.current;
|
|
}
|
|
|
|
getCorp(corpId: string): CorpConfig | null {
|
|
return this.corps.get(corpId) ?? null;
|
|
}
|
|
|
|
resolve(corpId?: string): CorpConfig | null {
|
|
if (corpId) return this.getCorp(corpId);
|
|
return this.current;
|
|
}
|
|
|
|
switchCorp(corpId: string): boolean {
|
|
if (!this.stateful) return false;
|
|
const corp = this.corps.get(corpId);
|
|
if (!corp) return false;
|
|
this.current = corp;
|
|
this.defaultCorpId = corp.id;
|
|
return true;
|
|
}
|
|
|
|
listCorps(): Array<{ id: string; name: string; sourceType: string; current: boolean }> {
|
|
return Array.from(this.corps.values()).map((corp) => ({
|
|
id: corp.id,
|
|
name: corp.name,
|
|
sourceType: corp.source.type,
|
|
current: corp.id === this.current?.id
|
|
}));
|
|
}
|
|
|
|
private selectCurrent(fallback?: CorpConfig): void {
|
|
if (!this.stateful) {
|
|
this.current = null;
|
|
return;
|
|
}
|
|
|
|
this.current = this.defaultCorpId !== undefined
|
|
? this.corps.get(this.defaultCorpId) ?? null
|
|
: fallback ?? null;
|
|
}
|
|
}
|