mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 20:24:33 +00:00
refactor(commands): dedupe cleanup path resolution
This commit is contained in:
26
src/commands/cleanup-utils.test.ts
Normal file
26
src/commands/cleanup-utils.test.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import { buildCleanupPlan } from "./cleanup-utils.js";
|
||||||
|
|
||||||
|
describe("buildCleanupPlan", () => {
|
||||||
|
test("resolves inside-state flags and workspace dirs", () => {
|
||||||
|
const cfg = {
|
||||||
|
agents: {
|
||||||
|
defaults: { workspace: "/tmp/openclaw-workspace-1" },
|
||||||
|
list: [{ workspace: "/tmp/openclaw-workspace-2" }],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const plan = buildCleanupPlan({
|
||||||
|
cfg: cfg as unknown as OpenClawConfig,
|
||||||
|
stateDir: "/tmp/openclaw-state",
|
||||||
|
configPath: "/tmp/openclaw-state/openclaw.json",
|
||||||
|
oauthDir: "/tmp/openclaw-oauth",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(plan.configInsideState).toBe(true);
|
||||||
|
expect(plan.oauthInsideState).toBe(false);
|
||||||
|
expect(new Set(plan.workspaceDirs)).toEqual(
|
||||||
|
new Set(["/tmp/openclaw-workspace-1", "/tmp/openclaw-workspace-2"]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -29,6 +29,23 @@ export function collectWorkspaceDirs(cfg: OpenClawConfig | undefined): string[]
|
|||||||
return [...dirs];
|
return [...dirs];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function buildCleanupPlan(params: {
|
||||||
|
cfg: OpenClawConfig | undefined;
|
||||||
|
stateDir: string;
|
||||||
|
configPath: string;
|
||||||
|
oauthDir: string;
|
||||||
|
}): {
|
||||||
|
configInsideState: boolean;
|
||||||
|
oauthInsideState: boolean;
|
||||||
|
workspaceDirs: string[];
|
||||||
|
} {
|
||||||
|
return {
|
||||||
|
configInsideState: isPathWithin(params.configPath, params.stateDir),
|
||||||
|
oauthInsideState: isPathWithin(params.oauthDir, params.stateDir),
|
||||||
|
workspaceDirs: collectWorkspaceDirs(params.cfg),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function isPathWithin(child: string, parent: string): boolean {
|
export function isPathWithin(child: string, parent: string): boolean {
|
||||||
const relative = path.relative(parent, child);
|
const relative = path.relative(parent, child);
|
||||||
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
||||||
|
|||||||
@@ -10,12 +10,7 @@ import {
|
|||||||
} from "../config/config.js";
|
} from "../config/config.js";
|
||||||
import { resolveGatewayService } from "../daemon/service.js";
|
import { resolveGatewayService } from "../daemon/service.js";
|
||||||
import { stylePromptHint, stylePromptMessage, stylePromptTitle } from "../terminal/prompt-style.js";
|
import { stylePromptHint, stylePromptMessage, stylePromptTitle } from "../terminal/prompt-style.js";
|
||||||
import {
|
import { buildCleanupPlan, listAgentSessionDirs, removePath } from "./cleanup-utils.js";
|
||||||
collectWorkspaceDirs,
|
|
||||||
isPathWithin,
|
|
||||||
listAgentSessionDirs,
|
|
||||||
removePath,
|
|
||||||
} from "./cleanup-utils.js";
|
|
||||||
|
|
||||||
export type ResetScope = "config" | "config+creds+sessions" | "full";
|
export type ResetScope = "config" | "config+creds+sessions" | "full";
|
||||||
|
|
||||||
@@ -123,9 +118,12 @@ export async function resetCommand(runtime: RuntimeEnv, opts: ResetOptions) {
|
|||||||
const stateDir = resolveStateDir();
|
const stateDir = resolveStateDir();
|
||||||
const configPath = resolveConfigPath();
|
const configPath = resolveConfigPath();
|
||||||
const oauthDir = resolveOAuthDir();
|
const oauthDir = resolveOAuthDir();
|
||||||
const configInsideState = isPathWithin(configPath, stateDir);
|
const { configInsideState, oauthInsideState, workspaceDirs } = buildCleanupPlan({
|
||||||
const oauthInsideState = isPathWithin(oauthDir, stateDir);
|
cfg,
|
||||||
const workspaceDirs = collectWorkspaceDirs(cfg);
|
stateDir,
|
||||||
|
configPath,
|
||||||
|
oauthDir,
|
||||||
|
});
|
||||||
|
|
||||||
if (scope !== "config") {
|
if (scope !== "config") {
|
||||||
if (dryRun) {
|
if (dryRun) {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
import { resolveGatewayService } from "../daemon/service.js";
|
import { resolveGatewayService } from "../daemon/service.js";
|
||||||
import { stylePromptHint, stylePromptMessage, stylePromptTitle } from "../terminal/prompt-style.js";
|
import { stylePromptHint, stylePromptMessage, stylePromptTitle } from "../terminal/prompt-style.js";
|
||||||
import { resolveHomeDir } from "../utils.js";
|
import { resolveHomeDir } from "../utils.js";
|
||||||
import { collectWorkspaceDirs, isPathWithin, removePath } from "./cleanup-utils.js";
|
import { buildCleanupPlan, removePath } from "./cleanup-utils.js";
|
||||||
|
|
||||||
type UninstallScope = "service" | "state" | "workspace" | "app";
|
type UninstallScope = "service" | "state" | "workspace" | "app";
|
||||||
|
|
||||||
@@ -161,9 +161,12 @@ export async function uninstallCommand(runtime: RuntimeEnv, opts: UninstallOptio
|
|||||||
const stateDir = resolveStateDir();
|
const stateDir = resolveStateDir();
|
||||||
const configPath = resolveConfigPath();
|
const configPath = resolveConfigPath();
|
||||||
const oauthDir = resolveOAuthDir();
|
const oauthDir = resolveOAuthDir();
|
||||||
const configInsideState = isPathWithin(configPath, stateDir);
|
const { configInsideState, oauthInsideState, workspaceDirs } = buildCleanupPlan({
|
||||||
const oauthInsideState = isPathWithin(oauthDir, stateDir);
|
cfg,
|
||||||
const workspaceDirs = collectWorkspaceDirs(cfg);
|
stateDir,
|
||||||
|
configPath,
|
||||||
|
oauthDir,
|
||||||
|
});
|
||||||
|
|
||||||
if (scopes.has("service")) {
|
if (scopes.has("service")) {
|
||||||
if (dryRun) {
|
if (dryRun) {
|
||||||
|
|||||||
Reference in New Issue
Block a user