Class: Ask::Tools::Glob

Inherits:
Ask::Tool
  • Object
show all
Defined in:
lib/ask/tools/shell/glob.rb

Overview

Find files matching a glob pattern, newest first.

Constant Summary collapse

MAX_RESULTS =
1000

Instance Method Summary collapse

Instance Method Details

#execute(pattern:, path: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ask/tools/shell/glob.rb', line 15

def execute(pattern:, path: nil)
  base = path ? File.expand_path(path) : Dir.pwd

  unless File.directory?(base)
    return Ask::Result.error(message: "Directory does not exist: #{base}")
  end

  files = Dir.glob(pattern, base: base).map { |f| File.join(base, f) }
  files = files.select { |f| File.file?(f) }
  files = files.sort_by { |f| -File.mtime(f).to_i }
  files = files.first(MAX_RESULTS)

  if files.empty?
    return Ask::Result.error(message: "No files found matching: #{pattern}")
  end

  result = files.join("\n")
  result << "\n... (#{files.size} files shown)" if files.size == MAX_RESULTS

  Ask::Result.ok(data: result, metadata: { count: files.size, truncated: files.size == MAX_RESULTS })
end