Skip to content

Commit c23746d

Browse files
authored
Merge pull request #135 from drivecore/feature/clear-all-config
Add ability to clear all configuration settings
2 parents 52b9c3a + 9c79f5f commit c23746d

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

packages/cli/src/commands/config.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
import chalk from 'chalk';
2+
import { createInterface } from 'readline/promises';
23
import { Logger } from 'mycoder-agent';
34

45
import { SharedOptions } from '../options.js';
56
import {
67
getConfig,
78
getDefaultConfig,
89
updateConfig,
10+
clearAllConfig,
911
} from '../settings/config.js';
1012
import { nameToLogIndex } from '../utils/nameToLogIndex.js';
1113

14+
/**
15+
* Prompts the user for confirmation with a yes/no question
16+
* @param question The question to ask the user
17+
* @returns True if the user confirmed, false otherwise
18+
*/
19+
async function confirm(question: string): Promise<boolean> {
20+
const rl = createInterface({
21+
input: process.stdin,
22+
output: process.stdout,
23+
});
24+
25+
try {
26+
const answer = await rl.question(`${question} (y/N): `);
27+
return answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes';
28+
} finally {
29+
rl.close();
30+
}
31+
}
32+
1233
import type { CommandModule, ArgumentsCamelCase } from 'yargs';
1334

1435
export interface ConfigOptions extends SharedOptions {
1536
command: 'get' | 'set' | 'list' | 'clear';
1637
key?: string;
1738
value?: string;
39+
all?: boolean;
1840
}
1941

2042
export const command: CommandModule<SharedOptions, ConfigOptions> = {
@@ -36,6 +58,11 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
3658
describe: 'Configuration value (for set command)',
3759
type: 'string',
3860
})
61+
.option('all', {
62+
describe: 'Clear all configuration settings (for clear command)',
63+
type: 'boolean',
64+
default: false,
65+
})
3966
.example('$0 config list', 'List all configuration values')
4067
.example(
4168
'$0 config get githubMode',
@@ -45,6 +72,10 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
4572
.example(
4673
'$0 config clear customPrompt',
4774
'Reset customPrompt to default value',
75+
)
76+
.example(
77+
'$0 config clear --all',
78+
'Clear all configuration settings',
4879
) as any; // eslint-disable-line @typescript-eslint/no-explicit-any
4980
},
5081
handler: async (argv: ArgumentsCamelCase<ConfigOptions>) => {
@@ -153,8 +184,26 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
153184

154185
// Handle 'clear' command
155186
if (argv.command === 'clear') {
187+
// Check if --all flag is provided
188+
if (argv.all) {
189+
// Confirm with the user before clearing all settings
190+
const isConfirmed = await confirm(
191+
'Are you sure you want to clear all configuration settings? This action cannot be undone.'
192+
);
193+
194+
if (!isConfirmed) {
195+
logger.info('Operation cancelled.');
196+
return;
197+
}
198+
199+
// Clear all settings
200+
clearAllConfig();
201+
logger.info('All configuration settings have been cleared. Default values will be used.');
202+
return;
203+
}
204+
156205
if (!argv.key) {
157-
logger.error('Key is required for clear command');
206+
logger.error('Key is required for clear command (or use --all to clear all settings)');
158207
return;
159208
}
160209

packages/cli/src/settings/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,14 @@ export const updateConfig = (config: Partial<Config>): Config => {
4646
fs.writeFileSync(configFile, JSON.stringify(updatedConfig, null, 2));
4747
return updatedConfig;
4848
};
49+
50+
/**
51+
* Clears all configuration settings by removing the config file
52+
* @returns The default configuration that will now be used
53+
*/
54+
export const clearAllConfig = (): Config => {
55+
if (fs.existsSync(configFile)) {
56+
fs.unlinkSync(configFile);
57+
}
58+
return defaultConfig;
59+
};

0 commit comments

Comments
 (0)