Class: Morpheus::Cli::CliRegistry

Inherits:
Object
  • Object
show all
Extended by:
Term::ANSIColor
Defined in:
lib/morpheus/cli/cli_registry.rb

Constant Summary collapse

ALIAS_SPLIT_REGEX =
/(\;)(?=(?:[^"']|"[^'"]*")*$)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCliRegistry

Returns a new instance of CliRegistry.



6
7
8
9
# File 'lib/morpheus/cli/cli_registry.rb', line 6

def initialize
  @commands = {} # this is command => Class that includes ::CliCommand
  @aliases = {} # this is alias => String full input string
end

Class Method Details

.add(klass, command_name = nil) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/morpheus/cli/cli_registry.rb', line 67

def add(klass, command_name=nil)
  klass_command_name = cli_ize(klass.name.split('::')[-1])
  if has_command?(klass_command_name)
    instance.remove(klass_command_name)
  end
  command_name ||= klass_command_name
  instance.add(command_name, klass)
end

.allObject



92
93
94
# File 'lib/morpheus/cli/cli_registry.rb', line 92

def all
  instance.all
end

.all_aliasesObject



96
97
98
# File 'lib/morpheus/cli/cli_registry.rb', line 96

def all_aliases
  instance.all_aliases
end

.cli_ize(klass_name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/morpheus/cli/cli_registry.rb', line 100

def cli_ize(klass_name)
  # borrowed from ActiveSupport
  return klass_name unless klass_name =~ /[A-Z-]|::/
  word = klass_name.to_s.gsub(/::/, '/')
  word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(?=\b|[^a-z])/) { "#{$1 && '_'}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word.chop.tr('_', '-')
end

.exec(command_name, args) ⇒ Object

todo: move execution out of the CliRegistry



21
22
23
# File 'lib/morpheus/cli/cli_registry.rb', line 21

def exec(command_name, args)
  exec_command(command_name, args)
end

.exec_alias(alias_name, args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/morpheus/cli/cli_registry.rb', line 36

def exec_alias(alias_name, args)
  #puts "exec_alias(#{alias_name}, #{args})"
  found_alias_command = instance.get_alias(alias_name)
  # support aliases of multiple commands, semicolon delimiter
  # todo: 
  all_commands = found_alias_command.gsub(ALIAS_SPLIT_REGEX, '__ALIAS_SPLIT_REGEX__').split('__ALIAS_SPLIT_REGEX__').collect {|it| it.to_s.strip }.select {|it| !it.empty?  }.compact
  print "#{dark} #=> executing alias #{alias_name} as #{all_commands.join('; ')}#{reset}\n" if Morpheus::Logging.debug?
  all_commands.each do |a_command_string|
    alias_args = a_command_string.to_s.split(/\s+/) # or just ' '
    command_name = alias_args.shift
    command_args = alias_args + args
    if command_name == alias_name
      # needs to be better than this
      print Term::ANSIColor.red,"alias '#{alias_name}' is calling itself? '#{found_alias_command}'", Term::ANSIColor.reset, "\n"
      exit 1
    end
    # this allows aliases to use other aliases
    # todo: prevent recursion infinite loop
    if has_alias?(command_name)
      exec_alias(command_name, command_args)
    elsif has_command?(command_name)
      #puts "executing alias #{found_alias_command} as #{command_name} with args #{args.join(' ')}"
      instance.get(command_name).new.handle(alias_args + args)
    else
      # raise UnrecognizedCommandError.new(command_name)
      print Term::ANSIColor.red,"alias '#{alias_name}' uses and unknown command: '#{command_name}'", Term::ANSIColor.reset, "\n"
      exit 1
    end
  end
end

.exec_command(command_name, args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/morpheus/cli/cli_registry.rb', line 25

def exec_command(command_name, args)
  #puts "exec_command(#{command_name}, #{args})"
  found_alias_command = instance.get_alias(command_name)
  if found_alias_command
    exec_alias(command_name, args)
  else
    #puts "running regular command #{command_name} with arguments #{args.join(' ')}"
    instance.get(command_name).new.handle(args)
  end
end

.has_alias?(alias_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/morpheus/cli/cli_registry.rb', line 84

def has_alias?(alias_name)
  if alias_name.nil? || alias_name == ''
    false
  else
    !instance.get_alias(alias_name).nil?
  end
end

.has_command?(command_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/morpheus/cli/cli_registry.rb', line 76

def has_command?(command_name)
  if command_name.nil? || command_name == ''
    false
  else
    !instance.get(command_name).nil?
  end
end

.instanceObject



16
17
18
# File 'lib/morpheus/cli/cli_registry.rb', line 16

def instance
  @instance ||= CliRegistry.new
end

.parse_alias_definition(input) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/morpheus/cli/cli_registry.rb', line 112

def parse_alias_definition(input)
  # todo: one multi group regex would work
  alias_name, command_string = nil, nil
  chunks = input.to_s.sub(/^alias\s+/, "").split('=')
  alias_name = chunks.shift
  command_string = chunks.compact.reject {|it| it.empty? }.join('=')
  command_string = command_string.strip.sub(/^'/, "").sub(/'\Z/, "").strip
  return alias_name, command_string
end

Instance Method Details

#add(cmd_name, klass) ⇒ Object



133
134
135
# File 'lib/morpheus/cli/cli_registry.rb', line 133

def add(cmd_name, klass)
  @commands[cmd_name.to_sym] = klass
end

#add_alias(alias_name, command_string) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/morpheus/cli/cli_registry.rb', line 149

def add_alias(alias_name, command_string)
  #return @commands[alias_name.to_sym]
  if self.class.has_command?(alias_name)
    raise "alias name '#{alias_name}' is invalid. That is the name of a morpheus command."
  elsif alias_name.to_s.downcase.strip == command_string.to_s.downcase.strip
    raise "alias #{alias_name}=#{command_string} is invalid..."
  end
  @aliases[alias_name.to_sym] = command_string
end

#allObject



125
126
127
# File 'lib/morpheus/cli/cli_registry.rb', line 125

def all
  @commands.reject {|cmd, klass| klass.hidden_command }
end

#all_aliasesObject



141
142
143
# File 'lib/morpheus/cli/cli_registry.rb', line 141

def all_aliases
  @aliases
end

#get(cmd_name) ⇒ Object



129
130
131
# File 'lib/morpheus/cli/cli_registry.rb', line 129

def get(cmd_name)
  @commands[cmd_name.to_sym]
end

#get_alias(alias_name) ⇒ Object



145
146
147
# File 'lib/morpheus/cli/cli_registry.rb', line 145

def get_alias(alias_name)
  @aliases[alias_name.to_sym]
end

#remove(cmd_name) ⇒ Object



137
138
139
# File 'lib/morpheus/cli/cli_registry.rb', line 137

def remove(cmd_name)
  @commands.delete(cmd_name.to_sym)
end

#remove_alias(alias_name) ⇒ Object



159
160
161
# File 'lib/morpheus/cli/cli_registry.rb', line 159

def remove_alias(alias_name)
  @aliases.delete(alias_name.to_sym)
end