|
1 | 1 | use clap::Parser; |
| 2 | +use std::collections::HashSet; |
2 | 3 | use std::fs; |
3 | 4 | use std::path::{Path, PathBuf}; |
4 | 5 | use std::process::Command; |
@@ -48,6 +49,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { |
48 | 49 | } |
49 | 50 | println!(); |
50 | 51 |
|
| 52 | + // Collect all unique base images from test projects |
| 53 | + println!("--- Collecting base images from test projects ---"); |
| 54 | + let base_images = collect_base_images(&test_dirs)?; |
| 55 | + println!("Found {} unique base image(s):", base_images.len()); |
| 56 | + for image in &base_images { |
| 57 | + println!(" - {}", image); |
| 58 | + } |
| 59 | + println!(); |
| 60 | + |
| 61 | + // Pull all base images to ensure they are current |
| 62 | + println!("--- Pulling base images ---"); |
| 63 | + pull_base_images(&base_images)?; |
| 64 | + println!(); |
| 65 | + |
51 | 66 | // Clean up any leftover temporary directories from previous runs |
52 | 67 | println!("--- Cleaning up leftover temporary directories ---"); |
53 | 68 | cleanup_leftover_temp_directories(&project_root)?; |
@@ -106,6 +121,48 @@ fn find_test_directories( |
106 | 121 | Ok(dirs) |
107 | 122 | } |
108 | 123 |
|
| 124 | +fn collect_base_images(test_dirs: &[PathBuf]) -> Result<Vec<String>, Box<dyn std::error::Error>> { |
| 125 | + let mut base_images = HashSet::new(); |
| 126 | + |
| 127 | + for test_dir in test_dirs { |
| 128 | + let config_path = test_dir.join("config.json"); |
| 129 | + if config_path.exists() { |
| 130 | + let config_content = fs::read_to_string(&config_path)?; |
| 131 | + let config: TestConfig = serde_json::from_str(&config_content).map_err(|e| { |
| 132 | + format!( |
| 133 | + "Failed to parse config.json in {}: {}", |
| 134 | + test_dir.display(), |
| 135 | + e |
| 136 | + ) |
| 137 | + })?; |
| 138 | + base_images.insert(config.base_image); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + let mut images: Vec<String> = base_images.into_iter().collect(); |
| 143 | + images.sort(); |
| 144 | + Ok(images) |
| 145 | +} |
| 146 | + |
| 147 | +fn pull_base_images(base_images: &[String]) -> Result<(), Box<dyn std::error::Error>> { |
| 148 | + for image in base_images { |
| 149 | + println!("Pulling base image: {}", image); |
| 150 | + let output = Command::new("docker") |
| 151 | + .args(["pull", image]) |
| 152 | + .output() |
| 153 | + .map_err(|e| format!("Failed to run docker pull command for {}: {}", image, e))?; |
| 154 | + |
| 155 | + if !output.status.success() { |
| 156 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 157 | + return Err(format!("Failed to pull base image {}: {}", image, stderr).into()); |
| 158 | + } |
| 159 | + |
| 160 | + println!("✓ Successfully pulled {}", image); |
| 161 | + } |
| 162 | + |
| 163 | + Ok(()) |
| 164 | +} |
| 165 | + |
109 | 166 | fn cleanup_leftover_temp_directories( |
110 | 167 | project_root: &Path, |
111 | 168 | ) -> Result<(), Box<dyn std::error::Error>> { |
|
0 commit comments