Class: Vagrant::UI::MachineReadable

Inherits:
Interface
  • Object
show all
Includes:
Vagrant::Util::SafePuts
Defined in:
lib/vagrant/ui.rb

Instance Attribute Summary

Attributes inherited from Interface

#opts, #stderr, #stdin, #stdout

Instance Method Summary collapse

Methods included from Vagrant::Util::SafePuts

#safe_puts

Methods inherited from Interface

#color?, #initialize_copy

Constructor Details

#initializeMachineReadable

Returns a new instance of MachineReadable.



93
94
95
96
97
# File 'lib/vagrant/ui.rb', line 93

def initialize
  super

  @lock = Mutex.new
end

Instance Method Details

#ask(*args) ⇒ Object



99
100
101
102
103
104
# File 'lib/vagrant/ui.rb', line 99

def ask(*args)
  super

  # Machine-readable can't ask for input
  raise Errors::UIExpectsTTY
end

#machine(type, *data) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vagrant/ui.rb', line 112

def machine(type, *data)
  opts = {}
  opts = data.pop if data.last.kind_of?(Hash)

  target = opts[:target] || ""

  # Prepare the data by replacing characters that aren't outputted
  data.each_index do |i|
    data[i] = data[i].to_s.dup
    data[i].gsub!(",", "%!(VAGRANT_COMMA)")
    data[i].gsub!("\n", "\\n")
    data[i].gsub!("\r", "\\r")
  end

  # Avoid locks in a trap context introduced from Ruby 2.0
  Thread.new do
    @lock.synchronize do
      safe_puts("#{Time.now.utc.to_i},#{target},#{type},#{data.join(",")}")
    end
  end.join(THREAD_MAX_JOIN_TIMEOUT)
end