diff --git a/cmd/triggerScan.go b/cmd/triggerScan.go index 19aafaa..2ca14dc 100644 --- a/cmd/triggerScan.go +++ b/cmd/triggerScan.go @@ -2,9 +2,11 @@ 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" ) @@ -12,6 +14,8 @@ import ( // 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", @@ -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{ @@ -66,6 +103,8 @@ func NewTriggerScanCommand() *cobra.Command { }, } + cmd.Flags().BoolVar(&wait, "wait", false, "Wait for scan to complete") + return cmd }