1use std::env::set_current_dir;
2use anyhow::anyhow;
3use cnctd_shell::Shell;
4
5pub struct DockerImage;
6
7impl DockerImage {
8 pub async fn publish(account: &str, image: &str, dockerfile_path: &str, project_dir: &str) -> anyhow::Result<()> {
9 set_current_dir(project_dir)?;
10
11 println!("building image");
12
13 Shell::run(&format!("docker build --platform=linux/amd64 --tag {} --file {}/Dockerfile .", image, dockerfile_path), true).await?;
14
15 Shell::run(&format!("docker tag {} {}/{}:latest", image, account, image), true).await?;
16
17 Shell::run(&format!("docker push {}/{}:latest", account, image), true).await?;
18
19 Shell::run(&format!("docker rmi {}", image), true).await?;
20
21 Shell::run(&format!("docker image prune -f --filter dangling=true"), true).await?;
22
23 Shell::run(&format!("docker system prune -f --filter until=60m --filter label=maintainer={}/{}", account, image), true).await?;
24
25 Ok(())
26 }
27}
28
29pub async fn check_for_docker() -> Result<(), anyhow::Error> {
30 match Shell::run_with_exit_status("docker -v", false).await? {
31 0 => Ok(()),
32 _ => Err(anyhow!("Docker not installed"))
33 }
34}