On Tue, 2010-02-16 at 17:24 +0100, Michal Fojtik wrote:
I just fixed some issue with Ruby 1.8.6 (each_char). This version should work.
There's a few issues with this:
+Options: +BANNER
- opts.on( '-i', '--id ID', 'ID for operation') { |id| options[:id] = id }
- opts.on( nil, '--image-id ID', 'Image ID') { |id| options[:image_id] = id }
- opts.on( nil, '--arch ARCH', 'Architecture (x86, x86_64)') { |id| options[:architecture] = id }
- opts.on( nil, '--flavor_id FLAVOR', 'Flavor') { |id| options[:flavor_id] = id }
- opts.on( '-l', '--list', 'List collections/operations') { |id| options[:list] = true }
I haven't tracked down the cause, but when I run 'deltacloudc -l flavors' I get no output at all - I should get a nice error message telling me that I can only do one or the other.
- opts.on( '-u', '--url', 'API url ($API_URL variable)') { |url| options[:api_url] = url }
That needs to be '--url URL' - otherwise the argument does not get passed through
+def convert_to_plain(xml)
- doc = Nokogiri::XML(xml)
- doc.root.traverse do |elem|
- yield "-----------------------" if elem.children.size>1
- next unless elem.children.size.eql?(1)
- if not elem.eql?(doc.root) and not elem.name.eql?('text')
yield "#{string_to_width(elem.name, 15)}: #{elem.content.strip}"
- end
- end
- return nil
+end
I think we need to do more than that - the output looks very funky right now.
+def string_to_width(string, width, options = {})
- options.merge!(
- :pad => ' ', :align => :left
- )
- new_string = ""
- i = 0
- string.each_byte do |c|
- break if i == width
- new_string += c.chr
- i += 1
- end
same as 'new_string = string[0,width]'
- # add padding
- if new_string.length < width
- padding = (options[:pad] * (width - new_string.length))
- new_string = options[:align] == :left ?
new_string + padding : padding + new_string
- end
- new_string
+end
You can do all this with sprintf (seeing how options[:align] is always :left, can be simplified even further):
align = options[:align] == :left ? "-" : "" sprintf "%#{align}#{width}s", string[0, width]
+rest = RestClient::Resource.new( options[:api_url] , :accept => 'application/xml' )
+doc = Nokogiri::XML(rest.get(:accept => "application/xml")) +version, driver = doc.xpath('/api/@version'), doc.xpath('/api/@driver') +collections = doc.xpath('/api/link/@rel').collect { |rel| rel.to_s }
- ....
Why is this all done custom in deltacloudc when the client library has proper methods and objects for all this ? deltacloudc should really just be a wrapper around that, and call, for example, Deltacloud.flavors etc. and then print out the returned objects in some way (you could even monkey-patch 'to_plain' methods into each of those domain classes)
David