mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 15:06:36 +01:00
Add check for app update (#88)
* add check for app update; fix ETUI error handling * validate user args
This commit is contained in:
@@ -32,7 +32,7 @@ func setupScreen(output *os.File) *frame.Frame {
|
||||
}
|
||||
|
||||
// nolint:funlen,gocognit
|
||||
func OutputToEphemeralTUI(workerErrs <-chan error, subscription *partybus.Subscription) int {
|
||||
func OutputToEphemeralTUI(workerErrs <-chan error, subscription *partybus.Subscription) error {
|
||||
output := os.Stderr
|
||||
|
||||
// hide cursor
|
||||
@@ -42,8 +42,14 @@ func OutputToEphemeralTUI(workerErrs <-chan error, subscription *partybus.Subscr
|
||||
|
||||
fr := setupScreen(output)
|
||||
if fr == nil {
|
||||
return 1
|
||||
return fmt.Errorf("unable to setup screen")
|
||||
}
|
||||
var isClosed bool
|
||||
defer func() {
|
||||
if !isClosed {
|
||||
frame.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
var err error
|
||||
var wg = &sync.WaitGroup{}
|
||||
@@ -53,39 +59,49 @@ func OutputToEphemeralTUI(workerErrs <-chan error, subscription *partybus.Subscr
|
||||
eventLoop:
|
||||
for {
|
||||
select {
|
||||
case err := <-workerErrs:
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case e, ok := <-events:
|
||||
if !ok {
|
||||
// is this unexpected? if so should we indicate this?
|
||||
break eventLoop
|
||||
}
|
||||
switch e.Type {
|
||||
case imgbomEvent.AppUpdateAvailable:
|
||||
err = appUpdateAvailableHandler(ctx, fr, e, wg)
|
||||
if err != nil {
|
||||
log.Errorf("unable to show AppUpdateAvailable event: %+v", err)
|
||||
}
|
||||
|
||||
case stereoscopeEvent.ReadImage:
|
||||
err = imageReadHandler(ctx, fr, e, wg)
|
||||
if err != nil {
|
||||
log.Errorf("unable to show read image event: %+v", err)
|
||||
log.Errorf("unable to show ReadImage event: %+v", err)
|
||||
}
|
||||
|
||||
case stereoscopeEvent.FetchImage:
|
||||
err = imageFetchHandler(ctx, fr, e, wg)
|
||||
if err != nil {
|
||||
log.Errorf("unable to show fetch image event: %+v", err)
|
||||
log.Errorf("unable to show FetchImage event: %+v", err)
|
||||
}
|
||||
|
||||
case imgbomEvent.CatalogerStarted:
|
||||
err = catalogerStartedHandler(ctx, fr, e, wg)
|
||||
if err != nil {
|
||||
log.Errorf("unable to show catalog image start event: %+v", err)
|
||||
log.Errorf("unable to show CatalogerStarted event: %+v", err)
|
||||
}
|
||||
case imgbomEvent.CatalogerFinished:
|
||||
// we may have other background processes still displaying progress, wait for them to
|
||||
// finish before discontinuing dynamic content and showing the final report
|
||||
wg.Wait()
|
||||
frame.Close()
|
||||
isClosed = true
|
||||
fmt.Println()
|
||||
|
||||
err := common.CatalogerFinishedHandler(e)
|
||||
if err != nil {
|
||||
log.Errorf("unable to show catalog image finished event: %+v", err)
|
||||
log.Errorf("unable to show CatalogerFinished event: %+v", err)
|
||||
}
|
||||
|
||||
// this is the last expected event
|
||||
@@ -99,5 +115,5 @@ eventLoop:
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -143,3 +143,20 @@ func catalogerStartedHandler(ctx context.Context, fr *frame.Frame, event partybu
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func appUpdateAvailableHandler(_ context.Context, fr *frame.Frame, event partybus.Event, _ *sync.WaitGroup) error {
|
||||
newVersion, err := imgbomEventParsers.ParseAppUpdateAvailable(event)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bad AppUpdateAvailable event: %w", err)
|
||||
}
|
||||
|
||||
line, err := fr.Prepend()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
message := color.Magenta.Sprintf("New Update Available: %s", newVersion)
|
||||
_, _ = io.WriteString(line, message)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,17 +7,14 @@ import (
|
||||
"github.com/wagoodman/go-partybus"
|
||||
)
|
||||
|
||||
func LoggerUI(workerErrs <-chan error, subscription *partybus.Subscription) int {
|
||||
var returnCode int
|
||||
|
||||
func LoggerUI(workerErrs <-chan error, subscription *partybus.Subscription) error {
|
||||
events := subscription.Events()
|
||||
eventLoop:
|
||||
for {
|
||||
select {
|
||||
case err := <-workerErrs:
|
||||
if err != nil {
|
||||
log.Errorf(err.Error())
|
||||
returnCode = 1
|
||||
return err
|
||||
}
|
||||
case e, ok := <-events:
|
||||
if !ok {
|
||||
@@ -38,5 +35,5 @@ eventLoop:
|
||||
}
|
||||
}
|
||||
|
||||
return returnCode
|
||||
return nil
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,4 +4,4 @@ import (
|
||||
"github.com/wagoodman/go-partybus"
|
||||
)
|
||||
|
||||
type UI func(<-chan error, *partybus.Subscription) int
|
||||
type UI func(<-chan error, *partybus.Subscription) error
|
||||
|
||||
Reference in New Issue
Block a user