class EpubForge::Action::CliCommand

Public Class Methods

new( command, undo = nil, opts = {} ) click to toggle source

undo is an action that would be expected to reverse the consequences of this action. You can do without it if the action can’t fail, if it has no real consequences, or if a prior action, when undone, will wipe out those consequences. For example, if an earlier command created the directory that the current command is writing a file to, the earlier command would be expected to delete the directory.

# File lib/epubforge/action/cli_command.rb, line 9
def initialize( command, undo = nil, opts = {} )
  @command = command
  @undo = undo
  @opts = opts
  @remote = opts[:remote]   # Is this going to be executed here, or on a different server?  Usually in the form "username@host"
  @verbose = opts[:verbose]
  @local_dir = opts[:local_dir]     # the local directory to cd into before executing the command
  @remote_dir = opts[:remote_dir]   # the remote directory to cd into before executing the command
end

Public Instance Methods

execute( cmd = :cmd ) click to toggle source
# File lib/epubforge/action/cli_command.rb, line 19
def execute( cmd = :cmd )
  @remote ? remote_exec( cmd ) : local_exec( cmd )
end
undo() click to toggle source
# File lib/epubforge/action/cli_command.rb, line 23
def undo
  execute( :undo )
end

Protected Instance Methods

local_exec( cmd ) click to toggle source
# File lib/epubforge/action/cli_command.rb, line 28
def local_exec( cmd )
  cmd = (cmd == :undo ? @undo : @command)
  return pseudo_success if cmd.fwf_blank?

  execute_locally = @local_dir ? "cd #{@local_dir} && " : ""
  
  @msg = "attempting to run locally:  #{cmd}"
  `#{execute_locally}#{cmd}`
  print_result
  $?  
end
print_result() click to toggle source
pseudo_success() click to toggle source
# File lib/epubforge/action/cli_command.rb, line 56
def pseudo_success
  unless @pseudo_success_object
    @pseudo_success_object = Object.new
    m = Module.new do
      def success?
        true
      end
    end
  
    @pseudo_success_object.extend( m )
  end
  
  @pseudo_success_object
end
remote_exec( cmd ) click to toggle source
# File lib/epubforge/action/cli_command.rb, line 40
def remote_exec( cmd )
  cmd = (cmd == :undo ? @undo : @command)
  return pseudo_success if cmd.fwf_blank?

  execute_remotely = (@remote_dir ? "cd #{@remote_dir} && " : "") + cmd

  @msg = "attempting to run remotely (#{@remote}):  #{execute_remotely}"
  `ssh #{@remote} "#{execute_remotely}"`
  print_result
  $?
end
success?() click to toggle source
# File lib/epubforge/action/cli_command.rb, line 60
def success?
  true
end