Class: AwsCliWrapper::Inner

Inherits:
Struct
  • Object
show all
Defined in:
lib/aws_cli_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binding, service) ⇒ Inner

Returns a new instance of Inner.



8
9
10
# File 'lib/aws_cli_wrapper.rb', line 8

def initialize(binding, service)
  super(binding, _format_token(service))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



12
13
14
# File 'lib/aws_cli_wrapper.rb', line 12

def method_missing(meth, *args)
  _run(meth, *args)
end

Instance Attribute Details

#bindingObject

Returns the value of attribute binding

Returns:

  • (Object)

    the current value of binding



7
8
9
# File 'lib/aws_cli_wrapper.rb', line 7

def binding
  @binding
end

#serviceObject

Returns the value of attribute service

Returns:

  • (Object)

    the current value of service



7
8
9
# File 'lib/aws_cli_wrapper.rb', line 7

def service
  @service
end

Instance Method Details

#_erb(file) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/aws_cli_wrapper.rb', line 16

def _erb(file)
  path = "#{file}.json.erb"
  if File.exists?(path)
    tmp = Tempfile.new(file.gsub('/', '_') + '.json')
    tmp.write(ERB.new(File.read(path)).result(binding))
    tmp.close # flush to disk
    tmp.path
  end
end

#_format_token(s) ⇒ Object



26
27
28
# File 'lib/aws_cli_wrapper.rb', line 26

def _format_token(s)
  s.to_s.gsub(/[ _]/, '-')
end

#_run(_command, _opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aws_cli_wrapper.rb', line 30

def _run(_command, _opts = {})
  command = _format_token(_command)
  opts = _opts.inject({}){|o,(k,v)| o.update(_format_token(k) => v) }

  if file = opts.delete('json')
    path = ((tmp = erb(file)) ? tmp : file + '.json')
    opts['cli-input-json'] = 'file://' + path
  end

  sh = (
    ['aws', service, command] +
    opts.map{|(k,v)| "--#{k} '#{v}'" }
  ) * ' '
  puts sh

  out = %x{#{sh}}
  begin
    JSON.parse(out)
  rescue JSON::ParserError
    out
  end
end