Skip to content

Commit

Permalink
Change process and dry runners to accept command
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 10, 2017
1 parent 1f1e2f7 commit 6799dd4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lib/tty/command.rb
Expand Up @@ -148,8 +148,8 @@ def command(*args)
def execute_command(cmd)
mutex = Mutex.new
dry_run = @dry_run || cmd.options[:dry_run] || false
@runner = select_runner(@printer, dry_run)
mutex.synchronize { @runner.run(cmd) }
@runner = select_runner(dry_run).new(cmd, @printer)
mutex.synchronize { @runner.run! }
end

# @api private
Expand Down Expand Up @@ -179,11 +179,11 @@ def find_printer_class(name)
end

# @api private
def select_runner(printer, dry_run)
def select_runner(dry_run)
if dry_run
DryRunner.new(printer)
DryRunner
else
ProcessRunner.new(printer)
ProcessRunner
end
end
end # Command
Expand Down
10 changes: 8 additions & 2 deletions lib/tty/command/dry_runner.rb
Expand Up @@ -5,11 +5,17 @@
module TTY
class Command
class DryRunner
def initialize(printer)
attr_reader :cmd

def initialize(cmd, printer)
@cmd = cmd
@printer = printer
end

def run(cmd)
# Show command without running
#
# @api public
def run!
cmd.to_command
message = "#{@printer.decorate('(dry run)', :blue)} "
message << @printer.decorate(cmd.to_command, :yellow, :bold)
Expand Down
19 changes: 10 additions & 9 deletions lib/tty/command/process_runner.rb
Expand Up @@ -11,28 +11,30 @@ class Command
class ProcessRunner
include Execute

attr_reader :cmd
# Initialize a Runner object
#
# @param [Printer] printer
# the printer to use for logging
#
# @api private
def initialize(printer)
def initialize(cmd, printer)
@cmd = cmd
@printer = printer
end

# Execute child process
# @api public
def run(cmd)
def run!
@printer.print_command_start(cmd)
start = Time.now

spawn(cmd) do |pid, stdin, stdout, stderr|
stdin.write cmd.options[:data]
stdout_data, stderr_data = read_streams(cmd, stdout, stderr)
stdout_data, stderr_data = read_streams(stdout, stderr)

runtime = Time.now - start
handle_timeout(cmd, runtime, pid)
handle_timeout(runtime, pid)
status = waitpid(pid)

@printer.print_command_exit(cmd, status, runtime)
Expand All @@ -43,30 +45,29 @@ def run(cmd)

# Stop a process marked by pid
#
# @param [Cmd] cmd
# @param [Integer] pid
#
# @api public
def terminate(cmd, pid)
def terminate(pid)
signal = cmd.options[:signal] || :TERM
::Process.kill(signal, pid)
end

private

# @api private
def handle_timeout(cmd, runtime, pid)
def handle_timeout(runtime, pid)
timeout = cmd.options[:timeout]
return unless timeout

t = timeout - runtime
if t < 0.0
terminate(cmd, pid)
terminate(pid)
end
end

# @api private
def read_streams(cmd, stdout, stderr)
def read_streams(stdout, stderr)
stdout_data = ''
stderr_data = Truncator.new
timeout = cmd.options[:timeout]
Expand Down

0 comments on commit 6799dd4

Please sign in to comment.