Skip to content

Commit

Permalink
Add option to wait for scan to complete
Browse files Browse the repository at this point in the history
  • Loading branch information
curtis-fugue committed Aug 14, 2019
1 parent c0c3351 commit 526de82
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions cmd/triggerScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package cmd

import (
"fmt"
"time"

"github.com/fugue/fugue-client/client/scans"
"github.com/fugue/fugue-client/format"
"github.com/fugue/fugue-client/models"
"github.com/go-openapi/runtime"
"github.com/spf13/cobra"
)

// NewTriggerScanCommand returns a command that scans a specified environment
func NewTriggerScanCommand() *cobra.Command {

var wait bool

cmd := &cobra.Command{
Use: "scan [environment_id]",
Short: "Trigger a scan",
Expand Down Expand Up @@ -42,15 +46,48 @@ func NewTriggerScanCommand() *cobra.Command {

params := scans.NewGetScanParams()
params.ScanID = scanID
resp, err := client.Scans.GetScan(params, auth)
CheckErr(err)

scan := resp.Payload
var scan *models.ScanWithSummary
var summary models.ResourceSummary
for {
resp, err := client.Scans.GetScan(params, auth)
CheckErr(err)
scan = resp.Payload
if resp.Payload.ResourceSummary != nil {
summary = *resp.Payload.ResourceSummary
}
if scan.Status != "IN_PROGRESS" || !wait {
break
}
time.Sleep(time.Second * 30)
}

var items []interface{}

items := []interface{}{
Item{"SCAN_ID", scan.ID},
Item{"CREATED_AT", format.Unix(scan.CreatedAt)},
Item{"STATUS", scan.Status},
if !wait {
items = []interface{}{
Item{"SCAN_ID", scan.ID},
Item{"CREATED_AT", format.Unix(scan.CreatedAt)},
Item{"STATUS", scan.Status},
}
} else {
message := "-"
if scan.Message != "" {
message = scan.Message
}
items = []interface{}{
Item{"SCAN_ID", scan.ID},
Item{"CREATED_AT", format.Unix(scan.CreatedAt)},
Item{"FINISHED_AT", format.Unix(scan.FinishedAt)},
Item{"STATUS", scan.Status},
Item{"MESSAGE", message},
Item{"RESOURCE_COUNT", summary.Total},
Item{"RESOURCE_TYPES", summary.ResourceTypes},
Item{"COMPLIANT", summary.Compliant},
Item{"NONCOMPLIANT", summary.Noncompliant},
Item{"RULES_PASSED", summary.RulesPassed},
Item{"RULES_FAILED", summary.RulesFailed},
}
}

table, err := format.Table(format.TableOpts{
Expand All @@ -66,6 +103,8 @@ func NewTriggerScanCommand() *cobra.Command {
},
}

cmd.Flags().BoolVar(&wait, "wait", false, "Wait for scan to complete")

return cmd
}

Expand Down

0 comments on commit 526de82

Please sign in to comment.