"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/lib/vagrant/alias.rb" (20 Nov 2020, 1445 Bytes) of package /linux/misc/vagrant-2.2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 require "vagrant/registry"
    2 
    3 module Vagrant
    4   # This class imports and processes CLI aliases stored in ~/.vagrant.d/aliases
    5   class Alias
    6     def initialize(env)
    7       @aliases = Registry.new
    8       @env = env
    9 
   10       if env.aliases_path.file?
   11         env.aliases_path.readlines.each do |line|
   12           # separate keyword-command pairs
   13           keyword, command = interpret(line)
   14 
   15           if keyword && command
   16             register(keyword, command)
   17           end
   18         end
   19       end
   20     end
   21 
   22     # This returns all the registered alias commands.
   23     def commands
   24       @aliases
   25     end
   26 
   27     # This interprets a raw line from the aliases file.
   28     def interpret(line)
   29       # is it a comment?
   30       return nil if line.strip.start_with?("#")
   31 
   32       keyword, command = line.split("=", 2).collect(&:strip)
   33 
   34       # validate the keyword
   35       if keyword.match(/\s/i)
   36         raise Errors::AliasInvalidError, alias: line, message: "Alias keywords must not contain any whitespace."
   37       end
   38 
   39       [keyword, command]
   40     end
   41 
   42     # This registers an alias.
   43     def register(keyword, command)
   44       @aliases.register(keyword.to_sym) do
   45         lambda do |args|
   46           # directly execute shell commands
   47           if command.start_with?("!")
   48             return Util::SafeExec.exec "#{command[1..-1]} #{args.join(" ")}".strip
   49           end
   50 
   51           return CLI.new(command.split.concat(args), @env).execute
   52         end
   53       end
   54     end
   55   end
   56 end