Class: Morpheus::Cli::AliasCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::AliasCommand
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, #handle_subcommand, included, #interactive?, #noninteractive, #print_usage, #subcommand_aliases, #subcommand_usage, #subcommands, #verify_access_token!
Constructor Details
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
|
# 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
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" +
"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]
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
else
begin
Morpheus::Cli::CliRegistry.instance.add_alias(alias_name, command_string)
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 => err
raise err
print_red_alert "#{err.message}"
return false
end
end
if Morpheus::Cli::Shell.instance
Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands()
end
end
|
#export(args) ⇒ Object
100
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
|
# File 'lib/morpheus/cli/alias_command.rb', line 100
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. = "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)
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
add(args)
else
handle_subcommand(args)
end
end
|
#list(args) ⇒ Object
169
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
|
# File 'lib/morpheus/cli/alias_command.rb', line 169
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. = "This outputs a list of your defined aliases."
end
optparse.parse!(args)
my_aliases = Morpheus::Cli::CliRegistry.all_aliases.collect {|k,v|
{name: k, command_string: v}
}
if 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'
my_aliases = my_aliases.sort {|x,y| x[:command_string].to_s.downcase <=> y[:command_string].to_s.downcase }
else
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'
my_aliases.each do |it|
out << "alias #{it[:name]}='#{it[:command_string]}'"
out << "\n"
end
else
if num_aliases == 0
out << "Found #{num_aliases} aliases"
elsif num_aliases == 1
out << "Found #{num_aliases} alias"
else
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
131
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
|
# File 'lib/morpheus/cli/alias_command.rb', line 131
def remove(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage("[alias1] [alias2]")
build_common_options(opts, options, [])
opts. = "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)
alias_names.each do |alias_name|
Morpheus::Cli::CliRegistry.instance.remove_alias(alias_name)
end
Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands() if Morpheus::Cli::Shell.instance
end
|
#usage ⇒ Object
19
20
21
22
|
# File 'lib/morpheus/cli/alias_command.rb', line 19
def usage
out = "Usage: morpheus #{command_name} [name]='[command]'"
out
end
|