|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/MakeNowJust/heredoc" |
| 5 | + commonParams "github.com/checkmarx/ast-cli/internal/params" |
| 6 | + "github.com/checkmarx/ast-cli/internal/services" |
| 7 | + "github.com/checkmarx/ast-cli/internal/wrappers" |
| 8 | + "github.com/pkg/errors" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +type EngineView struct { |
| 13 | + EngineID string `format:"name:Engine ID"` |
| 14 | + EngineName string `format:"name:Engine Name"` |
| 15 | + ApiName string `format:"name:Api Name"` |
| 16 | + ApiURL string `format:"name:Api URL"` |
| 17 | + Description string `format:"name:Description"` |
| 18 | +} |
| 19 | + |
| 20 | +// engine command implementation |
| 21 | +func EngineCommand(engineWrapper wrappers.EngineWrapper) *cobra.Command { |
| 22 | + EngineCmd := &cobra.Command{ |
| 23 | + Use: "engine", |
| 24 | + Short: "manages the engine ", |
| 25 | + Long: "The scan command enables the ability to manage engine in Checkmarx One.", |
| 26 | + } |
| 27 | + listScanCmd := listEngineSubCmd(engineWrapper) |
| 28 | + EngineCmd.AddCommand(listScanCmd) |
| 29 | + return EngineCmd |
| 30 | +} |
| 31 | + |
| 32 | +// list Api of engines implementation |
| 33 | +func listEngineSubCmd(engineWrapper wrappers.EngineWrapper) *cobra.Command { |
| 34 | + enginelistCmd := &cobra.Command{ |
| 35 | + Use: "list-api", |
| 36 | + Short: "lists all APIs of CXOne engines", |
| 37 | + Long: "The list command provides a list of all APIs of the engines in Checkmarx One.", |
| 38 | + Example: heredoc.Doc( |
| 39 | + `$cx engine list-api |
| 40 | + `, |
| 41 | + ), |
| 42 | + RunE: runListEnginesCommand(engineWrapper), |
| 43 | + } |
| 44 | + enginelistCmd.PersistentFlags().String(commonParams.EngineName, "", "Name of the engine") |
| 45 | + enginelistCmd.PersistentFlags().String(commonParams.EngineOutputFormat, "table", "Name of format") |
| 46 | + return enginelistCmd |
| 47 | +} |
| 48 | + |
| 49 | +// This will call the get method to list the APIs of the engine/engines |
| 50 | + |
| 51 | +func runListEnginesCommand(engineWrapper wrappers.EngineWrapper) func(cmd *cobra.Command, args []string) error { |
| 52 | + return func(cmd *cobra.Command, args []string) error { |
| 53 | + var allEngineModel *wrappers.EnginesCollectionResponseModel |
| 54 | + var errorModel *wrappers.ErrorModel |
| 55 | + engineName, _ := cmd.Flags().GetString(commonParams.EngineName) |
| 56 | + var err error |
| 57 | + allEngineModel, err = engineWrapper.Get(engineName) |
| 58 | + if err != nil { |
| 59 | + return errors.Wrapf(err, "%s", "failed getting") |
| 60 | + } |
| 61 | + if errorModel != nil { |
| 62 | + return errors.Errorf(services.ErrorCodeFormat, failedGettingAll, errorModel.Code, errorModel.Message) |
| 63 | + } else if allEngineModel != nil { |
| 64 | + views, err := toEngineView(allEngineModel.Engines) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + err = printByOutputFormat(cmd, views) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Converting the values of EngineResponseModel (returned from API) proper view which can be presented by format |
| 78 | +func toEngineView(engines []wrappers.EngineResponseModel) ([]*EngineView, error) { |
| 79 | + |
| 80 | + views := make([]*EngineView, 0) |
| 81 | + for i := 0; i < len(engines); i++ { |
| 82 | + for j := 0; j < len(engines[i].Apis); j++ { |
| 83 | + views = append(views, &EngineView{ |
| 84 | + EngineID: engines[i].EngineID, |
| 85 | + EngineName: engines[i].EngineName, |
| 86 | + ApiName: engines[i].Apis[j].ApiName, |
| 87 | + ApiURL: engines[i].Apis[j].ApiURL, |
| 88 | + Description: engines[i].Apis[j].Description, |
| 89 | + }) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return views, nil |
| 94 | +} |
0 commit comments