Class: HaveAPI::CLI::Cli
- Inherits:
-
Object
- Object
- HaveAPI::CLI::Cli
- Defined in:
- lib/haveapi/cli/cli.rb
Class Attribute Summary collapse
-
.auth_methods ⇒ Object
Returns the value of attribute auth_methods.
Class Method Summary collapse
Instance Method Summary collapse
- #api_url ⇒ Object
- #authenticate(action) ⇒ Object
- #describe_resource(path) ⇒ Object
- #format_output(action, response, out = $>) ⇒ Object
- #header_for(action, param) ⇒ Object
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #list_actions(v = nil) ⇒ Object
- #list_auth(v = nil) ⇒ Object
- #list_resources(v = nil) ⇒ Object
- #list_versions ⇒ Object
- #nested_resource(prefix, children, actions = false) ⇒ Object
- #options ⇒ Object
- #param_option(name, p) ⇒ Object
- #parameters(action) ⇒ Object
- #params_valid?(action) ⇒ Boolean
- #print_examples(action) ⇒ Object
- #show_version ⇒ Object
Constructor Details
#initialize ⇒ Cli
Returns a new instance of Cli.
23 24 25 26 27 28 29 30 31 32 33 34 35 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/haveapi/cli/cli.rb', line 23 def initialize @config = read_config || {} args, @opts = @api = HaveAPI::Client::Communicator.new(api_url, @opts[:version]) @api.identity = $0.split('/').last if @action method(@action.first).call( * @action[1..-1] ) exit end if (@opts[:help] && args.empty?) || args.empty? puts @global_opt.help puts "\nAvailable resources:" list_resources exit(true) end resources = args[0].split('.') if args.count == 1 describe_resource(resources) exit(true) end action = @api.get_action(resources, args[1].to_sym, args[2..-1]) action.update_description(@api.describe_action(action)) if authenticate(action) @input_params = parameters(action) if action unless params_valid?(action) warn 'Missing required parameters' end ret = action.execute(@input_params, raw: @opts[:raw]) if ret[:status] format_output(action, ret[:response]) else warn "Action failed: #{ret[:message]}" if ret[:errors] && ret[:errors].any? puts 'Errors:' ret[:errors].each do |param, e| puts "\t#{param}: #{e.join('; ')}" end end print_examples(action) end else warn "Action #{ARGV[0]}##{ARGV[1]} not valid" exit(false) end end |
Class Attribute Details
.auth_methods ⇒ Object
Returns the value of attribute auth_methods.
11 12 13 |
# File 'lib/haveapi/cli/cli.rb', line 11 def auth_methods @auth_methods end |
Class Method Details
.register_auth_method(name, klass) ⇒ Object
17 18 19 20 |
# File 'lib/haveapi/cli/cli.rb', line 17 def register_auth_method(name, klass) @auth_methods ||= {} @auth_methods[name] = klass end |
.run ⇒ Object
13 14 15 |
# File 'lib/haveapi/cli/cli.rb', line 13 def run c = new end |
Instance Method Details
#api_url ⇒ Object
83 84 85 |
# File 'lib/haveapi/cli/cli.rb', line 83 def api_url @opts[:client] end |
#authenticate(action) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/haveapi/cli/cli.rb', line 366 def authenticate(action) if action.auth? if @auth @auth.communicator = @api @auth.validate @auth.authenticate if @opts[:save] cfg = server_config(api_url) cfg[:auth][@opts[:auth]] = @auth.save cfg[:last_auth] = @opts[:auth] write_config end else # FIXME: exit as auth is needed and has not been selected end return true end false end |
#describe_resource(path) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/haveapi/cli/cli.rb', line 258 def describe_resource(path) desc = @api.describe_resource(path) unless desc warn "Resource #{path.join('.')} does not exist" exit(false) end unless desc[:resources].empty? puts 'Resources:' desc[:resources].each_key do |r| puts " #{r}" end end puts '' if !desc[:resources].empty? && !desc[:actions].empty? unless desc[:actions].empty? puts 'Actions:' desc[:actions].each_key do |a| puts " #{a}" end end end |
#format_output(action, response, out = $>) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/haveapi/cli/cli.rb', line 306 def format_output(action, response, out = $>) if @opts[:raw] puts response return end return if response.empty? tp.set :io, out namespace = action.namespace(:output).to_sym case action.output_layout.to_sym when :object_list, :hash_list cols = [] action.params.each do |name, p| if p[:type] == 'Resource' cols << { name => { display_method: ->(r) { r[name] && "#{r[name][p[:value_label].to_sym]} (##{r[name][p[:value_id].to_sym]})" } } } else cols << name end end tp response[namespace], *cols when :object, :hash response[namespace].each do |k, v| if action.params[k][:type] == 'Resource' out << "#{k}: #{v[action.params[k][:value_label].to_sym]}\n" else out << "#{k}: #{v}\n" end end when :custom PP.pp(response[namespace], out) end end |
#header_for(action, param) ⇒ Object
356 357 358 359 360 361 362 363 364 |
# File 'lib/haveapi/cli/cli.rb', line 356 def header_for(action, param) params = action.params if params.has_key?(param) && params[param][:label] params[param][:label] else param.to_s.upcase end end |
#list_actions(v = nil) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/haveapi/cli/cli.rb', line 246 def list_actions(v=nil) desc = @api.describe_api(v) desc[:resources].each do |resource, children| nested_resource(resource, children, true) end end |
#list_auth(v = nil) ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/haveapi/cli/cli.rb', line 230 def list_auth(v=nil) desc = @api.describe_api(v) desc[:authentication].each_key do |auth| puts auth if Cli.auth_methods.has_key?(auth) end end |
#list_resources(v = nil) ⇒ Object
238 239 240 241 242 243 244 |
# File 'lib/haveapi/cli/cli.rb', line 238 def list_resources(v=nil) desc = @api.describe_api(v) desc[:resources].each do |resource, children| nested_resource(resource, children, false) end end |
#list_versions ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/haveapi/cli/cli.rb', line 218 def list_versions desc = @api.available_versions desc[:versions].each do |v, _| next if v == :default v_int = v.to_s.to_i puts "#{v_int == desc[:default] ? '*' : ' '} v#{v}" end end |
#nested_resource(prefix, children, actions = false) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/haveapi/cli/cli.rb', line 285 def nested_resource(prefix, children, actions=false) if actions children[:actions].each do |action, _| puts "#{prefix}##{action}" end else puts prefix end children[:resources].each do |resource, children| nested_resource("#{prefix}.#{resource}", children, actions) end end |
#options ⇒ Object
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 113 114 115 116 117 118 119 120 121 122 123 124 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 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/haveapi/cli/cli.rb', line 87 def = { client: default_url, verbose: false, } @global_opt = OptionParser.new do |opts| opts. = "Usage: #{$0} [options] <resource> <action> [objects ids] [-- [parameters]]" opts.on('-u', '--api URL', 'API URL') do |url| [:client] = url end opts.on('-a', '--auth METHOD', Cli.auth_methods.keys, 'Authentication method') do |m| [:auth] = m @auth = Cli.auth_methods[m].new(server_config([:client])[:auth][m]) @auth.(opts) end opts.on('--list-versions', 'List all available API versions') do @action = [:list_versions] end opts.on('--list-auth-methods [VERSION]', 'List available authentication methods') do |v| @action = [:list_auth, v && v.sub(/^v/, '')] end opts.on('--list-resources [VERSION]', 'List all resource in API version') do |v| @action = [:list_resources, v && v.sub(/^v/, '')] end opts.on('--list-actions [VERSION]', 'List all resources and actions in API version') do |v| @action = [:list_actions, v && v.sub(/^v/, '')] end opts.on('--version VERSION', 'Use specified API version') do |v| [:version] = v end opts.on('-r', '--raw', 'Print raw response as is') do [:raw] = true end opts.on('-s', '--save', 'Save credentials to config file for later use') do [:save] = true end opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v| [:verbose] = v end opts.on('--client-version', 'Show client version') do @action = [:show_version] end opts.on('-h', '--help', 'Show this message') do [:help] = true end end args = [] ARGV.each do |arg| if arg == '--' break else args << arg end end @global_opt.parse!(args) unless [:auth] cfg = server_config([:client]) @auth = Cli.auth_methods[cfg[:last_auth]].new(cfg[:auth][cfg[:last_auth]]) if cfg[:last_auth] end [args, ] end |
#param_option(name, p) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/haveapi/cli/cli.rb', line 204 def param_option(name, p) ret = '--' name = name.to_s.dasherize if p[:type] == 'Boolean' ret += "[no-]#{name}" else ret += "#{name} #{name.underscore.upcase}" end ret end |
#parameters(action) ⇒ Object
168 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 |
# File 'lib/haveapi/cli/cli.rb', line 168 def parameters(action) = {} sep = ARGV.index('--') @action_opt = OptionParser.new do |opts| opts. = '' action.input[:parameters].each do |name, p| opts.on(param_option(name, p), p[:description] || p[:label] || '') do |*args| [name] = args.first end end opts.on('-h', '--help', 'Show this message') do @opts[:help] = true end end if @opts[:help] puts @global_opt.help puts '' puts 'Action description:' puts action.description, "\n" print 'Action parameters:' puts @action_opt.help print_examples(action) exit end return {} unless sep @action_opt.parse!(ARGV[sep+1..-1]) end |
#params_valid?(action) ⇒ Boolean
390 391 392 |
# File 'lib/haveapi/cli/cli.rb', line 390 def params_valid?(action) true # FIXME end |
#print_examples(action) ⇒ Object
299 300 301 302 303 304 |
# File 'lib/haveapi/cli/cli.rb', line 299 def print_examples(action) unless action.examples.empty? puts "\nExamples:\n" ExampleFormatter.format_examples(self, action) end end |
#show_version ⇒ Object
254 255 256 |
# File 'lib/haveapi/cli/cli.rb', line 254 def show_version puts HaveAPI::Client::VERSION end |