Skip to content
This repository
  • 4 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
Jun 01, 2012
Mitchell Hashimoto New easy command APIs: argv, info, error, success
Some new APIs were added to the easy command operations. `info`,
`error`, and `success` are simple ways to output messages to the UI
without resorting to "puts" in Ruby, since the Vagrant UI object is the
idiomatic way to do communication with the world.

Additionally, `argv` was added which gives commands access to the
command-line arguments that are remaining that does not include the
vagrant binary or subcommand.

Also, behavior was changed: Previously, easy commands would run for
every target VM. Now, it is only run once with the primary VM. In the
next commit, I plan on adding a new flag that signifies an easy command
is meant to work with a named VM.
9cb5386
Mitchell Hashimoto Primary VMs are respected again.
At some point in the past, primary VMs were no longer respected for
commands. This fixes taht.
100e1f7
Mitchell Hashimoto Added `arg` and `arg_extra` API to easy commands
Easy commands can now easily get arguments from the command line.

Using `arg`, you can get named arguments on the command line, such as
"--foo" or "-f"

With `arg_extra`, you can get any remaining arguments after a "--"
on the command line, which is a common pattern used to grab longer
parameters.
83917a6
Mitchell Hashimoto Easy commands now take a target VM name
It is assumed to be the first arg, as long as the first arg doesn't
begin as a flag "-foo"
fd9ad90
6 lib/vagrant/command.rb
@@ -100,7 +100,11 @@ def with_target_vms(names=nil, options=nil)
100 100 end
101 101
102 102 # Make sure we're only working with one VM if single target
103   - raise Errors::MultiVMTargetRequired if options[:single_target] && vms.length != 1
  103 + if options[:single_target] && vms.length != 1
  104 + vm = @env.primary_vm
  105 + raise Errors::MultiVMTargetRequired if !vm
  106 + vms = [vm]
  107 + end
104 108
105 109 # If we asked for reversed ordering, then reverse it
106 110 vms.reverse! if options[:reverse]
1  lib/vagrant/easy.rb
... ... @@ -1,6 +1,7 @@
1 1 module Vagrant
2 2 module Easy
3 3 autoload :CommandBase, "vagrant/easy/command_base"
  4 + autoload :CommandAPI, "vagrant/easy/command_api"
4 5 autoload :Operations, "vagrant/easy/operations"
5 6
6 7 # This creates a new easy command. This typically is not called
88 lib/vagrant/easy/command_api.rb
... ... @@ -0,0 +1,88 @@
  1 +require "delegate"
  2 +require "optparse"
  3 +
  4 +require "vagrant/easy/operations"
  5 +
  6 +module Vagrant
  7 + module Easy
  8 + # This is the API that easy commands have access to. It is a subclass
  9 + # of Operations so it has access to all those methods as well.
  10 + class CommandAPI < DelegateClass(Operations)
  11 + attr_reader :argv
  12 +
  13 + def initialize(vm, argv)
  14 + super(Operations.new(vm))
  15 +
  16 + @argv = argv
  17 + @vm = vm
  18 + end
  19 +
  20 + # Gets the value of an argument from the command line. Many arguments
  21 + # can be given as a parameter and the first matching one will be returned.
  22 + #
  23 + # @return [String]
  24 + def arg(*names)
  25 + # Mangle the names a bit to add "=VALUE" to every flag.
  26 + names = names.map do |name|
  27 + "#{name}=VALUE"
  28 + end
  29 +
  30 + # Create a basic option parser
  31 + parser = OptionParser.new
  32 +
  33 + # Add on a matcher for this thing
  34 + result = nil
  35 + parser.on(*names) do |value|
  36 + result = value
  37 + end
  38 +
  39 + begin
  40 + parser.parse!(@argv.dup)
  41 + rescue OptionParser::MissingArgument
  42 + # Missing argument means the argument existed but had no data,
  43 + # so we mark it as an empty string
  44 + result = ""
  45 + rescue OptionParser::InvalidOption
  46 + # Ignore!
  47 + end
  48 +
  49 + # Return the results
  50 + result
  51 + end
  52 +
  53 + # Returns any extra arguments that are past a "--" on the command line.
  54 + #
  55 + # @return [String]
  56 + def arg_extra
  57 + # Split the arguments and remove the "--"
  58 + remaining = @argv.drop_while { |v| v != "--" }
  59 + remaining.shift
  60 +
  61 + # Return the remaining arguments
  62 + remaining.join(" ")
  63 + end
  64 +
  65 + # Outputs an error message to the UI.
  66 + #
  67 + # @param [String] message Message to send.
  68 + def error(message)
  69 + @vm.ui.error(message)
  70 + end
  71 +
  72 + # Outputs a normal message to the UI. Use this for any standard-level
  73 + # messages.
  74 + #
  75 + # @param [String] message Message to send.
  76 + def info(message)
  77 + @vm.ui.info(message)
  78 + end
  79 +
  80 + # Outputs a success message to the UI.
  81 + #
  82 + # @param [String] message Message to send.
  83 + def success(message)
  84 + @vm.ui.success(message)
  85 + end
  86 + end
  87 + end
  88 +end
25 lib/vagrant/easy/command_base.rb
@@ -44,14 +44,31 @@ def execute
44 44 end
45 45
46 46 # Parse the options
47   - argv = parse_options(opts)
48   - return if !argv
  47 + argv = nil
  48 + begin
  49 + argv = parse_options(opts)
  50 + rescue Errors::CLIInvalidOptions
  51 + # This means that an invalid flag such as "--foo" was passed.
  52 + # We usually show the help at this point (in built-in commands),
  53 + # but since we don't know what our implementation does, we just
  54 + # pass the flags through now.
  55 + argv = @argv.dup
  56 + end
  57 +
  58 + # If argv is nil then `parse_options` halted execution and we
  59 + # halt our own execution here.
  60 + return 0 if !argv
  61 +
  62 + # The Multi-VM argument is the first argument as long as the
  63 + # first argument is not a flag.
  64 + names = nil
  65 + names = argv[0] if argv[0] !~ /^-/
49 66
50 67 # Run the action for each VM.
51 68 @logger.info("Running easy command: #{@command}")
52   - with_target_vms(argv) do |vm|
  69 + with_target_vms(names) do |vm|
53 70 @logger.debug("Running easy command for VM: #{vm.name}")
54   - @runner.call(Operations.new(vm))
  71 + @runner.call(CommandAPI.new(vm, argv))
55 72 end
56 73
57 74 # Exit status 0 every time for now

No commit comments for this range

Something went wrong with that request. Please try again.