(c *apiClient)
| 646 | } |
| 647 | |
| 648 | func cmdRulesAdd(c *apiClient) { |
| 649 | fmt.Println() |
| 650 | fmt.Println(" === Add injection rule ===") |
| 651 | fmt.Println() |
| 652 | |
| 653 | tools, err := c.getObservedTools() |
| 654 | if err == nil && len(tools) > 0 { |
| 655 | fmt.Println(" Observed tools:") |
| 656 | for i, t := range tools { |
| 657 | fmt.Printf(" %d) %s (%s)\n", i+1, t.Name, t.Format) |
| 658 | } |
| 659 | fmt.Println() |
| 660 | |
| 661 | choice := interactiveReadLine(" Select tool # (or type tool name): ") |
| 662 | var toolName string |
| 663 | var selectedSchema map[string]any |
| 664 | if idx, err := strconv.Atoi(choice); err == nil && idx >= 1 && idx <= len(tools) { |
| 665 | toolName = tools[idx-1].Name |
| 666 | selectedSchema = tools[idx-1].Schema |
| 667 | } else { |
| 668 | toolName = choice |
| 669 | } |
| 670 | if toolName == "" { |
| 671 | fmt.Println(" cancelled.") |
| 672 | return |
| 673 | } |
| 674 | |
| 675 | name := interactiveReadLineDefault(" Rule name", "inject-"+toolName) |
| 676 | args := buildArguments(selectedSchema) |
| 677 | timing := interactiveReadLineDefault(" Timing (before/replace)", "before") |
| 678 | modelPattern := interactiveReadLineDefault(" Model pattern (empty=all)", "") |
| 679 | maxInjStr := interactiveReadLineDefault(" Max injections (0=1)", "0") |
| 680 | maxInj, _ := strconv.Atoi(maxInjStr) |
| 681 | |
| 682 | rule := InjectionRule{ |
| 683 | Name: name, |
| 684 | Enabled: true, |
| 685 | ToolName: toolName, |
| 686 | Arguments: args, |
| 687 | Timing: timing, |
| 688 | ModelPattern: modelPattern, |
| 689 | MaxInjections: maxInj, |
| 690 | } |
| 691 | |
| 692 | ruleJSON, _ := json.MarshalIndent(rule, " ", " ") |
| 693 | fmt.Printf("\n %s\n\n", string(ruleJSON)) |
| 694 | |
| 695 | confirm := interactiveReadLine(" Confirm? (y/n): ") |
| 696 | if strings.ToLower(confirm) != "y" { |
| 697 | fmt.Println(" cancelled.") |
| 698 | return |
| 699 | } |
| 700 | if err := c.patchRule(rule); err != nil { |
| 701 | fmt.Printf(" error: %v\n", err) |
| 702 | } else { |
| 703 | fmt.Println(" rule created.") |
| 704 | } |
| 705 | } else { |
no test coverage detected