mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 22:21:37 +00: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: {
|
export async function runServiceUninstall(params: {
|
||||||
serviceNoun: string;
|
serviceNoun: string;
|
||||||
service: GatewayService;
|
service: GatewayService;
|
||||||
@@ -145,11 +158,12 @@ export async function runServiceStart(params: {
|
|||||||
const json = Boolean(params.opts?.json);
|
const json = Boolean(params.opts?.json);
|
||||||
const { stdout, emit, fail } = createActionIO({ action: "start", json });
|
const { stdout, emit, fail } = createActionIO({ action: "start", json });
|
||||||
|
|
||||||
let loaded = false;
|
const loaded = await resolveServiceLoadedOrFail({
|
||||||
try {
|
serviceNoun: params.serviceNoun,
|
||||||
loaded = await params.service.isLoaded({ env: process.env });
|
service: params.service,
|
||||||
} catch (err) {
|
fail,
|
||||||
fail(`${params.serviceNoun} service check failed: ${String(err)}`);
|
});
|
||||||
|
if (loaded === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!loaded) {
|
if (!loaded) {
|
||||||
@@ -192,11 +206,12 @@ export async function runServiceStop(params: {
|
|||||||
const json = Boolean(params.opts?.json);
|
const json = Boolean(params.opts?.json);
|
||||||
const { stdout, emit, fail } = createActionIO({ action: "stop", json });
|
const { stdout, emit, fail } = createActionIO({ action: "stop", json });
|
||||||
|
|
||||||
let loaded = false;
|
const loaded = await resolveServiceLoadedOrFail({
|
||||||
try {
|
serviceNoun: params.serviceNoun,
|
||||||
loaded = await params.service.isLoaded({ env: process.env });
|
service: params.service,
|
||||||
} catch (err) {
|
fail,
|
||||||
fail(`${params.serviceNoun} service check failed: ${String(err)}`);
|
});
|
||||||
|
if (loaded === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!loaded) {
|
if (!loaded) {
|
||||||
@@ -241,11 +256,12 @@ export async function runServiceRestart(params: {
|
|||||||
const json = Boolean(params.opts?.json);
|
const json = Boolean(params.opts?.json);
|
||||||
const { stdout, emit, fail } = createActionIO({ action: "restart", json });
|
const { stdout, emit, fail } = createActionIO({ action: "restart", json });
|
||||||
|
|
||||||
let loaded = false;
|
const loaded = await resolveServiceLoadedOrFail({
|
||||||
try {
|
serviceNoun: params.serviceNoun,
|
||||||
loaded = await params.service.isLoaded({ env: process.env });
|
service: params.service,
|
||||||
} catch (err) {
|
fail,
|
||||||
fail(`${params.serviceNoun} service check failed: ${String(err)}`);
|
});
|
||||||
|
if (loaded === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!loaded) {
|
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(
|
function registerLazyCoreCommand(
|
||||||
program: Command,
|
program: Command,
|
||||||
ctx: ProgramContext,
|
ctx: ProgramContext,
|
||||||
@@ -247,14 +258,7 @@ function registerLazyCoreCommand(
|
|||||||
placeholder.allowUnknownOption(true);
|
placeholder.allowUnknownOption(true);
|
||||||
placeholder.allowExcessArguments(true);
|
placeholder.allowExcessArguments(true);
|
||||||
placeholder.action(async (...actionArgs) => {
|
placeholder.action(async (...actionArgs) => {
|
||||||
// Some registrars install multiple top-level commands (e.g. status/health/sessions).
|
removeEntryCommands(program, entry);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await entry.register({ program, ctx, argv: process.argv });
|
await entry.register({ program, ctx, argv: process.argv });
|
||||||
await reparseProgramFromActionArgs(program, actionArgs);
|
await reparseProgramFromActionArgs(program, actionArgs);
|
||||||
});
|
});
|
||||||
@@ -273,14 +277,7 @@ export async function registerCoreCliByName(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some registrars install multiple top-level commands (e.g. status/health/sessions).
|
removeEntryCommands(program, entry);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await entry.register({ program, ctx, argv });
|
await entry.register({ program, ctx, argv });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user