hammer_cli 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/config/cli_config.template.yml +2 -0
- data/lib/hammer_cli/abstract.rb +6 -2
- data/lib/hammer_cli/apipie/read_command.rb +2 -2
- data/lib/hammer_cli/apipie/resource.rb +4 -1
- data/lib/hammer_cli/apipie/write_command.rb +1 -3
- data/lib/hammer_cli/output/adapter/abstract.rb +5 -1
- data/lib/hammer_cli/output/adapter/base.rb +6 -2
- data/lib/hammer_cli/output/adapter/csv.rb +6 -2
- data/lib/hammer_cli/output/adapter/silent.rb +4 -1
- data/lib/hammer_cli/output/adapter/table.rb +7 -2
- data/lib/hammer_cli/output/output.rb +9 -2
- data/lib/hammer_cli/output/record_collection.rb +35 -0
- data/lib/hammer_cli/output.rb +1 -0
- data/lib/hammer_cli/version.rb +1 -1
- data/test/unit/output/adapter/abstract_test.rb +2 -2
- data/test/unit/output/adapter/base_test.rb +4 -4
- data/test/unit/output/adapter/csv_test.rb +8 -7
- data/test/unit/output/adapter/table_test.rb +9 -9
- data/test/unit/output/output_test.rb +16 -4
- data/test/unit/output/record_collection_test.rb +34 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 367645958584805f4e59145418616c507fd9144a
|
4
|
+
data.tar.gz: d89c9715a3b0b66dacb21708d878c12e9c542e84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eb7b32bc92e6d4ebedf52c8e9334ba2acab69808967b02a6e6b92110f936fccc9ede2febfb20e970eaa994c1b05582f83b46b964aef4c2307c2f6f01d4f75d3
|
7
|
+
data.tar.gz: 28164cbd24f5a912b4de8e97e10c917a2690378750ccfd6125f1e4104ff43bea883408f2fca13b70bcbd6abda325f4470b8607b1c4626303b50d7f9bfec0f4d8
|
data/lib/hammer_cli/abstract.rb
CHANGED
@@ -122,8 +122,12 @@ module HammerCLI
|
|
122
122
|
|
123
123
|
protected
|
124
124
|
|
125
|
-
def
|
126
|
-
output.
|
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.
|
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
|
-
|
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
|
-
|
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
|
@@ -27,7 +27,11 @@ module HammerCLI::Output::Adapter
|
|
27
27
|
$stderr.puts msg.format(msg_params)
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
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
|
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
|
-
|
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
|
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
|
-
|
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) || '')
|
@@ -8,11 +8,16 @@ module HammerCLI::Output::Adapter
|
|
8
8
|
[:screen, :flat]
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
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 =
|
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
|
20
|
-
adapter.
|
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
|
data/lib/hammer_cli/output.rb
CHANGED
@@ -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
|
|
data/lib/hammer_cli/version.rb
CHANGED
@@ -57,8 +57,8 @@ describe HammerCLI::Output::Adapter::Abstract do
|
|
57
57
|
|
58
58
|
end
|
59
59
|
|
60
|
-
it "should raise not implemented on
|
61
|
-
proc { adapter.
|
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 "
|
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.
|
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.
|
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 "
|
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.
|
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.
|
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.
|
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.
|
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.
|
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 "
|
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.
|
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.
|
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.
|
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.
|
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.
|
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(:
|
57
|
-
out.
|
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(:
|
62
|
-
out.
|
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.
|
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-
|
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.
|
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
|