Skip to content

Commit 97704ef

Browse files
authored
Merge pull request #126 from drivecore/feature/117-config-validation
Add validation for config keys and filter list output
2 parents d85ad5a + 9b1ef68 commit 97704ef

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

packages/cli/src/commands/config.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,24 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
5959
if (argv.command === 'list') {
6060
logger.info('Current configuration:');
6161
const defaultConfig = getDefaultConfig();
62-
Object.entries(config).forEach(([key, value]) => {
62+
63+
// Get all valid config keys
64+
const validKeys = Object.keys(defaultConfig);
65+
66+
// Filter and sort config entries
67+
const configEntries = Object.entries(config)
68+
.filter(([key]) => validKeys.includes(key))
69+
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
70+
71+
// Display config entries with default indicators
72+
configEntries.forEach(([key, value]) => {
6373
const isDefault =
6474
JSON.stringify(value) ===
6575
JSON.stringify(defaultConfig[key as keyof typeof defaultConfig]);
66-
const valueDisplay = chalk.green(value);
67-
const statusIndicator = isDefault
68-
? chalk.dim(' (default)')
69-
: chalk.blue(' (custom)');
70-
logger.info(` ${key}: ${valueDisplay}${statusIndicator}`);
76+
const valueDisplay = isDefault
77+
? chalk.dim(`${value} (default)`)
78+
: chalk.green(value);
79+
logger.info(` ${key}: ${valueDisplay}`);
7180
});
7281
return;
7382
}
@@ -101,6 +110,16 @@ export const command: CommandModule<SharedOptions, ConfigOptions> = {
101110
return;
102111
}
103112

113+
// Validate that the key exists in default config
114+
const defaultConfig = getDefaultConfig();
115+
if (!(argv.key in defaultConfig)) {
116+
logger.error(`Invalid configuration key '${argv.key}'`);
117+
logger.info(
118+
`Valid configuration keys: ${Object.keys(defaultConfig).join(', ')}`,
119+
);
120+
return;
121+
}
122+
104123
// Parse the value based on current type or infer boolean/number
105124
let parsedValue: string | boolean | number = argv.value;
106125

packages/cli/tests/commands/config.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,37 @@ describe.skip('Config Command', () => {
6565
interactive: false,
6666
command: 'list',
6767
} as any);
68+
it('should filter out invalid config keys in list command', async () => {
69+
// Mock getConfig to return config with invalid keys
70+
vi.mocked(getConfig).mockReturnValue({
71+
githubMode: false,
72+
invalidKey: 'some value',
73+
} as any);
74+
75+
// Mock getDefaultConfig to return only valid keys
76+
vi.mocked(getDefaultConfig).mockReturnValue({
77+
githubMode: false,
78+
});
79+
80+
await command.handler!({
81+
_: ['config', 'config', 'list'],
82+
logLevel: 'info',
83+
interactive: false,
84+
command: 'list',
85+
} as any);
86+
87+
expect(getConfig).toHaveBeenCalled();
88+
expect(getDefaultConfig).toHaveBeenCalled();
89+
90+
// Should show the valid key
91+
expect(mockLogger.info).toHaveBeenCalledWith(
92+
expect.stringContaining('githubMode'),
93+
);
94+
95+
// Should not show the invalid key
96+
const infoCallArgs = mockLogger.info.mock.calls.flat();
97+
expect(infoCallArgs.join()).not.toContain('invalidKey');
98+
});
6899

69100
expect(getConfig).toHaveBeenCalled();
70101
expect(mockLogger.info).toHaveBeenCalledWith('Current configuration:');
@@ -147,6 +178,21 @@ describe.skip('Config Command', () => {
147178
);
148179
});
149180

181+
it('should validate key exists in default config for set command', async () => {
182+
await command.handler!({
183+
_: ['config', 'config', 'set', 'invalidKey', 'value'],
184+
logLevel: 'info',
185+
interactive: false,
186+
command: 'set',
187+
key: 'invalidKey',
188+
value: 'value',
189+
} as any);
190+
191+
expect(mockLogger.error).toHaveBeenCalledWith(
192+
expect.stringContaining('Invalid configuration key'),
193+
);
194+
});
195+
150196
it('should handle unknown command', async () => {
151197
await command.handler!({
152198
_: ['config', 'config', 'unknown'],

0 commit comments

Comments
 (0)