Skip to content

Commit b7652b3

Browse files
feat(logging): show image index and total to indicate build progress
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent c52f375 commit b7652b3

File tree

6 files changed

+66
-21
lines changed

6 files changed

+66
-21
lines changed

pkg/build/build_phase.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ func (phase *BuildPhase) AfterImages(ctx context.Context) error {
265265
}
266266
}
267267
} else {
268-
img := image.NewMultiplatformImage(name, images)
268+
img := image.NewMultiplatformImage(name, images, taskId, len(images))
269269
phase.Conveyor.imagesTree.SetMultiplatformImage(img)
270270

271271
// TODO: Separate LocalStagesStorage and RepoStagesStorage interfaces, local should not include metadata publishing methods at all
272272
if _, isLocal := phase.Conveyor.StorageManager.GetStagesStorage().(*storage.LocalStagesStorage); !isLocal {
273-
if err := logboek.Context(ctx).LogProcess(logging.ImageLogProcessName(name, img.IsFinal, "")).
273+
if err := logboek.Context(ctx).LogProcess(img.LogDetailedName()).
274274
Options(func(options types.LogProcessOptionsInterface) {
275275
options.Style(logging.ImageMetadataStyle())
276276
}).

pkg/build/image/image.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ type Image struct {
115115

116116
baseStageImage *stage.StageImage
117117
stageAsBaseImage stage.Interface
118+
119+
logImageIndex int
120+
logTotalImages int
118121
}
119122

120123
func (i *Image) LogName() string {
@@ -130,7 +133,7 @@ func (i *Image) LogDetailedName() string {
130133
if i.ShouldLogPlatform() {
131134
targetPlatformForLog = i.TargetPlatform
132135
}
133-
return logging.ImageLogProcessName(i.Name, i.IsFinal, targetPlatformForLog)
136+
return logging.ImageLogProcessName(i.Name, i.IsFinal, targetPlatformForLog, logging.WithProgress(i.logImageIndex+1, i.logTotalImages))
134137
}
135138

136139
func (i *Image) LogProcessStyle() color.Style {

pkg/build/image/image_tree.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ func (tree *ImagesTree) Calculate(ctx context.Context) error {
126126
tree.imagesSets = builder.GetImagesSets()
127127
tree.images = builder.GetImages()
128128

129+
for ind, img := range tree.images {
130+
img.logImageIndex = ind
131+
img.logTotalImages = len(tree.images)
132+
}
133+
129134
return nil
130135
}
131136

pkg/build/image/multiplatform_image.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/werf/common-go/pkg/util"
55
"github.com/werf/werf/v2/pkg/image"
66
common_image "github.com/werf/werf/v2/pkg/image"
7+
"github.com/werf/werf/v2/pkg/logging"
78
)
89

910
type MultiplatformImage struct {
@@ -15,17 +16,22 @@ type MultiplatformImage struct {
1516
stageID common_image.StageID
1617
stageDesc *common_image.StageDesc
1718
finalStageDesc *common_image.StageDesc
19+
20+
logImageIndex int
21+
logImageTotal int
1822
}
1923

20-
func NewMultiplatformImage(name string, images []*Image) *MultiplatformImage {
24+
func NewMultiplatformImage(name string, images []*Image, logImageIndex, logImageTotal int) *MultiplatformImage {
2125
if len(images) == 0 {
2226
panic("expected at least one image")
2327
}
2428

2529
img := &MultiplatformImage{
26-
Name: name,
27-
IsFinal: images[0].IsFinal,
28-
Images: images,
30+
Name: name,
31+
IsFinal: images[0].IsFinal,
32+
Images: images,
33+
logImageIndex: logImageIndex,
34+
logImageTotal: logImageTotal,
2935
}
3036

3137
metaStageDeps := util.MapFuncToSlice(images, func(img *Image) string {
@@ -72,3 +78,7 @@ func (img *MultiplatformImage) GetStageDesc() *image.StageDesc {
7278
func (img *MultiplatformImage) SetStageDesc(stageDesc *common_image.StageDesc) {
7379
img.stageDesc = stageDesc
7480
}
81+
82+
func (img *MultiplatformImage) LogDetailedName() string {
83+
return logging.ImageLogProcessName(img.Name, img.IsFinal, "", logging.WithProgress(img.logImageIndex+1, img.logImageTotal))
84+
}

pkg/cleaning/cleanup.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@ func (m *cleanupManager) gitHistoryBasedCleanup(ctx context.Context) error {
396396
var reachedStageIDs []string
397397
var hitStageIDCommitList map[string][]string
398398
// TODO(multiarch): iterate target platforms
399-
if err := logboek.Context(ctx).LogProcess(logging.ImageLogProcessName(imageName, false, "")).DoError(func() error {
399+
400+
header := logging.ImageLogProcessName(imageName, false, "", logging.WithProgress(taskId+1, len(imagePairs)))
401+
if err := logboek.Context(ctx).LogProcess(header).DoError(func() error {
400402
if logboek.Context(ctx).Streams().Width() > 120 {
401403
m.printStageIDCommitListTable(ctx, imageName)
402404
m.printStageIDCustomTagListTable(ctx)

pkg/logging/image.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
var (
10-
finalImageNameFormat = "🛳️ image %s"
11-
intermediateImageNameFormat = "🏗️️ image %s"
10+
finalImagePrettyPrefix = "🛳️ "
11+
intermediateImagePrettyPrefix = "🏗️️ "
1212
)
1313

1414
func ImageLogName(name string) string {
@@ -18,26 +18,51 @@ func ImageLogName(name string) string {
1818
return name
1919
}
2020

21-
func ImageLogProcessName(name string, isFinal bool, targetPlatform string) string {
22-
appendPlatformFunc := func(name string) string {
23-
if targetPlatform == "" {
24-
return name
25-
}
26-
return fmt.Sprintf("%s [%s]", name, targetPlatform)
21+
func WithProgress(index, total int) Option {
22+
return func(o *Options) {
23+
o.withProgress = true
24+
o.index = index
25+
o.total = total
26+
}
27+
}
28+
29+
type Options struct {
30+
withProgress bool
31+
index int
32+
total int
33+
}
34+
35+
type Option func(*Options)
36+
37+
func ImageLogProcessName(name string, isFinal bool, targetPlatform string, opts ...Option) string {
38+
options := &Options{}
39+
for _, opt := range opts {
40+
opt(options)
2741
}
2842

29-
logName := ImageLogName(name)
43+
var res string
44+
res += "image" + " " + ImageLogName(name)
45+
46+
if targetPlatform != "" {
47+
res += " [" + targetPlatform + "]"
48+
}
49+
50+
if options.withProgress {
51+
res = fmt.Sprintf("(%d/%d)", options.index, options.total) + " " + res
52+
}
3053

3154
if isFinal {
32-
return appendPlatformFunc(fmt.Sprintf(finalImageNameFormat, logName))
55+
res = finalImagePrettyPrefix + res
3356
} else {
34-
return appendPlatformFunc(fmt.Sprintf(intermediateImageNameFormat, logName))
57+
res = intermediateImagePrettyPrefix + res
3558
}
59+
60+
return res
3661
}
3762

3863
func DisablePrettyLog() {
39-
finalImageNameFormat = "image %s"
40-
intermediateImageNameFormat = "image %s"
64+
finalImagePrettyPrefix = ""
65+
intermediateImagePrettyPrefix = ""
4166
}
4267

4368
func ImageDefaultStyle(isFinal bool) color.Style {

0 commit comments

Comments
 (0)