Class: Clacky::Tools::Write

Inherits:
Base
  • Object
show all
Defined in:
lib/clacky/tools/write.rb

Instance Method Summary collapse

Methods inherited from Base

#category, #description, #name, #parameters, #to_function_definition

Instance Method Details

#execute(path:, content:, working_dir: nil) ⇒ Object



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
# File 'lib/clacky/tools/write.rb', line 24

def execute(path:, content:, working_dir: nil)
  # Validate path
  if path.nil? || path.strip.empty?
    return { error: "Path cannot be empty" }
  end

  begin
    # Expand ~ to home directory, resolve relative paths against working_dir
    path = expand_path(path, working_dir: working_dir)

    # Ensure parent directory exists
    dir = File.dirname(path)
    FileUtils.mkdir_p(dir) unless Dir.exist?(dir)

    # Write content to file
    File.write(path, content)

    {
      path: File.expand_path(path),
      bytes_written: content.bytesize,
      error: nil
    }
  rescue Errno::EACCES => e
    { error: "Permission denied: #{e.message}" }
  rescue Errno::ENOSPC => e
    { error: "No space left on device: #{e.message}" }
  rescue StandardError => e
    { error: "Failed to write file: #{e.message}" }
  end
end

#format_call(args) ⇒ Object



55
56
57
58
# File 'lib/clacky/tools/write.rb', line 55

def format_call(args)
  path = args[:path] || args['path']
  "Write(#{Utils::PathHelper.safe_basename(path)})"
end

#format_result(result) ⇒ Object



60
61
62
63
64
65
# File 'lib/clacky/tools/write.rb', line 60

def format_result(result)
  return result[:error] if result[:error]

  bytes = result[:bytes_written] || result['bytes_written'] || 0
  "Written #{bytes} bytes"
end