Class: Conductor::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/conductor/command.rb

Overview

Command runner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Command

Instantiate a command runner

Parameters:

  • command (String)

    The command



13
14
15
16
17
# File 'lib/conductor/command.rb', line 13

def initialize(command)
  parts = Shellwords.split(command)
  self.path = parts[0]
  self.args = parts[1..].join(" ")
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/conductor/command.rb', line 6

def args
  @args
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/conductor/command.rb', line 6

def path
  @path
end

Instance Method Details

#runString

Run the command

Returns:

  • (String)

    result of running STDIN through command



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/conductor/command.rb', line 55

def run
  stdin = Conductor.stdin

  raise "Command path not found" unless @path

  use_stdin = true
  if /\$\{?file\}?/.match?(args)
    use_stdin = false
    args.sub!(/\$\{?file\}?/, %("#{Env.env[:filepath]}"))
  else
    raise "No input" unless stdin

  end

  if use_stdin
    `echo #{Shellwords.escape(stdin.utf8)} | #{Env} #{path} #{args}`
  else
    `#{Env} #{path} #{args}`
  end
end