scp method

void scp({
  1. required List<String> from,
  2. required String to,
  3. String? fromHost,
  4. String? toHost,
  5. String? fromUser,
  6. String? toUser,
  7. bool recursive = false,
  8. Progress? progress,
})

Run scp (secure copy) to copy files between remote hosts.

from the from path fromHost the host the from path exists on. If fromHost isn't specified it is assumed that the from path is on the local machine fromUser the user to use to authenticate against the fromHost. You may only specify fromUser if fromHost is passed.

to the path on the toHost to copy the files to. toHost the host the to path exists on. If toHost isn't specified it is assumed that the from path is on the local machine toUser the user to use to authenticate against the toHost. You may only specify toUser if toHost is passed. Set recursive to true to do a recursive copy from the from path. recursive defaults to false. EXPERIMENTAL

Implementation

void scp({
  required List<String> from,
  required String to,
  String? fromHost,
  String? toHost,
  String? fromUser,
  String? toUser,
  bool recursive = false,
  Progress? progress,
}) {
  // toUser is only valid if toHost is given
  if (toUser != null && toHost == null) {
    throw ScpException('[toUser] is only valid if toHost is also past');
  }

  // fomrUser is only valid if fromHost is given
  if (fromUser != null && fromHost == null) {
    throw ScpException('[fromUser] is only valid if toHost is also past');
  }

  final cmdArgs = <String>[];

  if (recursive) {
    cmdArgs.add('-r');
  }

  // build fromArg user@host:/path
  var fromArg = '';
  if (fromHost != null) {
    var fromUserArg = '';
    if (fromUser != null) {
      fromUserArg = '$fromUser@';
    }
    // quote the [from] arg to stop it being
    // glob expanded. When [host] is not null he [from] arg describes
    // the remote file system so local expansion makes no sense.
    fromArg = '"$fromUserArg$fromHost:${from.join(" ")}"';
  } else {
    fromArg = from.join(' ');
  }

  cmdArgs.add(fromArg);

  // build toArg user@host:/path
  var toArg = '';
  if (toHost != null) {
    var toUserArg = '';
    if (toUser != null) {
      toUserArg = '$toUser@';
    }
    // quote the [to] arg to stop it being
    // glob expanded. When [host] is not null he [to] arg describes
    // the remote file system so local expansion makes no sense.
    toArg = '"$toUserArg$toHost:$to"';
  } else {
    toArg = to;
  }

  cmdArgs.add(toArg);

  progress ??= Progress.devNull();

  try {
    startFromArgs('scp', cmdArgs, progress: progress, terminal: true);
  } on RunException catch (e) {
    final error = _scpErrors[e.exitCode!];
    throw RunException(
      e.cmdLine,
      e.exitCode,
      red('scp exit code: ${e.exitCode} - $error'),
      stackTrace: e.stackTrace,
    );
  }
}