mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 12:41:37 +00:00
refactor(cli): share directory list command flow
This commit is contained in:
@@ -110,6 +110,42 @@ export function registerDirectoryCli(program: Command) {
|
|||||||
return { cfg, channelId, accountId, plugin };
|
return { cfg, channelId, accountId, plugin };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const runDirectoryList = async (params: {
|
||||||
|
opts: {
|
||||||
|
channel?: unknown;
|
||||||
|
account?: unknown;
|
||||||
|
query?: unknown;
|
||||||
|
limit?: unknown;
|
||||||
|
json?: unknown;
|
||||||
|
};
|
||||||
|
action: "listPeers" | "listGroups";
|
||||||
|
unsupported: string;
|
||||||
|
title: string;
|
||||||
|
emptyMessage: string;
|
||||||
|
}) => {
|
||||||
|
const { cfg, channelId, accountId, plugin } = await resolve({
|
||||||
|
channel: params.opts.channel as string | undefined,
|
||||||
|
account: params.opts.account as string | undefined,
|
||||||
|
});
|
||||||
|
const fn =
|
||||||
|
params.action === "listPeers" ? plugin.directory?.listPeers : plugin.directory?.listGroups;
|
||||||
|
if (!fn) {
|
||||||
|
throw new Error(`Channel ${channelId} does not support directory ${params.unsupported}`);
|
||||||
|
}
|
||||||
|
const result = await fn({
|
||||||
|
cfg,
|
||||||
|
accountId,
|
||||||
|
query: (params.opts.query as string | undefined) ?? null,
|
||||||
|
limit: parseLimit(params.opts.limit),
|
||||||
|
runtime: defaultRuntime,
|
||||||
|
});
|
||||||
|
if (params.opts.json) {
|
||||||
|
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printDirectoryList({ title: params.title, emptyMessage: params.emptyMessage, entries: result });
|
||||||
|
};
|
||||||
|
|
||||||
withChannel(directory.command("self").description("Show the current account user")).action(
|
withChannel(directory.command("self").description("Show the current account user")).action(
|
||||||
async (opts) => {
|
async (opts) => {
|
||||||
try {
|
try {
|
||||||
@@ -155,26 +191,13 @@ export function registerDirectoryCli(program: Command) {
|
|||||||
.option("--limit <n>", "Limit results")
|
.option("--limit <n>", "Limit results")
|
||||||
.action(async (opts) => {
|
.action(async (opts) => {
|
||||||
try {
|
try {
|
||||||
const { cfg, channelId, accountId, plugin } = await resolve({
|
await runDirectoryList({
|
||||||
channel: opts.channel as string | undefined,
|
opts,
|
||||||
account: opts.account as string | undefined,
|
action: "listPeers",
|
||||||
|
unsupported: "peers",
|
||||||
|
title: "Peers",
|
||||||
|
emptyMessage: "No peers found.",
|
||||||
});
|
});
|
||||||
const fn = plugin.directory?.listPeers;
|
|
||||||
if (!fn) {
|
|
||||||
throw new Error(`Channel ${channelId} does not support directory peers`);
|
|
||||||
}
|
|
||||||
const result = await fn({
|
|
||||||
cfg,
|
|
||||||
accountId,
|
|
||||||
query: (opts.query as string | undefined) ?? null,
|
|
||||||
limit: parseLimit(opts.limit),
|
|
||||||
runtime: defaultRuntime,
|
|
||||||
});
|
|
||||||
if (opts.json) {
|
|
||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
printDirectoryList({ title: "Peers", emptyMessage: "No peers found.", entries: result });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
defaultRuntime.error(danger(String(err)));
|
defaultRuntime.error(danger(String(err)));
|
||||||
defaultRuntime.exit(1);
|
defaultRuntime.exit(1);
|
||||||
@@ -187,26 +210,13 @@ export function registerDirectoryCli(program: Command) {
|
|||||||
.option("--limit <n>", "Limit results")
|
.option("--limit <n>", "Limit results")
|
||||||
.action(async (opts) => {
|
.action(async (opts) => {
|
||||||
try {
|
try {
|
||||||
const { cfg, channelId, accountId, plugin } = await resolve({
|
await runDirectoryList({
|
||||||
channel: opts.channel as string | undefined,
|
opts,
|
||||||
account: opts.account as string | undefined,
|
action: "listGroups",
|
||||||
|
unsupported: "groups",
|
||||||
|
title: "Groups",
|
||||||
|
emptyMessage: "No groups found.",
|
||||||
});
|
});
|
||||||
const fn = plugin.directory?.listGroups;
|
|
||||||
if (!fn) {
|
|
||||||
throw new Error(`Channel ${channelId} does not support directory groups`);
|
|
||||||
}
|
|
||||||
const result = await fn({
|
|
||||||
cfg,
|
|
||||||
accountId,
|
|
||||||
query: (opts.query as string | undefined) ?? null,
|
|
||||||
limit: parseLimit(opts.limit),
|
|
||||||
runtime: defaultRuntime,
|
|
||||||
});
|
|
||||||
if (opts.json) {
|
|
||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
printDirectoryList({ title: "Groups", emptyMessage: "No groups found.", entries: result });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
defaultRuntime.error(danger(String(err)));
|
defaultRuntime.error(danger(String(err)));
|
||||||
defaultRuntime.exit(1);
|
defaultRuntime.exit(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user