-
Notifications
You must be signed in to change notification settings - Fork 4
/
bash_tool_rg.go
90 lines (85 loc) · 2.08 KB
/
bash_tool_rg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package tools
import (
"fmt"
"os/exec"
)
type RipGrepTool UserFunction
var RipGrep = RipGrepTool{
Name: "rg",
Description: "Search for a pattern in files using ripgrep.",
Inputs: &InputSchema{
Type: "object",
Properties: map[string]ParameterObject{
"pattern": {
Type: "string",
Description: "The pattern to search for.",
},
"path": {
Type: "string",
Description: "The path to search in.",
},
"case_sensitive": {
Type: "boolean",
Description: "Whether the search should be case sensitive.",
},
"line_number": {
Type: "boolean",
Description: "Whether to show line numbers.",
},
"hidden": {
Type: "boolean",
Description: "Whether to search hidden files and directories.",
},
},
Required: []string{"pattern"},
},
}
func (r RipGrepTool) Call(input Input) (string, error) {
pattern, ok := input["pattern"].(string)
if !ok {
return "", fmt.Errorf("pattern must be a string")
}
cmd := exec.Command("rg", pattern)
if input["path"] != nil {
path, ok := input["path"].(string)
if !ok {
return "", fmt.Errorf("path must be a string")
}
cmd.Args = append(cmd.Args, path)
}
if input["case_sensitive"] != nil {
caseSensitive, ok := input["case_sensitive"].(bool)
if !ok {
return "", fmt.Errorf("case_sensitive must be a boolean")
}
if caseSensitive {
cmd.Args = append(cmd.Args, "--case-sensitive")
}
}
if input["line_number"] != nil {
lineNumber, ok := input["line_number"].(bool)
if !ok {
return "", fmt.Errorf("line_number must be a boolean")
}
if lineNumber {
cmd.Args = append(cmd.Args, "--line-number")
}
}
if input["hidden"] != nil {
hidden, ok := input["hidden"].(bool)
if !ok {
return "", fmt.Errorf("hidden must be a boolean")
}
if hidden {
cmd.Args = append(cmd.Args, "--hidden")
}
}
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to run rg: %w, output: %v", err, string(output))
}
return string(output), nil
}
func (r RipGrepTool) UserFunction() UserFunction {
return UserFunction(RipGrep)
}