Dockerのビルドのレスポンスを整形して出力する

F#でDockerイメージをビルドする - locabloではレスポンスをそのまま出力していたのでパースして出力できるようにしました。

F#っぽいコードにならなかったのが残念...

let formatResponse line =
    let root = JObject.Parse(line)
    let hasProp key =
        root.Properties()
        |> Seq.map (fun x -> x.Name)
        |> Seq.contains key
    let valueOf (key : string) = string root.[key]

    let newLine = System.Environment.NewLine
    let builder = new System.Text.StringBuilder()
    if hasProp "id" then
        builder.AppendFormat("{0}: ", valueOf "id") |> ignore
    if hasProp "progress" then
        builder.AppendFormat("{0} {1}{2}", valueOf "status", valueOf "progress", newLine) |> ignore
    elif hasProp "stream" then
        builder.AppendFormat("{0}", valueOf "stream") |> ignore
    elif hasProp "status" then
        builder.AppendFormat("{0}{1}", valueOf "status", newLine) |> ignore
    else
        builder.Append(line) |> ignore

    builder.ToString()

let outputBuildResponse (responseStream : Stream) : unit =
    seq {
        use reader = new StreamReader(responseStream, Text.Encoding.UTF8)
        while not reader.EndOfStream do
            yield reader.ReadLine()
    }
    |> Seq.map formatResponse
    |> Seq.iter (printf "%s")

前回の記事の出力がこれで、

{"stream":"Step 1/2 : FROM busybox\n"}
{"status":"Pulling from library/busybox","id":"latest"}
{"status":"Pulling fs layer","progressDetail":{},"id":"1cae461a1479"}
{"status":"Downloading","progressDetail":{"current":16384,"total":699311},"progress":"[=\u003e                                                 ] 16.38 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Downloading","progressDetail":{"current":260087,"total":699311},"progress":"[==================\u003e                                ] 260.1 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Downloading","progressDetail":{"current":424839,"total":699311},"progress":"[==============================\u003e                    ] 424.8 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Downloading","progressDetail":{"current":490375,"total":699311},"progress":"[===================================\u003e               ] 490.4 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Downloading","progressDetail":{"current":539527,"total":699311},"progress":"[======================================\u003e            ] 539.5 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Downloading","progressDetail":{"current":621447,"total":699311},"progress":"[============================================\u003e      ] 621.4 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Downloading","progressDetail":{"current":699311,"total":699311},"progress":"[==================================================\u003e] 699.3 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Verifying Checksum","progressDetail":{},"id":"1cae461a1479"}
{"status":"Download complete","progressDetail":{},"id":"1cae461a1479"}
{"status":"Extracting","progressDetail":{"current":32768,"total":699311},"progress":"[==\u003e                                                ] 32.77 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Extracting","progressDetail":{"current":699311,"total":699311},"progress":"[==================================================\u003e] 699.3 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Extracting","progressDetail":{"current":699311,"total":699311},"progress":"[==================================================\u003e] 699.3 kB/699.3 kB","id":"1cae461a1479"}
{"status":"Pull complete","progressDetail":{},"id":"1cae461a1479"}
{"status":"Digest: sha256:c79345819a6882c31b41bc771d9a94fc52872fa651b36771fbe0c8461d7ee558"}
{"status":"Status: Downloaded newer image for busybox:latest"}
{"stream":" ---\u003e c75bebcdd211\n"}
{"stream":"Step 2/2 : RUN ls\n"}
{"stream":" ---\u003e Running in 74350655bd58\n"}
{"stream":"bin\ndev\netc\nhome\nproc\nroot\nsys\ntmp\nusr\nvar\n"}
{"stream":" ---\u003e 7fc898365619\n"}
{"stream":"Removing intermediate container 74350655bd58\n"}
{"stream":"Successfully built 7fc898365619\n"}

formatResponse関数を使ってパースして出力するとこうなります。

Step 1/2 : FROM busybox
latest: Pulling from library/busybox
1cae461a1479: Pulling fs layer
1cae461a1479: Downloading [=>                                                 ] 15.93 kB/699.3 kB
1cae461a1479: Downloading [===============>                                   ] 211.8 kB/699.3 kB
1cae461a1479: Downloading [=======================>                           ] 326.5 kB/699.3 kB
1cae461a1479: Downloading [=========================>                         ] 359.3 kB/699.3 kB
1cae461a1479: Downloading [============================>                      ] 392.1 kB/699.3 kB
1cae461a1479: Downloading [==============================>                    ] 424.8 kB/699.3 kB
1cae461a1479: Downloading [=================================>                 ]   474 kB/699.3 kB
1cae461a1479: Downloading [=====================================>             ] 523.1 kB/699.3 kB
1cae461a1479: Downloading [========================================>          ] 572.3 kB/699.3 kB
1cae461a1479: Downloading [============================================>      ] 621.4 kB/699.3 kB
1cae461a1479: Downloading [==============================================>    ] 654.2 kB/699.3 kB
1cae461a1479: Downloading [=================================================> ]   687 kB/699.3 kB
1cae461a1479: Downloading [==================================================>] 699.3 kB/699.3 kB
1cae461a1479: Verifying Checksum
1cae461a1479: Download complete
1cae461a1479: Extracting [==>                                                ] 32.77 kB/699.3 kB
1cae461a1479: Extracting [=======================================>           ] 557.1 kB/699.3 kB
1cae461a1479: Extracting [==================================================>] 699.3 kB/699.3 kB
1cae461a1479: Extracting [==================================================>] 699.3 kB/699.3 kB
1cae461a1479: Pull complete
Digest: sha256:c79345819a6882c31b41bc771d9a94fc52872fa651b36771fbe0c8461d7ee558
Status: Downloaded newer image for busybox:latest
 ---> c75bebcdd211
Step 2/2 : RUN ls
 ---> Running in 5c8584208f21
bin
dev
etc
home
proc
root
sys
tmp
usr
var
 ---> b95a8e54a8f1
Removing intermediate container 5c8584208f21
Successfully built b95a8e54a8f1