writeFileToVm function

Future<void> writeFileToVm(
  1. String? ipAddress,
  2. String remotePath,
  3. String content
)

Implementation

Future<void> writeFileToVm(
  String? ipAddress,
  String remotePath,
  String content,
) async {
  if (ipAddress == null) {
    throw StateError('Cannot write file to VM: IP is null.');
  }

  // Copy via the system `scp` binary (key auth). This relies on the SSH key
  // already installed by setupDirectSsh and, like ssh, is exempt from macOS
  // Local Network privacy when the worker runs as a LaunchAgent.
  final local = File('/tmp/openci-upload-${const Uuid().v4()}');
  local.writeAsStringSync(content);
  try {
    final result = await Process.run('/usr/bin/scp', [
      ..._sshBaseOpts,
      '-o',
      'BatchMode=yes',
      '-i',
      _sshKeyPath,
      local.path,
      '$sshUser@$ipAddress:$remotePath',
    ]);
    if (result.exitCode != 0) {
      throw Exception('Failed to scp file to $remotePath: ${result.stderr}');
    }
  } finally {
    try {
      local.deleteSync();
    } catch (_) {}
  }
}