Class: Vagrant::Easy::CommandBase

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/easy/command_base.rb

Overview

Base class for all easy commands. This contains the basic code that knows how to run the easy commands.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CommandBase) initialize(*args, &block)

Returns a new instance of CommandBase



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

def initialize(*args, &block)
  if self.class == CommandBase
    raise "CommandBase must not be instantiated directly. Please subclass."
  end

  # Let the regular command state setup
  super

  # Get the command we're listening to and the block we're invoking
  # when we get that command, do some basic validation.
  @command = self.class.instance_variable_get(:@command)
  @runner  = self.class.instance_variable_get(:@runner)
  if !@command || !@runner
    raise ArgumentError, "CommandBase requires both a command and a runner"
  end

  @logger  = Log4r::Logger.new("vagrant::easy_command::#{@command}")
end

Instance Attribute Details

- (Object) command (readonly)

This is the command that this easy command responds to



9
10
11
# File 'lib/vagrant/easy/command_base.rb', line 9

def command
  @command
end

Class Method Details

+ (Object) configure(name, &block)

This is called by the EasyCommand.create method when creating an easy command to set the invocation command.



13
14
15
16
17
18
19
# File 'lib/vagrant/easy/command_base.rb', line 13

def self.configure(name, &block)
  # We use class-level instance variables so that each class has
  # its own single command/runner. If we use class variables then this
  # whole base sharse a single one.
  @command = name
  @runner  = block
end

Instance Method Details

- (Object) execute



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vagrant/easy/command_base.rb', line 40

def execute
  # Build up a basic little option parser
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant #{@command}"
  end

  # Parse the options
  argv = nil
  begin
    argv = parse_options(opts)
  rescue Errors::CLIInvalidOptions
    # This means that an invalid flag such as "--foo" was passed.
    # We usually show the help at this point (in built-in commands),
    # but since we don't know what our implementation does, we just
    # pass the flags through now.
    argv = @argv.dup
  end

  # If argv is nil then `parse_options` halted execution and we
  # halt our own execution here.
  return 0 if !argv

  # The Multi-VM argument is the first argument as long as the
  # first argument is not a flag.
  names = nil
  names = argv[0] if argv[0] !~ /^-/

  # Run the action for each VM.
  @logger.info("Running easy command: #{@command}")
  with_target_vms(names) do |vm|
    @logger.debug("Running easy command for VM: #{vm.name}")
    @runner.call(CommandAPI.new(vm, argv))
  end

  # Exit status 0 every time for now
  0
end