mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(cli): dedupe service-load and command-removal loops
This commit is contained in:
@@ -84,6 +84,19 @@ async function handleServiceNotLoaded(params: {
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveServiceLoadedOrFail(params: {
|
||||
serviceNoun: string;
|
||||
service: GatewayService;
|
||||
fail: ReturnType<typeof createActionIO>["fail"];
|
||||
}): Promise<boolean | null> {
|
||||
try {
|
||||
return await params.service.isLoaded({ env: process.env });
|
||||
} catch (err) {
|
||||
params.fail(`${params.serviceNoun} service check failed: ${String(err)}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function runServiceUninstall(params: {
|
||||
serviceNoun: string;
|
||||
service: GatewayService;
|
||||
@@ -145,11 +158,12 @@ export async function runServiceStart(params: {
|
||||
const json = Boolean(params.opts?.json);
|
||||
const { stdout, emit, fail } = createActionIO({ action: "start", json });
|
||||
|
||||
let loaded = false;
|
||||
try {
|
||||
loaded = await params.service.isLoaded({ env: process.env });
|
||||
} catch (err) {
|
||||
fail(`${params.serviceNoun} service check failed: ${String(err)}`);
|
||||
const loaded = await resolveServiceLoadedOrFail({
|
||||
serviceNoun: params.serviceNoun,
|
||||
service: params.service,
|
||||
fail,
|
||||
});
|
||||
if (loaded === null) {
|
||||
return;
|
||||
}
|
||||
if (!loaded) {
|
||||
@@ -192,11 +206,12 @@ export async function runServiceStop(params: {
|
||||
const json = Boolean(params.opts?.json);
|
||||
const { stdout, emit, fail } = createActionIO({ action: "stop", json });
|
||||
|
||||
let loaded = false;
|
||||
try {
|
||||
loaded = await params.service.isLoaded({ env: process.env });
|
||||
} catch (err) {
|
||||
fail(`${params.serviceNoun} service check failed: ${String(err)}`);
|
||||
const loaded = await resolveServiceLoadedOrFail({
|
||||
serviceNoun: params.serviceNoun,
|
||||
service: params.service,
|
||||
fail,
|
||||
});
|
||||
if (loaded === null) {
|
||||
return;
|
||||
}
|
||||
if (!loaded) {
|
||||
@@ -241,11 +256,12 @@ export async function runServiceRestart(params: {
|
||||
const json = Boolean(params.opts?.json);
|
||||
const { stdout, emit, fail } = createActionIO({ action: "restart", json });
|
||||
|
||||
let loaded = false;
|
||||
try {
|
||||
loaded = await params.service.isLoaded({ env: process.env });
|
||||
} catch (err) {
|
||||
fail(`${params.serviceNoun} service check failed: ${String(err)}`);
|
||||
const loaded = await resolveServiceLoadedOrFail({
|
||||
serviceNoun: params.serviceNoun,
|
||||
service: params.service,
|
||||
fail,
|
||||
});
|
||||
if (loaded === null) {
|
||||
return false;
|
||||
}
|
||||
if (!loaded) {
|
||||
|
||||
@@ -237,6 +237,17 @@ function removeCommand(program: Command, command: Command) {
|
||||
}
|
||||
}
|
||||
|
||||
function removeEntryCommands(program: Command, entry: CoreCliEntry) {
|
||||
// Some registrars install multiple top-level commands (e.g. status/health/sessions).
|
||||
// Remove placeholders/old registrations for all names in the entry before re-registering.
|
||||
for (const cmd of entry.commands) {
|
||||
const existing = program.commands.find((c) => c.name() === cmd.name);
|
||||
if (existing) {
|
||||
removeCommand(program, existing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function registerLazyCoreCommand(
|
||||
program: Command,
|
||||
ctx: ProgramContext,
|
||||
@@ -247,14 +258,7 @@ function registerLazyCoreCommand(
|
||||
placeholder.allowUnknownOption(true);
|
||||
placeholder.allowExcessArguments(true);
|
||||
placeholder.action(async (...actionArgs) => {
|
||||
// Some registrars install multiple top-level commands (e.g. status/health/sessions).
|
||||
// Remove placeholders/old registrations for all names in the entry before re-registering.
|
||||
for (const cmd of entry.commands) {
|
||||
const existing = program.commands.find((c) => c.name() === cmd.name);
|
||||
if (existing) {
|
||||
removeCommand(program, existing);
|
||||
}
|
||||
}
|
||||
removeEntryCommands(program, entry);
|
||||
await entry.register({ program, ctx, argv: process.argv });
|
||||
await reparseProgramFromActionArgs(program, actionArgs);
|
||||
});
|
||||
@@ -273,14 +277,7 @@ export async function registerCoreCliByName(
|
||||
return false;
|
||||
}
|
||||
|
||||
// Some registrars install multiple top-level commands (e.g. status/health/sessions).
|
||||
// Remove placeholders/old registrations for all names in the entry before re-registering.
|
||||
for (const cmd of entry.commands) {
|
||||
const existing = program.commands.find((c) => c.name() === cmd.name);
|
||||
if (existing) {
|
||||
removeCommand(program, existing);
|
||||
}
|
||||
}
|
||||
removeEntryCommands(program, entry);
|
||||
await entry.register({ program, ctx, argv });
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user