Class: VagrantSpec::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_spec/command/base.rb

Overview

This command plugin routes to the available subcommands

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, env) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant_spec/command/base.rb', line 12

def initialize(argv, env)
  super
  @main_args,
  @sub_command,
  @sub_args       = split_main_and_subcommand(argv)
  @valid_commands = Dir.glob(File.join(File.dirname(__FILE__), '*'))
                       .collect { |f| File.basename(f).gsub(/\.rb$/, '') }
                       .select  { |f| f != 'base' }
  @subcommands    = Vagrant::Registry.new
  @env            = env
  @opts           = nil
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



11
12
13
# File 'lib/vagrant_spec/command/base.rb', line 11

def env
  @env
end

#optsObject

Returns the value of attribute opts.



11
12
13
# File 'lib/vagrant_spec/command/base.rb', line 11

def opts
  @opts
end

#subcommandsObject

Returns the value of attribute subcommands.



11
12
13
# File 'lib/vagrant_spec/command/base.rb', line 11

def subcommands
  @subcommands
end

#valid_commandsObject

Returns the value of attribute valid_commands.



11
12
13
# File 'lib/vagrant_spec/command/base.rb', line 11

def valid_commands
  @valid_commands
end

Class Method Details

.synopsisObject



7
8
9
# File 'lib/vagrant_spec/command/base.rb', line 7

def self.synopsis
  'test deployments to clustered systems'
end

Instance Method Details

#executeObject



25
26
27
28
29
# File 'lib/vagrant_spec/command/base.rb', line 25

def execute
  register_subcommands
  return unless parse_main_args
  return unless parse_subcommand
end

#helpObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant_spec/command/base.rb', line 41

def help
  opts = OptionParser.new do |o|
    o.banner = "\nUsage: vagrant spec <command> [<args>]"
    o.separator ''
    o.separator 'Available subcommands:'
    @valid_commands.sort.each { |cmd| o.separator "     #{cmd}" }
    o.separator ''
    o.separator 'For help on any individual command run `vagrant spec ' \
                '<command> -h`'
  end
  parse_options(opts) && @opts = opts
end

#parse_main_argsObject



59
60
61
62
63
64
# File 'lib/vagrant_spec/command/base.rb', line 59

def parse_main_args
  if @main_args.include?('-h') || @main_args.include?('--help')
    return help
  end
  true
end

#parse_subcommandObject



66
67
68
69
70
71
# File 'lib/vagrant_spec/command/base.rb', line 66

def parse_subcommand
  klass = @subcommands.get(@sub_command.to_sym) if @sub_command
  return print_help if @sub_command.nil? || klass.nil?
  klass.new(@sub_args, @env).execute
  true
end


54
55
56
57
# File 'lib/vagrant_spec/command/base.rb', line 54

def print_help
  help
  @env.ui.info @opts
end

#register_subcommandsObject



31
32
33
34
35
36
37
38
39
# File 'lib/vagrant_spec/command/base.rb', line 31

def register_subcommands
  @valid_commands.each do |cmd|
    @subcommands.register cmd.to_sym do
      cmd_str = cmd.to_s
      require "vagrant_spec/command/#{cmd_str}"
      Object.const_get "VagrantSpec::Command::#{cmd_str.capitalize}"
    end
  end
end