Class: Ask::Tools::Write

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

Overview

Write content to a file, creating parent directories automatically.

Constant Summary collapse

MAX_CONTENT_SIZE =
500_000

Instance Method Summary collapse

Instance Method Details

#execute(path:, content:) ⇒ Object



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

def execute(path:, content:)
  path = File.expand_path(path)

  if content.length > MAX_CONTENT_SIZE
    return Ask::Result.error(
      message: "Content too large (#{content.length} bytes). Maximum is #{MAX_CONTENT_SIZE} bytes."
    )
  end

  dir = File.dirname(path)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  bytes = File.write(path, content)
  Ask::Result.ok(data: { path: path, bytes: bytes },
                  metadata: { path: path, bytes: bytes })
end