Class: Skytap::Commands::Base
- Inherits:
-
Object
- Object
- Skytap::Commands::Base
show all
- Includes:
- Help
- Defined in:
- lib/skytap/commands/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Help
#description, #help!, #help?, included, #parameters, #synopsis, #version?
Constructor Details
#initialize(logger, args, global_options, command_options, programmatic_context = false, &invoker) ⇒ Base
Returns a new instance of Base.
14
15
16
17
18
19
20
21
|
# File 'lib/skytap/commands/base.rb', line 14
def initialize(logger, args, global_options, command_options, programmatic_context=false, &invoker)
@logger = logger
@args = args
@global_options = global_options
@command_options = command_options || {}
@programmatic_context = programmatic_context
@invoker = invoker
end
|
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
12
13
14
|
# File 'lib/skytap/commands/base.rb', line 12
def args
@args
end
|
#command_options ⇒ Object
Returns the value of attribute command_options.
12
13
14
|
# File 'lib/skytap/commands/base.rb', line 12
def command_options
@command_options
end
|
#error ⇒ Object
Returns the value of attribute error.
11
12
13
|
# File 'lib/skytap/commands/base.rb', line 11
def error
@error
end
|
#global_options ⇒ Object
Returns the value of attribute global_options.
12
13
14
|
# File 'lib/skytap/commands/base.rb', line 12
def global_options
@global_options
end
|
#invoker ⇒ Object
Returns the value of attribute invoker.
12
13
14
|
# File 'lib/skytap/commands/base.rb', line 12
def invoker
@invoker
end
|
#logger ⇒ Object
Returns the value of attribute logger.
12
13
14
|
# File 'lib/skytap/commands/base.rb', line 12
def logger
@logger
end
|
Class Method Details
.command_name ⇒ Object
48
49
50
|
# File 'lib/skytap/commands/base.rb', line 48
def command_name
name.split('::').last.underscore
end
|
.make_from(resource, spec = {}) ⇒ Object
A factory that makes a command class from the given resource
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/skytap/commands/base.rb', line 29
def make_from(resource, spec={})
spec ||= {}
Class.new(Base).tap do |klass|
klass.instance_eval do
self.container = true
self.subcommands = [Index, Show, Create, Update, Destroy].reject do |sub|
spec['skip_actions'].try(:include?, sub.command_name)
end.collect do |sub|
sub_spec = spec['actions'].try(:[], sub.command_name)
sub.make_for(self, sub_spec)
end
self.spec = spec
alias_method :run!, :help!
end
Skytap::Commands.const_set(resource.classify, klass)
end
end
|
Instance Method Details
#api_token ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/skytap/commands/base.rb', line 228
def api_token
return @_api_token if @_api_token
api_token = global_options[:'api-token']
if api_token.blank?
if solicit_user_input?
api_token = ask('API token:') while api_token.blank?
else
raise Skytap::Error.new('Must provide --api-token')
end
end
@_api_token = api_token
end
|
#ask(question, choices = {}) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/skytap/commands/base.rb', line 97
def ask(question, choices={})
return unless solicit_user_input?
default = choices.delete(:default)
raise "Default choice must be in the choices hash" if default && !choices.has_key?(default)
if choices.present?
letters = " [#{choices.collect{|l, _| l == default ? l.upcase : l.downcase}.join}]"
end
loop do
line = "#{question}#{letters} ".color(:yellow)
$stdout.print line
$stdout.flush
answer = $stdin.gets.try(&:strip)
if choices.blank?
break answer
elsif answer.blank? && default
break choices[default]
elsif choices.has_key?(answer.downcase)
break choices[answer.downcase]
end
end
end
|
#ask_param ⇒ Object
91
92
93
94
95
|
# File 'lib/skytap/commands/base.rb', line 91
def ask_param
name = ask('Name: '.bright)
value = ask('Value: '.bright)
[name, value]
end
|
#command_line_params ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/skytap/commands/base.rb', line 155
def command_line_params
param = global_options[:param]
return {} if param.blank?
case param
when Hash
param
when String
Hash[*param.split(':', 2)]
when Array
split = param.collect {|v| v.split(':', 2)}.flatten
Hash[*split]
else
param
end
end
|
#composed_params ⇒ Object
121
122
123
|
# File 'lib/skytap/commands/base.rb', line 121
def composed_params
noninteractive_params.merge(interactive_params)
end
|
#expected_args ⇒ Object
72
73
74
|
# File 'lib/skytap/commands/base.rb', line 72
def expected_args
ActiveSupport::OrderedHash.new
end
|
#expected_options ⇒ Object
Expected command-specific options (not global options)
77
78
79
|
# File 'lib/skytap/commands/base.rb', line 77
def expected_options
{}
end
|
#file_params ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/skytap/commands/base.rb', line 125
def file_params
file = global_options[:'params-file'] or return {}
file = File.expand_path(file)
raise 'Params file not found' unless File.exist?(file)
case File.basename(file)
when /\.json$/i
format = 'json'
when /\.xml$/i
format = 'xml'
else
format = global_options[:'http-format']
end
body = File.open(file, &:read)
deserialized = case format
when 'json'
JSON.load(body)
when 'xml'
parsed = Hash.from_xml(body)
if parsed.is_a?(Hash)
parsed.values.first
else
parsed
end
else
raise Skytap::Error.new("Unknown format: #{format.inspect}")
end
end
|
#find_id(arg) ⇒ Object
Returns an ID string from an URL, path or ID
82
83
84
|
# File 'lib/skytap/commands/base.rb', line 82
def find_id(arg)
arg.to_s =~ /(.*\/)?(.+)$/ && $2
end
|
#interactive_params ⇒ Object
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
|
# File 'lib/skytap/commands/base.rb', line 176
def interactive_params
if !solicit_user_input? ||
!ask_interactively ||
parameters.blank? ||
(required_parameters.empty? && noninteractive_params.present?) ||
(required_parameters.present? && (required_parameters - noninteractive_params.keys).empty?)
return {}
end
param_info = Templates::Help.new('command' => self).parameters_table
logger.info param_info << "\n"
params = {}
loop do
answer = ask('include a param?', ActiveSupport::OrderedHash['y', 'y', 'n', 'n', 'q', 'q', '?', '?', :default, 'n'])
case answer.downcase
when 'y'
k, v = ask_param
params[k] = v
when 'n'
break
when 'q'
logger.info 'Bye.'
exit
else
logger.puts "y - include an HTTP request parameter\nn - stop adding parameters (default answer)\nq - quit\n? - show this message\n\n EOF\n end\n end\n\n params\nend\n"
|
#invoke ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/skytap/commands/base.rb', line 53
def invoke
if matching_subcommand
matching_subcommand.new(logger, args[1..-1], global_options, command_options, @programmatic_context, &invoker).invoke
elsif help?
help!
elsif version?
logger.puts "skytap version #{Skytap::VERSION}"
exit
elsif container && args.present?
self.error = "Subcommand '#{args.first}' not found"
help!
exit(false)
else
validate_args
validate_command_options
run!
end
end
|
#noninteractive_params ⇒ Object
172
173
174
|
# File 'lib/skytap/commands/base.rb', line 172
def noninteractive_params
@noninteractive_params ||= file_params.merge(command_line_params)
end
|
#programmatic? ⇒ Boolean
23
24
25
|
# File 'lib/skytap/commands/base.rb', line 23
def programmatic?
!!@programmatic_context
end
|
86
87
88
|
# File 'lib/skytap/commands/base.rb', line 86
def solicit_user_input?
global_options[:ask] && !programmatic? && $stdin.tty?
end
|
#username ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/skytap/commands/base.rb', line 215
def username
return @_username if @_username
username = global_options[:username]
if username.blank?
if solicit_user_input?
username = ask('Skytap username:') while username.blank?
else
raise Skytap::Error.new('Must provide --username')
end
end
@_username = username
end
|