oto/lib/cli/commands/command_manager.dart
toki c344fff3ec feat: command catalog 구조 refactor 및 문서화 완료
- lib/cli/commands/command_catalog.dart 추가: CLI 명령어 카탈로그 시스템 구현
- lib/oto/commands/command_catalog.dart 추가: OTO command catalog 구현
- test/oto_catalog_cli_test.dart 추가: 카탈로그 테스트 케이스
- CLI 명령어 라우팅 및 자동 생성 파이프라인 구축
- README.md, ROADMAP.md, current.md 업데이트
- milestone 문서들 (cli-automation-baseline, structured-automation-surface) 업데이트
2026-05-21 22:37:50 +09:00

102 lines
2.9 KiB
Dart

import 'dart:async';
import 'package:dart_framework/utils/system_util.dart';
import 'command_base.dart';
import 'package:oto/cli/cli.dart';
class CommandManager {
Map<String, CommandBase> commandMap = {};
CommandManager(List<CommandBase> commands) {
for (var command in commands) {
commandMap[command.name] = command;
}
}
Future execute(String? command, List<String> parameters) async {
if (command == null) {
printHelp();
} else {
if (commandMap.containsKey(command)) {
var argStr = '';
for (var arg in parameters) {
argStr += '$arg, ';
}
var printHelp = false;
var commandItem = commandMap[command]!;
if (argStr.isNotEmpty) {
if (parameters[0].contains('-h')) {
commandItem.printHelp();
printHelp = true;
} else {
argStr = argStr.replaceFirst(', ', '', argStr.length - 3);
argStr = ', Arguments: [$argStr]';
}
}
if (!printHelp) {
if (parameters.isEmpty && !commandItem.shouldExecuteWithoutParameters()) {
commandItem.printHelp();
} else {
if (commandItem.shouldPrintExecuteLog(parameters)) {
await CLI.println('Execute command: $command$argStr',
color: Color.green);
}
try {
await commandItem.execute(parameters);
} on Exception catch (e) {
print('');
var message = e.toString().replaceAll('Exception:', '');
await CLI.println(
'${CLI.style('Error:', background: Color.red)} $message');
print('');
}
}
}
} else {
printHelp(append: [
'${CLI.style('Error:', background: Color.red)} The ${CLI.style('"$command"', color: Color.red, style: Style.bold)} command does not exist.'
]);
}
}
return simpleFuture;
}
void printHelp({List<String>? append}) {
var serviceName = CLI.current.config.serviceName;
var exeName = CLI.executableFileName;
final commandTag = '{COMMAND}';
var list = [
'',
'A command-line $serviceName.',
'',
'Usage: ${CLI.style('$exeName <command> <-h|[arguments]>', color: Color.greenStrong)}',
'',
'Available Command:',
commandTag,
'',
'Command options:',
' -h Print help for the current command.',
'',
'',
"Run `$exeName <command> -h` for more information about a command.",
];
var index = list.indexOf(commandTag);
list.removeAt(index);
if (commandMap.isNotEmpty) {
List<String> commandDescs = [];
for (var item in commandMap.entries) {
commandDescs.add(item.value.getCommandDescription());
}
list.insertAll(index, commandDescs);
}
if (append != null) {
list.insertAll(0, append);
list.insert(0, '');
}
CLI.print(list);
}
}