diff --git a/lib/ex_docker_build/dockerfile_parser.ex b/lib/ex_docker_build/dockerfile_parser.ex index 62273cb..404998b 100644 --- a/lib/ex_docker_build/dockerfile_parser.ex +++ b/lib/ex_docker_build/dockerfile_parser.ex @@ -3,10 +3,17 @@ defmodule ExDockerBuild.DockerfileParser do @continuation ~r/^.*\\\s*$/ @instruction ~r/^\s*(\w+)\s+(.*)$/ - @spec parse!(Path.t()) :: list(String.t()) | no_return() - def parse!(path) do + @spec parse!(Path.t() | String.t()) :: list(String.t()) | no_return() + def parse!(path_or_content) do + content = + if File.exists?(path_or_content) do + File.read!(path_or_content) + else + path_or_content + end + {parsed_lines, _} = - File.read!(path) + content |> String.split("\n") |> Enum.reduce({[], false}, fn line, {acc, continuation?} -> case parse_line(line, continuation?) do diff --git a/test/dockerfile_parser_test.exs b/test/dockerfile_parser_test.exs index 9107b4e..393fca5 100644 --- a/test/dockerfile_parser_test.exs +++ b/test/dockerfile_parser_test.exs @@ -58,4 +58,22 @@ defmodule ExDockerBuild.DockerfileParserTest do "set -xe && REBAR3_DOWNLOAD_URL=\"https://github.com/erlang/rebar3/archive/${REBAR3_VERSION}.tar.gz\" && REBAR3_DOWNLOAD_SHA256=\"40b3c85440f3235c7b149578d0211bdf57d1c66390f888bb771704f8abc71033\" && mkdir -p /usr/src/rebar3-src && curl -fSL -o rebar3-src.tar.gz \"$REBAR3_DOWNLOAD_URL\" && echo \"$REBAR3_DOWNLOAD_SHA256 rebar3-src.tar.gz\" | sha256sum -c - && tar -xzf rebar3-src.tar.gz -C /usr/src/rebar3-src --strip-components=1 && rm rebar3-src.tar.gz && cd /usr/src/rebar3-src && HOME=$PWD ./bootstrap && install -v ./rebar3 /usr/local/bin/ && rm -rf /usr/src/rebar3-src"} ] end + + test "parses file content instead of a file" do + content = """ + FROM elixir:1.7.3 + VOLUME /Users/kiro/test:/data + RUN echo "hello-world!!!!" > /data/myfile.txt + CMD ["cat", "/data/myfile.txt"] + """ + + result = Parser.parse!(content) + + assert result == [ + {"FROM", "elixir:1.7.3"}, + {"VOLUME", "/Users/kiro/test:/data"}, + {"RUN", "echo \"hello-world!!!!\" > /data/myfile.txt"}, + {"CMD", "[\"cat\", \"/data/myfile.txt\"]"} + ] + end end