Class: WinRM::FS::FileManager
- Inherits:
-
Object
- Object
- WinRM::FS::FileManager
- Defined in:
- lib/winrm-fs/file_manager.rb
Overview
Perform file transfer operations between a local machine and winrm endpoint
Instance Method Summary collapse
-
#checksum(path) ⇒ Object
Gets the MD5 checksum of the specified file if it exists, otherwise ”.
-
#create_dir(path) ⇒ Boolean
Create the specifed directory recursively.
-
#delete(path) ⇒ Boolean
Deletes the file or directory at the specified path.
-
#download(remote_path, local_path) ⇒ Object
Downloads the specified remote file to the specified local path.
-
#exists?(path) ⇒ Boolean
Checks to see if the given path exists on the target file system.
-
#initialize(service) ⇒ FileManager
constructor
Creates a new FileManager instance.
-
#temp_dir ⇒ String
Gets the current user’s TEMP directory on the remote system, for example ‘C:/Windows/Temp’.
-
#upload(local_path, remote_path) {|Number, The, Path, Target| ... } ⇒ Fixnum
Upload one or more local files and directories to a remote directory.
Constructor Details
#initialize(service) ⇒ FileManager
Creates a new FileManager instance
27 28 29 30 |
# File 'lib/winrm-fs/file_manager.rb', line 27 def initialize(service) @service = service @logger = service.logger end |
Instance Method Details
#checksum(path) ⇒ Object
Gets the MD5 checksum of the specified file if it exists, otherwise ”
35 36 37 38 39 |
# File 'lib/winrm-fs/file_manager.rb', line 35 def checksum(path) @logger.debug("checksum: #{path}") script = WinRM::FS::Scripts.render('checksum', path: path) @service.create_executor { |e| e.run_powershell_script(script).stdout.chomp } end |
#create_dir(path) ⇒ Boolean
Create the specifed directory recursively
44 45 46 47 48 |
# File 'lib/winrm-fs/file_manager.rb', line 44 def create_dir(path) @logger.debug("create_dir: #{path}") script = WinRM::FS::Scripts.render('create_dir', path: path) @service.create_executor { |e| e.run_powershell_script(script)[:exitcode] == 0 } end |
#delete(path) ⇒ Boolean
Deletes the file or directory at the specified path
53 54 55 56 57 |
# File 'lib/winrm-fs/file_manager.rb', line 53 def delete(path) @logger.debug("deleting: #{path}") script = WinRM::FS::Scripts.render('delete', path: path) @service.create_executor { |e| e.run_powershell_script(script)[:exitcode] == 0 } end |
#download(remote_path, local_path) ⇒ Object
Downloads the specified remote file to the specified local path
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/winrm-fs/file_manager.rb', line 62 def download(remote_path, local_path) @logger.debug("downloading: #{remote_path} -> #{local_path}") script = WinRM::FS::Scripts.render('download', path: remote_path) output = @service.create_executor { |e| e.run_powershell_script(script) } return false if output[:exitcode] != 0 contents = output.stdout.gsub('\n\r', '') out = Base64.decode64(contents) IO.binwrite(local_path, out) true end |
#exists?(path) ⇒ Boolean
Checks to see if the given path exists on the target file system.
76 77 78 79 80 |
# File 'lib/winrm-fs/file_manager.rb', line 76 def exists?(path) @logger.debug("exists?: #{path}") script = WinRM::FS::Scripts.render('exists', path: path) @service.create_executor { |e| e.run_powershell_script(script)[:exitcode] == 0 } end |
#temp_dir ⇒ String
Gets the current user’s TEMP directory on the remote system, for example ‘C:/Windows/Temp’
85 86 87 88 89 |
# File 'lib/winrm-fs/file_manager.rb', line 85 def temp_dir @guest_temp ||= begin (@service.create_executor { |e| e.run_cmd('echo %TEMP%') }).stdout.chomp.gsub('\\', '/') end end |
#upload(local_path, remote_path) {|Number, The, Path, Target| ... } ⇒ Fixnum
Upload one or more local files and directories to a remote directory
109 110 111 112 113 114 |
# File 'lib/winrm-fs/file_manager.rb', line 109 def upload(local_path, remote_path, &block) @service.create_executor do |executor| file_transporter ||= WinRM::FS::Core::FileTransporter.new(executor) file_transporter.upload(local_path, remote_path, &block)[0] end end |