run method

Future<void> run(
  1. ArgResults results
)

Executes the command with the parsed results.

Implementation

Future<void> run(ArgResults results) async {
  if (results['help'] == true) {
    _printHelp();
    return;
  }

  final filePath = results['file'] as String;
  final file = File(filePath);

  if (!file.existsSync()) {
    throw Exception('File not found: $filePath');
  }

  log('📝 Reading file: $filePath');
  String content = await file.readAsString();

  // If previously injected, clean it up first (idempotent).
  if (content.contains(_startMarker)) {
    log('⚠️  Detected previous injection, updating...');
    content = _removeExistingInjection(content);
  }

  // Inject the new snippet.
  content = _injectCode(content);

  // Persist changes.
  await file.writeAsString(content);
  log('✅ Success: $filePath');
  log('💡 REST API initialization is ready!');
}