run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  final args = argResults!;
  final rest = args.rest;
  final force = args['force'] as bool;

  // Default to 'ci' if no workflow name provided
  final workflowName = rest.isEmpty ? 'ci' : rest.first;

  // Get the appropriate template
  final templateContent = _getTemplate(workflowName);

  // Ensure .github/workflows directory exists
  final workflowsDir = Directory(p.join('.github', 'workflows'));
  if (!workflowsDir.existsSync()) {
    workflowsDir.createSync(recursive: true);
    stdout.writeln('Created .github/workflows/ directory');
  }

  // Create the workflow dart file in .github/workflows
  final workflowFile = File(
    p.join('.github', 'workflows', '$workflowName.dart'),
  );
  if (workflowFile.existsSync() && !force) {
    stderr.writeln(
      'Error: .github/workflows/$workflowName.dart already exists',
    );
    stderr.writeln('Use --force to overwrite');
    return 1;
  }

  workflowFile.writeAsStringSync(templateContent);
  stdout.writeln('Created .github/workflows/$workflowName.dart');

  stdout.writeln('');
  stdout.writeln('✓ Initialization complete!');
  stdout.writeln('');
  stdout.writeln('Next steps:');
  stdout.writeln(
    '  1. Edit .github/workflows/$workflowName.dart to customize your workflow',
  );
  stdout.writeln('  2. Run: dart run gha_cli generate');
  stdout.writeln('');

  return 0;
}