Class: HaveAPI::CLI::Cli
- Inherits:
-
Object
- Object
- HaveAPI::CLI::Cli
- Defined in:
- lib/haveapi/cli/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #api_url ⇒ Object
- #format_output(action, response) ⇒ Object
- #header_for(action, param) ⇒ Object
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #list_actions(v) ⇒ Object
- #list_resources(v) ⇒ 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
- #translate_action(action) ⇒ Object
Constructor Details
#initialize ⇒ Cli
Returns a new instance of Cli.
13 14 15 16 17 18 19 20 21 22 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 |
# File 'lib/haveapi/cli/cli.rb', line 13 def initialize args, @opts = @api = HaveAPI::Client::Communicator.new(api_url) if @action method(@action.first).call( * @action[1..-1] ) exit end if @opts[:help] || args.empty? puts @global_opt.help exit(true) end resources = args[0].split('.') action = translate_action(args[1].to_sym) action = @api.get_action(resources, action, args[2..-1]) @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].any? puts 'Errors:' ret[:errors].each do |param, e| puts "\t#{param}: #{e.join('; ')}" end end end else warn "Action #{ARGV[0]}##{ARGV[1]} not valid" exit(false) end end |
Class Method Details
.run ⇒ Object
9 10 11 |
# File 'lib/haveapi/cli/cli.rb', line 9 def self.run c = new end |
Instance Method Details
#api_url ⇒ Object
60 61 62 |
# File 'lib/haveapi/cli/cli.rb', line 60 def api_url @opts[:client] end |
#format_output(action, response) ⇒ Object
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 |
# File 'lib/haveapi/cli/cli.rb', line 227 def format_output(action, response) if @opts[:raw] puts response return end return if response.empty? s = action.structure namespace = action.namespace(:output).to_sym case action.layout.to_sym when :list tp response[namespace] when :object response[namespace].each do |k, v| puts "#{k}: #{v}" end when :custom pp response[namespace] end end |
#header_for(action, param) ⇒ Object
255 256 257 258 259 260 261 262 263 |
# File 'lib/haveapi/cli/cli.rb', line 255 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) ⇒ Object
207 208 209 210 211 |
# File 'lib/haveapi/cli/cli.rb', line 207 def list_actions(v) @api.describe_api(v)[:resources].each do |resource, children| nested_resource(resource, children, true) end end |
#list_resources(v) ⇒ Object
201 202 203 204 205 |
# File 'lib/haveapi/cli/cli.rb', line 201 def list_resources(v) @api.describe_api(v)[:resources].each do |resource, children| nested_resource(resource, children, false) end end |
#list_versions ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/haveapi/cli/cli.rb', line 189 def list_versions desc = @api.describe_api desc[:versions].each do |v, _| next if v == :default v_int = v.to_s.to_i puts "#{v_int == desc[:default_version] ? '*' : ' '} v#{v}" end end |
#nested_resource(prefix, children, actions = false) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/haveapi/cli/cli.rb', line 213 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
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 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 |
# File 'lib/haveapi/cli/cli.rb', line 64 def = { client: default_url, verbose: false, } @global_opt = OptionParser.new do |opts| opts. = "Usage: #{$0} [options] <resource> <action> [objects ids] [-- [parameters]]" opts.on('-a', '--api URL', 'API URL') do |url| [:client] = url end opts.on('--list-versions', 'List all available API versions') do @action = [:list_versions] end opts.on('--list-resources VERSION', 'List all resource in API version') do |v| @action = [:list_resources, v.sub(/^v/, '')] end opts.on('--list-actions VERSION', 'List all resources and actions in API version') do |v| @action = [:list_actions, v.sub(/^v/, '')] end opts.on('-r', '--raw', 'Print raw response as is') do [:raw] = true end opts.on('-u', '--username USER', 'User name') do |u| [:user] = u end opts.on('-p', '--password PASSWORD', 'Password') do |p| [:password] = p end opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v| [:verbose] = v 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) # p options #p ARGV [args, ] end |
#param_option(name, p) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/haveapi/cli/cli.rb', line 161 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
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 |
# File 'lib/haveapi/cli/cli.rb', line 128 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 '' print 'Action parameters:' puts @action_opt.help exit end return {} unless sep @action_opt.parse!(ARGV[sep+1..-1]) end |
#params_valid?(action) ⇒ Boolean
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/haveapi/cli/cli.rb', line 265 def params_valid?(action) if action.auth? @opts[:user] ||= ask('User name: ') { |q| q.default = nil } @opts[:password] ||= ask('Password: ') do |q| q.default = nil q.echo = false end end if action.auth? && !(@opts[:user] || @opts[:password]) return false end @api.login(@opts[:user], @opts[:password]) true end |
#translate_action(action) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/haveapi/cli/cli.rb', line 175 def translate_action(action) tr = { list: :index, new: :create, change: :update } if tr.has_key?(action) return tr[action] end action end |