dew/packages/kanban/lib/src/commands/unarchive_command.dart

71 lines
2.1 KiB
Dart

import 'package:dew_core/dew_core.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:path/path.dart' as p;
import '../kanban_config.dart';
import '../ticket_store.dart';
class UnarchiveCommand extends DewCommand with DewToolCommand {
final FileSystem _fs;
UnarchiveCommand({this._fs = const LocalFileSystem()}) {
argParser
..addOption(
'id',
abbr: 'i',
mandatory: true,
help: 'Ticket ID to unarchive.',
)
..addOption(
'column',
abbr: 'c',
help: 'Column to restore to. Defaults to the first configured column.',
);
}
@override
final String name = 'unarchive';
@override
final String description = 'Restore an archived ticket to a column.';
@override
final String toolName = 'kanban_unarchive_ticket';
@override
Future<String> callAsTool(Map<String, dynamic> args) async {
final id = '${args['id']}'.toUpperCase();
final context = await ProjectContext.find(fs: _fs);
final config = context.config.kanban;
final kanbanDir = context.dirs.kanban;
final store = TicketStore(
kanbanDir: kanbanDir,
prefix: config.prefix,
fs: context.fs,
);
final ticket = await store.findById(id);
if (ticket == null) throw ArgumentError('Ticket $id not found.');
if (ticket.column != 'archive') return '$id is not archived.';
final columnArg = args['column'] != null ? '${args['column']}' : null;
final targetColumn = columnArg ?? config.columns.first.id;
if (!config.columns.any((c) => c.id == targetColumn)) {
throw ArgumentError(
'Unknown column "$targetColumn". '
'Valid: ${config.columns.map((c) => c.id).join(', ')}',
);
}
final targetDir = context.fs.directory(p.join(kanbanDir, targetColumn));
await targetDir.create(recursive: true);
final srcFile = context.fs.file(p.join(kanbanDir, 'archive', '$id.md'));
final dstFile = context.fs.file(p.join(targetDir.path, '$id.md'));
await srcFile.rename(dstFile.path);
return 'Restored $id to "$targetColumn".';
}
}