mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 15:21:23 +00:00
refactor(cli): dedupe approvals allowlist actions
This commit is contained in:
@@ -95,6 +95,14 @@ function exitWithError(message: string): never {
|
|||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function requireTrimmedNonEmpty(value: string, message: string): string {
|
||||||
|
const trimmed = value.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
exitWithError(message);
|
||||||
|
}
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
async function loadWritableSnapshotTarget(opts: ExecApprovalsCliOpts): Promise<{
|
async function loadWritableSnapshotTarget(opts: ExecApprovalsCliOpts): Promise<{
|
||||||
snapshot: ExecApprovalsSnapshot;
|
snapshot: ExecApprovalsSnapshot;
|
||||||
nodeId: string | null;
|
nodeId: string | null;
|
||||||
@@ -262,6 +270,28 @@ function isEmptyAgent(agent: ExecApprovalsAgent): boolean {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function loadWritableAllowlistAgent(opts: ExecApprovalsCliOpts): Promise<{
|
||||||
|
nodeId: string | null;
|
||||||
|
source: "gateway" | "node" | "local";
|
||||||
|
targetLabel: string;
|
||||||
|
baseHash: string;
|
||||||
|
file: ExecApprovalsFile;
|
||||||
|
agentKey: string;
|
||||||
|
agent: ExecApprovalsAgent;
|
||||||
|
allowlistEntries: NonNullable<ExecApprovalsAgent["allowlist"]>;
|
||||||
|
}> {
|
||||||
|
const { snapshot, nodeId, source, targetLabel, baseHash } =
|
||||||
|
await loadWritableSnapshotTarget(opts);
|
||||||
|
const file = snapshot.file ?? { version: 1 };
|
||||||
|
file.version = 1;
|
||||||
|
|
||||||
|
const agentKey = resolveAgentKey(opts.agent);
|
||||||
|
const agent = ensureAgent(file, agentKey);
|
||||||
|
const allowlistEntries = Array.isArray(agent.allowlist) ? agent.allowlist : [];
|
||||||
|
|
||||||
|
return { nodeId, source, targetLabel, baseHash, file, agentKey, agent, allowlistEntries };
|
||||||
|
}
|
||||||
|
|
||||||
export function registerExecApprovalsCli(program: Command) {
|
export function registerExecApprovalsCli(program: Command) {
|
||||||
const formatExample = (cmd: string, desc: string) =>
|
const formatExample = (cmd: string, desc: string) =>
|
||||||
` ${theme.command(cmd)}\n ${theme.muted(desc)}`;
|
` ${theme.command(cmd)}\n ${theme.muted(desc)}`;
|
||||||
@@ -364,17 +394,9 @@ export function registerExecApprovalsCli(program: Command) {
|
|||||||
.option("--agent <id>", 'Agent id (defaults to "*")')
|
.option("--agent <id>", 'Agent id (defaults to "*")')
|
||||||
.action(async (pattern: string, opts: ExecApprovalsCliOpts) => {
|
.action(async (pattern: string, opts: ExecApprovalsCliOpts) => {
|
||||||
try {
|
try {
|
||||||
const trimmed = pattern.trim();
|
const trimmed = requireTrimmedNonEmpty(pattern, "Pattern required.");
|
||||||
if (!trimmed) {
|
const { nodeId, source, targetLabel, baseHash, file, agentKey, agent, allowlistEntries } =
|
||||||
exitWithError("Pattern required.");
|
await loadWritableAllowlistAgent(opts);
|
||||||
}
|
|
||||||
const { snapshot, nodeId, source, targetLabel, baseHash } =
|
|
||||||
await loadWritableSnapshotTarget(opts);
|
|
||||||
const file = snapshot.file ?? { version: 1 };
|
|
||||||
file.version = 1;
|
|
||||||
const agentKey = resolveAgentKey(opts.agent);
|
|
||||||
const agent = ensureAgent(file, agentKey);
|
|
||||||
const allowlistEntries = Array.isArray(agent.allowlist) ? agent.allowlist : [];
|
|
||||||
if (allowlistEntries.some((entry) => normalizeAllowlistEntry(entry) === trimmed)) {
|
if (allowlistEntries.some((entry) => normalizeAllowlistEntry(entry) === trimmed)) {
|
||||||
defaultRuntime.log("Already allowlisted.");
|
defaultRuntime.log("Already allowlisted.");
|
||||||
return;
|
return;
|
||||||
@@ -398,17 +420,9 @@ export function registerExecApprovalsCli(program: Command) {
|
|||||||
.option("--agent <id>", 'Agent id (defaults to "*")')
|
.option("--agent <id>", 'Agent id (defaults to "*")')
|
||||||
.action(async (pattern: string, opts: ExecApprovalsCliOpts) => {
|
.action(async (pattern: string, opts: ExecApprovalsCliOpts) => {
|
||||||
try {
|
try {
|
||||||
const trimmed = pattern.trim();
|
const trimmed = requireTrimmedNonEmpty(pattern, "Pattern required.");
|
||||||
if (!trimmed) {
|
const { nodeId, source, targetLabel, baseHash, file, agentKey, agent, allowlistEntries } =
|
||||||
exitWithError("Pattern required.");
|
await loadWritableAllowlistAgent(opts);
|
||||||
}
|
|
||||||
const { snapshot, nodeId, source, targetLabel, baseHash } =
|
|
||||||
await loadWritableSnapshotTarget(opts);
|
|
||||||
const file = snapshot.file ?? { version: 1 };
|
|
||||||
file.version = 1;
|
|
||||||
const agentKey = resolveAgentKey(opts.agent);
|
|
||||||
const agent = ensureAgent(file, agentKey);
|
|
||||||
const allowlistEntries = Array.isArray(agent.allowlist) ? agent.allowlist : [];
|
|
||||||
const nextEntries = allowlistEntries.filter(
|
const nextEntries = allowlistEntries.filter(
|
||||||
(entry) => normalizeAllowlistEntry(entry) !== trimmed,
|
(entry) => normalizeAllowlistEntry(entry) !== trimmed,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user