From 80c1c74ca0a2e22b6ad3c8ce07c3dd3d971459c6 Mon Sep 17 00:00:00 2001 From: Wagner Silva Date: Mon, 5 Jan 2026 14:44:23 -0300 Subject: [PATCH] feat(config): update config validation and alias resolution --- README.md | 8 +++++++- src/cli.ts | 15 +++++++++++++-- src/config.ts | 35 +++++++++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index aa77642..17d5d08 100644 --- a/README.md +++ b/README.md @@ -218,7 +218,13 @@ git-commit-ai config --set backend=llamacpp git-commit-ai config --set model=gpt-4o git-commit-ai config --set temperature=0.5 -# List valid config keys +# Use short aliases +git-commit-ai config --set lang=pt # → default_language +git-commit-ai config --set scope=api # → default_scope +git-commit-ai config --set type=feat # → default_type +git-commit-ai config --set temp=0.5 # → temperature + +# List valid config keys and aliases git-commit-ai config --list-keys # Create/edit config file manually diff --git a/src/cli.ts b/src/cli.ts index 3a82e45..ab5abbc 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,7 +3,7 @@ import chalk from "chalk"; import ora from "ora"; import { createInterface } from "node:readline"; -import { loadConfig, saveConfig, showConfig, getConfigPath, updateConfig, VALID_CONFIG_KEYS } from "./config.js"; +import { loadConfig, saveConfig, showConfig, getConfigPath, updateConfig, VALID_CONFIG_KEYS, CONFIG_ALIASES } from "./config.js"; import { createBackend, detectBackend, @@ -547,7 +547,18 @@ export function createProgram(): Command { if (options.listKeys) { console.log(chalk.bold("Valid config keys:")); for (const key of VALID_CONFIG_KEYS) { - console.log(` ${key}`); + // Find alias for this key + const alias = Object.entries(CONFIG_ALIASES).find(([, v]) => v === key)?.[0]; + if (alias) { + console.log(` ${key} ${chalk.dim(`(alias: ${alias})`)}`); + } else { + console.log(` ${key}`); + } + } + console.log(); + console.log(chalk.bold("Short aliases:")); + for (const [alias, fullKey] of Object.entries(CONFIG_ALIASES)) { + console.log(` ${alias} → ${fullKey}`); } return; } diff --git a/src/config.ts b/src/config.ts index b73e347..a874f14 100644 --- a/src/config.ts +++ b/src/config.ts @@ -33,6 +33,17 @@ export const VALID_CONFIG_KEYS = [ export type ConfigKey = typeof VALID_CONFIG_KEYS[number]; +/** + * Short aliases for config keys + */ +export const CONFIG_ALIASES: Record = { + lang: "default_language", + scope: "default_scope", + type: "default_type", + url: "ollama_url", + temp: "temperature", +}; + export function getConfigPath(): string { return join(homedir(), ".config", "git-commit-ai", "config.toml"); } @@ -154,16 +165,25 @@ export function showConfig(config: Config): string { * Returns an object with success status and message */ export function updateConfig(key: string, value: string): { success: boolean; message: string } { + // Resolve alias to full key name + const resolvedKey = CONFIG_ALIASES[key] || key; + // Validate key - if (!VALID_CONFIG_KEYS.includes(key as ConfigKey)) { + if (!VALID_CONFIG_KEYS.includes(resolvedKey as ConfigKey)) { + const aliasHelp = Object.entries(CONFIG_ALIASES) + .map(([alias, full]) => `${alias} → ${full}`) + .join(", "); return { success: false, - message: `Invalid config key: "${key}". Valid keys: ${VALID_CONFIG_KEYS.join(", ")}`, + message: `Invalid config key: "${key}". Valid keys: ${VALID_CONFIG_KEYS.join(", ")}. Aliases: ${aliasHelp}`, }; } + + // Use resolved key from here + const configKey = resolvedKey as ConfigKey; // Validate backend value - if (key === "backend" && !VALID_BACKENDS.includes(value as BackendType)) { + if (configKey === "backend" && !VALID_BACKENDS.includes(value as BackendType)) { return { success: false, message: `Invalid backend: "${value}". Valid backends: ${VALID_BACKENDS.join(", ")}`, @@ -171,7 +191,7 @@ export function updateConfig(key: string, value: string): { success: boolean; me } // Validate temperature value - if (key === "temperature") { + if (configKey === "temperature") { const temp = parseFloat(value); if (isNaN(temp) || temp < 0 || temp > 1) { return { @@ -184,7 +204,7 @@ export function updateConfig(key: string, value: string): { success: boolean; me // Load current config and update const config = loadConfig(); - switch (key) { + switch (configKey) { case "backend": config.backend = value as BackendType; break; @@ -212,8 +232,11 @@ export function updateConfig(key: string, value: string): { success: boolean; me } saveConfig(config); + + // Show alias resolution in message if applicable + const keyDisplay = key !== configKey ? `${key} (${configKey})` : configKey; return { success: true, - message: `Config updated: ${key} = "${value}"`, + message: `Config updated: ${keyDisplay} = "${value}"`, }; }