Class: Vagrant::Json::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-json/command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject

Executes this command.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vagrant-json/command.rb', line 9

def execute

  options = {}
  opts = OptionParser.new do |opt|
    opt.banner = 'Usage: vagrant json-status'
    opt.separator ''

    opt.on('--pretty', 'Pretty print JSON.') do |p|
      options[:pretty] = true
    end
  end

  # Parse any commandline options
  argv = parse_options(opts)
  return unless argv

  entries = {
    machines: {}
  }

  # Get all vagrant machines
  @env.machine_index.each do |entry|

    entries[:machines][entry.id] = {
      name:       entry.name,
      provider:   entry.provider,
      state:      entry.state,
      directory:  entry.vagrantfile_path
    }
  end

  @env.ui.info (options[:pretty] ? JSON.pretty_generate(entries) : JSON.generate(entries))
  0
end