scp method
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.
If you don't pass progress, the default Progress uses encoding.
If you pass progress, encoding is ignored.
EXPERIMENTAL
Throws RunException.
Throws ScpException.
@Throwing(RunException)
@Throwing(ScpException)
Implementation
void scp({
required List<String> from,
required String to,
String? fromHost,
String? toHost,
String? fromUser,
String? toUser,
bool recursive = false,
Progress? progress,
Encoding encoding = utf8,
}) {
// 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');
}
cmdArgs.add('--');
// 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(encoding: encoding);
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,
);
}
}