Skip to main content

systemprompt_cli/shared/
docker.rs

1use anyhow::{Context, Result, anyhow};
2use std::io::Write;
3use std::path::Path;
4use std::process::Command;
5
6use super::process::run_command_in_dir;
7
8pub fn build_docker_image(context_dir: &Path, dockerfile: &Path, image: &str) -> Result<()> {
9    run_command_in_dir(
10        "docker",
11        &[
12            "build",
13            "--no-cache",
14            "-f",
15            &dockerfile.to_string_lossy(),
16            "-t",
17            image,
18            ".",
19        ],
20        &context_dir.to_path_buf(),
21    )
22}
23
24pub fn docker_login(registry: &str, username: &str, token: &str) -> Result<()> {
25    let mut command = Command::new("docker");
26    command.args(["login", registry, "-u", username, "--password-stdin"]);
27    command.stdin(std::process::Stdio::piped());
28
29    let mut child = command
30        .spawn()
31        .with_context(|| format!("failed to spawn `docker login {registry}`"))?;
32    if let Some(mut stdin) = child.stdin.take() {
33        stdin.write_all(token.as_bytes())?;
34    }
35
36    let status = child
37        .wait()
38        .with_context(|| format!("failed waiting on `docker login {registry}`"))?;
39    if !status.success() {
40        return Err(anyhow!("Docker login failed"));
41    }
42
43    Ok(())
44}
45
46pub fn docker_push(image: &str) -> Result<()> {
47    let status = Command::new("docker")
48        .args(["push", image])
49        .status()
50        .with_context(|| format!("failed to spawn `docker push {image}`"))?;
51
52    if !status.success() {
53        return Err(anyhow!("Docker push failed for image: {}", image));
54    }
55
56    Ok(())
57}