Class: Ask::Tools::Write
- Inherits:
-
Ask::Tool
- Object
- Ask::Tool
- Ask::Tools::Write
- 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 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ask/tools/shell/write.rb', line 18 def execute(path:, content:) path = File.(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 # Never destroy what the model never saw: if Read only showed part of # this file and it hasn't changed since, the rest may hold something # the overwrite would silently erase. if Shell::FileLedger.partially_seen?(path) entry = Shell::FileLedger.entry_for(path) seen = entry.lines_seen return Ask::Result.error( message: "Refusing to overwrite #{path}: only part of the file has been read " \ "(lines #{seen.first + 1}–#{seen.last} shown, more lines exist). " \ "Re-read the full file first." ) 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 |