From 318d04551f67f0e91126bbd7fe7172cfa2fb5452 Mon Sep 17 00:00:00 2001 From: Julian Einwag Date: Thu, 12 Aug 2021 15:04:05 +0200 Subject: [PATCH] add INCLUDE_OPTIONAL command --- README.md | 3 +++ dockerfile-plus/Dockerfile | 3 ++- dockerfile-plus/src/main.rs | 13 +++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a9860f0..0d6d8dc 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ RUN echo "Hello World" ENTRYPOINT [ "mybin" ] ``` +### INCLUDE_OPTIONAL+ + +The same as `INCLUDE+`, but the build won't fail if the referenced file does not exist. ## Roadmap The next features in line would be: diff --git a/dockerfile-plus/Dockerfile b/dockerfile-plus/Dockerfile index d0daf65..2ed0033 100644 --- a/dockerfile-plus/Dockerfile +++ b/dockerfile-plus/Dockerfile @@ -4,12 +4,13 @@ FROM rust:latest as builder USER root WORKDIR /rust-src -COPY . /rust-src RUN apt update && apt upgrade -y && apt install -y gcc-x86-64-linux-gnu gcc-aarch64-linux-gnu RUN rustup target add "$(uname -m)-unknown-linux-musl" +COPY . /rust-src + RUN --mount=type=cache,target=/rust-src/target \ --mount=type=cache,target=/root/.cargo/git \ --mount=type=cache,target=/root/.cargo/registry \ diff --git a/dockerfile-plus/src/main.rs b/dockerfile-plus/src/main.rs index 2be5a22..ec57296 100644 --- a/dockerfile-plus/src/main.rs +++ b/dockerfile-plus/src/main.rs @@ -106,6 +106,7 @@ struct DockerfileOptions { } const INCLUDE_COMMAND: &str = "INCLUDE+"; +const INCLUDE_OPTIONAL_COMMAND: &str = "INCLUDE_OPTIONAL+"; async fn dockerfile_trap( mut client: LlbBridgeClient, @@ -127,7 +128,15 @@ async fn dockerfile_trap( .with_context(|| format!("Could not read file \"{}\". Remember that the file path is relative to the build context, not the Dockerfile path.", file_path))?; //recurse for l2 in std::str::from_utf8(&bytes)?.to_string().lines() { - replace(&l2.to_string(), r, c, &ctx)? ; + replace(&l2.to_string(), r, c, &ctx)?; + } + } else if let Some(file_path) = l.trim().strip_prefix(INCLUDE_OPTIONAL_COMMAND) { + if let Ok(bytes) = + executor::block_on(read_file(c, &ctx, file_path.trim_start().to_string(), None)) { + //recurse + for l2 in std::str::from_utf8(&bytes)?.to_string().lines() { + replace(&l2.to_string(), r, c, &ctx)?; + } } } else { r.push(l.to_string()); @@ -136,7 +145,7 @@ async fn dockerfile_trap( } for line in dockerfile_contents.lines() { - replace(&line.to_string(), &mut result, &mut client, &context_layer)? ; + replace(&line.to_string(), &mut result, &mut client, &context_layer)?; } let dockerfile_contents = result.join("\n"); dockerfile_frontend.solve(&dockerfile_contents).await