Skip to content

Commit

Permalink
Support for copying from other named image layers
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjarobot committed Jul 29, 2021
1 parent 26410ef commit 5981f0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Dockerfile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module Dockerfile =
type BuildStage =
| Name of string
| Index of int
member this.AsString =
match this with
| Name n -> n
| Index i -> string i

type Signal =
| Sigquit
Expand Down Expand Up @@ -133,19 +137,32 @@ module Dockerfile =
|> sprintf "ADD %s"
else
sprintf "ADD %s %s" (sources |> String.concat " ") dest
| Copy (SingleSource (source), dest, from) ->
| Copy (SingleSource (source), dest, None) ->
if source |> stringContainsWhitepace || dest |> stringContainsWhitepace then
[source; dest] |> stringsToQuotedArray
|> sprintf "COPY %s"
else
sprintf "COPY %s %s" source dest
| Copy (MultipleSources (sources), dest, from) ->
| Copy (SingleSource (source), dest, Some (from)) ->
if source |> stringContainsWhitepace || dest |> stringContainsWhitepace then
[source; dest] |> stringsToQuotedArray
|> sprintf "COPY --from=%s %s" from.AsString
else
sprintf "COPY --from=%s %s %s" from.AsString source dest
| Copy (MultipleSources (sources), dest, None) ->
if sources |> List.exists(stringContainsWhitepace) || dest |> stringContainsWhitepace then
sources@[dest] |> stringsToQuotedArray
|> sprintf "COPY %s"
else
sources@[dest] |> String.concat " "
|> sprintf "COPY %s"
| Copy (MultipleSources (sources), dest, Some(from)) ->
if sources |> List.exists(stringContainsWhitepace) || dest |> stringContainsWhitepace then
sources@[dest] |> stringsToQuotedArray
|> sprintf "COPY --from=%s %s" from.AsString
else
sources@[dest] |> String.concat " "
|> sprintf "COPY --from=%s %s" from.AsString
| Entrypoint (Exec (executable, args)) ->
sprintf "ENTRYPOINT %s" (executable::args |> stringsToQuotedArray)
| Entrypoint (ShellCommand (command)) ->
Expand Down
5 changes: 5 additions & 0 deletions tests/DockerfileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ let ``COPY multiple sources with whitespace`` () =
let instruction = Copy (MultipleSources (["path to/source"; "some/other source"]), "/dest/in/image", None) |> printInstruction
Assert.Equal ("""COPY ["path to/source", "some/other source", "/dest/in/image"]""", instruction)

[<Fact>]
let ``COPY from named layer`` () =
let instruction = Copy (SingleSource ("path/to/source"), "/dest/in/image", Some (BuildStage.Name "builder")) |> printInstruction
Assert.Equal ("COPY --from=builder path/to/source /dest/in/image", instruction)

[<Fact>]
let ``ENTRYPOINT exec no args`` () =
let instruction = Entrypoint (Exec ("/bin/bash", [])) |> printInstruction
Expand Down

0 comments on commit 5981f0a

Please sign in to comment.