1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-10-01 08:44:09 +02:00

add command to update images

This commit is contained in:
Robin Appelman 2025-08-26 17:13:18 +02:00
commit 85335d84de
5 changed files with 93 additions and 44 deletions

View file

@ -12,55 +12,65 @@ pub async fn image_exists(docker: &Docker, image: &str) -> bool {
docker.inspect_image(image).await.is_ok()
}
pub async fn update_image(docker: &Docker, image: &str) -> Result<()> {
if image_exists(docker, image).await {
force_pull_image(docker, image).await?;
}
Ok(())
}
pub async fn pull_image(docker: &Docker, image: &str) -> Result<()> {
if docker.inspect_image(image).await.is_err() {
println!("Pulling image {}", image);
if !image_exists(docker, image).await {
force_pull_image(docker, image).await?;
}
Ok(())
}
let mut info_stream = docker.create_image(
Some(CreateImageOptions {
from_image: if image.contains(':') {
image.to_string()
} else {
format!("{}:latest", image)
},
..Default::default()
}),
None,
None,
);
pub async fn force_pull_image(docker: &Docker, image: &str) -> Result<()> {
println!("Pulling image {}", image);
let mut bars: HashMap<String, u16> = HashMap::new();
let mut info_stream = docker.create_image(
Some(CreateImageOptions {
from_image: if image.contains(':') {
image.to_string()
} else {
format!("{}:latest", image)
},
..Default::default()
}),
None,
None,
);
let mut stdout = stdout();
while let Some(info) = info_stream.next().await {
let info: CreateImageInfo = info
.into_diagnostic()
.wrap_err_with(|| format!("Error while pulling image {}", image))?;
if let (Some(id), Some(status), Some(progress)) = (info.id, info.status, info.progress)
{
match bars.get(&id) {
Some(pos) => {
let offset = bars.len() as u16 - pos;
write!(
stdout,
"{}{}{} - {:12} {}{}",
cursor::Save,
cursor::Up(offset),
id,
status,
progress,
cursor::Restore
)
.into_diagnostic()?;
}
None => {
writeln!(stdout, "{} - {:12} {}", id, status, progress)
.into_diagnostic()?;
bars.insert(id, bars.len() as u16);
}
let mut bars: HashMap<String, u16> = HashMap::new();
let mut stdout = stdout();
while let Some(info) = info_stream.next().await {
let info: CreateImageInfo = info
.into_diagnostic()
.wrap_err_with(|| format!("Error while pulling image {}", image))?;
if let (Some(id), Some(status), Some(progress)) = (info.id, info.status, info.progress) {
match bars.get(&id) {
Some(pos) => {
let offset = bars.len() as u16 - pos;
write!(
stdout,
"{}{}{} - {:12} {}{}",
cursor::Save,
cursor::Up(offset),
id,
status,
progress,
cursor::Restore
)
.into_diagnostic()?;
}
None => {
writeln!(stdout, "{} - {:12} {}", id, status, progress).into_diagnostic()?;
bars.insert(id, bars.len() as u16);
}
stdout.flush().into_diagnostic()?;
}
stdout.flush().into_diagnostic()?;
}
}
Ok(())