fix(config): allow get on config properties without key
This commit is contained in:
@@ -12,7 +12,7 @@ export default command({
|
|||||||
const { mode, keyValue: keyValues } = argv._;
|
const { mode, keyValue: keyValues } = argv._;
|
||||||
|
|
||||||
if (mode === 'get') {
|
if (mode === 'get') {
|
||||||
const config = await getConfig();
|
const config = await getConfig({}, true);
|
||||||
for (const key of keyValues) {
|
for (const key of keyValues) {
|
||||||
if (hasOwn(config, key)) {
|
if (hasOwn(config, key)) {
|
||||||
console.log(`${key}=${config[key as keyof typeof config]}`);
|
console.log(`${key}=${config[key as keyof typeof config]}`);
|
||||||
|
|||||||
@@ -104,14 +104,24 @@ const readConfigFile = async (): Promise<RawConfig> => {
|
|||||||
return ini.parse(configString);
|
return ini.parse(configString);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getConfig = async (cliConfig?: RawConfig): Promise<ValidConfig> => {
|
export const getConfig = async (
|
||||||
|
cliConfig?: RawConfig,
|
||||||
|
suppressErrors?: boolean,
|
||||||
|
): Promise<ValidConfig> => {
|
||||||
const config = await readConfigFile();
|
const config = await readConfigFile();
|
||||||
const parsedConfig: Record<string, unknown> = {};
|
const parsedConfig: Record<string, unknown> = {};
|
||||||
|
|
||||||
for (const key of Object.keys(configParsers) as ConfigKeys[]) {
|
for (const key of Object.keys(configParsers) as ConfigKeys[]) {
|
||||||
const parser = configParsers[key];
|
const parser = configParsers[key];
|
||||||
const value = cliConfig?.[key] ?? config[key];
|
const value = cliConfig?.[key] ?? config[key];
|
||||||
|
|
||||||
|
if (suppressErrors) {
|
||||||
|
try {
|
||||||
parsedConfig[key] = parser(value);
|
parsedConfig[key] = parser(value);
|
||||||
|
} catch {}
|
||||||
|
} else {
|
||||||
|
parsedConfig[key] = parser(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return parsedConfig as ValidConfig;
|
return parsedConfig as ValidConfig;
|
||||||
|
|||||||
@@ -25,19 +25,6 @@ export default testSuite(({ describe }) => {
|
|||||||
expect(stderr).toMatch('Invalid config property OPENAI_KEY: Must start with "sk-"');
|
expect(stderr).toMatch('Invalid config property OPENAI_KEY: Must start with "sk-"');
|
||||||
});
|
});
|
||||||
|
|
||||||
await test('set config file', async () => {
|
|
||||||
await aicommits(['config', 'set', openAiToken]);
|
|
||||||
|
|
||||||
const configFile = await fs.readFile(configPath, 'utf8');
|
|
||||||
expect(configFile).toMatch(openAiToken);
|
|
||||||
});
|
|
||||||
|
|
||||||
await test('get config file', async () => {
|
|
||||||
const { stdout } = await aicommits(['config', 'get', 'OPENAI_KEY']);
|
|
||||||
|
|
||||||
expect(stdout).toBe(openAiToken);
|
|
||||||
});
|
|
||||||
|
|
||||||
await test('reading unknown config', async () => {
|
await test('reading unknown config', async () => {
|
||||||
await fs.appendFile(configPath, 'UNKNOWN=1');
|
await fs.appendFile(configPath, 'UNKNOWN=1');
|
||||||
|
|
||||||
@@ -63,11 +50,26 @@ export default testSuite(({ describe }) => {
|
|||||||
await aicommits(['config', 'set', timeout]);
|
await aicommits(['config', 'set', timeout]);
|
||||||
|
|
||||||
const configFile = await fs.readFile(configPath, 'utf8');
|
const configFile = await fs.readFile(configPath, 'utf8');
|
||||||
|
|
||||||
expect(configFile).toMatch(timeout);
|
expect(configFile).toMatch(timeout);
|
||||||
|
|
||||||
|
const get = await aicommits(['config', 'get', 'timeout']);
|
||||||
|
expect(get.stdout).toBe(timeout);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await test('set config file', async () => {
|
||||||
|
await aicommits(['config', 'set', openAiToken]);
|
||||||
|
|
||||||
|
const configFile = await fs.readFile(configPath, 'utf8');
|
||||||
|
expect(configFile).toMatch(openAiToken);
|
||||||
|
});
|
||||||
|
|
||||||
|
await test('get config file', async () => {
|
||||||
|
const { stdout } = await aicommits(['config', 'get', 'OPENAI_KEY']);
|
||||||
|
|
||||||
|
expect(stdout).toBe(openAiToken);
|
||||||
|
});
|
||||||
|
|
||||||
await fixture.rm();
|
await fixture.rm();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user