remoteArchiveCommand function

String remoteArchiveCommand(
  1. String src, {
  2. required ArchiveFormat format,
  3. required bool isDir,
})

Returns a POSIX sh command that builds an archive of src (a file or directory per isDir) in format and prints the resulting temporary archive path as its only stdout line.

A temporary output path is created before archiving:

  • First, mktemp is used when available, producing a unique path such as /tmp/tmp.ABc123XYZ.
  • If mktemp is unavailable, a fallback path is constructed from ${TMPDIR:-/tmp} and the current shell PID, for example: /tmp/omnyshell-dl.12345.

The archive is written directly to that path and no filename extension is added. For example:

src = /home/alice/project
tmp = /tmp/tmp.ABc123XYZ

With ArchiveFormat.tarGz, the resulting file at /tmp/tmp.ABc123XYZ contains a gzip-compressed tar archive of project/.

On success, the command prints the temporary archive path and exits with status 0. On failure, the temporary file is removed and the command exits with a non-zero status.

src is embedded as a single-quoted literal. Caller is expected to have validated the combination with archiveError.

Implementation

String remoteArchiveCommand(
  String src, {
  required ArchiveFormat format,
  required bool isDir,
}) {
  final archiver = switch ((format, isDir)) {
    // gzip a single file in place to the temp file.
    (ArchiveFormat.gz, _) => r'gzip -c -- "$src" > "$tmp"',
    // tar+gzip a directory, relative to its parent so the dir name is preserved.
    (ArchiveFormat.tarGz, _) =>
      r'tar -czf "$tmp" -C "$(dirname "$src")" -- "$(basename "$src")"',
    // zip a directory recursively (cd into the parent to keep relative paths).
    // `zip` rejects a `--` separator, so it is omitted.
    (ArchiveFormat.zip, true) =>
      r'( cd "$(dirname "$src")" && zip -r -q "$tmp" "$(basename "$src")" )',
    // zip a single file, junking the path so only the basename is stored.
    (ArchiveFormat.zip, false) => r'zip -j -q "$tmp" "$src"',
  };
  final quoted = _singleQuote(src);
  return "src=$quoted; "
      "tmp=\$(mktemp 2>/dev/null) || tmp=\"\${TMPDIR:-/tmp}/omnyshell-dl.\$\$\"; "
      "rm -f \"\$tmp\"; "
      "if $archiver; then printf '%s\\n' \"\$tmp\"; "
      "else rm -f \"\$tmp\"; exit 1; fi";
}