Class: Morpheus::Cli::AliasCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/alias_command.rb

Overview

This command allows the creation of an alias these aliases are stored in the $MORPHEUS_CLI_HOME/.morpheusrc See Morpheus::Cli::DotFile

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #verify_access_token!

Constructor Details

#initializeAliasCommand

set_default_subcommand :add



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

def initialize() 
end

Instance Method Details

#add(args) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/morpheus/cli/alias_command.rb', line 38

def add(args)
  options = {}
  do_export = false
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = usage
    #build_common_options(opts, options, [])
    opts.on( '-e', '--export', "Export this alias to your .morpheus_profile for future use" ) do
      do_export = true
    end
    opts.on('-h', '--help', "Prints this help" ) do
      puts opts
      puts "Commands:"
      subcommands.sort.each {|cmd, method|
          puts "\t#{cmd.to_s}"
        }
      puts "" + 
            "This defines an alias of a command.\n" + 
            "The [name] should be a one word .\n" + 
            "The [command] must be quoted if it is more than one word.\n" + 
            "The [command] may include multiple commands, semicolon delimited.\n" + 
            #"Example: alias cloud='clouds' .\n" + 
            "Aliases can be exported for future use with the -e option.\n" + 
            "You can use just `alias` instead of `alias add`.\n" +
            "For more information, see https://github.com/gomorpheus/morpheus-cli/wiki/Alias"
      exit
    end
  end
  optparse.parse!(args)
  if args.count != 1
    puts optparse
    exit 1
  end
  alias_definition = args[0]

    
    begin
      alias_name, command_string = Morpheus::Cli::CliRegistry.parse_alias_definition(alias_definition)
      if alias_name.empty? || command_string.empty?
        print_red_alert "invalid alias syntax: #{alias_definition}"
        return false
      end
      Morpheus::Cli::CliRegistry.instance.add_alias(alias_name, command_string)
      #print "registered alias #{alias_name}", "\n"
      if do_export
        # puts "exporting alias '#{alias_name}' now..."
        morpheus_profile = Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename)
        morpheus_profile.export_aliases({(alias_name) => command_string})
      end
    rescue Morpheus::Cli::CliRegistry::BadAlias => err
      print_red_alert "#{err.message}"
      return false
    rescue => err
      # raise err  
      print_red_alert "#{err.message}"
      return false
    end

  if Morpheus::Cli::Shell.instance
    Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands()
  end

end

#export(args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/morpheus/cli/alias_command.rb', line 101

def export(args)
  options = {}
  do_export = false
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[alias] [alias2] [alias3]")
    build_common_options(opts, options, [])
    opts.footer = "Export an alias, saving it to your .morpheus_profile for future use"
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  alias_names = args
  alias_names.each do |arg|
    if !Morpheus::Cli::CliRegistry.has_alias?(arg)
      print_red_alert "alias not found by name '#{arg}'"
      return false
    end
  end
  alias_definitions = {}
  alias_names.each do |alias_name|
    alias_definitions[alias_name] = Morpheus::Cli::CliRegistry.instance.get_alias(alias_name)
  end
  morpheus_profile = Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename)
  morpheus_profile.export_aliases(alias_definitions)
  
  # Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands() if Morpheus::Cli::Shell.instance

end

#handle(args) ⇒ Object



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

def handle(args)
  if args.empty?
    puts usage
    exit 127
  elsif self.class.has_subcommand?(args[0])
    handle_subcommand(args)
  elsif args.count == 1 || (args.count == 2 && args.include?('-e'))
    add(args)
  else
    handle_subcommand(args)
    #list([])
  end
end

#list(args) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/morpheus/cli/alias_command.rb', line 170

def list(args)
  options = {format:'friendly', sort:'name'}
  do_remove = false
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    opts.on( '-f', '--format FORMAT', "The format for the output: export, json, friendly (default)." ) do |val|
      options[:format] = val
    end
    build_common_options(opts, options, [:list, :json])
    opts.footer = "This outputs a list of your defined aliases."
  end
  optparse.parse!(args)

  #my_aliases = Morpheus::Cli::CliRegistry.all_aliases
  my_aliases = Morpheus::Cli::CliRegistry.all_aliases.collect {|k,v|
    {name: k, command_string: v}
  }

  # todo: generic support :list options on a local Array
  if options[:phrase]
    # my_aliases = my_aliases.grep(/^#{Regexp.escape(options[:phrase])}/)
    match_regex = /#{Regexp.escape(options[:phrase])}/
    my_aliases = my_aliases.select {|it| 
      it[:name].to_s =~ match_regex || it[:command_string].to_s =~ match_regex
    }
  end

  options[:sort] ||= 'name'
  options[:direction] ||= 'asc'

  if options[:sort]
    if options[:sort].to_s == 'name'
      my_aliases = my_aliases.sort {|x,y| x[:name].to_s.downcase <=> y[:name].to_s.downcase }
    elsif options[:sort].to_s == 'ts'
      # just relies on the order they were registered in, heh...
      my_aliases = my_aliases.sort {|x,y| x[:command_string].to_s.downcase <=> y[:command_string].to_s.downcase }
    else
      # a-z is the default, and the best
    end
  end

  if options[:direction] == 'desc'
    my_aliases = my_aliases.reverse
  end
  if options[:offset]
    my_aliases = my_aliases.slice(options[:offset].to_i, my_aliases.size)
  end
  if options[:max]
    my_aliases = my_aliases.first(options[:max].to_i)
  end
  num_aliases = my_aliases.size
  out = ""
  if options[:json]
    options[:format] = 'json'
  end
  if options[:format] == 'json' || options[:json]
    alias_json = {}
    my_aliases.each do |it|
      alias_json[it[:name]] = it[:command_string]
    end
    out << JSON.pretty_generate({aliases: alias_json})
    out << "\n"
  elsif options[:format] == 'export' || options[:format] == 'config'
    # out << "# morpheus aliases for #{`whoami`}\n" # windows!
    #out << "# morpheus aliases\n"
    my_aliases.each do |it|
      out <<  "alias #{it[:name]}='#{it[:command_string]}'"
      out << "\n"
    end
  else 
    # friendly
    #out << cyan
    if num_aliases == 0
      #print "You have #{num_aliases} aliases defined."
      out << "Found #{num_aliases} aliases"
    elsif num_aliases == 1
      #print "You have just one alias defined."
      out <<  "Found #{num_aliases} alias"
    else
      #print "You have #{num_aliases} aliases defined."
      out <<  "Found #{num_aliases} aliases"
    end
    if options[:phrase]
      out << " matching '#{options[:phrase]}'"
    end
    out <<  "\n"
    out << reset
    my_aliases.each do |it|
      out <<  "\t#{cyan}#{it[:name]}#{reset}='#{it[:command_string]}'"
      out << "\n"
    end
    out << reset
  end
  print out
end

#remove(args) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/morpheus/cli/alias_command.rb', line 132

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[alias1] [alias2]")
    build_common_options(opts, options, [])
    opts.footer = "This is how you remove alias definitions from your .morpheus_profile" + "\n"
                  "Pass one or more alias names to remove."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  alias_names = args
  alias_names.each do |arg|
    if !Morpheus::Cli::CliRegistry.has_alias?(arg)
      print_red_alert "alias not found by name '#{arg}'"
      return false
    end
  end
  morpheus_profile = Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename)
  morpheus_profile.remove_aliases(alias_names)

  # unregister them
  alias_names.each do |alias_name|
    Morpheus::Cli::CliRegistry.instance.remove_alias(alias_name)
  end

  # if args.count == 1
  #   puts "removed alias '#{alias_names[0]}'"
  # else
  #   puts "removed aliases '#{alias_names.join(', ')}'"
  # end

  Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands() if Morpheus::Cli::Shell.instance

end

#usageObject



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

def usage
  out = "Usage: morpheus #{command_name} [name]='[command]'"
  out
end