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, digest = 'SHA1') ⇒ Object
Gets the SHA1 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, first = true) ⇒ 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(connection) ⇒ 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(connection) ⇒ FileManager
Creates a new FileManager instance
26 27 28 29 |
# File 'lib/winrm-fs/file_manager.rb', line 26 def initialize(connection) @connection = connection @logger = connection.logger end |
Instance Method Details
#checksum(path, digest = 'SHA1') ⇒ Object
Gets the SHA1 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, digest = 'SHA1') @logger.debug("checksum with #{digest}: #{path}") script = WinRM::FS::Scripts.render('checksum', path: path, digest: digest) @connection.shell(:powershell) { |e| e.run(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) @connection.shell(:powershell) { |e| e.run(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) @connection.shell(:powershell) { |e| e.run(script).exitcode == 0 } end |
#download(remote_path, local_path, first = true) ⇒ 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, first = true) @logger.debug("downloading: #{remote_path} -> #{local_path}") script = WinRM::FS::Scripts.render('download', path: remote_path) output = @connection.shell(:powershell) { |e| e.run(script) } contents = output.stdout.gsub('\n\r', '') return false if output.exitcode != 0 download_dir(remote_path, local_path, first) if contents.empty? IO.binwrite(local_path, Base64.decode64(contents)) unless contents.empty? 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) @connection.shell(:powershell) { |e| e.run(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 @temp_dir ||= begin (@connection.shell(:powershell) { |e| e.run('$env:TEMP') }).stdout.chomp.tr('\\', '/') 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) @connection.shell(:powershell) do |shell| file_transporter ||= WinRM::FS::Core::FileTransporter.new(shell) file_transporter.upload(local_path, remote_path, &block)[0] end end |