diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 5324c8950..f1e9c5ee6 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -83,6 +83,7 @@ require "vagrant/registry" module Vagrant autoload :Action, 'vagrant/action' + autoload :Alias, 'vagrant/alias' autoload :BatchAction, 'vagrant/batch_action' autoload :Box, 'vagrant/box' autoload :BoxCollection, 'vagrant/box_collection' diff --git a/lib/vagrant/alias.rb b/lib/vagrant/alias.rb new file mode 100644 index 000000000..1d9ef2b8e --- /dev/null +++ b/lib/vagrant/alias.rb @@ -0,0 +1,36 @@ +require "vagrant/registry" + +module Vagrant + # This class imports and processes CLI aliases stored in ~/.vagrant.d/aliases + class Alias + def initialize(env) + @aliases = Registry.new + + aliases_file = env.home_path.join("aliases") + if aliases_file.file? + aliases_file.readlines.each do |line| + # skip comments + next if line.strip.start_with?("#") + + # separate keyword-command pairs + keyword, command = line.split("=").collect(&:strip) + + @aliases.register(keyword.to_sym) do + lambda do |args| + # directly execute shell commands + if command.start_with?("!") + return exec "#{command[1..-1]} #{args.join(" ")}".strip + end + + return CLI.new(command.split.concat(args), env).execute + end + end + end + end + end + + def commands + @aliases + end + end +end diff --git a/lib/vagrant/cli.rb b/lib/vagrant/cli.rb index cca8c0184..2e4902b39 100644 --- a/lib/vagrant/cli.rb +++ b/lib/vagrant/cli.rb @@ -26,6 +26,14 @@ module Vagrant command_plugin = nil if @sub_command command_plugin = Vagrant.plugin("2").manager.commands[@sub_command.to_sym] + + if !command_plugin + alias_command = Alias.new(@env).commands[@sub_command.to_sym] + + if alias_command + return alias_command.call(@sub_args) + end + end end if !command_plugin || !@sub_command