vagrant aliases proof-of-concept

This commit is contained in:
Zachary Flower 2018-02-10 17:55:36 -07:00
parent d075f183ac
commit bbb3cdaa9a
3 changed files with 45 additions and 0 deletions

View File

@ -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'

36
lib/vagrant/alias.rb Normal file
View File

@ -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

View File

@ -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