From 638064f74a7c909daff3b4a7739a527817c94638 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Fri, 24 Nov 2023 00:43:55 +0100 Subject: [PATCH] Collapse multiple spaces in layer labels (#430) --- examples/images/Dockerfile-large.svg | 94 ++++++++++++------------- examples/images/Dockerfile-layers.svg | 44 ++++++------ examples/images/Dockerfile-legend.svg | 30 ++++---- internal/cmd/root_test.go | 4 +- internal/dockerfile2dot/convert.go | 17 +++-- internal/dockerfile2dot/convert_test.go | 2 +- 6 files changed, 100 insertions(+), 91 deletions(-) diff --git a/examples/images/Dockerfile-large.svg b/examples/images/Dockerfile-large.svg index 373c044..61be8a7 100644 --- a/examples/images/Dockerfile-large.svg +++ b/examples/images/Dockerfile-large.svg @@ -1,7 +1,7 @@ - external_image_0->stage_0 - - + + @@ -36,8 +36,8 @@ external_image_0->stage_1 - - + + @@ -49,8 +49,8 @@ external_image_0->stage_2 - - + + @@ -61,20 +61,20 @@ external_image_0->stage_5 - - + + stage_0->stage_2 - - + + stage_1->stage_2 - - + + @@ -85,8 +85,8 @@ stage_2->stage_4 - - + + @@ -97,8 +97,8 @@ stage_5->stage_6 - - + + @@ -109,20 +109,20 @@ stage_5->stage_7 - - + + stage_4->stage_6 - - + + stage_4->stage_7 - - + + @@ -139,14 +139,14 @@ external_image_1->stage_3 - - + + stage_3->stage_4 - - + + @@ -157,8 +157,8 @@ external_image_2->stage_4 - - + + @@ -191,8 +191,8 @@ external_image_5->stage_4 - - + + @@ -205,8 +205,8 @@ external_image_6->stage_4 - - + + @@ -241,8 +241,8 @@ external_image_9->stage_4 - - + + @@ -253,8 +253,8 @@ external_image_10->stage_4 - - + + @@ -288,8 +288,8 @@ external_image_13->stage_4 - - + + @@ -301,8 +301,8 @@ external_image_14->stage_4 - - + + @@ -336,8 +336,8 @@ external_image_17->stage_4 - - + + @@ -349,8 +349,8 @@ external_image_18->stage_4 - - + + @@ -385,8 +385,8 @@ external_image_21->stage_4 - - + + diff --git a/examples/images/Dockerfile-layers.svg b/examples/images/Dockerfile-layers.svg index 26c2538..7ee0001 100644 --- a/examples/images/Dockerfile-layers.svg +++ b/examples/images/Dockerfile-layers.svg @@ -1,7 +1,7 @@ - stage_0_layer_1 -RUN   apt-get upd... +RUN apt-get updat... stage_0_layer_0->stage_0_layer_1 - - + + @@ -51,8 +51,8 @@ stage_0_layer_1->stage_2_layer_1 - - + + @@ -63,8 +63,8 @@ external_image_0->stage_0_layer_0 - - + + @@ -81,8 +81,8 @@ stage_1_layer_0->stage_1_layer_1 - - + + @@ -93,8 +93,8 @@ stage_1_layer_1->stage_2_layer_2 - - + + @@ -105,8 +105,8 @@ external_image_1->stage_1_layer_0 - - + + @@ -129,14 +129,14 @@ stage_2_layer_0->stage_2_layer_1 - - + + stage_2_layer_1->stage_2_layer_2 - - + + @@ -147,8 +147,8 @@ stage_2_layer_2->stage_2_layer_3 - - + + @@ -159,8 +159,8 @@ external_image_3->stage_2_layer_0 - - + + diff --git a/examples/images/Dockerfile-legend.svg b/examples/images/Dockerfile-legend.svg index 4f48e3d..b60944a 100644 --- a/examples/images/Dockerfile-legend.svg +++ b/examples/images/Dockerfile-legend.svg @@ -1,7 +1,7 @@ - key:e->key2:w - - + + key:e->key2:w - - + + @@ -60,8 +60,8 @@ external_image_0->stage_0 - - + + @@ -72,8 +72,8 @@ stage_0->stage_2 - - + + @@ -90,14 +90,14 @@ external_image_1->stage_1 - - + + stage_1->stage_2 - - + + @@ -120,8 +120,8 @@ external_image_3->stage_2 - - + + diff --git a/internal/cmd/root_test.go b/internal/cmd/root_test.go index aa6d00e..a09c8a2 100644 --- a/internal/cmd/root_test.go +++ b/internal/cmd/root_test.go @@ -176,7 +176,7 @@ It creates a visual graph representation of the build process. label=ubuntu; margin=16; stage_0_layer_0 [ fillcolor=white, label="FROM ubuntu:lates...", penwidth=0.5, shape=box, style="filled,rounded", width=2 ]; - stage_0_layer_1 [ fillcolor=white, label="RUN apt-get upd...", penwidth=0.5, shape=box, style="filled,rounded", width=2 ]; + stage_0_layer_1 [ fillcolor=white, label="RUN apt-get updat...", penwidth=0.5, shape=box, style="filled,rounded", width=2 ]; } ; @@ -231,7 +231,7 @@ It creates a visual graph representation of the build process. style="filled,rounded", width=2]; stage_0_layer_1 [fillcolor=white, - label="RUN apt-get upd...", + label="RUN apt-get updat...", penwidth=0.5, shape=box, style="filled,rounded", diff --git a/internal/dockerfile2dot/convert.go b/internal/dockerfile2dot/convert.go index a482fa5..85c8233 100644 --- a/internal/dockerfile2dot/convert.go +++ b/internal/dockerfile2dot/convert.go @@ -9,18 +9,27 @@ import ( "github.com/moby/buildkit/frontend/dockerfile/parser" ) +// newLayer creates a new layer object with a modified label. func newLayer( - child *parser.Node, - replacements map[string]string, - maxLabelLength int, + child *parser.Node, argReplacements map[string]string, maxLabelLength int, ) (layer Layer) { - label := replaceArgVars(child.Original, replacements) + // Replace argument variables in the original label. + label := replaceArgVars(child.Original, argReplacements) + + // Replace double quotes with single quotes. label = strings.Replace(label, "\"", "'", -1) + + // Collapse multiple spaces into a single space. + label = strings.Join(strings.Fields(label), " ") + + // Truncate the label if it exceeds the maximum length. if len(label) > maxLabelLength { label = truncate.Truncate( label, maxLabelLength, "...", truncate.PositionEnd, ) } + + // Set the label of the layer object. layer.Label = label return diff --git a/internal/dockerfile2dot/convert_test.go b/internal/dockerfile2dot/convert_test.go index cd45b11..193cac9 100644 --- a/internal/dockerfile2dot/convert_test.go +++ b/internal/dockerfile2dot/convert_test.go @@ -113,7 +113,7 @@ RUN \ WaitFors: []WaitFor{{Name: "ubuntu", Type: waitForType(waitForFrom)}}, }, { - Label: "RUN --mount=typ...", + Label: "RUN --mount=type=...", WaitFors: []WaitFor{ {Name: "buildcache", Type: waitForType(waitForMount)}, {Name: "artifacts", Type: waitForType(waitForMount)},