vagrant/lib/vagrant/cli.rb

122 lines
4.1 KiB
Ruby
Raw Normal View History

2011-12-17 17:14:05 +00:00
require 'log4r'
2011-12-17 16:18:43 +00:00
require 'optparse'
require 'vagrant/util/experimental'
2010-08-24 06:44:42 +00:00
module Vagrant
# Manages the command line interface to Vagrant.
2012-11-07 05:05:14 +00:00
class CLI < Vagrant.plugin("2", :command)
2018-01-16 22:15:43 +00:00
def initialize(argv, env)
2011-12-17 17:14:05 +00:00
super
@logger = Log4r::Logger.new("vagrant::cli")
@main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
2011-12-17 17:14:05 +00:00
if Vagrant::Util::Experimental.feature_enabled?("typed_triggers")
2019-03-21 17:11:38 +00:00
ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant")
@triggers = Vagrant::Plugin::V2::Trigger.new(env, env.vagrantfile.config.trigger, nil, ui)
end
2018-01-16 22:15:43 +00:00
Util::CheckpointClient.instance.setup(env).check
2011-12-17 17:14:05 +00:00
@logger.info("CLI: #{@main_args.inspect} #{@sub_command.inspect} #{@sub_args.inspect}")
end
2010-08-25 23:09:51 +00:00
def execute
if @main_args.include?("-h") || @main_args.include?("--help")
2011-12-17 16:18:43 +00:00
# Help is next in short-circuiting everything. Print
# the help and exit.
help
return 0
2010-08-25 23:09:51 +00:00
end
2011-12-17 17:14:05 +00:00
if @sub_command == "login"
$stderr.puts "WARNING: This command has been deprecated and aliased to `vagrant cloud auth login`"
end
2011-12-17 17:14:05 +00:00
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_plugin = nil
if @sub_command
command_plugin = Vagrant.plugin("2").manager.commands[@sub_command.to_sym]
2018-02-11 00:55:36 +00:00
if !command_plugin
alias_command = Alias.new(@env).commands[@sub_command.to_sym]
if alias_command
return alias_command.call(@sub_args)
end
end
2012-04-19 20:59:48 +00:00
end
if !command_plugin || !@sub_command
help
return 1
end
command_class = command_plugin[0].call
2011-12-17 17:14:05 +00:00
@logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
2018-01-16 22:15:43 +00:00
Util::CheckpointClient.instance.display
# Initialize and execute the command class, returning the exit status.
2014-01-14 05:42:40 +00:00
result = 0
begin
@triggers.fire_triggers(@sub_command.to_sym, :before, nil, :command) if Vagrant::Util::Experimental.feature_enabled?("typed_triggers")
2014-01-14 05:42:40 +00:00
result = command_class.new(@sub_args, @env).execute
@triggers.fire_triggers(@sub_command.to_sym, :after, nil, :command) if Vagrant::Util::Experimental.feature_enabled?("typed_triggers")
2014-01-14 05:42:40 +00:00
rescue Interrupt
@env.ui.info(I18n.t("vagrant.cli_interrupt"))
result = 1
end
result = 0 if !result.is_a?(Integer)
return result
2010-08-24 06:44:42 +00:00
end
2011-12-17 16:18:43 +00:00
2011-12-17 17:14:05 +00:00
# This prints out the help for the CLI.
2011-12-17 16:18:43 +00:00
def help
# We use the optionparser for this. Its just easier. We don't use
# an optionparser above because I don't think the performance hits
# of creating a whole object are worth checking only a couple flags.
opts = OptionParser.new do |o|
2014-02-08 08:20:50 +00:00
o.banner = "Usage: vagrant [options] <command> [<args>]"
o.separator ""
o.on("-v", "--version", "Print the version and exit.")
o.on("-h", "--help", "Print this help.")
o.separator ""
2014-02-08 08:20:50 +00:00
o.separator "Common commands:"
2011-12-17 20:02:19 +00:00
# Add the available subcommands as separators in order to print them
# out as well.
commands = {}
longest = 0
Vagrant.plugin("2").manager.commands.each do |key, data|
# Skip non-primary commands. These only show up in extended
# help output.
next if !data[1][:primary]
key = key.to_s
klass = data[0].call
commands[key] = klass.synopsis
longest = key.length if key.length > longest
2012-04-19 20:59:48 +00:00
end
2011-12-17 20:02:19 +00:00
commands.keys.sort.each do |key|
o.separator " #{key.ljust(longest+2)} #{commands[key]}"
@env.ui.machine("cli-command", key.dup)
2011-12-17 20:02:19 +00:00
end
o.separator ""
o.separator "For help on any individual command run `vagrant COMMAND -h`"
2014-01-11 17:15:14 +00:00
o.separator ""
o.separator "Additional subcommands are available, but are either more advanced"
o.separator "or not commonly used. To see all subcommands, run the command"
o.separator "`vagrant list-commands`."
2011-12-17 16:18:43 +00:00
end
@env.ui.info(opts.help, prefix: false)
2011-12-17 16:18:43 +00:00
end
2010-08-24 06:44:42 +00:00
end
end