github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/internal/ui/logger_ui.go (about)

     1  package ui
     2  
     3  import (
     4  	gosbomEvent "github.com/nextlinux/gosbom/gosbom/event"
     5  	"github.com/nextlinux/gosbom/internal/log"
     6  	"github.com/wagoodman/go-partybus"
     7  )
     8  
     9  type loggerUI struct {
    10  	unsubscribe func() error
    11  }
    12  
    13  // NewLoggerUI writes all events to the common application logger and writes the final report to the given writer.
    14  func NewLoggerUI() UI {
    15  	return &loggerUI{}
    16  }
    17  
    18  func (l *loggerUI) Setup(unsubscribe func() error) error {
    19  	l.unsubscribe = unsubscribe
    20  	return nil
    21  }
    22  
    23  func (l loggerUI) Handle(event partybus.Event) error {
    24  	// ignore all events except for the final event
    25  	if event.Type != gosbomEvent.Exit {
    26  		return nil
    27  	}
    28  
    29  	if err := handleExit(event); err != nil {
    30  		log.Warnf("unable to show catalog image finished event: %+v", err)
    31  	}
    32  
    33  	// this is the last expected event, stop listening to events
    34  	return l.unsubscribe()
    35  }
    36  
    37  func (l loggerUI) Teardown(_ bool) error {
    38  	return nil
    39  }