hammer_cli 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 009e7aa80bff80abe875cff5efe57e351b27901e
4
- data.tar.gz: 0efeda00147b71a2a90be95c3cd2c6aa0f37a5d5
3
+ metadata.gz: 367645958584805f4e59145418616c507fd9144a
4
+ data.tar.gz: d89c9715a3b0b66dacb21708d878c12e9c542e84
5
5
  SHA512:
6
- metadata.gz: 4af2dc0f31f5dd8e2335b99d0600d209192b81d26feaa11bd68d0e5cac1745378b357e9612ae13723edb789aa288212780e304b15bffad3c938bc3b22c7591fe
7
- data.tar.gz: 02e6d0e3e9613c9252f75c96db03f20936b2250cd040da01c9ce824569d99bedcb8c3a9a40a58345316b9c5347d13ddc25679703b858949409c7fc582a8e3d68
6
+ metadata.gz: 0eb7b32bc92e6d4ebedf52c8e9334ba2acab69808967b02a6e6b92110f936fccc9ede2febfb20e970eaa994c1b05582f83b46b964aef4c2307c2f6f01d4f75d3
7
+ data.tar.gz: 28164cbd24f5a912b4de8e97e10c917a2690378750ccfd6125f1e4104ff43bea883408f2fca13b70bcbd6abda325f4470b8607b1c4626303b50d7f9bfec0f4d8
@@ -10,6 +10,8 @@
10
10
  # :watch_plain: true disables color output of logger.watch in Clamp commands
11
11
  :watch_plain: false
12
12
 
13
+
13
14
  #:log_dir: '/var/log/foreman'
14
15
  :log_dir: '~/.foreman/log'
15
16
  :log_level: 'error'
17
+ :log_api_calls: false
@@ -122,8 +122,12 @@ module HammerCLI
122
122
 
123
123
  protected
124
124
 
125
- def print_records(definition, records)
126
- output.print_records(definition, records)
125
+ def print_record(definition, record)
126
+ output.print_record(definition, record)
127
+ end
128
+
129
+ def print_collection(definition, collection)
130
+ output.print_collection(definition, collection)
127
131
  end
128
132
 
129
133
  def print_message(msg, msg_params={})
@@ -6,7 +6,7 @@ module HammerCLI::Apipie
6
6
 
7
7
  def execute
8
8
  d = retrieve_data
9
- logger.watch "Retrieved data: ", d
9
+ logger.debug "Retrieved data: " + d.ai(:raw => true) if HammerCLI::Settings.get(:log_api_calls)
10
10
  print_data d
11
11
  return HammerCLI::EX_OK
12
12
  end
@@ -18,7 +18,7 @@ module HammerCLI::Apipie
18
18
  end
19
19
 
20
20
  def print_data(records)
21
- print_records(output_definition, records)
21
+ print_collection(output_definition, records)
22
22
  end
23
23
 
24
24
  def request_params
@@ -34,7 +34,10 @@ module HammerCLI::Apipie
34
34
  end
35
35
 
36
36
  def call(method_name, params=nil)
37
- instance.send(method_name, params)
37
+ Logging.logger[resource_class.name].debug "Calling '#{method_name}' with params #{params.ai}" if HammerCLI::Settings.get(:log_api_calls)
38
+ result = instance.send(method_name, params)
39
+ Logging.logger[resource_class.name].debug "Method '#{method_name}' responded with #{result[0].ai}" if HammerCLI::Settings.get(:log_api_calls)
40
+ result
38
41
  end
39
42
 
40
43
  private
@@ -7,9 +7,7 @@ module HammerCLI::Apipie
7
7
  include HammerCLI::Messages
8
8
 
9
9
  def execute
10
- response = send_request
11
- logger.watch "Retrieved data: ", response
12
- print_success_message(response)
10
+ print_success_message(send_request)
13
11
  return HammerCLI::EX_OK
14
12
  end
15
13
 
@@ -27,7 +27,11 @@ module HammerCLI::Output::Adapter
27
27
  $stderr.puts msg.format(msg_params)
28
28
  end
29
29
 
30
- def print_records(fields, data)
30
+ def print_record(fields, record)
31
+ raise NotImplementedError
32
+ end
33
+
34
+ def print_collection(fields, collection)
31
35
  raise NotImplementedError
32
36
  end
33
37
 
@@ -8,10 +8,14 @@ module HammerCLI::Output::Adapter
8
8
  [:flat, :screen]
9
9
  end
10
10
 
11
- def print_records(fields, data)
11
+ def print_record(fields, record)
12
+ print_collection(fields, [record].flatten(1))
13
+ end
14
+
15
+ def print_collection(fields, collection)
12
16
  self.fields = fields
13
17
 
14
- data.each do |d|
18
+ collection.each do |d|
15
19
  fields.collect do |f|
16
20
  render_field(f, d)
17
21
  end
@@ -16,12 +16,16 @@ module HammerCLI::Output::Adapter
16
16
  [:flat]
17
17
  end
18
18
 
19
- def print_records(fields, data)
19
+ def print_record(fields, record)
20
+ print_collection(fields, [record].flatten(1))
21
+ end
22
+
23
+ def print_collection(fields, collection)
20
24
  csv_string = generate do |csv|
21
25
  # labels
22
26
  csv << fields.select{ |f| !(f.class <= Fields::Id) || @context[:show_ids] }.map { |f| f.label }
23
27
  # data
24
- data.each do |d|
28
+ collection.each do |d|
25
29
  csv << fields.inject([]) do |row, f|
26
30
  unless f.class <= Fields::Id && !@context[:show_ids]
27
31
  value = (f.get_value(d) || '')
@@ -9,7 +9,10 @@ module HammerCLI::Output::Adapter
9
9
  def print_error(msg, details=[], msg_params={})
10
10
  end
11
11
 
12
- def print_records(fields, data)
12
+ def print_record(fields, record)
13
+ end
14
+
15
+ def print_collection(fields, collection)
13
16
  end
14
17
 
15
18
  end
@@ -8,11 +8,16 @@ module HammerCLI::Output::Adapter
8
8
  [:screen, :flat]
9
9
  end
10
10
 
11
- def print_records(fields, data)
11
+ def print_record(fields, record)
12
+ print_collection(fields, [record].flatten(1))
13
+ end
14
+
15
+ def print_collection(fields, collection)
12
16
 
13
- rows = data.collect do |d|
17
+ rows = collection.collect do |d|
14
18
  row = {}
15
19
  fields.each do |f|
20
+
16
21
  row[f.label.to_sym] = f.get_value(d) || ""
17
22
  end
18
23
  row
@@ -16,8 +16,15 @@ module HammerCLI::Output
16
16
  adapter.print_error(msg.to_s, details, msg_params)
17
17
  end
18
18
 
19
- def print_records(definition, records)
20
- adapter.print_records(definition.fields, [records].flatten(1))
19
+ def print_record(definition, record)
20
+ adapter.print_record(definition.fields, record)
21
+ end
22
+
23
+ def print_collection(definition, collection)
24
+ unless collection.class <= HammerCLI::Output::RecordCollection
25
+ collection = HammerCLI::Output::RecordCollection.new([collection].flatten(1))
26
+ end
27
+ adapter.print_collection(definition.fields, collection)
21
28
  end
22
29
 
23
30
  def adapter
@@ -0,0 +1,35 @@
1
+ module HammerCLI::Output
2
+
3
+
4
+ class MetaData
5
+
6
+ attr_accessor :total, :subtotal, :page, :per_page, :search, :sort_by, :sort_order
7
+
8
+ def initialize(options={})
9
+ @total = options[:total]
10
+ @subtotal = options[:subtotal]
11
+ @page = options[:page]
12
+ @per_page = options[:per_page]
13
+ @search = options[:search]
14
+ @sort_by = options[:sort_by]
15
+ @sort_order = options[:sort_order]
16
+ end
17
+
18
+ end
19
+
20
+
21
+ class RecordCollection < Array
22
+
23
+ attr_accessor :meta
24
+
25
+ def initialize(data, options={})
26
+ super [data].flatten(1)
27
+ if options.has_key?(:meta) && options[:meta].class <= MetaData
28
+ @meta = options[:meta]
29
+ else
30
+ @meta = MetaData.new(options)
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -4,4 +4,5 @@ require File.join(File.dirname(__FILE__), 'output/formatters')
4
4
  require File.join(File.dirname(__FILE__), 'output/adapter')
5
5
  require File.join(File.dirname(__FILE__), 'output/definition')
6
6
  require File.join(File.dirname(__FILE__), 'output/dsl')
7
+ require File.join(File.dirname(__FILE__), 'output/record_collection')
7
8
 
@@ -1,5 +1,5 @@
1
1
  module HammerCLI
2
2
  def self.version
3
- @version ||= Gem::Version.new '0.0.11'
3
+ @version ||= Gem::Version.new '0.0.12'
4
4
  end
5
5
  end
@@ -57,8 +57,8 @@ describe HammerCLI::Output::Adapter::Abstract do
57
57
 
58
58
  end
59
59
 
60
- it "should raise not implemented on print_records" do
61
- proc { adapter.print_records([], []) }.must_raise NotImplementedError
60
+ it "should raise not implemented on print_collection" do
61
+ proc { adapter.print_collection([], HammerCLI::Output::RecordCollection.new([])) }.must_raise NotImplementedError
62
62
  end
63
63
 
64
64
  context "error messages" do
@@ -4,22 +4,22 @@ describe HammerCLI::Output::Adapter::Base do
4
4
 
5
5
  let(:adapter) { HammerCLI::Output::Adapter::Base.new }
6
6
 
7
- context "print_records" do
7
+ context "print_collection" do
8
8
 
9
9
  let(:field_name) { Fields::DataField.new(:path => [:name], :label => "Name") }
10
10
  let(:fields) {
11
11
  [field_name]
12
12
  }
13
- let(:data) {[{
13
+ let(:data) { HammerCLI::Output::RecordCollection.new [{
14
14
  :name => "John Doe"
15
15
  }]}
16
16
 
17
17
  it "should print field name" do
18
- proc { adapter.print_records(fields, data) }.must_output(/.*Name[ ]*:.*/, "")
18
+ proc { adapter.print_collection(fields, data) }.must_output(/.*Name[ ]*:.*/, "")
19
19
  end
20
20
 
21
21
  it "should print field value" do
22
- proc { adapter.print_records(fields, data) }.must_output(/.*John Doe.*/, "")
22
+ proc { adapter.print_collection(fields, data) }.must_output(/.*John Doe.*/, "")
23
23
  end
24
24
 
25
25
  end
@@ -4,24 +4,25 @@ describe HammerCLI::Output::Adapter::CSValues do
4
4
 
5
5
  let(:adapter) { HammerCLI::Output::Adapter::CSValues.new }
6
6
 
7
- context "print_records" do
7
+ context "print_collection" do
8
8
 
9
9
  let(:field_name) { Fields::DataField.new(:path => [:name], :label => "Name") }
10
10
  let(:field_started_at) { Fields::DataField.new(:path => [:started_at], :label => "Started At") }
11
11
  let(:fields) {
12
12
  [field_name, field_started_at]
13
13
  }
14
- let(:data) {[{
14
+ let(:data) { HammerCLI::Output::RecordCollection.new [{
15
15
  :name => "John Doe",
16
16
  :started_at => "2000"
17
17
  }]}
18
18
 
19
+
19
20
  it "should print column name" do
20
- proc { adapter.print_records(fields, data) }.must_output(/.*Name,Started At.*/, "")
21
+ proc { adapter.print_collection(fields, data) }.must_output(/.*Name,Started At.*/, "")
21
22
  end
22
23
 
23
24
  it "should print field value" do
24
- proc { adapter.print_records(fields, data) }.must_output(/.*John Doe.*/, "")
25
+ proc { adapter.print_collection(fields, data) }.must_output(/.*John Doe.*/, "")
25
26
  end
26
27
 
27
28
  context "handle ids" do
@@ -31,14 +32,14 @@ describe HammerCLI::Output::Adapter::CSValues do
31
32
  }
32
33
 
33
34
  it "should ommit column of type Id by default" do
34
- out, err = capture_io { adapter.print_records(fields, data) }
35
+ out, err = capture_io { adapter.print_collection(fields, data) }
35
36
  out.wont_match(/.*Id.*/)
36
37
  out.wont_match(/.*John Doe,.*/)
37
38
  end
38
39
 
39
40
  it "should print column of type Id when --show-ids is set" do
40
41
  adapter = HammerCLI::Output::Adapter::CSValues.new( { :show_ids => true } )
41
- out, err = capture_io { adapter.print_records(fields, data) }
42
+ out, err = capture_io { adapter.print_collection(fields, data) }
42
43
  out.must_match(/.*Id.*/)
43
44
  end
44
45
  end
@@ -52,7 +53,7 @@ describe HammerCLI::Output::Adapter::CSValues do
52
53
  end
53
54
 
54
55
  adapter = HammerCLI::Output::Adapter::CSValues.new({}, { :DataField => [ DotFormatter.new ]})
55
- out, err = capture_io { adapter.print_records(fields, data) }
56
+ out, err = capture_io { adapter.print_collection(fields, data) }
56
57
  out.must_match(/.*-DOT-.*/)
57
58
  end
58
59
  end
@@ -4,22 +4,22 @@ describe HammerCLI::Output::Adapter::Table do
4
4
 
5
5
  let(:adapter) { HammerCLI::Output::Adapter::Table.new }
6
6
 
7
- context "print_records" do
7
+ context "print_collection" do
8
8
 
9
9
  let(:field_name) { Fields::DataField.new(:path => [:name], :label => "Name") }
10
10
  let(:fields) {
11
11
  [field_name]
12
12
  }
13
- let(:data) {[{
13
+ let(:data) { HammerCLI::Output::RecordCollection.new [{
14
14
  :name => "John Doe"
15
15
  }]}
16
16
 
17
- it "should print column name" do
18
- proc { adapter.print_records(fields, data) }.must_output(/.*NAME.*/, "")
17
+ it "should print column name " do
18
+ proc { adapter.print_collection(fields, data) }.must_output(/.*NAME.*/, "")
19
19
  end
20
20
 
21
21
  it "should print field value" do
22
- proc { adapter.print_records(fields, data) }.must_output(/.*John Doe.*/, "")
22
+ proc { adapter.print_collection(fields, data) }.must_output(/.*John Doe.*/, "")
23
23
  end
24
24
 
25
25
  context "handle ids" do
@@ -29,19 +29,19 @@ describe HammerCLI::Output::Adapter::Table do
29
29
  }
30
30
 
31
31
  it "should ommit column of type Id by default" do
32
- out, err = capture_io { adapter.print_records(fields, data) }
32
+ out, err = capture_io { adapter.print_collection(fields, data) }
33
33
  out.wont_match(/.*ID.*/)
34
34
  end
35
35
 
36
36
  it "should print column of type Id when --show-ids is set" do
37
37
  adapter = HammerCLI::Output::Adapter::Table.new( { :show_ids => true } )
38
- out, err = capture_io { adapter.print_records(fields, data) }
38
+ out, err = capture_io { adapter.print_collection(fields, data) }
39
39
  out.must_match(/.*ID.*/)
40
40
  end
41
41
  end
42
42
 
43
43
  context "formatters" do
44
- it "should apply formatters" do
44
+ it "should apply formatters" do
45
45
  class DotFormatter < HammerCLI::Output::Formatters::FieldFormatter
46
46
  def format(data)
47
47
  '-DOT-'
@@ -49,7 +49,7 @@ describe HammerCLI::Output::Adapter::Table do
49
49
  end
50
50
 
51
51
  adapter = HammerCLI::Output::Adapter::Table.new({}, { :DataField => [ DotFormatter.new ]})
52
- out, err = capture_io { adapter.print_records(fields, data) }
52
+ out, err = capture_io { adapter.print_collection(fields, data) }
53
53
  out.must_match(/.*-DOT-.*/)
54
54
  end
55
55
  end
@@ -53,13 +53,25 @@ describe HammerCLI::Output::Output do
53
53
  let(:collection) { [item1, item2] }
54
54
 
55
55
  it "prints single resource" do
56
- adapter.any_instance.expects(:print_records).with([], [{}])
57
- out.print_records(definition, item1)
56
+ adapter.any_instance.expects(:print_record).with([], item1)
57
+ out.print_record(definition, item1)
58
58
  end
59
59
 
60
+ it "prints single resource as collection" do
61
+ adapter.any_instance.expects(:print_collection).with([], instance_of(HammerCLI::Output::RecordCollection))
62
+ out.print_collection(definition, item1)
63
+ end
64
+
65
+
60
66
  it "prints array of resources" do
61
- adapter.any_instance.expects(:print_records).with([], collection)
62
- out.print_records(definition, collection)
67
+ adapter.any_instance.expects(:print_collection).with([], instance_of(HammerCLI::Output::RecordCollection))
68
+ out.print_collection(definition, collection)
69
+ end
70
+
71
+ it "prints recordset" do
72
+ data = HammerCLI::Output::RecordCollection.new(collection)
73
+ adapter.any_instance.expects(:print_collection).with([], data)
74
+ out.print_collection(definition, data)
63
75
  end
64
76
 
65
77
  end
@@ -0,0 +1,34 @@
1
+ require File.join(File.dirname(__FILE__), '../test_helper')
2
+
3
+ describe HammerCLI::Output::RecordCollection do
4
+ let(:data) { [1, 2, 3] }
5
+ let(:meta) { { :total => 6, :page => 2, :per_page => 3, :subtotal => 5,
6
+ :search => 'name~=xx', :sort_by => 'name', :sort_order => 'ASC' } }
7
+ let(:set) { HammerCLI::Output::RecordCollection.new(data, meta) }
8
+
9
+ it "should keep records and its meta data" do
10
+ set.must_equal data
11
+ set.meta.total.must_equal 6
12
+ set.meta.subtotal.must_equal 5
13
+ set.meta.total.must_equal 6
14
+ set.meta.page.must_equal 2
15
+ set.meta.per_page.must_equal 3
16
+ set.meta.search.must_equal 'name~=xx'
17
+ set.meta.sort_by.must_equal 'name'
18
+ set.meta.sort_order.must_equal 'ASC'
19
+ end
20
+
21
+ it "should wrap the data into list" do
22
+ record = { :key => :value, :key2 => :value }
23
+ rs = HammerCLI::Output::RecordCollection.new(record)
24
+ rs.must_be_kind_of Array
25
+ end
26
+
27
+ it "sould accept MetaData as option" do
28
+ metadata = HammerCLI::Output::MetaData.new(meta)
29
+ set = HammerCLI::Output::RecordCollection.new(data, :meta => metadata)
30
+ set.meta.must_equal metadata
31
+ set.meta.total.must_equal 6
32
+ end
33
+
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hammer_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Bačovský
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-26 00:00:00.000000000 Z
12
+ date: 2013-12-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: clamp
@@ -181,6 +181,7 @@ files:
181
181
  - lib/hammer_cli/output/fields.rb
182
182
  - lib/hammer_cli/output/formatters.rb
183
183
  - lib/hammer_cli/output/output.rb
184
+ - lib/hammer_cli/output/record_collection.rb
184
185
  - lib/hammer_cli/settings.rb
185
186
  - lib/hammer_cli/shell.rb
186
187
  - lib/hammer_cli/utils.rb
@@ -204,6 +205,7 @@ files:
204
205
  - test/unit/output/fields_test.rb
205
206
  - test/unit/output/formatters_test.rb
206
207
  - test/unit/output/output_test.rb
208
+ - test/unit/output/record_collection_test.rb
207
209
  - test/unit/settings_test.rb
208
210
  - test/unit/test_helper.rb
209
211
  - test/unit/utils_test.rb
@@ -228,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
230
  version: '0'
229
231
  requirements: []
230
232
  rubyforge_project:
231
- rubygems_version: 2.0.8
233
+ rubygems_version: 2.0.13
232
234
  signing_key:
233
235
  specification_version: 4
234
236
  summary: Universal command-line interface
@@ -251,6 +253,7 @@ test_files:
251
253
  - test/unit/output/fields_test.rb
252
254
  - test/unit/output/formatters_test.rb
253
255
  - test/unit/output/output_test.rb
256
+ - test/unit/output/record_collection_test.rb
254
257
  - test/unit/settings_test.rb
255
258
  - test/unit/test_helper.rb
256
259
  - test/unit/utils_test.rb