wavefront-cli 4.4.1 → 4.4.2
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/HISTORY.md +4 -0
- data/lib/wavefront-cli/base.rb +14 -4
- data/lib/wavefront-cli/display/printer/terse.rb +30 -14
- data/lib/wavefront-cli/version.rb +1 -1
- data/spec/wavefront-cli/display/printer/terse_spec.rb +12 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 873187a820bc7cb2248cc10eb22ce76c12d95925651baa3c53972d5f7440a892
|
4
|
+
data.tar.gz: e0ab5dd4af22d88eb3f5d412dcaa08fb2e58906bf4044178c074dacde734159e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4432d6d046bc4c55530f10d8e765e5528f04432a9cfbdc4ab4e9a0899beae778885b70035c4cc570c669c3865ce0aa91c537320c6e2d62e0167e0b0123ea44e7
|
7
|
+
data.tar.gz: 2e9bb61ac0e004f412e3e00420f3e93d4795961ec3f63dc58ff6077efd1d502da4de880126ae562e5f992bc78196ee4ba61b151f14db43e62917bfbbe743be92
|
data/HISTORY.md
CHANGED
data/lib/wavefront-cli/base.rb
CHANGED
@@ -363,11 +363,11 @@ module WavefrontCli
|
|
363
363
|
end
|
364
364
|
|
365
365
|
def load_json(file)
|
366
|
-
|
366
|
+
read_json(IO.read(file))
|
367
367
|
end
|
368
368
|
|
369
369
|
def load_yaml(file)
|
370
|
-
|
370
|
+
read_yaml(IO.read(file))
|
371
371
|
end
|
372
372
|
|
373
373
|
# Read STDIN and return a Ruby object, assuming that STDIN is
|
@@ -383,9 +383,9 @@ module WavefrontCli
|
|
383
383
|
raw = STDIN.read
|
384
384
|
|
385
385
|
if raw.start_with?('---')
|
386
|
-
|
386
|
+
read_yaml(raw)
|
387
387
|
else
|
388
|
-
|
388
|
+
read_json(raw)
|
389
389
|
end
|
390
390
|
rescue RuntimeError
|
391
391
|
raise Wavefront::Exception::UnparseableInput
|
@@ -642,5 +642,15 @@ module WavefrontCli
|
|
642
642
|
aggr
|
643
643
|
end
|
644
644
|
# rubocop:enable Metrics/MethodLength
|
645
|
+
|
646
|
+
private
|
647
|
+
|
648
|
+
def read_json(io)
|
649
|
+
JSON.parse(io, symbolize_names: true)
|
650
|
+
end
|
651
|
+
|
652
|
+
def read_yaml(io)
|
653
|
+
YAML.safe_load(io, symbolize_names: true)
|
654
|
+
end
|
645
655
|
end
|
646
656
|
end
|
@@ -8,7 +8,7 @@ module WavefrontDisplayPrinter
|
|
8
8
|
# Print values which are per-row. The terse listings, primarily
|
9
9
|
#
|
10
10
|
class Terse
|
11
|
-
attr_reader :data
|
11
|
+
attr_reader :data
|
12
12
|
|
13
13
|
# @param data [Hash] data to display, from a response object
|
14
14
|
# @param keys [Array[Symbol]] keys to display, in order
|
@@ -18,32 +18,48 @@ module WavefrontDisplayPrinter
|
|
18
18
|
@fmt = format_string(data, keys)
|
19
19
|
end
|
20
20
|
|
21
|
+
# @return [String] used to format output
|
22
|
+
#
|
21
23
|
def format_string(data, keys)
|
22
24
|
keys.map { |k| "%-#{data.longest_value_of(k)}<#{k}>s" }.join(' ')
|
23
25
|
end
|
24
26
|
|
27
|
+
# Flatten nested data.
|
28
|
+
# @param data [Map,Hash] data to flatten
|
29
|
+
# @param keys [Array[Symbol]] keys of interest. We don't bother working on
|
30
|
+
# things we'll only throw away
|
31
|
+
#
|
25
32
|
def stringify(data, keys)
|
26
|
-
data.map { |e| e.tap { keys.each { |k| e[k] =
|
33
|
+
data.map { |e| e.tap { keys.each { |k| e[k] = value_as_string(e[k]) } } }
|
27
34
|
end
|
28
35
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
# Turn a (potentially) more complicated structure into a string
|
37
|
+
# @param value [Object]
|
38
|
+
# @return [String]
|
39
|
+
#
|
40
|
+
def value_as_string(value)
|
41
|
+
return value.join(', ') if value.is_a?(Array)
|
42
|
+
|
43
|
+
return map_to_string(value) if value.is_a?(Map)
|
44
|
+
|
45
|
+
value
|
37
46
|
end
|
38
47
|
|
48
|
+
# If we get a hash as a value (tags, for instance) we squash it down to a
|
49
|
+
# "key1=val1;key2=val2" kind of string. Note that this doesn't handle
|
50
|
+
# nested hashes. It shouldn't have to.
|
51
|
+
#
|
52
|
+
# @param value [Map,Hash] { k1: 'v1', k2: 'v2' }
|
53
|
+
# @return [String] 'k1=v1;k2=v2'
|
54
|
+
#
|
39
55
|
def map_to_string(value)
|
40
|
-
|
41
|
-
key: value.keys[0],
|
42
|
-
value: value.values.join(','))
|
56
|
+
value.map { |k, v| "#{k}=#{v}" }.join(';')
|
43
57
|
end
|
44
58
|
|
59
|
+
# Format every element according to the format string @fmt
|
60
|
+
#
|
45
61
|
def to_s
|
46
|
-
data.map { |e| format(fmt, e).rstrip }.join("\n")
|
62
|
+
data.map { |e| format(@fmt, e).rstrip }.join("\n")
|
47
63
|
rescue KeyError
|
48
64
|
raise WavefrontCli::Exception::UserError, 'field not found'
|
49
65
|
end
|
@@ -17,7 +17,7 @@ class WavefrontDisplayPrinterTerse < MiniTest::Test
|
|
17
17
|
@wf = WavefrontDisplayPrinter::Terse.new(TERSE_DATA, %i[id name])
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def test_format_string
|
21
21
|
assert_equal('%-3<id>s', wf.format_string(TERSE_DATA, [:id]))
|
22
22
|
assert_equal('%-3<id>s %-5<name>s',
|
23
23
|
wf.format_string(TERSE_DATA, %i[id name]))
|
@@ -45,12 +45,19 @@ class WavefrontDisplayPrinterTerse < MiniTest::Test
|
|
45
45
|
[:things]))
|
46
46
|
end
|
47
47
|
|
48
|
-
def
|
49
|
-
assert_equal('
|
50
|
-
|
48
|
+
def test_map_to_string
|
49
|
+
assert_equal('key1=value1;key2=value2;key3=value3',
|
50
|
+
wf.map_to_string(key1: 'value1',
|
51
|
+
key2: 'value2',
|
52
|
+
key3: 'value3'))
|
51
53
|
end
|
52
54
|
|
53
|
-
def
|
55
|
+
def test_value_as_string
|
56
|
+
assert_equal('a, b, c', wf.value_as_string(%w[a b c]))
|
57
|
+
assert_equal('abc', wf.value_as_string('abc'))
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_end_to_end_1
|
54
61
|
input, expected = OutputTester.new.in_and_out('alerts-input.json',
|
55
62
|
'alerts-human-terse')
|
56
63
|
out = WavefrontDisplayPrinter::Terse.new(input, %i[id status name]).to_s
|