Class: VagrantPlugins::Triggers::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-triggers/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(ui, machine, options = {}) ⇒ DSL

Returns a new instance of DSL.



8
9
10
11
12
13
# File 'lib/vagrant-triggers/dsl.rb', line 8

def initialize(ui, machine, options = {})
  @logger  = Log4r::Logger.new("vagrant::plugins::triggers::dsl")
  @machine = machine
  @options = options
  @ui      = ui
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/vagrant-triggers/dsl.rb', line 46

def method_missing(method, *args, &block)
  # If the @ui object responds to the given method, call it
  if @ui.respond_to?(method)
    @ui.send(method, *args, *block)
  else
    super(method, *args, &block)
  end
end

Instance Method Details

#error(message, *opts) ⇒ Object

Raises:



15
16
17
# File 'lib/vagrant-triggers/dsl.rb', line 15

def error(message, *opts)
  raise Errors::DSLError, @ui.error(message, *opts)
end

#run(raw_command, options = {}) ⇒ Object Also known as: execute



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-triggers/dsl.rb', line 19

def run(raw_command, options = {})
  info I18n.t("vagrant_triggers.action.trigger.executing_command", :command => raw_command)
  command     = Shellwords.shellsplit(raw_command)
  env_backup  = ENV.to_hash
  begin
    build_environment
    result = Vagrant::Util::Subprocess.execute(command[0], *command[1..-1])
  rescue Vagrant::Errors::CommandUnavailable, Vagrant::Errors::CommandUnavailableWindows
    raise Errors::CommandUnavailable, :command => command[0]
  ensure
    ENV.replace(env_backup)
  end
  if result.exit_code != 0 && !@options[:force]
    raise Errors::CommandFailed, :command => raw_command, :stderr => result.stderr
  end
  if @options[:stdout]
    info I18n.t("vagrant_triggers.action.trigger.command_output", :output => result.stdout)
  end
  result.stdout
end

#run_remote(raw_command, options = {}) ⇒ Object Also known as: execute_remote



41
42
43
# File 'lib/vagrant-triggers/dsl.rb', line 41

def run_remote(raw_command, options = {})
  run("vagrant ssh -c '#{raw_command}' #{@machine.name}", options)
end