|
| 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/wrappers" |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +type EngineAPIDetails struct { |
| 11 | + EngineId string `format:"name:Engine ID"` |
| 12 | + EngineName string `format:"name:Engine Name"` |
| 13 | + ApiName string `format:"name:Api Name"` |
| 14 | + ApiURL string `format:"name:Api URL"` |
| 15 | + Description string `format:"name:Description"` |
| 16 | +} |
| 17 | + |
| 18 | +func NewEnginesCommand( |
| 19 | + enginesWrapper wrappers.EnginesWrapper, |
| 20 | +) *cobra.Command { |
| 21 | + enginesCmd := &cobra.Command{ |
| 22 | + Use: "engines", |
| 23 | + Short: "Manage engines", |
| 24 | + Long: "The engines command enables the ability to manage engines in Checkmarx One.", |
| 25 | + Annotations: map[string]string{ |
| 26 | + "command:doc": heredoc.Doc( |
| 27 | + ``, |
| 28 | + ), |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + getEngineListCmd := getEnginesListSubCommand( |
| 33 | + enginesWrapper, |
| 34 | + ) |
| 35 | + enginesCmd.AddCommand( |
| 36 | + getEngineListCmd, |
| 37 | + ) |
| 38 | + return enginesCmd |
| 39 | +} |
| 40 | + |
| 41 | +func getEnginesListSubCommand( |
| 42 | + enginesWrapper wrappers.EnginesWrapper, |
| 43 | +) *cobra.Command { |
| 44 | + getEngineListCmd := &cobra.Command{ |
| 45 | + Use: "list-api", |
| 46 | + Short: "Get list of all engines", |
| 47 | + Long: "The list-api command is used to get list of all engine apis in Checkmarx One.", |
| 48 | + Example: heredoc.Doc( |
| 49 | + ` |
| 50 | + $ cx engines list-api --engine-name <Engine Name> --output-format <output-format> |
| 51 | + `, |
| 52 | + ), |
| 53 | + Annotations: map[string]string{ |
| 54 | + "command:doc": heredoc.Doc( |
| 55 | + ``, |
| 56 | + ), |
| 57 | + }, |
| 58 | + RunE: runEngineGetListCommand( |
| 59 | + enginesWrapper, |
| 60 | + ), |
| 61 | + } |
| 62 | + getEngineListCmd.PersistentFlags().String(commonParams.EngineName, "", "Filters Engines by EngineName") |
| 63 | + getEngineListCmd.PersistentFlags().String(commonParams.OutputFormat, "table", "Show Engine Details based on Output Format") |
| 64 | + return getEngineListCmd |
| 65 | +} |
| 66 | + |
| 67 | +func runEngineGetListCommand( |
| 68 | + enginesWrapper wrappers.EnginesWrapper, |
| 69 | +) func(cmd *cobra.Command, args []string) error { |
| 70 | + return func(cmd *cobra.Command, args []string) error { |
| 71 | + var engineApiResponse *wrappers.EngineListResponseModel |
| 72 | + engineName, _ := cmd.Flags().GetString(commonParams.EngineName) |
| 73 | + engineApiResponse = enginesWrapper.Get(engineName) |
| 74 | + |
| 75 | + if engineApiResponse != nil { |
| 76 | + views := ShowEngineList(engineApiResponse.Engines) |
| 77 | + return printByOutputFormat(cmd, views) |
| 78 | + } |
| 79 | + return nil |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func ShowEngineList(engines []wrappers.EngineList) []*EngineAPIDetails { |
| 84 | + |
| 85 | + views := make([]*EngineAPIDetails, 0) |
| 86 | + for i := 0; i < len(engines); i++ { |
| 87 | + for j := 0; j < len(engines[i].APIs); j++ { |
| 88 | + views = append(views, &EngineAPIDetails{ |
| 89 | + EngineId: engines[i].EngineId, |
| 90 | + EngineName: engines[i].EngineName, |
| 91 | + ApiName: engines[i].APIs[j].ApiName, |
| 92 | + ApiURL: engines[i].APIs[j].ApiURL, |
| 93 | + Description: engines[i].APIs[j].Description, |
| 94 | + }) |
| 95 | + } |
| 96 | + } |
| 97 | + return views |
| 98 | +} |
0 commit comments