Class: MasonServer::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/mason_server/command_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, command, handler) ⇒ CommandRunner

Returns a new instance of CommandRunner.



7
8
9
10
11
# File 'lib/mason_server/command_runner.rb', line 7

def initialize(directory, command, handler)
  @directory = directory
  @command   = command
  @handler   = handler
end

Class Method Details

.run(directory, command, &handler) ⇒ Object



3
4
5
# File 'lib/mason_server/command_runner.rb', line 3

def self.run(directory, command, &handler)
  new(directory, command, handler).run
end

Instance Method Details

#<<(chunk) ⇒ Object



27
28
29
# File 'lib/mason_server/command_runner.rb', line 27

def <<(chunk)
  @handler.call(chunk)
end

#fileObject



40
41
42
# File 'lib/mason_server/command_runner.rb', line 40

def file
  @tmp_file ||= @directory.join("mason#{$$}").expand_path
end

#file_contentObject



36
37
38
# File 'lib/mason_server/command_runner.rb', line 36

def file_content
  "#!/bin/sh\n#{@command}"
end

#file_writeObject



31
32
33
34
# File 'lib/mason_server/command_runner.rb', line 31

def file_write
  File.open(file, "w") { |f| f.puts file_content }
  file.chmod(0700)
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mason_server/command_runner.rb', line 13

def run
  file_write

  status = Open4.spawn(
    file.to_s,
    :dir   => @directory,
    :out   => self,
    :err   => self,
    :quiet => true
  )

  status.exitstatus
end