Module: Crew::Context::Local
- Defined in:
- lib/crew/context/local.rb
Instance Method Summary collapse
- #cd(dir) ⇒ Object
- #reconnect! ⇒ Object
- #save_data(data, target_path) ⇒ Object
- #save_file(local_path, target_path) ⇒ Object
- #sh_raw(cmd, stdin = nil) ⇒ Object
- #sh_with_code(cmd, opts = {}) ⇒ Object
- #start_shell ⇒ Object
- #stop_shell ⇒ Object
Instance Method Details
#cd(dir) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/crew/context/local.rb', line 13 def cd(dir) @pwds.push dir @home.logger.cd dir if block_given? begin yield ensure @pwds.pop @home.logger.cd @pwds.last end end end |
#reconnect! ⇒ Object
62 63 64 |
# File 'lib/crew/context/local.rb', line 62 def reconnect! @context.reconnect! end |
#save_data(data, target_path) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/crew/context/local.rb', line 48 def save_data(data, target_path) with_callbacks do @home.logger.sh "Saving #{data.size} bytes to #{target_path}" File.open(target_path, 'w') { |f| f << data } end end |
#save_file(local_path, target_path) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/crew/context/local.rb', line 55 def save_file(local_path, target_path) with_callbacks do @home.logger.sh "Saving #{local_path} bytes to #{target_path}" system("cp #{escape(local_path)} #{escape(target_path)}") end end |
#sh_raw(cmd, stdin = nil) ⇒ Object
26 27 28 29 |
# File 'lib/crew/context/local.rb', line 26 def sh_raw(cmd, stdin = nil) stdout_str, stderr_str, status = Open3.capture3(cmd, opts) [stdout_str, stderr_str, status.exitstatus] end |
#sh_with_code(cmd, opts = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/crew/context/local.rb', line 31 def sh_with_code(cmd, opts = {}) runner = "bash -l -c %s" stdin = nil opts[:chdir] ||= File.(@pwds.last, @pwds[-2]) if opts[:sudo] enter_sudo_password stdin ||= @sudo_password opts = { stdin_data: @sudo_password } @home.logger.sh "SUDO #{cmd}" cmd = "sudo -S #{runner % escape(cmd)}" else @home.logger.sh cmd cmd = runner % escape(cmd) end sh_raw cmd, stdin end |
#start_shell ⇒ Object
4 5 6 7 |
# File 'lib/crew/context/local.rb', line 4 def start_shell @pwds ||= [Dir.pwd] super end |
#stop_shell ⇒ Object
9 10 11 |
# File 'lib/crew/context/local.rb', line 9 def stop_shell super end |