main
 1# frozen_string_literal: true
 2
 3Elelem::Plugins.register(:grep) do |agent|
 4  agent.toolbox.add("grep",
 5    description: "Search file contents",
 6    params: { pattern: { type: "string" }, path: { type: "string" }, glob: { type: "string" } },
 7    required: ["pattern"]
 8  ) do |a|
 9    path = a["path"] || "."
10    glob = a["glob"]
11    rg_args = ["rg", "-n", a["pattern"], path]
12    rg_args += ["-g", glob] if glob
13    result = agent.toolbox.exec(*rg_args)
14    next result if result[:ok]
15
16    grep_args = ["grep", "-rn"]
17    grep_args += ["--include", glob] if glob
18    grep_args += [a["pattern"], path]
19    agent.toolbox.exec(*grep_args)
20  end
21end