run method

Future<bool> run(
  1. BlueprintConfig config,
  2. String targetDir, {
  3. bool dryRun = false,
})

Sends an apply or plan command to the engine.

Implementation

Future<bool> run(
  BlueprintConfig config,
  String targetDir, {
  bool dryRun = false,
}) async {
  final binPath = enginePath;
  if (verbose) {
    print('[bridge] Using engine: $binPath');
  }

  final process = await Process.start(
    binPath,
    [],
    workingDirectory: targetDir,
  );

  final id = const Uuid().v4();
  final command = {
    'id': id,
    'type': 'command',
    'command': dryRun ? 'plan' : 'apply',
    'payload': config.toJson(),
  };
  process.stdin.writeln(jsonEncode(command));
  await process.stdin.close();

  bool success = false;
  final errors = <String>[];

  await for (final line in process.stdout
      .transform(utf8.decoder)
      .transform(const LineSplitter())) {
    if (line.trim().isEmpty) continue;
    try {
      final msg = EngineMessage.fromJson(
          jsonDecode(line) as Map<String, dynamic>);
      onMessage(msg);
      if (msg is DoneMessage) {
        success = msg.status == 'ok';
      }
      if (msg is LogMessage && msg.level == 'error') {
        errors.add(msg.message);
      }
    } catch (e) {
      if (verbose) print('[bridge] parse error: $e, line: $line');
    }
  }

  await for (final line in process.stderr
      .transform(utf8.decoder)
      .transform(const LineSplitter())) {
    if (line.trim().isNotEmpty) {
      errors.add('[stderr] $line');
    }
  }

  final exitCode = await process.exitCode;
  if (exitCode != 0 && !success) {
    throw ProcessException(binPath, [], errors.join('\n'), exitCode);
  }
  return success;
}