Class: Morpheus::Cli::CliRegistry

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

Defined Under Namespace

Classes: BadAlias

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCliRegistry

Returns a new instance of CliRegistry.



10
11
12
13
# File 'lib/morpheus/cli/cli_registry.rb', line 10

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



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

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



139
140
141
# File 'lib/morpheus/cli/cli_registry.rb', line 139

def all
  instance.all
end

.all_aliasesObject



143
144
145
# File 'lib/morpheus/cli/cli_registry.rb', line 143

def all_aliases
  instance.all_aliases
end

.cli_ize(klass_name) ⇒ Object



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

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



68
69
70
# File 'lib/morpheus/cli/cli_registry.rb', line 68

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

.exec_alias(alias_name, args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/morpheus/cli/cli_registry.rb', line 83

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
  Morpheus::Logging::DarkPrinter.puts "executing alias #{alias_name} as #{all_commands.join('; ')}" 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



72
73
74
75
76
77
78
79
80
81
# File 'lib/morpheus/cli/cli_registry.rb', line 72

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)


131
132
133
134
135
136
137
# File 'lib/morpheus/cli/cli_registry.rb', line 131

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)


123
124
125
126
127
128
129
# File 'lib/morpheus/cli/cli_registry.rb', line 123

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

.instanceObject



63
64
65
# File 'lib/morpheus/cli/cli_registry.rb', line 63

def instance
  @instance ||= CliRegistry.new
end

.parse_alias_definition(input) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/morpheus/cli/cli_registry.rb', line 159

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



28
29
30
# File 'lib/morpheus/cli/cli_registry.rb', line 28

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

#add_alias(alias_name, command_string) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/morpheus/cli/cli_registry.rb', line 44

def add_alias(alias_name, command_string)
  #return @commands[alias_name.to_sym]
  if self.class.has_command?(alias_name)
    raise BadAlias.new "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 BadAlias.new "alias #{alias_name}=#{command_string} is invalid..."
  end
  @aliases[alias_name.to_sym] = command_string
end

#allObject



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

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

#all_aliasesObject



36
37
38
# File 'lib/morpheus/cli/cli_registry.rb', line 36

def all_aliases
  @aliases
end

#flushObject



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

def flush
  @commands = {}
  @aliases = {}
end

#get(cmd_name) ⇒ Object



24
25
26
# File 'lib/morpheus/cli/cli_registry.rb', line 24

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

#get_alias(alias_name) ⇒ Object



40
41
42
# File 'lib/morpheus/cli/cli_registry.rb', line 40

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

#remove(cmd_name) ⇒ Object



32
33
34
# File 'lib/morpheus/cli/cli_registry.rb', line 32

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

#remove_alias(alias_name) ⇒ Object



54
55
56
# File 'lib/morpheus/cli/cli_registry.rb', line 54

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