Skip to content

Commit

Permalink
feat: support docker_image
Browse files Browse the repository at this point in the history
  • Loading branch information
bxb100 committed Nov 24, 2024
1 parent df23edb commit 979d792
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/languages/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Docker {
Ok(Cow::Borrowed(path))
}

async fn docker_cmd() -> Result<Cmd> {
pub(crate) async fn docker_cmd() -> Result<Cmd> {
let mut command = Cmd::new("docker", "run container");
command.arg("run").arg("--rm");

Expand Down Expand Up @@ -212,6 +212,7 @@ impl LanguageImpl for Docker {
.args(&cmds[1..])
.args(hook_args.as_ref())
.args(batch)
.check(false)
.stderr(std::process::Stdio::inherit())
.envs(env_vars.as_ref());

Expand Down
77 changes: 77 additions & 0 deletions src/languages/docker_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::hook::Hook;
use crate::languages::docker::Docker;
use crate::languages::{LanguageImpl, DEFAULT_VERSION};
use crate::run::run_by_batch;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::debug;

#[derive(Debug, Copy, Clone)]
pub struct DockerImage;

impl LanguageImpl for DockerImage {
fn default_version(&self) -> &str {
DEFAULT_VERSION
}

fn environment_dir(&self) -> Option<&str> {
None
}

async fn install(&self, _: &Hook) -> anyhow::Result<()> {
Ok(())
}

async fn check_health(&self) -> anyhow::Result<()> {
todo!()
}

async fn run(
&self,
hook: &Hook,
filenames: &[&String],
env_vars: Arc<HashMap<&'static str, String>>,
) -> anyhow::Result<(i32, Vec<u8>)> {
let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?;

let cmds = Arc::new(cmds);
let hook_args = Arc::new(hook.args.clone());

let run = move |batch: Vec<String>| {
let cmds = cmds.clone();
let hook_args = hook_args.clone();
let env_vars = env_vars.clone();

async move {
let mut cmd = Docker::docker_cmd().await?;
let cmd = cmd
.args(&cmds[..])
.args(hook_args.as_ref())
.args(batch)
.stderr(std::process::Stdio::inherit())
.check(false)
.envs(env_vars.as_ref());

debug!(%cmd, "docker");

let mut output = cmd.output().await?;
output.stdout.extend(output.stderr);
let code = output.status.code().unwrap_or(1);
anyhow::Ok((code, output.stdout))
}
};

let results = run_by_batch(hook, filenames, run).await?;

// Collect results
let mut combined_status = 0;
let mut combined_output = Vec::new();

for (code, output) in results {
combined_status |= code;
combined_output.extend(output);
}

Ok((combined_status, combined_output))
}
}
7 changes: 7 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::config::Language;
use crate::hook::Hook;

mod docker;
mod docker_image;
mod fail;
mod node;
mod python;
Expand All @@ -17,6 +18,7 @@ static NODE: node::Node = node::Node;
static SYSTEM: system::System = system::System;
static FAIL: fail::Fail = fail::Fail;
static DOCKER: docker::Docker = docker::Docker;
static DOCKER_IMAGE: docker_image::DockerImage = docker_image::DockerImage;

pub const DEFAULT_VERSION: &str = "default";

Expand All @@ -41,6 +43,7 @@ impl Language {
Self::System => SYSTEM.default_version(),
Self::Fail => FAIL.default_version(),
Self::Docker => DOCKER.default_version(),
Self::DockerImage => DOCKER_IMAGE.default_version(),
_ => todo!(),
}
}
Expand All @@ -52,6 +55,7 @@ impl Language {
Self::System => SYSTEM.environment_dir(),
Self::Fail => FAIL.environment_dir(),
Self::Docker => DOCKER.environment_dir(),
Self::DockerImage => DOCKER_IMAGE.environment_dir(),
_ => todo!(),
}
}
Expand All @@ -63,6 +67,7 @@ impl Language {
Self::System => SYSTEM.install(hook).await,
Self::Fail => FAIL.install(hook).await,
Self::Docker => DOCKER.install(hook).await,
Self::DockerImage => DOCKER_IMAGE.install(hook).await,
_ => todo!(),
}
}
Expand All @@ -74,6 +79,7 @@ impl Language {
Self::System => SYSTEM.check_health().await,
Self::Fail => FAIL.check_health().await,
Self::Docker => DOCKER.check_health().await,
Self::DockerImage => DOCKER_IMAGE.check_health().await,
_ => todo!(),
}
}
Expand All @@ -90,6 +96,7 @@ impl Language {
Self::System => SYSTEM.run(hook, filenames, env_vars).await,
Self::Fail => FAIL.run(hook, filenames, env_vars).await,
Self::Docker => DOCKER.run(hook, filenames, env_vars).await,
Self::DockerImage => DOCKER_IMAGE.run(hook, filenames, env_vars).await,
_ => todo!(),
}
}
Expand Down

0 comments on commit 979d792

Please sign in to comment.