Class: Conductor::Script

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

Overview

Script runner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Script

Initializes the given script.

Parameters:

  • script

    The script/path



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

def initialize(script)
  parts = Shellwords.split(script)
  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/script.rb', line 6

def args
  @args
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#runString

Execute the script

Returns:

  • (String)

    script results (STDOUT)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/conductor/script.rb', line 58

def run
  stdin = Conductor.stdin unless /\$\{?file\}?/.match?(args)

  raise "Script path not defined" unless @path

  raise "Script not executable" unless File.executable?(@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