Skip to content

Commit fe8d0f2

Browse files
committed
Fix issue #191: Improve config clear command and add documentation about config file locations
1 parent 3c5d8a6 commit fe8d0f2

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

packages/cli/src/commands/config.ts

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as path from 'path';
2+
13
import chalk from 'chalk';
24
import { Logger } from 'mycoder-agent';
35

@@ -8,6 +10,7 @@ import {
810
updateConfig,
911
getConfigAtLevel,
1012
clearConfigAtLevel,
13+
clearConfigKey,
1114
ConfigLevel,
1215
} from '../settings/config.js';
1316
import { nameToLogIndex } from '../utils/nameToLogIndex.js';
@@ -19,6 +22,10 @@ export interface ConfigOptions extends SharedOptions {
1922
key?: string;
2023
value?: string;
2124
all?: boolean;
25+
global?: boolean;
26+
g?: boolean;
27+
verbose?: boolean;
28+
v?: boolean;
2229
}
2330

2431
export const command: CommandModule<SharedOptions, ConfigOptions> = {
@@ -45,6 +52,18 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
4552
type: 'boolean',
4653
default: false,
4754
})
55+
.option('global', {
56+
alias: 'g',
57+
describe: 'Use global configuration instead of project-level',
58+
type: 'boolean',
59+
default: false,
60+
})
61+
.option('verbose', {
62+
alias: 'v',
63+
describe: 'Show detailed information including config file paths',
64+
type: 'boolean',
65+
default: false,
66+
})
4867
.example('$0 config list', 'List all configuration values')
4968
.example(
5069
'$0 config get githubMode',
@@ -115,6 +134,35 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
115134
// Handle 'list' command
116135
if (argv.command === 'list') {
117136
logger.info('Current configuration:');
137+
138+
// Show config file locations
139+
const {
140+
getSettingsDir,
141+
getProjectSettingsDir,
142+
} = require('../settings/settings.js');
143+
const globalConfigPath = path.join(getSettingsDir(), 'config.json');
144+
const projectDir = getProjectSettingsDir();
145+
const projectConfigPath = projectDir
146+
? path.join(projectDir, 'config.json')
147+
: 'Not available';
148+
149+
logger.info(`Global config file: ${chalk.blue(globalConfigPath)}`);
150+
logger.info(`Project config file: ${chalk.blue(projectConfigPath)}`);
151+
logger.info('');
152+
153+
// Show config file paths in verbose mode
154+
if (argv.verbose || argv.v) {
155+
const { getProjectConfigFile } = await import('../settings/config.js');
156+
const { getSettingsDir } = await import('../settings/settings.js');
157+
const globalConfigPath = path.join(getSettingsDir(), 'config.json');
158+
const projectConfigPath = getProjectConfigFile();
159+
160+
logger.info(`Global config: ${chalk.blue(globalConfigPath)}`);
161+
logger.info(
162+
`Project config: ${projectConfigPath ? chalk.blue(projectConfigPath) : chalk.dim('(not set)')}`,
163+
);
164+
logger.info('');
165+
}
118166
const defaultConfig = getDefaultConfig();
119167

120168
// Get all valid config keys
@@ -276,15 +324,8 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
276324
return;
277325
}
278326

279-
// Get the current config, create a new object without the specified key
280-
const currentConfig = getConfig();
281-
const { [argv.key]: _, ...newConfig } = currentConfig as Record<
282-
string,
283-
any
284-
>;
285-
286-
// Update the config file with the new object
287-
updateConfig(newConfig);
327+
// Clear the specified key from the configuration at the current level
328+
clearConfigKey(argv.key, configLevel);
288329

289330
// Get the default value that will now be used
290331
const defaultValue =
@@ -297,13 +338,23 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
297338
// Determine where the new value is coming from
298339
const isDefaultAfterClear =
299340
JSON.stringify(newValue) === JSON.stringify(defaultValue);
341+
342+
// Get the actual config values at each level
343+
const globalConfig = getConfigAtLevel(ConfigLevel.GLOBAL);
344+
const projectConfig = getConfigAtLevel(ConfigLevel.PROJECT);
345+
346+
// Check if key exists AND has a non-default value in each level
300347
const afterClearInGlobal =
301348
!isDefaultAfterClear &&
302-
argv.key in getConfigAtLevel(ConfigLevel.GLOBAL);
349+
argv.key in globalConfig &&
350+
JSON.stringify(globalConfig[argv.key]) !== JSON.stringify(defaultValue);
351+
303352
const afterClearInProject =
304353
!isDefaultAfterClear &&
305354
!afterClearInGlobal &&
306-
argv.key in getConfigAtLevel(ConfigLevel.PROJECT);
355+
argv.key in projectConfig &&
356+
JSON.stringify(projectConfig[argv.key]) !==
357+
JSON.stringify(defaultValue);
307358

308359
let sourceDisplay = '';
309360
if (isDefaultAfterClear) {

packages/cli/src/settings/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,17 @@ export const clearConfigKey = (
275275
}
276276

277277
// Create a new config without the specified key
278-
const { [key]: _, ...newConfig } = currentLevelConfig as Record<string, any>;
278+
const { [key]: removedValue, ...newConfig } = currentLevelConfig as Record<
279+
string,
280+
any
281+
>;
282+
console.log(`Removed value for key ${key}:`, removedValue);
279283

280284
// Write the updated config back to the file
285+
console.log(`Clearing key ${key} from ${targetFile}`);
286+
console.log(`Original config:`, JSON.stringify(currentLevelConfig, null, 2));
287+
console.log(`New config without key:`, JSON.stringify(newConfig, null, 2));
288+
281289
fs.writeFileSync(targetFile, JSON.stringify(newConfig, null, 2));
282290

283291
// Return the new merged configuration

0 commit comments

Comments
 (0)