|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/Escape-Technologies/cli/pkg/api" |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/oapi-codegen/runtime/types" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var startScanCmd = &cobra.Command{ |
| 16 | + Use: "start-scan [applicationId]", |
| 17 | + Short: "Trigger a scan on an application", |
| 18 | + Args: cobra.ExactArgs(1), |
| 19 | + Example: "escape-cli start-scan 123e4567-e89b-12d3-a456-426614174001", |
| 20 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 21 | + applicationID := args[0] |
| 22 | + fmt.Printf("Triggering scan for application %s\n\n", applicationID) |
| 23 | + |
| 24 | + client, err := api.NewAPIClient() |
| 25 | + if err != nil { |
| 26 | + return fmt.Errorf("failed to create API client: %w", err) |
| 27 | + } |
| 28 | + |
| 29 | + parsedUUID, err := uuid.Parse(applicationID) |
| 30 | + if err != nil { |
| 31 | + return fmt.Errorf("invalid UUID format: %w", err) |
| 32 | + } |
| 33 | + applicationId := types.UUID(parsedUUID) |
| 34 | + params := &api.PostApplicationsIdStartScanParams{ |
| 35 | + ContentType: api.PostApplicationsIdStartScanParamsContentTypeApplicationjson, |
| 36 | + } |
| 37 | + |
| 38 | + body := api.PostApplicationsIdStartScanJSONRequestBody{} |
| 39 | + scan, err := client.PostApplicationsIdStartScanWithResponse(context.Background(), applicationId, params, body) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + // Handle response |
| 44 | + var data interface{} |
| 45 | + if scan.JSON200 != nil { |
| 46 | + print( |
| 47 | + scan.JSON200, |
| 48 | + func() { |
| 49 | + fmt.Printf("-> Scan successfully launched\n") |
| 50 | + fmt.Printf("Scan ID: %s\n", scan.JSON200.Id) |
| 51 | + }, |
| 52 | + ) |
| 53 | + return nil |
| 54 | + } else if scan.JSON400 != nil { |
| 55 | + data = scan.JSON400 |
| 56 | + } else { |
| 57 | + data = scan.JSON500 |
| 58 | + } |
| 59 | + print( |
| 60 | + data, |
| 61 | + func() { |
| 62 | + var responseMessage map[string]interface{} |
| 63 | + json.Unmarshal(scan.Body, &responseMessage) |
| 64 | + |
| 65 | + // Print status code and error message |
| 66 | + fmt.Println(scan.HTTPResponse.Status) |
| 67 | + fmt.Printf("%s\n\n", responseMessage["message"]) |
| 68 | + }, |
| 69 | + ) |
| 70 | + os.Exit(1) |
| 71 | + return err |
| 72 | + }, |
| 73 | +} |
0 commit comments