Skip to content

Commit

Permalink
feat: Add options for proxy, model, timeout, and max-length
Browse files Browse the repository at this point in the history
  • Loading branch information
thitlwincoder committed Nov 23, 2023
1 parent 2899f27 commit 1a8fac7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
102 changes: 101 additions & 1 deletion lib/src/commands/config_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ class ConfigCommand extends Command<int> {
'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.',
);
}

Expand All @@ -38,10 +54,17 @@ Format the commit message according to the Conventional Commits specification.''
Future<int> 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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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'];

Expand All @@ -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;
}
}
22 changes: 21 additions & 1 deletion lib/src/utils/config.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import 'package:hive/hive.dart';

// store data in hive box
Future<void> setData(String key, dynamic value) async {
final box = await Hive.openBox<dynamic>('ai_commit_config');
await box.put(key, value);
}

// store openai api key
Future<void> setKey(String value) async {
await setData('api_key', value);
}

// store locale language
Future<void> setLocale(String value) async {
await setData('locale', value);
}

// store generate commit message count
Future<void> setCount(int value) async {
await setData('generate_count', value);
}

// store is_conventional value
Future<void> setConventional({required bool value}) async {
await setData('generate_conventional_commits', value);
await setData('is_conventional', value);
}

// store proxy server address
Future<void> setProxy(String value) async {
await setData('proxy', value);
}

// store model name
Future<void> setModel(String value) async {
await setData('model', value);
}

// store timeout value
Future<void> setTimeout(int value) async {
await setData('timeout', value);
}

void getConfig() {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 1a8fac7

Please sign in to comment.