|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/MakeNowJust/heredoc" |
| 5 | + "github.com/checkmarx/ast-cli/internal/commands/util/printer" |
| 6 | + "github.com/checkmarx/ast-cli/internal/params" |
| 7 | + "github.com/checkmarx/ast-cli/internal/services" |
| 8 | + "github.com/checkmarx/ast-cli/internal/wrappers" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func NewEnginesCommand( |
| 14 | + enginesWrapper wrappers.EnginesWrapper, |
| 15 | +) *cobra.Command { |
| 16 | + enginesCmd := &cobra.Command{ |
| 17 | + Use: "engines", |
| 18 | + Short: "Fetch supported API of scanner engines", |
| 19 | + Long: "The engines command enables the ability to fetch engines APIs list in Checkmarx One.", |
| 20 | + Annotations: map[string]string{ |
| 21 | + "command:doc": heredoc.Doc( |
| 22 | + ` |
| 23 | + https://checkmarx.com/resource/documents/en/34965-68643-scan.html |
| 24 | + `, |
| 25 | + ), |
| 26 | + }, |
| 27 | + } |
| 28 | + listEngineAPIcmd := enginesListAPISubCommand(enginesWrapper) |
| 29 | + enginesCmd.AddCommand(listEngineAPIcmd) |
| 30 | + return enginesCmd |
| 31 | +} |
| 32 | + |
| 33 | +func enginesListAPISubCommand( |
| 34 | + enginesWrapper wrappers.EnginesWrapper, |
| 35 | +) *cobra.Command { |
| 36 | + enginesListAPIcmd := &cobra.Command{ |
| 37 | + Use: "list-api", |
| 38 | + Short: "fetch the API list of scanner engines", |
| 39 | + Long: "The create list-api fetch the API list of scanner engines in Checkmarx One.", |
| 40 | + Example: heredoc.Doc( |
| 41 | + ` |
| 42 | + $ cx engines list-api --engine-name <Engine Name> |
| 43 | + `, |
| 44 | + ), |
| 45 | + Annotations: map[string]string{ |
| 46 | + "command:doc": heredoc.Doc( |
| 47 | + ` |
| 48 | + https://checkmarx.com/resource/documents/en/34965-68643-scan.html#UUID-a0bb20d5-5182-3fb4-3da0-0e263344ffe7 |
| 49 | + `, |
| 50 | + ), |
| 51 | + }, |
| 52 | + RunE: runEnginesListAPICommand(enginesWrapper), |
| 53 | + } |
| 54 | + enginesListAPIcmd.PersistentFlags().String("engine-name", "", "The name of the Checkmarx scanner engine to use.") |
| 55 | + |
| 56 | + addOutputFormatFlag( |
| 57 | + enginesListAPIcmd, |
| 58 | + printer.FormatTable, |
| 59 | + printer.FormatJSON, |
| 60 | + printer.FormatYAML, |
| 61 | + ) |
| 62 | + return enginesListAPIcmd |
| 63 | +} |
| 64 | + |
| 65 | +func runEnginesListAPICommand(enginesWrapper wrappers.EnginesWrapper) func(cmd *cobra.Command, args []string) error { |
| 66 | + //fmt.Println("Inside the command execution runEnginesListAPICommand function") |
| 67 | + return func(cmd *cobra.Command, args []string) error { |
| 68 | + var apiModels []wrappers.ApiModel |
| 69 | + var errorModel *wrappers.ErrorModel |
| 70 | + //fmt.Println("Before flag") |
| 71 | + engineName, err := cmd.Flags().GetString("engine-name") |
| 72 | + if err != nil { |
| 73 | + return errors.Wrapf(err, "%s", "Invalid 'engine-name' flag") |
| 74 | + } |
| 75 | + apiModels, errorModel, err = enginesWrapper.GetAllAPIs(engineName) |
| 76 | + if err != nil { |
| 77 | + return errors.Wrapf(err, "%s\n", "Failed to fetch all engines APIs") |
| 78 | + } |
| 79 | + |
| 80 | + //fmt.Println(apiModels) |
| 81 | + // Checking the response |
| 82 | + if errorModel != nil { |
| 83 | + return errors.Errorf(services.ErrorCodeFormat, "Failed to Getting All apis in error model", errorModel.Code, errorModel.Message) |
| 84 | + } else if apiModels != nil && len(apiModels) > 0 { |
| 85 | + f1, _ := cmd.Flags().GetString(params.OutputFormatFlag) |
| 86 | + if f1 == "table" { |
| 87 | + views := toAPIsViews(apiModels) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + err = printByOutputFormat(cmd, views) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } else { |
| 96 | + views := toEnginesView(apiModels) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + err = printByOutputFormat(cmd, views) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +type Engine struct { |
| 112 | + EngineID string `json:"engine_id"` |
| 113 | + EngineName string `json:"engine_name"` |
| 114 | + APIs []API `json:"apis"` |
| 115 | +} |
| 116 | + |
| 117 | +type API struct { |
| 118 | + ApiUrl string `json:"api_url"` |
| 119 | + ApiName string `json:"api_name"` |
| 120 | + Description string `json:"description"` |
| 121 | +} |
| 122 | + |
| 123 | +type EnginesView struct { |
| 124 | + Engines []Engine `json:"engines"` |
| 125 | +} |
| 126 | + |
| 127 | +func toEnginesView(models []wrappers.ApiModel) EnginesView { |
| 128 | + engineMap := make(map[string]Engine) |
| 129 | + |
| 130 | + // Group APIs by engine |
| 131 | + for _, model := range models { |
| 132 | + api := API{ |
| 133 | + ApiUrl: model.ApiUrl, |
| 134 | + ApiName: model.ApiName, |
| 135 | + Description: model.Description, |
| 136 | + } |
| 137 | + |
| 138 | + engine, exists := engineMap[model.EngineId] |
| 139 | + if !exists { |
| 140 | + engine = Engine{ |
| 141 | + EngineID: model.EngineId, |
| 142 | + EngineName: model.EngineName, |
| 143 | + APIs: []API{}, |
| 144 | + } |
| 145 | + } |
| 146 | + engine.APIs = append(engine.APIs, api) |
| 147 | + engineMap[model.EngineId] = engine |
| 148 | + } |
| 149 | + |
| 150 | + // Collect all engines |
| 151 | + var engines []Engine |
| 152 | + for _, engine := range engineMap { |
| 153 | + engines = append(engines, engine) |
| 154 | + } |
| 155 | + |
| 156 | + return EnginesView{ |
| 157 | + Engines: engines, |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func toAPIsViews(models []wrappers.ApiModel) []apiView { |
| 162 | + result := make([]apiView, len(models)) |
| 163 | + for i := 0; i < len(models); i++ { |
| 164 | + result[i] = toAPIView(models[i]) |
| 165 | + } |
| 166 | + return result |
| 167 | +} |
| 168 | +func toAPIView(model wrappers.ApiModel) apiView { |
| 169 | + return apiView{ |
| 170 | + ApiName: model.ApiName, |
| 171 | + Description: model.Description, |
| 172 | + ApiUrl: model.ApiUrl, |
| 173 | + EngineName: model.EngineName, |
| 174 | + EngineId: model.EngineId, |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +type apiView struct { |
| 179 | + ApiName string |
| 180 | + Description string |
| 181 | + ApiUrl string |
| 182 | + EngineName string |
| 183 | + EngineId string |
| 184 | +} |
0 commit comments