class Vagrant::Alias

This class imports and processes CLI aliases stored in ~/.vagrant.d/aliases

Public Class Methods

new(env) click to toggle source
# File lib/vagrant/alias.rb, line 6
def initialize(env)
  @aliases = Registry.new
  @env = env

  if env.aliases_path.file?
    env.aliases_path.readlines.each do |line|
      # separate keyword-command pairs
      keyword, command = interpret(line)

      if keyword && command
        register(keyword, command)
      end
    end
  end
end

Public Instance Methods

commands() click to toggle source

This returns all the registered alias commands.

# File lib/vagrant/alias.rb, line 23
def commands
  @aliases
end
interpret(line) click to toggle source

This interprets a raw line from the aliases file.

# File lib/vagrant/alias.rb, line 28
def interpret(line)
  # is it a comment?
  return nil if line.strip.start_with?("#")

  keyword, command = line.split("=", 2).collect(&:strip)

  # validate the keyword
  if keyword.match(/\s/i)
    raise Errors::AliasInvalidError, alias: line, message: "Alias keywords must not contain any whitespace."
  end

  [keyword, command]
end
register(keyword, command) click to toggle source

This registers an alias.

# File lib/vagrant/alias.rb, line 43
def register(keyword, command)
  @aliases.register(keyword.to_sym) do
    lambda do |args|
      # directly execute shell commands
      if command.start_with?("!")
        return Util::SafeExec.exec "#{command[1..-1]} #{args.join(" ")}".strip
      end

      return CLI.new(command.split.concat(args), @env).execute
    end
  end
end