Class: Beaker::SedCommand

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

Instance Attribute Summary

Attributes inherited from Command

#args, #command, #environment, #options

Instance Method Summary collapse

Methods inherited from Command

#args_string, #cmd_line, #options_string

Constructor Details

#initialize(platform, expression, filename, opts = {}) ⇒ Object

sets up a SedCommand for a particular platform

the purpose is to abstract away platform-dependent details of the sed command

Parameters:

  • platform (String)

    The host platform string

  • expression (String)

    The sed expression

  • filename (String)

    The file to apply the sed expression to

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Additional options

Options Hash (opts):

  • :temp_file (String)

    The temp file to use for in-place substitution (only applies to solaris hosts, they don’t provide the -i option)



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/beaker/command.rb', line 151

def initialize platform, expression, filename, opts = {}
  command = "sed -i -e \"#{expression}\" #{filename}"
  if /solaris|aix|osx|openbsd/.match?(platform)
    command.slice! '-i '
    temp_file = opts[:temp_file] ? opts[:temp_file] : "#{filename}.tmp"
    command << " > #{temp_file} && mv #{temp_file} #{filename} && rm -f #{temp_file}"
  end
  args = []
  opts['ENV'] ||= {}
  super(command, args, opts)
end