Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 1 addition & 52 deletions pkg/lib/kptops/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ package kptops

import (
"context"
"fmt"
"io"
"os"

fnresultv1 "github.com/kptdev/kpt/api/fnresult/v1"
"github.com/kptdev/kpt/pkg/fn"
"github.com/kptdev/kpt/pkg/lib/pkg"
"github.com/kptdev/kpt/pkg/lib/runneroptions"
"github.com/kptdev/kpt/pkg/printer"
"k8s.io/klog/v2"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

Expand All @@ -51,51 +46,5 @@ func (r *renderer) Render(ctx context.Context, pkg filesys.FileSystem, opts fn.R
FileSystem: pkg,
RunnerOptions: r.runnerOptions,
}
return rr.Execute(printer.WithContext(ctx, &packagePrinter{}))
}

type packagePrinter struct{}

var _ printer.Printer = &packagePrinter{}

const (
packagePrefixFormat = "Package %q:"
logDepth = 2
)

func (p *packagePrinter) PrintPackage(pkg *pkg.Pkg, _ bool) {
p.printfDepth(logDepth, packagePrefixFormat, pkg.DisplayPath)
}

func (p *packagePrinter) Printf(format string, args ...any) {
p.printfDepth(logDepth, format, args...)
}

func (p *packagePrinter) printfDepth(depth int, format string, args ...any) {
klog.InfofDepth(depth, format, args...)
}

func (p *packagePrinter) OptPrintf(opt *printer.Options, format string, args ...any) {
if opt == nil {
p.Printf(format, args...)
return
}
var prefix string
switch {
case opt.PkgDisplayName != "":
prefix = fmt.Sprintf(packagePrefixFormat, opt.PkgDisplayName)
case !opt.PkgDisplayPath.Empty():
prefix = fmt.Sprintf(packagePrefixFormat, string(opt.PkgDisplayPath))
case !opt.PkgPath.Empty():
prefix = fmt.Sprintf(packagePrefixFormat, string(opt.PkgPath))
}
p.printfDepth(logDepth, prefix+format, args...)
}

func (p *packagePrinter) OutStream() io.Writer {
return os.Stdout
}

func (p *packagePrinter) ErrStream() io.Writer {
return os.Stderr
return rr.Execute(printer.WithContext(ctx, printer.NewKlogPrinter()))
}
14 changes: 7 additions & 7 deletions pkg/lib/kptops/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestPackagePrinter(t *testing.T) {

func TestPackagePrinterStub(t *testing.T) {
t.Run("PrintPackage stub", func(t *testing.T) {
p := &packagePrinter{}
p := printer.NewKlogPrinter()
testPkg := &pkg.Pkg{
DisplayPath: "test/path",
}
Expand All @@ -178,7 +178,7 @@ func TestPackagePrinterStub(t *testing.T) {
})

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

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

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

assert.NotPanics(t, func() {
p.OptPrintf(nil, "test message")
})
})

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

assert.NotPanics(t, func() {
Expand All @@ -207,15 +207,15 @@ func TestPackagePrinterStub(t *testing.T) {
})

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

stream := p.OutStream()
assert.NotNil(t, stream)
assert.Equal(t, os.Stdout, stream)
})

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

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

p := &packagePrinter{}
p := printer.NewKlogPrinter()

tests := []struct {
name string
Expand Down
19 changes: 19 additions & 0 deletions pkg/printer/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package fake
import (
"context"
"io"
"time"

"github.com/kptdev/kpt/pkg/lib/pkg"
"github.com/kptdev/kpt/pkg/printer"
Expand All @@ -29,6 +30,24 @@ type Printer struct {
errStream io.Writer
}

func (np *Printer) WithField(string, string) printer.Printer { return np }

func (np *Printer) WithFields(printer.ContextualFields) printer.Printer { return np }

func (np *Printer) WithPackage(string) printer.Printer { return np }

func (np *Printer) WithFunction(string, string) printer.Printer { return np }

func (np *Printer) PrintRunning(string, int) {}

func (np *Printer) PrintPass(string, time.Duration) {}

func (np *Printer) PrintFail(string, time.Duration, error) {}

func (np *Printer) PrintResult(string, string, string) {}

func (np *Printer) PrintSummary(int, int, time.Duration) {}

func (np *Printer) PrintPackage(*pkg.Pkg, bool) {}

func (np *Printer) OptPrintf(*printer.Options, string, ...any) {}
Expand Down
Loading
Loading