()
| 136 | } |
| 137 | |
| 138 | func (cli *cliLapi) newContextDetectCmd() *cobra.Command { |
| 139 | var detectAll bool |
| 140 | |
| 141 | cmd := &cobra.Command{ |
| 142 | Use: "detect", |
| 143 | Short: "Detect available fields from the installed parsers", |
| 144 | Example: `cscli lapi context detect --all |
| 145 | cscli lapi context detect crowdsecurity/sshd-logs |
| 146 | `, |
| 147 | DisableAutoGenTag: true, |
| 148 | RunE: func(cmd *cobra.Command, args []string) error { |
| 149 | cfg := cli.cfg() |
| 150 | |
| 151 | if !detectAll && len(args) == 0 { |
| 152 | _ = cmd.Help() |
| 153 | return errors.New("please provide parsers to detect or --all flag") |
| 154 | } |
| 155 | |
| 156 | // to avoid all the log.Info from the loaders functions |
| 157 | log.SetLevel(log.WarnLevel) |
| 158 | |
| 159 | if err := exprhelpers.Init(nil); err != nil { |
| 160 | return fmt.Errorf("failed to init expr helpers: %w", err) |
| 161 | } |
| 162 | |
| 163 | hub, err := require.Hub(cfg, nil) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | |
| 168 | csParsers, err := parser.LoadParsers(cfg, hub) |
| 169 | if err != nil { |
| 170 | return fmt.Errorf("unable to load parsers: %w", err) |
| 171 | } |
| 172 | |
| 173 | fieldByParsers := make(map[string][]string) |
| 174 | |
| 175 | for _, node := range csParsers.Nodes { |
| 176 | if !detectAll && !slices.Contains(args, node.Name) { |
| 177 | continue |
| 178 | } |
| 179 | |
| 180 | if !detectAll { |
| 181 | args = removeFromSlice(node.Name, args) |
| 182 | } |
| 183 | |
| 184 | fieldByParsers[node.Name] = make([]string, 0) |
| 185 | fieldByParsers[node.Name] = detectNode(node, *csParsers.Ctx) |
| 186 | |
| 187 | subNodeFields := detectSubNode(node, *csParsers.Ctx) |
| 188 | for _, field := range subNodeFields { |
| 189 | if !slices.Contains(fieldByParsers[node.Name], field) { |
| 190 | fieldByParsers[node.Name] = append(fieldByParsers[node.Name], field) |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | fmt.Fprint(os.Stdout, "Acquisition :\n\n") |
no test coverage detected