Class: Covalence::Helpers::ShellInterpolation

Inherits:
Object
  • Object
show all
Defined in:
lib/covalence/helpers/shell_interpolation.rb

Class Method Summary collapse

Class Method Details

.parse_shell(input) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/covalence/helpers/shell_interpolation.rb', line 7

def self.parse_shell(input)
  Covalence::LOGGER.info "Evaluating requested interpolation: \"#{input}\""
  matches = input.scan(/.?\$\([^)]*\)+/)

  Covalence::LOGGER.debug "matches: #{matches}"
  matches.each do |cmd|
    if cmd[0] != "\\"
      cmd = cmd[1..-1] unless cmd[0] == "$"
      interpolated_value = Open3.capture2e(ENV, "echo \"#{cmd}\"")[0].chomp
      input = input.gsub(cmd, interpolated_value)
      Covalence::LOGGER.debug "updated value: #{input}"
    else
      input = input.gsub(cmd, cmd[1..-1])
    end
  end
  Covalence::LOGGER.info "Interpolated value: \"#{input}\""
  return input
end