All files / src/cli/docker container-runtime.ts

100% Statements 38/38
100% Branches 12/12
100% Functions 7/7
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133          5x       5x           34x 34x       30x   4x               21x 21x       19x 13x   6x   2x                   13x 13x                 13x     13x 8x       5x 5x 2x       3x 1x     2x               5x 15x   15x 21x 21x   13x 13x 13x   13x                     2x                         5x 5x 5x           5x 15x    
/**
 * Container runtime detection module.
 * Detects Docker or Podman and their compose variants, caching the result per process.
 */
 
import { spawnSync } from "child_process";
import { ContainerRuntime, ContainerRuntimeInfo } from "./types";
 
/** Module-level cached runtime info */
let cachedRuntime: ContainerRuntimeInfo | null = null;
 
/**
 * Try running a command and return true if it exits 0.
 */
function commandSucceeds(cmd: string, args: string[]): boolean {
  try {
    const result = spawnSync(cmd, args, {
      stdio: "pipe",
      encoding: "utf8",
    });
    return result.status === 0;
  } catch {
    return false;
  }
}
 
/**
 * Try running a command and return its stdout if it exits 0, otherwise undefined.
 */
function commandOutput(cmd: string, args: string[]): string | undefined {
  try {
    const result = spawnSync(cmd, args, {
      stdio: "pipe",
      encoding: "utf8",
    });
    if (result.status === 0 && result.stdout) {
      return result.stdout.trim();
    }
    return undefined;
  } catch {
    return undefined;
  }
}
 
/**
 * Extract version string from runtime --version output.
 * e.g. "Docker version 24.0.7, build afdd53b" → "24.0.7"
 *      "podman version 4.9.3" → "4.9.3"
 */
function parseVersion(output: string): string | undefined {
  const match = output.match(/(\d+\.\d+\.\d+)/);
  return match?.[1];
}
 
/**
 * Detect the compose command for a given runtime.
 * Priority for docker: docker compose → docker-compose
 * Priority for podman: podman compose → podman-compose → docker-compose (fallback)
 */
function detectComposeCmd(runtime: ContainerRuntime): string[] | null {
  const runtimeCmd = runtime;
 
  // Try "<runtime> compose version" (compose v2 plugin)
  if (commandSucceeds(runtimeCmd, ["compose", "version"])) {
    return [runtimeCmd, "compose"];
  }
 
  // Try "<runtime>-compose --version" (standalone compose)
  const standaloneCompose = `${runtimeCmd}-compose`;
  if (commandSucceeds(standaloneCompose, ["--version"])) {
    return [standaloneCompose];
  }
 
  // Cross-runtime fallback: try docker-compose as last resort
  if (commandSucceeds("docker-compose", ["--version"])) {
    return ["docker-compose"];
  }
 
  return null;
}
 
/**
 * Perform full container runtime detection.
 * Priority: docker > podman.
 * Checks runtime availability, daemon status, and compose command.
 */
export function detectContainerRuntime(): ContainerRuntimeInfo {
  const runtimes: ContainerRuntime[] = ["docker", "podman"];
 
  for (const runtime of runtimes) {
    const versionOutput = commandOutput(runtime, ["--version"]);
    if (versionOutput) {
      // Runtime binary exists, check if daemon is accessible
      const runtimeAvailable = commandSucceeds(runtime, ["info"]);
      const composeCmd = detectComposeCmd(runtime);
      const runtimeVersion = parseVersion(versionOutput);
 
      return {
        runtime,
        runtimeCmd: runtime,
        runtimeAvailable,
        composeCmd,
        runtimeVersion,
      };
    }
  }
 
  // No runtime found at all
  return {
    runtime: "docker",
    runtimeCmd: "docker",
    runtimeAvailable: false,
    composeCmd: null,
    runtimeVersion: undefined,
  };
}
 
/**
 * Get cached container runtime info.
 * Detects once per process and caches the result.
 */
export function getContainerRuntime(): ContainerRuntimeInfo {
  cachedRuntime ??= detectContainerRuntime();
  return cachedRuntime;
}
 
/**
 * Reset the runtime cache. Used in tests to allow re-detection.
 */
export function resetRuntimeCache(): void {
  cachedRuntime = null;
}