diff --git a/lib/src/commands/config_command.dart b/lib/src/commands/config_command.dart index 8dcc076..bb045fc 100644 --- a/lib/src/commands/config_command.dart +++ b/lib/src/commands/config_command.dart @@ -23,6 +23,22 @@ class ConfigCommand extends Command { 'conventional', help: ''' Format the commit message according to the Conventional Commits specification.''', + ) + ..addOption( + 'proxy', + help: 'Set proxy server for OpenAI API.', + ) + ..addOption( + 'model', + help: 'Set model name for OpenAI API.', + ) + ..addOption( + 'timeout', + help: 'Set timeout in milliseconds.', + ) + ..addOption( + 'max-length', + help: 'Set max length of commit message.', ); } @@ -38,10 +54,17 @@ Format the commit message according to the Conventional Commits specification.'' Future run() async { final arguments = argResults?.arguments ?? []; + // check `arguments` is empty + // show `usage` and return + if (arguments.isEmpty) { - usageException('Missing argument for "config".'); + _logger.info(usage); + return ExitCode.software.code; } + // get `key` value from args + // check value is start with "sk-" and store + final key = argResults?['key']; if (key != null) { @@ -54,6 +77,9 @@ Format the commit message according to the Conventional Commits specification.'' } } + // get `locale` value from args + // check valid language code and store + final locale = argResults?['locale']; if (locale != null) { @@ -68,6 +94,9 @@ Format the commit message according to the Conventional Commits specification.'' } } + // get `count` value from args + // check value is greater than 0 and less than or equal to 5 and store + final count = argResults?['count']; if (count != null) { @@ -91,6 +120,8 @@ Format the commit message according to the Conventional Commits specification.'' } } + // get `conventional` value from args and store + if (argResults?.wasParsed('conventional') ?? false) { final conventional = argResults?['conventional']; @@ -101,6 +132,75 @@ Format the commit message according to the Conventional Commits specification.'' } } + // get `proxy` value from args + // check valid URL and store + + final proxy = argResults?['proxy']; + + if (proxy != null) { + if (Uri.tryParse('$proxy')?.isAbsolute ?? false) { + _logger.success('Setting "proxy" to "$proxy".'); + await setProxy('$proxy'); + } else { + _logger.err('Proxy must be valid URL.'); + return ExitCode.software.code; + } + } + + // get `model` value from args + // check isNotEmpty and store + + final model = argResults?['model']; + + if (model != null) { + if (model is String && model.isNotEmpty) { + _logger.success('Setting "model" to "$model".'); + await setModel(model); + } else { + _logger.err('Model must be a string.'); + return ExitCode.software.code; + } + } + + // get `timeout` value from args + // check greater than 500ms and store + + final timeout = argResults?['timeout']; + + if (timeout != null) { + if (int.tryParse('$timeout') != null) { + final value = int.parse('$timeout'); + if (value > 500) { + _logger.success('Setting "timeout" to "$value".'); + await setProxy('$value'); + } else { + _logger.err('Timeout must be an greater than 500ms.'); + return ExitCode.software.code; + } + } + } + + // get `max-length` value from args + // check greater than 20 and store + + final maxLength = argResults?['max-length']; + + if (maxLength != null) { + if (int.tryParse('$maxLength') != null) { + final value = int.parse('$maxLength'); + if (value > 20) { + _logger.success('Setting "max-length" to "$value".'); + await setProxy('$value'); + } else { + _logger.err('Max length must be an greater than 20.'); + return ExitCode.software.code; + } + } else { + _logger.err('Max length must be an integer.'); + return ExitCode.software.code; + } + } + return ExitCode.software.code; } } diff --git a/lib/src/utils/config.dart b/lib/src/utils/config.dart index e4f093f..4182554 100644 --- a/lib/src/utils/config.dart +++ b/lib/src/utils/config.dart @@ -1,24 +1,44 @@ import 'package:hive/hive.dart'; +// store data in hive box Future setData(String key, dynamic value) async { final box = await Hive.openBox('ai_commit_config'); await box.put(key, value); } +// store openai api key Future setKey(String value) async { await setData('api_key', value); } +// store locale language Future setLocale(String value) async { await setData('locale', value); } +// store generate commit message count Future setCount(int value) async { await setData('generate_count', value); } +// store is_conventional value Future setConventional({required bool value}) async { - await setData('generate_conventional_commits', value); + await setData('is_conventional', value); +} + +// store proxy server address +Future setProxy(String value) async { + await setData('proxy', value); +} + +// store model name +Future setModel(String value) async { + await setData('model', value); +} + +// store timeout value +Future setTimeout(int value) async { + await setData('timeout', value); } void getConfig() { diff --git a/pubspec.yaml b/pubspec.yaml index af7b47d..9c5a94f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: ai_commit -description: A Very Good Project created by Very Good CLI. +description: Dart CLI for generate commit messages with OpenAI version: 0.0.1 publish_to: none