Module: Smartdc::CliHelper
- Included in:
- Smartdc::Cli::Analytic, Smartdc::Cli::Datacenter, Smartdc::Cli::Image, Smartdc::Cli::Key, Smartdc::Cli::Machine, Smartdc::Cli::MachineMetadata, Smartdc::Cli::MachineSnapshot, Smartdc::Cli::MachineTag, Smartdc::Cli::Package
- Defined in:
- lib/smartdc/cli_helper.rb
Instance Method Summary collapse
- #describe(name, content, options) ⇒ Object
- #horizontal(content, options = {}) ⇒ Object
- #output(response, options = {}) ⇒ Object
- #use_machine(id) ⇒ Object
- #vertical(content, options = {}) ⇒ Object
Instance Method Details
#describe(name, content, options) ⇒ Object
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 |
# File 'lib/smartdc/cli_helper.rb', line 80 def describe(name, content, ) if ['all'] or ['output'] == name [:exclude] ||= [] rows = [] content[name].each do |row| cols = [] if [:cols] == :all [:exclude].each do |col| row.delete(col.to_s) end cols = row.values else [:cols].each do |col| if col == 'key' cols << row[0] else cols << row[1][col] end end end rows << cols end title = ['all'] ? name : nil headings = [:cols] == :all ? content[name][0].keys : [:cols] puts Terminal::Table.new :title => title, :headings => headings, :rows => rows puts if ['all'] end end |
#horizontal(content, options = {}) ⇒ Object
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 |
# File 'lib/smartdc/cli_helper.rb', line 40 def horizontal(content, ={}) [:include] ||= [] [:exclude] ||= [] rows = [] headings = nil content.each do |row| [:exclude].each do |col| row.delete(col.to_s) end if ![:include].empty? cols = {} [:include].each do |col| cols[col.to_s] = row[col.to_s] if row.key?(col.to_s) end row = cols end rows << row.values headings = row.keys if headings.nil? end Terminal::Table.new :headings => headings, :rows => rows if !content.empty? end |
#output(response, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/smartdc/cli_helper.rb', line 15 def output(response, ={}) if ['raw'] puts response.body else if response.status < 300 if [:table] case [:table] when :horizontal, :h puts horizontal(response.content, ) when :vertical, :v puts vertical(response.content, ) end else if [:message] puts [:message] else puts response.content[[:only].to_s] end end else puts vertical(response.content) end end end |
#use_machine(id) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/smartdc/cli_helper.rb', line 5 def use_machine(id) if id.nil? clicfg = Smartdc::CliConfigure.new sdccfg = clicfg.read sdccfg[:use_machine] else id end end |
#vertical(content, options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/smartdc/cli_helper.rb', line 66 def vertical(content, ={}) [:exclude] ||= [] [:exclude].each do |col| content.delete(col.to_s) end rows = [] content.to_a.each do |row| rows << row end Terminal::Table.new :headings => ['key','value'], :rows => rows if !content.empty? end |