execCmds method

Future<List<int>> execCmds(
  1. String distribution,
  2. List<String> cmds,
  3. {String? user,
  4. required dynamic onMsg(
    1. String
    ),
  5. required Function onDone,
  6. bool showOutput = true}
)

Executes a command list in a WSL distro

Implementation

Future<List<int>> execCmds(
  String distribution,
  List<String> cmds, {
  String? user,
  required Function(String) onMsg,
  required Function onDone,
  bool showOutput = true,
}) async {
  List<int> processes = [];
  Process result = await Process.start(
      'wsl', ['-d', distribution, '-u', user ?? 'root'],
      mode: ProcessStartMode.normal, runInShell: true);

  Timer currentWaiter = Timer(const Duration(seconds: 60), () {
    result.kill();
    onDone();
  });

  result.stdout
      .cast<List<int>>()
      .transform(const Utf8Decoder())
      .listen((String line) {
    resultQueue.add(line);
    onMsg(line);
    currentWaiter.cancel();
    // No new output within the last 30 seconds
    currentWaiter = Timer(const Duration(seconds: 15), () {
      result.kill();
      onDone();
    });
  });

  // Log output to file
  result.stdin.writeln('script -B /tmp/currentsessionlog -f');
  // Start windows with output
  await Process.start(
      'wsl',
      [
        '-d',
        distribution,
        '-u',
        user ?? 'root',
        'tail',
        '-n',
        '+1',
        '-f',
        '/tmp/currentsessionlog'
      ],
      mode: showOutput ? ProcessStartMode.detached : ProcessStartMode.normal,
      runInShell: true);

  // Delay to allow tail to start
  await Future.delayed(const Duration(milliseconds: 500));

  for (var cmd in cmds) {
    result.stdin.writeln(cmd);
  }
  return processes;
}