Class: Chocomint::Tools::Glob

Inherits:
Base
  • Object
show all
Defined in:
lib/chocomint/tools/glob.rb

Overview

許可ディレクトリ内で glob パターンに一致するファイルを列挙する。

Instance Method Summary collapse

Methods inherited from Base

#to_tool_definition

Constructor Details

#initialize(path_guard:, base_dir: Dir.pwd) ⇒ Glob

Returns a new instance of Glob.



9
10
11
12
# File 'lib/chocomint/tools/glob.rb', line 9

def initialize(path_guard:, base_dir: Dir.pwd)
  @path_guard = path_guard
  @base_dir = base_dir
end

Instance Method Details

#call(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chocomint/tools/glob.rb', line 32

def call(args)
  measure do
    base_rel = args["base"] || allowed_root
    base_abs = @path_guard.resolve(base_rel)
    next fail(stderr: "no such directory: #{base_rel}") unless File.directory?(base_abs)

    matches = Dir.glob(File.join(base_abs, args["pattern"].to_s), File::FNM_PATHNAME)
                 .select { |p| File.file?(p) && @path_guard.allowed?(p) }
                 .map { |p| relative(p) }
                 .sort
    ok(stdout: matches.join("\n"))
  end
end

#descriptionObject



16
17
18
# File 'lib/chocomint/tools/glob.rb', line 16

def description
  "glob パターン (例: **/*.rb) に一致するファイルを列挙する。base は許可ディレクトリ内であること。"
end

#nameObject



14
# File 'lib/chocomint/tools/glob.rb', line 14

def name = "glob"

#schemaObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chocomint/tools/glob.rb', line 20

def schema
  {
    "type" => "object",
    "properties" => {
      "pattern" => { "type" => "string" },
      "base" => { "type" => "string" }
    },
    "required" => %w[pattern],
    "additionalProperties" => false
  }
end