1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
# # Helpers: Local helpers
# You don't need to invoke these helpers, they're already invoked automatically.
module Mina
module LocalHelpers
# ### local
# Executes a command.
#
# Returns nothing usually, but if `{ return: true }` is given, returns the
# STDOUT output.
#
# `options` is a hash of options:
#
# - `:pretty` - Prettify the output if true.
# - `:return` - If set to true, returns the output.
#
# Example
#
# local("ls", return: true)
def local(cmd, options = {})
script = cmd.join("\n") if cmd.is_a?(Array)
if options[:return] == true
`#{script}`
elsif simulate_mode?
Local.simulate(script)
else
result = Local.invoke(script, self)
Local.ensure_successful result, self
end
end
# ## Private methods
# `local` delegates to these.
module Local
extend self
# ### Local.simulate
# __Internal:__ Prints command.
def simulate(cmd)
str = "Executing the following:"
puts "#!/usr/bin/env bash"
puts "# #{str}"
puts "#"
puts cmd
0
end
# ### Local.invoke
# __Internal:__ Initiates an SSH session with script `script` with given
# `term_mode`. Called by `local`.
def invoke(script, this)
# Ruby 1.8.7 doesn't let you have empty symbols
term_mode = :"#{this.settings.term_mode}" if this.settings.term_mode
code = "#{script}"
# Certain environments can't do :pretty mode.
term_mode = :exec if term_mode == :pretty && !pretty_supported?
case term_mode
when :pretty
require 'shellwords'
code = Shellwords.escape(code)
this.pretty_system(code)
when :exec
Kernel.exec code
else
Kernel.system code
$?.exitstatus
end
end
# TODO: Move to concern
def pretty_supported?
# open4 is not supported under Windows.
# https://github.com/nadarei/mina/issues/58
require 'rbconfig'
! (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
end
# ### Local.ensure_successful
# __Internal:__ Halts the execution if the given result code is not
# successful (non-zero).
def ensure_successful(result, this)
this.die result if result.is_a?(Fixnum) && result > 0
result
end
end
end
end
|