From 9c79f5ff7caf20192422952500e884d69aba55d3 Mon Sep 17 00:00:00 2001 From: Ben Houston Date: Thu, 6 Mar 2025 20:52:55 -0500 Subject: [PATCH] Add ability to clear all configuration settings (fixes #133) --- packages/cli/src/commands/config.ts | 51 ++++++++++++++++++++++++++++- packages/cli/src/settings/config.ts | 11 +++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/config.ts b/packages/cli/src/commands/config.ts index 0b17a34..8ff72d5 100644 --- a/packages/cli/src/commands/config.ts +++ b/packages/cli/src/commands/config.ts @@ -1,4 +1,5 @@ import chalk from 'chalk'; +import { createInterface } from 'readline/promises'; import { Logger } from 'mycoder-agent'; import { SharedOptions } from '../options.js'; @@ -6,15 +7,36 @@ import { getConfig, getDefaultConfig, updateConfig, + clearAllConfig, } from '../settings/config.js'; import { nameToLogIndex } from '../utils/nameToLogIndex.js'; +/** + * Prompts the user for confirmation with a yes/no question + * @param question The question to ask the user + * @returns True if the user confirmed, false otherwise + */ +async function confirm(question: string): Promise { + const rl = createInterface({ + input: process.stdin, + output: process.stdout, + }); + + try { + const answer = await rl.question(`${question} (y/N): `); + return answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes'; + } finally { + rl.close(); + } +} + import type { CommandModule, ArgumentsCamelCase } from 'yargs'; export interface ConfigOptions extends SharedOptions { command: 'get' | 'set' | 'list' | 'clear'; key?: string; value?: string; + all?: boolean; } export const command: CommandModule = { @@ -36,6 +58,11 @@ export const command: CommandModule = { describe: 'Configuration value (for set command)', type: 'string', }) + .option('all', { + describe: 'Clear all configuration settings (for clear command)', + type: 'boolean', + default: false, + }) .example('$0 config list', 'List all configuration values') .example( '$0 config get githubMode', @@ -45,6 +72,10 @@ export const command: CommandModule = { .example( '$0 config clear customPrompt', 'Reset customPrompt to default value', + ) + .example( + '$0 config clear --all', + 'Clear all configuration settings', ) as any; // eslint-disable-line @typescript-eslint/no-explicit-any }, handler: async (argv: ArgumentsCamelCase) => { @@ -153,8 +184,26 @@ export const command: CommandModule = { // Handle 'clear' command if (argv.command === 'clear') { + // Check if --all flag is provided + if (argv.all) { + // Confirm with the user before clearing all settings + const isConfirmed = await confirm( + 'Are you sure you want to clear all configuration settings? This action cannot be undone.' + ); + + if (!isConfirmed) { + logger.info('Operation cancelled.'); + return; + } + + // Clear all settings + clearAllConfig(); + logger.info('All configuration settings have been cleared. Default values will be used.'); + return; + } + if (!argv.key) { - logger.error('Key is required for clear command'); + logger.error('Key is required for clear command (or use --all to clear all settings)'); return; } diff --git a/packages/cli/src/settings/config.ts b/packages/cli/src/settings/config.ts index 87a4f7d..4501378 100644 --- a/packages/cli/src/settings/config.ts +++ b/packages/cli/src/settings/config.ts @@ -46,3 +46,14 @@ export const updateConfig = (config: Partial): Config => { fs.writeFileSync(configFile, JSON.stringify(updatedConfig, null, 2)); return updatedConfig; }; + +/** + * Clears all configuration settings by removing the config file + * @returns The default configuration that will now be used + */ +export const clearAllConfig = (): Config => { + if (fs.existsSync(configFile)) { + fs.unlinkSync(configFile); + } + return defaultConfig; +};