lib/winrm-fs/file_manager.rb in winrm-fs-1.1.1 vs lib/winrm-fs/file_manager.rb in winrm-fs-1.2.0

- old
+ new

@@ -1,6 +1,5 @@ -# encoding: UTF-8 # # Copyright 2015 Shawn Neal <sneal@sneal.net> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -58,18 +57,18 @@ end # Downloads the specified remote file to the specified local path # @param [String] The full path on the remote machine # @param [String] The full path to write the file to locally - def download(remote_path, local_path) + 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) } - return false if output.exitcode != 0 contents = output.stdout.gsub('\n\r', '') - out = Base64.decode64(contents) - IO.binwrite(local_path, out) + 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 # Checks to see if the given path exists on the target file system. # @param [String] The full path to the directory or file @@ -83,11 +82,11 @@ # Gets the current user's TEMP directory on the remote system, for example # 'C:/Windows/Temp' # @return [String] Full path to the temp directory def temp_dir @guest_temp ||= begin - (@connection.shell(:powershell) { |e| e.run('$env:TEMP') }).stdout.chomp.gsub('\\', '/') + (@connection.shell(:powershell) { |e| e.run('$env:TEMP') }).stdout.chomp.tr('\\', '/') end end # Upload one or more local files and directories to a remote directory # @example copy a single file to a winrm endpoint @@ -109,9 +108,20 @@ # @return [Fixnum] The total number of bytes copied 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 + + private + + def download_dir(remote_path, local_path, first) + local_path = File.join(local_path, File.basename(remote_path)) if first + FileUtils.mkdir_p(local_path) unless File.directory?(local_path) + command = "Get-ChildItem #{remote_path} | Select-Object Name" + @connection.shell(:powershell) { |e| e.run(command) }.stdout.strip.split(/\n/).drop(2).each do |file| + download(File.join(remote_path.to_s, file.strip), File.join(local_path, file.strip), first = false) end end end end end