Module: Kitchen::Localhost::ShellOut
- Includes:
- ShellOut
- Included in:
- Driver::Localhost, Platform::Localhost, Transport::Localhost::Connection
- Defined in:
- lib/kitchen/localhost/shell_out.rb
Overview
Take Kitchen’s ShellOut module and add support for Windows Powershell.
Instance Method Summary collapse
-
#cp_r(source, dest) ⇒ Object
Do a recursive copy of one path to another, while running the paths through PowerShell on Windows platforms to translate any PSH variables.
-
#mkdir_p(path) ⇒ Object
Do a recursive mkdir of a given path, while running the path through PowerShell on Windows platforms to translate any PSH variables.
-
#rm_rf(path) ⇒ Object
Do a recursive delete of a given path, while running the path through PowerShell on Windows platforms to translate any PSH variables.
-
#run_command(cmd) ⇒ Object
Run a given command through the regular shell on any *nix systems or PowerShell on Windows.
-
#run_command_psh(cmd) ⇒ Object
Run a given command through PowerShell, accounting for quotes and other difficult-to-account for characters by writing the command out to a temp file, executing it, and cleaning up after.
- #run_command_sh ⇒ Object
-
#windows_os? ⇒ TrueClass, FalseClass
Check whether the localhost instance is Windows.
Instance Method Details
#cp_r(source, dest) ⇒ Object
Do a recursive copy of one path to another, while running the paths through PowerShell on Windows platforms to translate any PSH variables.
73 74 75 76 77 78 79 |
# File 'lib/kitchen/localhost/shell_out.rb', line 73 def cp_r(source, dest) if windows_os? source = run_command_psh("\"#{source}\"").strip dest = run_command_psh("\"#{dest}\"").strip end FileUtils.cp_r(source, dest) end |
#mkdir_p(path) ⇒ Object
Do a recursive mkdir of a given path, while running the path through PowerShell on Windows platforms to translate any PSH variables.
98 99 100 101 |
# File 'lib/kitchen/localhost/shell_out.rb', line 98 def mkdir_p(path) path = run_command_psh("\"#{path}\"").strip if windows_os? FileUtils.mkdir_p(path) end |
#rm_rf(path) ⇒ Object
Do a recursive delete of a given path, while running the path through PowerShell on Windows platforms to translate any PSH variables.
87 88 89 90 |
# File 'lib/kitchen/localhost/shell_out.rb', line 87 def rm_rf(path) path = run_command_psh("\"#{path}\"").strip if windows_os? FileUtils.rm_rf(path) end |
#run_command(cmd) ⇒ Object
Run a given command through the regular shell on any *nix systems or PowerShell on Windows.
41 42 43 |
# File 'lib/kitchen/localhost/shell_out.rb', line 41 def run_command(cmd) windows_os? ? run_command_psh(cmd) : run_command_sh(cmd) end |
#run_command_psh(cmd) ⇒ Object
Run a given command through PowerShell, accounting for quotes and other difficult-to-account for characters by writing the command out to a temp file, executing it, and cleaning up after.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kitchen/localhost/shell_out.rb', line 54 def run_command_psh(cmd) script = Tempfile.new(%w(kitchen-localhost .ps1)) script.write(cmd) script.close begin res = run_command_sh("powershell #{script.path}") ensure script.unlink end res end |
#run_command_sh ⇒ Object
33 |
# File 'lib/kitchen/localhost/shell_out.rb', line 33 alias_method :run_command_sh, :run_command |
#windows_os? ⇒ TrueClass, FalseClass
Check whether the localhost instance is Windows
108 109 110 |
# File 'lib/kitchen/localhost/shell_out.rb', line 108 def windows_os? !RUBY_PLATFORM.match(/mswin|mingw32|windows/).nil? end |