Class: Daytona::FileSystem
- Inherits:
-
Object
- Object
- Daytona::FileSystem
- Includes:
- Instrumentation
- Defined in:
- lib/daytona/file_system.rb
Instance Attribute Summary collapse
-
#sandbox_id ⇒ String
readonly
The Sandbox ID.
-
#toolbox_api ⇒ DaytonaToolboxApiClient::FileSystemApi
readonly
API client for Sandbox operations.
Instance Method Summary collapse
-
#create_folder(path, mode) ⇒ void
Creates a new directory in the Sandbox at the specified path with the given permissions.
-
#delete_file(path, recursive: false) ⇒ void
Deletes a file from the Sandbox.
-
#download_file(remote_path, local_path = nil) ⇒ File?
Downloads a file from the Sandbox.
-
#find_files(path, pattern) ⇒ Array<DaytonaApiClient::Match>
Searches for files containing a pattern, similar to the grep command.
-
#get_file_info(path) ⇒ DaytonaApiClient::FileInfo
Gets detailed information about a file or directory, including its size, permissions, and timestamps.
-
#initialize(sandbox_id:, toolbox_api:, otel_state: nil) ⇒ FileSystem
constructor
Initializes a new FileSystem instance.
-
#list_files(path) ⇒ Array<DaytonaApiClient::FileInfo>
Lists files and directories in a given path and returns their information, similar to the ls -l command.
-
#move_files(source, destination) ⇒ void
Moves or renames a file or directory.
-
#replace_in_files(files:, pattern:, new_value:) ⇒ Array<DaytonaApiClient::ReplaceResult>
Performs search and replace operations across multiple files.
-
#search_files(path, pattern) ⇒ DaytonaApiClient::SearchFilesResponse
Searches for files and directories whose names match the specified pattern.
-
#set_file_permissions(path:, mode: nil, owner: nil, group: nil) ⇒ void
Sets permissions and ownership for a file or directory.
-
#upload_file(source, remote_path) ⇒ void
Uploads a file to the specified path in the Sandbox.
-
#upload_files(files) ⇒ void
Uploads multiple files to the Sandbox.
Methods included from Instrumentation
Constructor Details
#initialize(sandbox_id:, toolbox_api:, otel_state: nil) ⇒ FileSystem
Initializes a new FileSystem instance.
21 22 23 24 25 |
# File 'lib/daytona/file_system.rb', line 21 def initialize(sandbox_id:, toolbox_api:, otel_state: nil) @sandbox_id = sandbox_id @toolbox_api = toolbox_api @otel_state = otel_state end |
Instance Attribute Details
#sandbox_id ⇒ String (readonly)
Returns The Sandbox ID.
11 12 13 |
# File 'lib/daytona/file_system.rb', line 11 def sandbox_id @sandbox_id end |
#toolbox_api ⇒ DaytonaToolboxApiClient::FileSystemApi (readonly)
Returns API client for Sandbox operations.
14 15 16 |
# File 'lib/daytona/file_system.rb', line 14 def toolbox_api @toolbox_api end |
Instance Method Details
#create_folder(path, mode) ⇒ void
This method returns an undefined value.
Creates a new directory in the Sandbox at the specified path with the given permissions.
42 43 44 45 46 47 |
# File 'lib/daytona/file_system.rb', line 42 def create_folder(path, mode) Sdk.logger.debug("Creating folder #{path} with mode #{mode}") toolbox_api.create_folder(path, mode) rescue StandardError => e raise Sdk::Error, "Failed to create folder: #{e.}" end |
#delete_file(path, recursive: false) ⇒ void
This method returns an undefined value.
Deletes a file from the Sandbox.
62 63 64 65 66 |
# File 'lib/daytona/file_system.rb', line 62 def delete_file(path, recursive: false) toolbox_api.delete_file(path, { recursive: }) rescue StandardError => e raise Sdk::Error, "Failed to delete file: #{e.}" end |
#download_file(remote_path, local_path = nil) ⇒ File?
Downloads a file from the Sandbox. Returns the file contents as a string. This method is useful when you want to load the file into memory without saving it to disk. It can only be used for smaller files.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/daytona/file_system.rb', line 136 def download_file(remote_path, local_path = nil) # rubocop:disable Metrics/MethodLength file = toolbox_api.download_file(remote_path) if local_path parent_dir = File.dirname(local_path) FileUtils.mkdir_p(parent_dir) unless parent_dir == '.' File.binwrite(local_path, file.open.read) nil else file end rescue StandardError => e raise Sdk::Error, "Failed to download file: #{e.}" end |
#find_files(path, pattern) ⇒ Array<DaytonaApiClient::Match>
Searches for files containing a pattern, similar to the grep command.
229 230 231 232 233 |
# File 'lib/daytona/file_system.rb', line 229 def find_files(path, pattern) toolbox_api.find_in_files(path, pattern) rescue StandardError => e raise Sdk::Error, "Failed to find files: #{e.}" end |
#get_file_info(path) ⇒ DaytonaApiClient::FileInfo
Gets detailed information about a file or directory, including its size, permissions, and timestamps.
86 87 88 89 90 |
# File 'lib/daytona/file_system.rb', line 86 def get_file_info(path) toolbox_api.get_file_info(path) rescue StandardError => e raise Sdk::Error, "Failed to get file info: #{e.}" end |
#list_files(path) ⇒ Array<DaytonaApiClient::FileInfo>
Lists files and directories in a given path and returns their information, similar to the ls -l command.
111 112 113 114 115 |
# File 'lib/daytona/file_system.rb', line 111 def list_files(path) toolbox_api.list_files({ path: }) rescue StandardError => e raise Sdk::Error, "Failed to list files: #{e.}" end |
#move_files(source, destination) ⇒ void
This method returns an undefined value.
Moves or renames a file or directory. The parent directory of the destination must exist.
286 287 288 289 290 |
# File 'lib/daytona/file_system.rb', line 286 def move_files(source, destination) toolbox_api.move_file(source, destination) rescue StandardError => e raise Sdk::Error, "Failed to move files: #{e.}" end |
#replace_in_files(files:, pattern:, new_value:) ⇒ Array<DaytonaApiClient::ReplaceResult>
Performs search and replace operations across multiple files.
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/daytona/file_system.rb', line 317 def replace_in_files(files:, pattern:, new_value:) replace_request = DaytonaApiClient::ReplaceRequest.new( files: files, pattern: pattern, new_value: new_value ) toolbox_api.replace_in_files(replace_request) rescue StandardError => e raise Sdk::Error, "Failed to replace in files: #{e.}" end |
#search_files(path, pattern) ⇒ DaytonaApiClient::SearchFilesResponse
Searches for files and directories whose names match the specified pattern. The pattern can be a simple string or a glob pattern.
253 254 255 256 257 |
# File 'lib/daytona/file_system.rb', line 253 def search_files(path, pattern) toolbox_api.search_files(path, pattern) rescue StandardError => e raise Sdk::Error, "Failed to search files: #{e.}" end |
#set_file_permissions(path:, mode: nil, owner: nil, group: nil) ⇒ void
This method returns an undefined value.
Sets permissions and ownership for a file or directory. Any of the parameters can be nil to leave that attribute unchanged.
352 353 354 355 356 357 358 359 360 361 |
# File 'lib/daytona/file_system.rb', line 352 def (path:, mode: nil, owner: nil, group: nil) opts = {} opts[:mode] = mode if mode opts[:owner] = owner if owner opts[:group] = group if group toolbox_api.(path, opts) rescue StandardError => e raise Sdk::Error, "Failed to set file permissions: #{e.}" end |
#upload_file(source, remote_path) ⇒ void
This method returns an undefined value.
Uploads a file to the specified path in the Sandbox. If a file already exists at the destination path, it will be overwritten.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/daytona/file_system.rb', line 173 def upload_file(source, remote_path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength if source.is_a?(String) && File.exist?(source) # Source is a file path File.open(source, 'rb') { |file| toolbox_api.upload_file(remote_path, file) } elsif source.respond_to?(:read) # Source is an IO object toolbox_api.upload_file(remote_path, source) else # Source is string content - create a temporary file Tempfile.create('daytona_upload') do |file| file.binmode file.write(source) file.rewind toolbox_api.upload_file(remote_path, file) end end rescue StandardError => e raise Sdk::Error, "Failed to upload file: #{e.}" end |
#upload_files(files) ⇒ void
This method returns an undefined value.
Uploads multiple files to the Sandbox. If files already exist at the destination paths, they will be overwritten.
208 209 210 211 212 |
# File 'lib/daytona/file_system.rb', line 208 def upload_files(files) files.each { |file_upload| upload_file(file_upload.source, file_upload.destination) } rescue StandardError => e raise Sdk::Error, "Failed to upload files: #{e.}" end |