From 98544010cff88e8107d2f50f0ee4fd1f20ee378e Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Fri, 16 Sep 2022 09:08:27 +0100 Subject: [PATCH] fix: updating and cancelling filesystem scan (#20) Signed-off-by: Owen Rumney --- pkg/controllers/filesystem/fs.go | 3 +- pkg/controllers/filesystem/help.go | 2 +- pkg/controllers/filesystem/key_bindings.go | 2 +- pkg/controllers/filesystem/local.go | 61 +++++++++++++--------- pkg/controllers/vulnerabilities/image.go | 4 +- pkg/widgets/files.go | 3 +- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/pkg/controllers/filesystem/fs.go b/pkg/controllers/filesystem/fs.go index da21698..bd8dbb3 100644 --- a/pkg/controllers/filesystem/fs.go +++ b/pkg/controllers/filesystem/fs.go @@ -146,7 +146,6 @@ func (c *Controller) RenderFilesystemFileReportd(report *output.Report) error { return fmt.Errorf("failed to render results report summary") //nolint:goerr113 } -func (c *Controller) ScanVulnerabilities(*gocui.Gui, *gocui.View) error { - c.UpdateStatus(fmt.Sprintf("Scanning %s", c.workingDireectory)) +func (c *Controller) ScanVulnerabilities(g *gocui.Gui, _ *gocui.View) error { return c.scanVulnerabilities() } diff --git a/pkg/controllers/filesystem/help.go b/pkg/controllers/filesystem/help.go index 09a7278..2c22c21 100644 --- a/pkg/controllers/filesystem/help.go +++ b/pkg/controllers/filesystem/help.go @@ -11,7 +11,7 @@ import ( var helpCommands = []string{ "", tml.Sprintf(" [s]can/update Scan filesystem"), - tml.Sprintf(" [c]hange directory Change target directory"), + tml.Sprintf(" change [p]ath Change target directory"), "", } diff --git a/pkg/controllers/filesystem/key_bindings.go b/pkg/controllers/filesystem/key_bindings.go index f57ec27..c96e12a 100644 --- a/pkg/controllers/filesystem/key_bindings.go +++ b/pkg/controllers/filesystem/key_bindings.go @@ -30,7 +30,7 @@ func (c *Controller) configureKeyBindings() error { return fmt.Errorf("error setting keybinding for moving right: %w", err) } - if err := c.Cui.SetKeybinding("", 'c', gocui.ModNone, c.showPathChange); err != nil { + if err := c.Cui.SetKeybinding("", 'p', gocui.ModNone, c.showPathChange); err != nil { return fmt.Errorf("error setting keybinding for moving right: %w", err) } diff --git a/pkg/controllers/filesystem/local.go b/pkg/controllers/filesystem/local.go index a287f88..1f2167e 100644 --- a/pkg/controllers/filesystem/local.go +++ b/pkg/controllers/filesystem/local.go @@ -59,39 +59,52 @@ func (c *Controller) scanVulnerabilities() error { scanChecks = append(scanChecks, "secret") } - report, err := c.DockerClient.ScanFilesystem(context.TODO(), c.workingDireectory, scanChecks, c) - if err != nil { - return err - } + go func() { + var cancellable context.Context + c.Lock() + defer c.Unlock() + cancellable, c.ActiveCancel = context.WithCancel(context.Background()) + + report, err := c.DockerClient.ScanFilesystem(cancellable, c.workingDireectory, scanChecks, c) + if err != nil { + logger.Errorf("error scanning filesystem: %v", err) + } - c.currentReport = report + c.currentReport = report - width := 20 - var targets []string + width := 20 + var targets []string - for _, result := range report.Results { - if result.HasIssues() { - targets = append(targets, result.Target) + for _, result := range report.Results { + if result.HasIssues() { + targets = append(targets, result.Target) + } } - } - root := createRootDir(targets) + root := createRootDir(targets) - var lines []string - lines = root.generateTree(lines, -1) + var lines []string + lines = root.generateTree(lines, -1) - for _, line := range lines { - parts := strings.Split(line, "|") - if len(parts[0]) > width { - width = len(parts[0]) + for _, line := range lines { + parts := strings.Split(line, "|") + if len(parts[0]) > width { + width = len(parts[0]) + } } - } - logger.Debugf("Updating the files view with the identified services") - if v, ok := c.Views[widgets.Files].(*widgets.FilesWidget); ok { - if err := v.RefreshFiles(lines, width); err != nil { - return err + select { + case <-cancellable.Done(): + + default: + logger.Debugf("Updating the files view with the identified services") + if v, ok := c.Views[widgets.Files].(*widgets.FilesWidget); ok { + if err := v.RefreshFiles(lines, width); err != nil { + logger.Errorf("error refreshing the files view: %v", err) + } + } } - } + + }() return nil } diff --git a/pkg/controllers/vulnerabilities/image.go b/pkg/controllers/vulnerabilities/image.go index 745c2af..3e1d368 100644 --- a/pkg/controllers/vulnerabilities/image.go +++ b/pkg/controllers/vulnerabilities/image.go @@ -69,8 +69,8 @@ func (c *Controller) ScanAllImages(gui *gocui.Gui, _ *gocui.View) error { if len(c.images) > 10 { lines := []string{ - fmt.Sprintf("Scanning %d images may take a while", len(c.images)), "", - "Press 't' to cancel if you get bored", "", + fmt.Sprintf("", "Scanning %d images may take a while", len(c.images)), "", + "Press 't' to terminate if you get bored", "", } x, y := gui.Size() diff --git a/pkg/widgets/files.go b/pkg/widgets/files.go index 8bcba71..28e6f3e 100644 --- a/pkg/widgets/files.go +++ b/pkg/widgets/files.go @@ -47,7 +47,6 @@ func (w *FilesWidget) ConfigureKeys(*gocui.Gui) error { if err := w.ctx.SetKeyBinding(w.name, 's', gocui.ModNone, func(gui *gocui.Gui, view *gocui.View) error { w.body = []string{" Scanning... "} - w.RefreshView() return w.ctx.ScanVulnerabilities(gui, view) }); err != nil { @@ -108,7 +107,7 @@ func (w *FilesWidget) RefreshFiles(files []string, fileWidth int) error { } w.body = files w.v.Highlight = true - w.RefreshView() + w.ctx.RefreshView(Files) _ = w.v.SetCursor(0, 0) return nil