Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion packages/cli/src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import chalk from 'chalk';
import { createInterface } from 'readline/promises';
import { Logger } from 'mycoder-agent';

import { SharedOptions } from '../options.js';
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<boolean> {
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<SharedOptions, ConfigOptions> = {
Expand All @@ -36,6 +58,11 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
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',
Expand All @@ -45,6 +72,10 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
.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<ConfigOptions>) => {
Expand Down Expand Up @@ -153,8 +184,26 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {

// 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;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/settings/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ export const updateConfig = (config: Partial<Config>): 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;
};
Loading