Skip to content

Commit

Permalink
fix: resolve comments
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Feb 24, 2025
1 parent 83d602b commit bbc19bb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
3 changes: 2 additions & 1 deletion cmd/notation/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ type BlobVerifyHandler interface {
// signatures.
type ListHandler interface {
Renderer

// OnReferenceResolved sets the artifact reference and media type for the
// handler.
OnReferenceResolved(reference string)

// OnSignatureListed adds the signature digest to be rendered.
OnSignatureListed(signatureManifest ocispec.Descriptor)
OnSignatureListed(signatureManifest ocispec.Descriptor) error
}
18 changes: 11 additions & 7 deletions cmd/notation/internal/display/metadata/tree/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ListHandler struct {

// sprinter is a streaming printer to print the signature digest nodes in
// a streaming fashion
sprinter *streamingPrinter
sprinter *StreamPrinter

// headerNode contains the headers of the output
//
Expand All @@ -43,7 +43,7 @@ type ListHandler struct {
func NewListHandler(printer *output.Printer) *ListHandler {
return &ListHandler{
printer: printer,
sprinter: newStreamingPrinter(" ", printer),
sprinter: newStreamPrinter(" ", printer),
}
}

Expand All @@ -55,20 +55,24 @@ func (h *ListHandler) OnReferenceResolved(reference string) {
}

// OnSignatureListed adds the signature digest to be printed.
func (h *ListHandler) OnSignatureListed(signatureManifest ocispec.Descriptor) {
func (h *ListHandler) OnSignatureListed(signatureManifest ocispec.Descriptor) error {
// print the header
if !h.headerPrinted {
h.headerNode.Print(h.printer)
if err := h.headerNode.Print(h.printer); err != nil {
return err
}

Check warning on line 63 in cmd/notation/internal/display/metadata/tree/list.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/internal/display/metadata/tree/list.go#L62-L63

Added lines #L62 - L63 were not covered by tests
h.headerPrinted = true
}
h.sprinter.PrintNode(newNode(signatureManifest.Digest.String()))
return h.sprinter.PrintNode(newNode(signatureManifest.Digest.String()))
}

// Render completes the rendering of the list of signature digests.
func (h *ListHandler) Render() error {
if h.sprinter.prevNode == nil {
if err := h.sprinter.Flush(); err != nil {
return err
}

Check warning on line 73 in cmd/notation/internal/display/metadata/tree/list.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/internal/display/metadata/tree/list.go#L72-L73

Added lines #L72 - L73 were not covered by tests
if !h.headerPrinted {
return h.printer.Printf("%s has no associated signatures\n", h.headerNode.Value)
}
h.sprinter.Complete()
return nil
}
27 changes: 15 additions & 12 deletions cmd/notation/internal/display/metadata/tree/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,42 @@ package tree

import "io"

// streamingPrinter prints the tree nodes in a streaming fashion.
type streamingPrinter struct {
// StreamPrinter prints the tree nodes in a streaming fashion.
type StreamPrinter struct {
w io.Writer
prefix string
prevNode *node
}

// newStreamingPrinter creates a new streaming printer.
// newStreamPrinter creates a new stream printer.
//
// prefix is the prefix string that will be inherited by the nodes that are
// printed.
func newStreamingPrinter(prefix string, w io.Writer) *streamingPrinter {
return &streamingPrinter{
func newStreamPrinter(prefix string, w io.Writer) *StreamPrinter {
return &StreamPrinter{
w: w,
prefix: prefix,
}
}

// PrintNode adds a new node to be ready to print.
func (p *streamingPrinter) PrintNode(node *node) {
func (p *StreamPrinter) PrintNode(node *node) error {
if p.prevNode == nil {
p.prevNode = node
return
return nil
}
if err := print(p.w, p.prefix, treeItemPrefix, p.prefix+subTreePrefix, p.prevNode); err != nil {
return err
}

Check warning on line 44 in cmd/notation/internal/display/metadata/tree/printer.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/internal/display/metadata/tree/printer.go#L43-L44

Added lines #L43 - L44 were not covered by tests
print(p.w, p.prefix, treeItemPrefix, p.prefix+subTreePrefix, p.prevNode)
p.prevNode = node
return nil
}

// Complete prints the last node and completes the printing.
func (p *streamingPrinter) Complete() {
// Flush prints the last node and completes the printing.
func (p *StreamPrinter) Flush() error {
if p.prevNode != nil {
// print the last node
print(p.w, p.prefix, treeItemPrefixLast, p.prefix+subTreePrefixLast, p.prevNode)
p.prevNode = nil
return print(p.w, p.prefix, treeItemPrefixLast, p.prefix+subTreePrefixLast, p.prevNode)
}
return nil
}
20 changes: 10 additions & 10 deletions cmd/notation/internal/display/metadata/tree/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestStreamingPrinter(t *testing.T) {
t.Run("empty output", func(t *testing.T) {
expected := ""
buff := &bytes.Buffer{}
p := newStreamingPrinter("", buff)
p.Complete()
p := newStreamPrinter("", buff)
p.Flush()

if buff.String() != expected {
t.Fatalf("expected %s, got %s", expected, buff.String())
Expand All @@ -33,9 +33,9 @@ func TestStreamingPrinter(t *testing.T) {
t.Run("one node", func(t *testing.T) {
expected := "└── a\n"
buff := &bytes.Buffer{}
p := newStreamingPrinter("", buff)
p := newStreamPrinter("", buff)
p.PrintNode(newNode("a"))
p.Complete()
p.Flush()

if buff.String() != expected {
t.Fatalf("expected %s, got %s", expected, buff.String())
Expand All @@ -47,10 +47,10 @@ func TestStreamingPrinter(t *testing.T) {
└── b
`
buff := &bytes.Buffer{}
p := newStreamingPrinter("", buff)
p := newStreamPrinter("", buff)
p.PrintNode(newNode("a"))
p.PrintNode(newNode("b"))
p.Complete()
p.Flush()

if buff.String() != expected {
t.Fatalf("expected %s, got %s", expected, buff.String())
Expand All @@ -68,7 +68,7 @@ func TestStreamingPrinter(t *testing.T) {
└── h
`
buff := &bytes.Buffer{}
p := newStreamingPrinter("", buff)
p := newStreamPrinter("", buff)
// create the tree
a := newNode("a")
b := a.Add("b")
Expand All @@ -82,7 +82,7 @@ func TestStreamingPrinter(t *testing.T) {
e.Add("h")
p.PrintNode(e)

p.Complete()
p.Flush()

if buff.String() != expected {
t.Fatalf("expected %s, got %s", expected, buff.String())
Expand All @@ -100,7 +100,7 @@ func TestStreamingPrinter(t *testing.T) {
│ └── h
`
buff := &bytes.Buffer{}
p := newStreamingPrinter(" │ ", buff)
p := newStreamPrinter(" │ ", buff)
// create the tree
a := newNode("a")
b := a.Add("b")
Expand All @@ -114,7 +114,7 @@ func TestStreamingPrinter(t *testing.T) {
e.Add("h")
p.PrintNode(e)

p.Complete()
p.Flush()

if buff.String() != expected {
t.Fatalf("expected %s, got %s", expected, buff.String())
Expand Down
5 changes: 1 addition & 4 deletions cmd/notation/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ func runList(ctx context.Context, opts *listOpts) error {
displayHandler.OnReferenceResolved(resolvedRef)

// list signatures
if err := listSignatures(ctx, sigRepo, manifestDesc, opts.maxSignatures, func(sigManifestDesc ocispec.Descriptor) error {
displayHandler.OnSignatureListed(sigManifestDesc)
return nil
}); err != nil {
if err := listSignatures(ctx, sigRepo, manifestDesc, opts.maxSignatures, displayHandler.OnSignatureListed); err != nil {
var errExceedMaxSignatures cmderr.ErrorExceedMaxSignatures
if !errors.As(err, &errExceedMaxSignatures) {
return err
Expand Down
14 changes: 9 additions & 5 deletions test/e2e/suite/command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,15 @@ var _ = Describe("notation list", func() {
artifact := GenerateArtifact("e2e-valid-multiple-signatures", "")

notation.Exec("list", artifact.ReferenceWithDigest()).
MatchContent(artifact.ReferenceWithDigest() + `
└── application/vnd.cncf.notary.signature
├── sha256:c3ebe4a20b6832328fc5078a7795ddc1114b896e13fca2add38109c3866b5fbf
└── sha256:90ceaff260d657d797c408ac73564a9c7bb9d86055877c2a811f0e63b8c6524f
`)
MatchKeyWords(
artifact.ReferenceWithDigest(),
// the order of the signatures is not guaranteed
"└── application/vnd.cncf.notary.signature",
" ├── ",
" └── ",
"sha256:c3ebe4a20b6832328fc5078a7795ddc1114b896e13fca2add38109c3866b5fbf",
"sha256:90ceaff260d657d797c408ac73564a9c7bb9d86055877c2a811f0e63b8c6524f",
)
})
})

Expand Down

0 comments on commit bbc19bb

Please sign in to comment.