---
 client/bin/deltacloudc        |   15 ++++---
 client/lib/plain_formatter.rb |   86 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 6 deletions(-)
 create mode 100644 client/lib/plain_formatter.rb

diff --git a/client/bin/deltacloudc b/client/bin/deltacloudc
index 7610a7f..0aa3d6e 100755
--- a/client/bin/deltacloudc
+++ b/client/bin/deltacloudc
@@ -19,7 +19,10 @@
 require 'rubygems'
 require 'optparse'
 require 'uri'
-require 'deltacloud'
+require 'lib/deltacloud'
+require 'lib/plain_formatter'
+
+include DeltaCloud::PlainFormatter
 
 options = {
   :verbose => false
@@ -66,7 +69,7 @@ options[:collection] = ARGV[0]
 options[:operation] = ARGV[1]
 
 # Connect to Deltacloud API and fetch all entry points
-client = DeltaCloud.new(url.user || ENV['API_USER'], url.password || 
ENV['API_PASSWORD'], api_url, { :verbose => options[:verbose] })
+client = DeltaCloud.new(url.user || ENV['API_USER'], url.password || 
ENV['API_PASSWORD'], api_url)
 collections = client.entry_points.keys
 
 # Exclude collection which don't have methods in client library yet
@@ -106,7 +109,7 @@ if options[:collection] and ( options[:operation].nil? or 
options[:operation].eq
   params.merge!(:id => options[:id]) if options[:id]
   params.merge!(:state => options[:state]) if options[:state]
   client.send(options[:collection].to_s, params).each do |model|
-    puts model.to_plain
+    puts format(model)
   end
   exit(0)
 end
@@ -121,7 +124,7 @@ if options[:collection] and options[:operation]
   # If collection is set and requested operation is 'show' just 'singularize'
   # collection name and print item with specified id (-i parameter)
   if options[:operation].eql?('show')
-    puts client.send(options[:collection].gsub(/s$/, ''), options[:id] 
).to_plain
+    puts format(client.send(options[:collection].gsub(/s$/, ''), options[:id]))
     exit(0)
   end
 
@@ -137,7 +140,7 @@ if options[:collection] and options[:operation]
     params.merge!(:image_id => options[:image_id]) if options[:image_id]
     params.merge!(:hwp_id => options[:hwp_id]) if options[:hwp_id]
     instance = client.create_instance(options[:image_id], params)
-    puts instance.to_plain
+    puts format(instance)
     exit(0)
   end
 
@@ -146,7 +149,7 @@ if options[:collection] and options[:operation]
     instance = client.instance(options[:id])
     instance.send("#{options[:operation]}!".to_s)
     instance = client.instance(options[:id])
-    puts instance.to_plain
+    puts format(instance)
     exit(0)
   end
 end
diff --git a/client/lib/plain_formatter.rb b/client/lib/plain_formatter.rb
new file mode 100644
index 0000000..cf26a2e
--- /dev/null
+++ b/client/lib/plain_formatter.rb
@@ -0,0 +1,86 @@
+module DeltaCloud
+  module PlainFormatter
+    module FormatObject
+
+      class Base
+        def initialize(obj)
+          @obj = obj
+        end
+      end
+
+      class Image < Base
+        def format
+          sprintf("%-10s | %-20s | %-6s | %-20s | %15s",
+              @obj.id[0,10],
+              @obj.name ? @obj.name[0, 20]: 'unknown',
+              @obj.architecture[0,6],
+              @obj.description[0,20],
+              @obj.owner_id[0,15]
+          )
+        end
+      end
+
+      class Realm < Base
+        def format
+          sprintf("%-10s | %-15s | %-5s | %10s GB",
+            @obj.id[0, 10],
+            @obj.name[0, 15],
+            @obj.state[0,5],
+            @obj.limit.to_s[0,10]
+          )
+        end
+      end
+
+      class HardwareProfile < Base
+        def format
+          sprintf("%-15s | %-6s | %10s | %10s ", @obj.id[0, 15],
+            @obj.architecture.value[0,6], @obj.memory.value.to_s[0,10], 
@obj.storage.value.to_s[0,10])
+        end
+      end
+
+      class Instance < Base
+        def format
+          sprintf("%-15s | %-15s | %-15s | %10s | %32s | %32s",
+            @obj.id ? @obj.id[0,15] : '-',
+            @obj.name ? @obj.name[0,15] : 'unknown',
+            @obj.image.name ? @obj.image.name[0,15] : 'unknown',
+            @obj.state ? @obj.state.to_s[0,10] : 'unknown',
+            @obj.public_addresses.join(',')[0,32],
+            @obj.private_addresses.join(',')[0,32]
+          )
+        end
+      end
+
+      class StorageVolume < Base
+        def format
+          sprintf("%-10s | %15s GB | %-10s | %-10s | %-15s",
+            @obj.id[0,10],
+            @obj.capacity ? @obj.capacity.to_s[0,15] : 'unknown',
+            @obj.device ? @obj.device[0,10] : 'unknown',
+            @obj.respond_to?('state') ? @obj.state[0,10] : 'unknown',
+            @obj.instance ? @obj.instance.name[0,15] : 'unknown'
+          )
+        end
+      end
+
+      class StorageSnapshot < Base
+        def format
+          sprintf("%-10s | %-15s | %-6s | %15s",
+            @obj.id[0,10],
+            @obj.storage_volume.respond_to?('name') ? 
@obj.storage_volume.name[0, 15] : 'unknown',
+            @obj.state ? @obj.state[0,10] : 'unknown',
+            @obj.created ? @obj.created[0,19] : 'unknown'
+          )
+        end
+      end
+
+    end
+
+    def format(obj)
+      object_name = obj.class.name.classify.gsub(/^DeltaCloud::/, '')
+      format_class = 
DeltaCloud::PlainFormatter::FormatObject.const_get(object_name)
+      format_class.new(obj).format
+    end
+
+  end
+end
-- 
1.7.1

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to