Skip to content

Commit 6abab2d

Browse files
author
SurbhiAgarwal1
committed
refactor(printer): unify stateful context-aware printer (#4698)
- Consolidate CLI and Porch klog printer logic into an exported Printer interface in pkg/printer. - Introduce stateful contextual scoping via WithField, WithFields, WithPackage, and WithFunction. - Provide structured lifecycle event printing (PrintRunning, PrintPass, PrintFail, PrintResult, PrintSummary). - Remove duplicate packagePrinter implementation in pkg/lib/kptops/render.go. - Add comprehensive unit tests verifying stateful field scoping and formatting. Signed-off-by: SurbhiAgarwal1 <surbhi.agarwal@example.com>
1 parent 263b5df commit 6abab2d

5 files changed

Lines changed: 344 additions & 96 deletions

File tree

pkg/lib/kptops/render.go

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@ package kptops
1616

1717
import (
1818
"context"
19-
"fmt"
20-
"io"
21-
"os"
2219

2320
fnresultv1 "github.com/kptdev/kpt/api/fnresult/v1"
2421
"github.com/kptdev/kpt/pkg/fn"
25-
"github.com/kptdev/kpt/pkg/lib/pkg"
2622
"github.com/kptdev/kpt/pkg/lib/runneroptions"
2723
"github.com/kptdev/kpt/pkg/printer"
28-
"k8s.io/klog/v2"
2924
"sigs.k8s.io/kustomize/kyaml/filesys"
3025
)
3126

@@ -51,51 +46,5 @@ func (r *renderer) Render(ctx context.Context, pkg filesys.FileSystem, opts fn.R
5146
FileSystem: pkg,
5247
RunnerOptions: r.runnerOptions,
5348
}
54-
return rr.Execute(printer.WithContext(ctx, &packagePrinter{}))
55-
}
56-
57-
type packagePrinter struct{}
58-
59-
var _ printer.Printer = &packagePrinter{}
60-
61-
const (
62-
packagePrefixFormat = "Package %q:"
63-
logDepth = 2
64-
)
65-
66-
func (p *packagePrinter) PrintPackage(pkg *pkg.Pkg, _ bool) {
67-
p.printfDepth(logDepth, packagePrefixFormat, pkg.DisplayPath)
68-
}
69-
70-
func (p *packagePrinter) Printf(format string, args ...any) {
71-
p.printfDepth(logDepth, format, args...)
72-
}
73-
74-
func (p *packagePrinter) printfDepth(depth int, format string, args ...any) {
75-
klog.InfofDepth(depth, format, args...)
76-
}
77-
78-
func (p *packagePrinter) OptPrintf(opt *printer.Options, format string, args ...any) {
79-
if opt == nil {
80-
p.Printf(format, args...)
81-
return
82-
}
83-
var prefix string
84-
switch {
85-
case opt.PkgDisplayName != "":
86-
prefix = fmt.Sprintf(packagePrefixFormat, opt.PkgDisplayName)
87-
case !opt.PkgDisplayPath.Empty():
88-
prefix = fmt.Sprintf(packagePrefixFormat, string(opt.PkgDisplayPath))
89-
case !opt.PkgPath.Empty():
90-
prefix = fmt.Sprintf(packagePrefixFormat, string(opt.PkgPath))
91-
}
92-
p.printfDepth(logDepth, prefix+format, args...)
93-
}
94-
95-
func (p *packagePrinter) OutStream() io.Writer {
96-
return os.Stdout
97-
}
98-
99-
func (p *packagePrinter) ErrStream() io.Writer {
100-
return os.Stderr
49+
return rr.Execute(printer.WithContext(ctx, printer.NewKlogPrinter()))
10150
}

pkg/lib/kptops/render_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func TestPackagePrinter(t *testing.T) {
163163

164164
func TestPackagePrinterStub(t *testing.T) {
165165
t.Run("PrintPackage stub", func(t *testing.T) {
166-
p := &packagePrinter{}
166+
p := printer.NewKlogPrinter()
167167
testPkg := &pkg.Pkg{
168168
DisplayPath: "test/path",
169169
}
@@ -178,7 +178,7 @@ func TestPackagePrinterStub(t *testing.T) {
178178
})
179179

180180
t.Run("Printf stub", func(t *testing.T) {
181-
p := &packagePrinter{}
181+
p := printer.NewKlogPrinter()
182182

183183
assert.NotPanics(t, func() {
184184
p.Printf("test message")
@@ -190,15 +190,15 @@ func TestPackagePrinterStub(t *testing.T) {
190190
})
191191

192192
t.Run("OptPrintf stub with nil options", func(t *testing.T) {
193-
p := &packagePrinter{}
193+
p := printer.NewKlogPrinter()
194194

195195
assert.NotPanics(t, func() {
196196
p.OptPrintf(nil, "test message")
197197
})
198198
})
199199

200200
t.Run("OptPrintf stub with options", func(t *testing.T) {
201-
p := &packagePrinter{}
201+
p := printer.NewKlogPrinter()
202202
opt := printer.NewOpt().DisplayName("my-package")
203203

204204
assert.NotPanics(t, func() {
@@ -207,15 +207,15 @@ func TestPackagePrinterStub(t *testing.T) {
207207
})
208208

209209
t.Run("OutStream stub", func(t *testing.T) {
210-
p := &packagePrinter{}
210+
p := printer.NewKlogPrinter()
211211

212212
stream := p.OutStream()
213213
assert.NotNil(t, stream)
214214
assert.Equal(t, os.Stdout, stream)
215215
})
216216

217217
t.Run("ErrStream stub", func(t *testing.T) {
218-
p := &packagePrinter{}
218+
p := printer.NewKlogPrinter()
219219

220220
stream := p.ErrStream()
221221
assert.NotNil(t, stream)
@@ -236,7 +236,7 @@ func TestPrinterLoggingDepth(t *testing.T) {
236236
}
237237
expectedFile := filepath.Base(filename)
238238

239-
p := &packagePrinter{}
239+
p := printer.NewKlogPrinter()
240240

241241
tests := []struct {
242242
name string

pkg/printer/fake/fake.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package fake
1717
import (
1818
"context"
1919
"io"
20+
"time"
2021

2122
"github.com/kptdev/kpt/pkg/lib/pkg"
2223
"github.com/kptdev/kpt/pkg/printer"
@@ -29,6 +30,24 @@ type Printer struct {
2930
errStream io.Writer
3031
}
3132

33+
func (np *Printer) WithField(string, string) printer.Printer { return np }
34+
35+
func (np *Printer) WithFields(printer.ContextualFields) printer.Printer { return np }
36+
37+
func (np *Printer) WithPackage(string) printer.Printer { return np }
38+
39+
func (np *Printer) WithFunction(string, string) printer.Printer { return np }
40+
41+
func (np *Printer) PrintRunning(string, int) {}
42+
43+
func (np *Printer) PrintPass(string, time.Duration) {}
44+
45+
func (np *Printer) PrintFail(string, time.Duration, error) {}
46+
47+
func (np *Printer) PrintResult(string, string, string) {}
48+
49+
func (np *Printer) PrintSummary(int, int, time.Duration) {}
50+
3251
func (np *Printer) PrintPackage(*pkg.Pkg, bool) {}
3352

3453
func (np *Printer) OptPrintf(*printer.Options, string, ...any) {}

0 commit comments

Comments
 (0)