Permalink
Browse files

Refactor the SSH helpers to be cleaner. Thanks codeclimate.

  • Loading branch information...
1 parent 415eb89 commit fb42e2548335a4ff3f82e55e447a0758927362d8 @rstacruz rstacruz committed Sep 11, 2012
Showing with 55 additions and 22 deletions.
  1. +3 −0 lib/mina/exec_helpers.rb
  2. +52 −22 lib/mina/ssh_helpers.rb
@@ -35,6 +35,9 @@ def pretty_system(code)
private
+ # ## Private methods
+ # `pretty_system` delegates to these.
+
# ### handle_sigint!(count)
# Called when a `^C` is pressed. The param `count` is how many times it's
# been pressed since. Returns nothing.
@@ -19,38 +19,20 @@ module SshHelpers
# ssh("ls", return: true)
def ssh(cmd, options={})
- cmd = cmd.join("\n") if cmd.is_a?(Array)
-
require 'shellwords'
- result = 0
+ cmd = cmd.join("\n") if cmd.is_a?(Array)
script = Shellwords.escape(cmd)
if options[:return] == true
- result = `#{ssh_command} -- #{script}`
+ `#{ssh_command} -- #{script}`
elsif simulate_mode?
- str = "Executing the following via '#{ssh_command}':"
- puts "#!/usr/bin/env bash"
- puts "# #{str}"
- puts "#"
-
- puts cmd
+ ssh_simulate cmd
else
- code = "#{ssh_command} -- #{script}"
- if settings.term_mode.to_s == 'pretty'
- result = pretty_system(code)
- elsif settings.term_mode.to_s == 'exec'
- exec code
- else
- system code
- result = $?.to_i
- end
+ ensure_successful ssh_invoke(script, settings.term_mode)
end
-
- die result if result.is_a?(Fixnum) && result > 0
- result
end
# ### ssh_command
@@ -72,5 +54,53 @@ def ssh_command
args << " -t"
"ssh #{args}"
end
+
+ private
+
+ # ## Private methods
+ # `ssh` delegates to these.
+
+ # ### ssh_simulate
+ # __Internal:__ Prints SSH command. Called by `ssh`.
+
+ def ssh_simulate(cmd)
+ str = "Executing the following via '#{ssh_command}':"
+ puts "#!/usr/bin/env bash"
+ puts "# #{str}"
+ puts "#"
+
+ puts cmd
+
+ 0
+ end
+
+ # ### ssh_invoke
+ # __Internal:__ Initiates an SSH session with script `script` with given
+ # `term_mode`. Called by `ssh`.
+
+ def ssh_invoke(script, term_mode)
+ term_mode = :"#{term_mode}"
+ code = "#{ssh_command} -- #{script}"
+
+ case term_mode
+ when :pretty
+ pretty_system(code)
+ when :exec
+ exec code
+ else
+ system code
+ $?.to_i
+ end
+ end
+
+ # ### ensure_successful
+ # __Internal:__ Halts the execution if the given result code is not
+ # successful (non-zero).
+
+ def ensure_successful(result)
+ die result if result.is_a?(Fixnum) && result > 0
+ result
+ end
+
end
end

0 comments on commit fb42e25

Please sign in to comment.