Skip to content

Commit

Permalink
fix: updating and cancelling filesystem scan (#20)
Browse files Browse the repository at this point in the history
Signed-off-by: Owen Rumney <owen@owenrumney.co.uk>
  • Loading branch information
owenrumney authored Sep 16, 2022
1 parent e41e48a commit 9854401
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
3 changes: 1 addition & 2 deletions pkg/controllers/filesystem/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion pkg/controllers/filesystem/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var helpCommands = []string{
"",
tml.Sprintf(" <blue>[s]</blue>can/update Scan filesystem"),
tml.Sprintf(" <blue>[c]</blue>hange directory Change target directory"),
tml.Sprintf(" change <blue>[p]</blue>ath Change target directory"),
"",
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/filesystem/key_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
61 changes: 37 additions & 24 deletions pkg/controllers/filesystem/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/vulnerabilities/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions pkg/widgets/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9854401

Please sign in to comment.