Skip to content

Commit f89b4a3

Browse files
author
benleibowitz
committed
feat(commands/config.ts): add support for 'describe' mode to print detailed help for config parameters
fix(commands/config.ts): improve error handling for 'get' and 'set' modes when no config keys are provided The changes in this commit add a new 'describe' mode to the config command, which allows users to get detailed information about the available configuration parameters. This includes a description of each parameter and the accepted values. The 'describe' mode can be used in two ways: 1. To print help for all available config parameters: ``` oco config describe ``` 2. To print help for specific config parameters: ``` oco config describe OCO_MODEL OCO_API_KEY ``` Additionally, the commit fixes the error handling for the 'get' and 'set' modes, ensuring that an error is thrown if no config keys are provided. These changes improve the usability and discoverability of the configuration system, making it easier for users to understand and manage their opencommit settings.
1 parent 7945f44 commit f89b4a3

File tree

2 files changed

+143
-4
lines changed

2 files changed

+143
-4
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ Simply set any of the variables above like this:
132132
oco config set OCO_MODEL=gpt-4o-mini
133133
```
134134

135+
To see all available configuration parameters and their accepted values:
136+
137+
```sh
138+
oco config describe
139+
```
140+
141+
To see details for a specific parameter:
142+
143+
```sh
144+
oco config describe OCO_MODEL
145+
```
146+
135147
Configure [GitMoji](https://gitmoji.dev/) to preface a message.
136148

137149
```sh

src/commands/config.ts

Lines changed: 131 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export enum CONFIG_KEYS {
3131

3232
export enum CONFIG_MODES {
3333
get = 'get',
34-
set = 'set'
34+
set = 'set',
35+
describe = 'describe'
3536
}
3637

3738
export const MODEL_LIST = {
@@ -603,28 +604,154 @@ export const setConfig = (
603604
outro(`${chalk.green('✔')} config successfully set`);
604605
};
605606

607+
// --- HELP MESSAGE GENERATION ---
608+
function getConfigKeyDetails(key) {
609+
switch (key) {
610+
case CONFIG_KEYS.OCO_MODEL:
611+
return {
612+
description: 'The AI model to use for generating commit messages',
613+
values: MODEL_LIST
614+
};
615+
case CONFIG_KEYS.OCO_AI_PROVIDER:
616+
return {
617+
description: 'The AI provider to use',
618+
values: Object.values(OCO_AI_PROVIDER_ENUM)
619+
};
620+
case CONFIG_KEYS.OCO_PROMPT_MODULE:
621+
return {
622+
description: 'The prompt module to use for commit message generation',
623+
values: Object.values(OCO_PROMPT_MODULE_ENUM)
624+
};
625+
case CONFIG_KEYS.OCO_LANGUAGE:
626+
return {
627+
description: 'The language to use for commit messages',
628+
values: Object.keys(i18n)
629+
};
630+
case CONFIG_KEYS.OCO_TEST_MOCK_TYPE:
631+
return {
632+
description: 'The type of test mock to use',
633+
values: ['commit-message', 'prompt-module-commitlint-config']
634+
};
635+
case CONFIG_KEYS.OCO_DESCRIPTION:
636+
case CONFIG_KEYS.OCO_EMOJI:
637+
case CONFIG_KEYS.OCO_WHY:
638+
case CONFIG_KEYS.OCO_ONE_LINE_COMMIT:
639+
case CONFIG_KEYS.OCO_OMIT_SCOPE:
640+
case CONFIG_KEYS.OCO_GITPUSH:
641+
return {
642+
description: 'Boolean flag',
643+
values: ['true', 'false']
644+
};
645+
case CONFIG_KEYS.OCO_TOKENS_MAX_INPUT:
646+
case CONFIG_KEYS.OCO_TOKENS_MAX_OUTPUT:
647+
return {
648+
description: 'Integer number',
649+
values: ['Any positive integer']
650+
};
651+
case CONFIG_KEYS.OCO_API_KEY:
652+
return {
653+
description: 'API key for the selected provider',
654+
values: ['String (required for most providers)']
655+
};
656+
case CONFIG_KEYS.OCO_API_URL:
657+
return {
658+
description: 'Custom API URL',
659+
values: ["URL string (must start with 'http://' or 'https://')"]
660+
};
661+
case CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER:
662+
return {
663+
description: 'Template placeholder for commit messages',
664+
values: ["String (must start with $)"]
665+
};
666+
default:
667+
return {
668+
description: 'String value',
669+
values: ['Any string']
670+
};
671+
}
672+
}
673+
674+
function printConfigKeyHelp(param) {
675+
if (!Object.values(CONFIG_KEYS).includes(param)) {
676+
console.log(chalk.red(`Unknown config parameter: ${param}`));
677+
return;
678+
}
679+
680+
const details = getConfigKeyDetails(param as CONFIG_KEYS);
681+
682+
console.log(chalk.bold(`\n${param}:`));
683+
console.log(chalk.gray(` Description: ${details.description}`));
684+
685+
if (Array.isArray(details.values)) {
686+
console.log(chalk.gray(' Accepted values:'));
687+
details.values.forEach(value => {
688+
console.log(chalk.gray(` - ${value}`));
689+
});
690+
} else {
691+
console.log(chalk.gray(' Accepted values by provider:'));
692+
Object.entries(details.values).forEach(([provider, values]) => {
693+
console.log(chalk.gray(` ${provider}:`));
694+
(values as string[]).forEach(value => {
695+
console.log(chalk.gray(` - ${value}`));
696+
});
697+
});
698+
}
699+
}
700+
701+
function printAllConfigHelp() {
702+
console.log(chalk.bold('Available config parameters:'));
703+
for (const key of Object.values(CONFIG_KEYS)) {
704+
printConfigKeyHelp(key);
705+
}
706+
console.log(chalk.gray('\nFor more help, see https://github.com/di-sukharev/opencommit'));
707+
}
708+
606709
export const configCommand = command(
607710
{
608711
name: COMMANDS.config,
609-
parameters: ['<mode>', '<key=values...>']
712+
parameters: ['<mode>', '[key=values...]'],
713+
help: {
714+
description: 'Configure opencommit settings',
715+
examples: [
716+
'Describe all config parameters: oco config describe',
717+
'Describe a specific parameter: oco config describe OCO_MODEL',
718+
'Get a config value: oco config get OCO_MODEL',
719+
'Set a config value: oco config set OCO_MODEL=gpt-4'
720+
]
721+
}
610722
},
611723
async (argv) => {
612724
try {
613725
const { mode, keyValues } = argv._;
614726
intro(`COMMAND: config ${mode} ${keyValues}`);
615727

616-
if (mode === CONFIG_MODES.get) {
728+
if (mode === CONFIG_MODES.describe) {
729+
if (!keyValues || keyValues.length === 0) {
730+
printAllConfigHelp();
731+
} else {
732+
for (const key of keyValues) {
733+
printConfigKeyHelp(key);
734+
}
735+
}
736+
process.exit(0);
737+
} else if (mode === CONFIG_MODES.get) {
738+
if (!keyValues || keyValues.length === 0) {
739+
throw new Error('No config keys specified for get mode');
740+
}
617741
const config = getConfig() || {};
618742
for (const key of keyValues) {
619743
outro(`${key}=${config[key as keyof typeof config]}`);
620744
}
621745
} else if (mode === CONFIG_MODES.set) {
746+
if (!keyValues || keyValues.length === 0) {
747+
throw new Error('No config keys specified for set mode');
748+
}
622749
await setConfig(
623750
keyValues.map((keyValue) => keyValue.split('=') as [string, string])
624751
);
625752
} else {
626753
throw new Error(
627-
`Unsupported mode: ${mode}. Valid modes are: "set" and "get"`
754+
`Unsupported mode: ${mode}. Valid modes are: "set", "get", and "describe"`
628755
);
629756
}
630757
} catch (error) {

0 commit comments

Comments
 (0)