Skip to content

Commit

Permalink
ENSRainbow: Add purge command (#285)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomek Kopacki <[email protected]>
Co-authored-by: lightwalker.eth <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2025
1 parent 3cb4466 commit 1a4c1ab
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/ensrainbow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"ingest": "bun src/cli.ts ingest",
"validate": "bun src/cli.ts validate",
"validate:lite": "bun src/cli.ts validate --lite",
"purge": "bun src/cli.ts purge",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
Expand Down
23 changes: 23 additions & 0 deletions apps/ensrainbow/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ describe("CLI", () => {
});
});

describe("purge command", () => {
it("should remove the database directory", async () => {
// Create test directory
await mkdtemp(testDataDir);

// Run purge command
await cli.parse(["purge", "--data-dir", testDataDir]);

// Verify directory was removed
await expect(rm(testDataDir)).rejects.toThrow();
});

it("should handle errors gracefully", async () => {
const nonExistentDir = join(tempDir, "non-existent");

// Run purge command on non-existent directory
await cli.parse(["purge", "--data-dir", nonExistentDir]);

// Verify directory still doesn't exist
await expect(rm(nonExistentDir)).rejects.toThrow();
});
});

describe("CLI Interface", () => {
describe("ingest command", () => {
it("should execute ingest command with custom data directory", async () => {
Expand Down
21 changes: 21 additions & 0 deletions apps/ensrainbow/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ArgumentsCamelCase, Argv } from "yargs";
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { ingestCommand } from "./commands/ingest-command";
import { purgeCommand } from "./commands/purge-command";
import { serverCommand } from "./commands/server-command";
import { validateCommand } from "./commands/validate-command";
import { getDefaultDataSubDir, getEnvPort } from "./lib/env";
Expand Down Expand Up @@ -32,6 +33,10 @@ interface ValidateArgs {
lite: boolean;
}

interface PurgeArgs {
"data-dir": string;
}

export interface CLIOptions {
exitProcess?: boolean;
}
Expand Down Expand Up @@ -112,6 +117,22 @@ export function createCLI(options: CLIOptions = {}) {
});
},
)
.command(
"purge",
"Completely wipe all files from the specified data directory",
(yargs: Argv) => {
return yargs.option("data-dir", {
type: "string",
description: "Directory containing LevelDB data",
default: getDefaultDataSubDir(),
});
},
async (argv: ArgumentsCamelCase<PurgeArgs>) => {
await purgeCommand({
dataDir: argv["data-dir"],
});
},
)
.demandCommand(1, "You must specify a command")
.strict()
.help();
Expand Down
21 changes: 21 additions & 0 deletions apps/ensrainbow/src/commands/purge-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { rm } from "fs/promises";
import { logger } from "../utils/logger";

export interface PurgeCommandOptions {
dataDir: string;
}

export async function purgeCommand(options: PurgeCommandOptions): Promise<void> {
const { dataDir } = options;

try {
logger.info(`Removing database directory at ${dataDir}...`);
await rm(dataDir, { recursive: true, force: true });
logger.info("Database directory removed successfully.");
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';

logger.error(`Failed to remove database directory: ${errorMessage}`);
throw error;
}
}

0 comments on commit 1a4c1ab

Please sign in to comment.