Sha256: ebcf91d7653d7e74af11cd11c4d13232a30ed85f0a034a3fcef73d27ebb0acac

Contents?: true

Size: 1.41 KB

Versions: 31

Compression:

Stored size: 1.41 KB

Contents

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

    # This returns all the registered alias commands.
    def commands
      @aliases
    end

    # This interprets a raw line from the aliases file.
    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

    # This registers an alias.
    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
  end
end

Version data entries

31 entries across 27 versions & 4 rubygems

Version Path
vagrant-unbundled-2.3.6.0 lib/vagrant/alias.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/lib/vagrant/alias.rb
vagrant-unbundled-2.3.3.0 lib/vagrant/alias.rb
vagrant-unbundled-2.3.2.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.19.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.18.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.16.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.14.0 lib/vagrant/alias.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/lib/vagrant/alias.rb
vagrant-unbundled-2.2.10.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.9.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.8.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.7.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.6.2 lib/vagrant/alias.rb
vagrant-unbundled-2.2.6.1 lib/vagrant/alias.rb
vagrant-unbundled-2.2.6.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.5.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.4.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.3.0 lib/vagrant/alias.rb
vagrant-unbundled-2.2.2.0 lib/vagrant/alias.rb