wavefront-cli 4.2.1 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -0
  3. data/.rubocop.yml +1 -14
  4. data/.travis.yml +4 -4
  5. data/Gemfile +2 -0
  6. data/HISTORY.md +82 -60
  7. data/Rakefile +2 -0
  8. data/bin/wf +1 -0
  9. data/lib/wavefront-cli/alert.rb +13 -5
  10. data/lib/wavefront-cli/apitoken.rb +2 -0
  11. data/lib/wavefront-cli/base.rb +95 -47
  12. data/lib/wavefront-cli/cloudintegration.rb +6 -0
  13. data/lib/wavefront-cli/command_mixins/acl.rb +6 -1
  14. data/lib/wavefront-cli/command_mixins/tag.rb +2 -0
  15. data/lib/wavefront-cli/commands/.rubocop.yml +3 -4
  16. data/lib/wavefront-cli/commands/alert.rb +2 -1
  17. data/lib/wavefront-cli/commands/apitoken.rb +6 -0
  18. data/lib/wavefront-cli/commands/base.rb +30 -21
  19. data/lib/wavefront-cli/commands/cloudintegration.rb +2 -0
  20. data/lib/wavefront-cli/commands/config.rb +2 -0
  21. data/lib/wavefront-cli/commands/dashboard.rb +2 -0
  22. data/lib/wavefront-cli/commands/derivedmetric.rb +2 -0
  23. data/lib/wavefront-cli/commands/event.rb +3 -1
  24. data/lib/wavefront-cli/commands/integration.rb +2 -1
  25. data/lib/wavefront-cli/commands/link.rb +2 -0
  26. data/lib/wavefront-cli/commands/message.rb +2 -0
  27. data/lib/wavefront-cli/commands/metric.rb +2 -0
  28. data/lib/wavefront-cli/commands/notificant.rb +2 -0
  29. data/lib/wavefront-cli/commands/proxy.rb +2 -0
  30. data/lib/wavefront-cli/commands/query.rb +2 -0
  31. data/lib/wavefront-cli/commands/savedsearch.rb +2 -0
  32. data/lib/wavefront-cli/commands/serviceaccount.rb +58 -0
  33. data/lib/wavefront-cli/commands/settings.rb +2 -0
  34. data/lib/wavefront-cli/commands/source.rb +3 -1
  35. data/lib/wavefront-cli/commands/user.rb +4 -1
  36. data/lib/wavefront-cli/commands/usergroup.rb +3 -0
  37. data/lib/wavefront-cli/commands/webhook.rb +2 -0
  38. data/lib/wavefront-cli/commands/window.rb +2 -0
  39. data/lib/wavefront-cli/commands/write.rb +2 -0
  40. data/lib/wavefront-cli/config.rb +23 -12
  41. data/lib/wavefront-cli/constants.rb +10 -4
  42. data/lib/wavefront-cli/controller.rb +53 -22
  43. data/lib/wavefront-cli/dashboard.rb +3 -0
  44. data/lib/wavefront-cli/derivedmetric.rb +12 -11
  45. data/lib/wavefront-cli/display/alert.rb +7 -4
  46. data/lib/wavefront-cli/display/apitoken.rb +2 -0
  47. data/lib/wavefront-cli/display/base.rb +34 -8
  48. data/lib/wavefront-cli/display/cloudintegration.rb +4 -2
  49. data/lib/wavefront-cli/display/dashboard.rb +2 -0
  50. data/lib/wavefront-cli/display/derivedmetric.rb +2 -0
  51. data/lib/wavefront-cli/display/distribution.rb +2 -0
  52. data/lib/wavefront-cli/display/event.rb +2 -0
  53. data/lib/wavefront-cli/display/externallink.rb +2 -0
  54. data/lib/wavefront-cli/display/integration.rb +2 -0
  55. data/lib/wavefront-cli/display/maintenancewindow.rb +2 -0
  56. data/lib/wavefront-cli/display/message.rb +7 -3
  57. data/lib/wavefront-cli/display/metric.rb +3 -1
  58. data/lib/wavefront-cli/display/notificant.rb +3 -1
  59. data/lib/wavefront-cli/display/printer/long.rb +36 -11
  60. data/lib/wavefront-cli/display/printer/sparkline.rb +3 -0
  61. data/lib/wavefront-cli/display/printer/terse.rb +6 -1
  62. data/lib/wavefront-cli/display/proxy.rb +2 -0
  63. data/lib/wavefront-cli/display/query.rb +35 -24
  64. data/lib/wavefront-cli/display/savedsearch.rb +2 -0
  65. data/lib/wavefront-cli/display/serviceaccount.rb +60 -0
  66. data/lib/wavefront-cli/display/settings.rb +2 -0
  67. data/lib/wavefront-cli/display/source.rb +18 -4
  68. data/lib/wavefront-cli/display/user.rb +56 -10
  69. data/lib/wavefront-cli/display/usergroup.rb +25 -19
  70. data/lib/wavefront-cli/display/webhook.rb +2 -0
  71. data/lib/wavefront-cli/display/write.rb +6 -2
  72. data/lib/wavefront-cli/event.rb +83 -62
  73. data/lib/wavefront-cli/exception.rb +3 -0
  74. data/lib/wavefront-cli/externallink.rb +6 -4
  75. data/lib/wavefront-cli/integration.rb +2 -0
  76. data/lib/wavefront-cli/maintenancewindow.rb +28 -17
  77. data/lib/wavefront-cli/message.rb +4 -0
  78. data/lib/wavefront-cli/metric.rb +4 -1
  79. data/lib/wavefront-cli/notificant.rb +2 -0
  80. data/lib/wavefront-cli/opt_handler.rb +2 -0
  81. data/lib/wavefront-cli/output/base.rb +8 -3
  82. data/lib/wavefront-cli/output/csv.rb +2 -0
  83. data/lib/wavefront-cli/output/csv/base.rb +2 -0
  84. data/lib/wavefront-cli/output/csv/query.rb +14 -7
  85. data/lib/wavefront-cli/output/hcl.rb +2 -0
  86. data/lib/wavefront-cli/output/hcl/alert.rb +2 -0
  87. data/lib/wavefront-cli/output/hcl/base.rb +10 -4
  88. data/lib/wavefront-cli/output/hcl/dashboard.rb +17 -8
  89. data/lib/wavefront-cli/output/hcl/notificant.rb +2 -0
  90. data/lib/wavefront-cli/output/hcl/stdlib/array.rb +2 -0
  91. data/lib/wavefront-cli/output/hcl/stdlib/string.rb +2 -0
  92. data/lib/wavefront-cli/output/json.rb +2 -0
  93. data/lib/wavefront-cli/output/ruby.rb +2 -0
  94. data/lib/wavefront-cli/output/wavefront.rb +2 -0
  95. data/lib/wavefront-cli/output/wavefront/query.rb +7 -4
  96. data/lib/wavefront-cli/output/yaml.rb +2 -0
  97. data/lib/wavefront-cli/proxy.rb +15 -3
  98. data/lib/wavefront-cli/query.rb +14 -12
  99. data/lib/wavefront-cli/savedsearch.rb +2 -0
  100. data/lib/wavefront-cli/serviceaccount.rb +179 -0
  101. data/lib/wavefront-cli/settings.rb +2 -0
  102. data/lib/wavefront-cli/source.rb +2 -0
  103. data/lib/wavefront-cli/stdlib/array.rb +3 -0
  104. data/lib/wavefront-cli/stdlib/string.rb +17 -11
  105. data/lib/wavefront-cli/user.rb +10 -4
  106. data/lib/wavefront-cli/usergroup.rb +4 -2
  107. data/lib/wavefront-cli/version.rb +3 -1
  108. data/lib/wavefront-cli/webhook.rb +2 -0
  109. data/lib/wavefront-cli/write.rb +80 -46
  110. data/spec/.rubocop.yml +3 -16
  111. data/spec/constants.rb +21 -0
  112. data/spec/spec_helper.rb +8 -422
  113. data/spec/support/command_base.rb +82 -0
  114. data/spec/support/minitest_assertions.rb +262 -0
  115. data/spec/support/output_tester.rb +32 -0
  116. data/spec/support/supported_commands.rb +19 -0
  117. data/spec/test_mixins/acl.rb +169 -0
  118. data/spec/test_mixins/delete.rb +25 -0
  119. data/spec/test_mixins/deleteundelete.rb +106 -0
  120. data/spec/test_mixins/describe.rb +24 -0
  121. data/spec/test_mixins/dump.rb +43 -0
  122. data/spec/test_mixins/general.rb +11 -0
  123. data/spec/test_mixins/history.rb +35 -0
  124. data/spec/test_mixins/import.rb +65 -0
  125. data/spec/test_mixins/list.rb +29 -0
  126. data/spec/test_mixins/search.rb +98 -0
  127. data/spec/test_mixins/set.rb +47 -0
  128. data/spec/test_mixins/tag.rb +99 -0
  129. data/spec/wavefront-cli/alert_spec.rb +288 -111
  130. data/spec/wavefront-cli/apitoken_spec.rb +53 -24
  131. data/spec/wavefront-cli/base_spec.rb +9 -27
  132. data/spec/wavefront-cli/cloudintegration_spec.rb +65 -29
  133. data/spec/wavefront-cli/commands/alert_spec.rb +1 -0
  134. data/spec/wavefront-cli/commands/base_spec.rb +15 -13
  135. data/spec/wavefront-cli/commands/cloudintegration_spec.rb +1 -0
  136. data/spec/wavefront-cli/commands/config_spec.rb +3 -0
  137. data/spec/wavefront-cli/commands/dashboard_spec.rb +1 -0
  138. data/spec/wavefront-cli/commands/derivedmetric_spec.rb +1 -0
  139. data/spec/wavefront-cli/commands/event_spec.rb +1 -0
  140. data/spec/wavefront-cli/commands/link_spec.rb +1 -0
  141. data/spec/wavefront-cli/commands/message_spec.rb +1 -0
  142. data/spec/wavefront-cli/commands/metric_spec.rb +1 -0
  143. data/spec/wavefront-cli/commands/proxy_spec.rb +1 -0
  144. data/spec/wavefront-cli/commands/query_spec.rb +1 -0
  145. data/spec/wavefront-cli/commands/webhook_spec.rb +1 -0
  146. data/spec/wavefront-cli/commands/window_spec.rb +1 -0
  147. data/spec/wavefront-cli/commands/write_spec.rb +1 -0
  148. data/spec/wavefront-cli/config_spec.rb +25 -14
  149. data/spec/wavefront-cli/controller_spec.rb +13 -3
  150. data/spec/wavefront-cli/dashboard_spec.rb +125 -76
  151. data/spec/wavefront-cli/derivedmetric_spec.rb +83 -67
  152. data/spec/wavefront-cli/display/base_spec.rb +5 -7
  153. data/spec/wavefront-cli/display/printer/long_spec.rb +20 -16
  154. data/spec/wavefront-cli/display/printer/terse_spec.rb +5 -4
  155. data/spec/wavefront-cli/event_spec.rb +360 -18
  156. data/spec/wavefront-cli/externallink_spec.rb +92 -58
  157. data/spec/wavefront-cli/integration_spec.rb +129 -31
  158. data/spec/wavefront-cli/maintenancewindow_spec.rb +270 -32
  159. data/spec/wavefront-cli/message_spec.rb +73 -30
  160. data/spec/wavefront-cli/metric_spec.rb +60 -22
  161. data/spec/wavefront-cli/notificant_spec.rb +45 -32
  162. data/spec/wavefront-cli/opt_handler_spec.rb +4 -1
  163. data/spec/wavefront-cli/output/csv/query_spec.rb +21 -19
  164. data/spec/wavefront-cli/output/csv_spec.rb +5 -2
  165. data/spec/wavefront-cli/output/hcl_spec.rb +5 -2
  166. data/spec/wavefront-cli/output/helpers.rb +18 -0
  167. data/spec/wavefront-cli/output/json_spec.rb +3 -1
  168. data/spec/wavefront-cli/output/ruby_spec.rb +3 -1
  169. data/spec/wavefront-cli/output/wavefront/query_spec.rb +3 -1
  170. data/spec/wavefront-cli/output/wavefront_spec.rb +6 -4
  171. data/spec/wavefront-cli/output/yaml_spec.rb +3 -1
  172. data/spec/wavefront-cli/proxy_spec.rb +49 -27
  173. data/spec/wavefront-cli/query_spec.rb +174 -92
  174. data/spec/wavefront-cli/resources/responses/query.json +1 -0
  175. data/spec/wavefront-cli/resources/updates/alert.json +15 -0
  176. data/spec/wavefront-cli/resources/updates/dashboard.json +1 -0
  177. data/spec/wavefront-cli/savedsearch_spec.rb +35 -18
  178. data/spec/wavefront-cli/serviceaccount_spec.rb +399 -0
  179. data/spec/wavefront-cli/settings_spec.rb +42 -11
  180. data/spec/wavefront-cli/source_spec.rb +120 -23
  181. data/spec/wavefront-cli/stdlib/array_spec.rb +2 -1
  182. data/spec/wavefront-cli/stdlib/string_spec.rb +9 -6
  183. data/spec/wavefront-cli/user_spec.rb +278 -108
  184. data/spec/wavefront-cli/usergroup_spec.rb +152 -102
  185. data/spec/wavefront-cli/webhook_spec.rb +30 -15
  186. data/spec/wavefront-cli/write_spec.rb +25 -1
  187. data/wavefront-cli.gemspec +5 -3
  188. metadata +65 -21
  189. data/spec/wavefront-cli/commands/spec_helper.rb +0 -3
  190. data/spec/wavefront-cli/display/spec_helper.rb +0 -3
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
  require_relative 'command_mixins/tag'
3
5
  require_relative 'command_mixins/acl'
@@ -12,6 +14,7 @@ module WavefrontCli
12
14
 
13
15
  def list_filter(list)
14
16
  return list unless options[:nosystem]
17
+
15
18
  list.tap { |l| l.response.items.delete_if { |d| d[:systemOwned] } }
16
19
  end
17
20
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
  require_relative 'command_mixins/tag'
3
5
 
@@ -25,20 +27,19 @@ module WavefrontCli
25
27
  end
26
28
 
27
29
  def do_create
28
- wf.create(build_body)
30
+ wf.create(create_body)
29
31
  end
30
32
 
31
33
  # rubocop:disable Metrics/AbcSize
32
- def build_body
33
- ret = { query: options[:'<query>'],
34
- name: options[:'<name>'],
35
- minutes: options[:range].to_i,
36
- includeObsoleteMetrics: options[:obsolete],
37
- processRateMinutes: options[:interval].to_i }
38
-
39
- ret[:additionalInformation] = options[:desc] if options[:desc]
40
- ret[:tags] = options[:ctag] if valid_tags?
41
- ret
34
+ def create_body
35
+ { query: options[:'<query>'],
36
+ name: options[:'<name>'],
37
+ minutes: options[:range].to_i,
38
+ includeObsoleteMetrics: options[:obsolete],
39
+ processRateMinutes: options[:interval].to_i }.tap do |b|
40
+ b[:additionalInformation] = options[:desc] if options[:desc]
41
+ b[:tags] = options[:ctag] if valid_tags?
42
+ end
42
43
  end
43
44
  # rubocop:enable Metrics/AbcSize
44
45
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -52,13 +54,14 @@ module WavefrontDisplay
52
54
  puts data.max
53
55
  end
54
56
 
55
- # rubocop:disable Metrics/AbcSize
56
57
  def do_summary
57
58
  kw = data.keys.map(&:size).max + 2
58
- data.delete_if { |_k, v| v.zero? } unless options[:all]
59
- data.sort.each { |k, v| puts format("%-#{kw}s%s", k, v) }
59
+ data.sort.each do |k, v|
60
+ next if v.zero? && !options[:all]
61
+
62
+ puts format("%-#{kw}<key>s%<value>s", key: k, value: v)
63
+ end
60
64
  end
61
- # rubocop:enable Metrics/AbcSize
62
65
 
63
66
  def do_queries
64
67
  if options[:brief]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../constants'
2
4
 
3
5
  module WavefrontDisplay
@@ -36,6 +38,7 @@ module WavefrontDisplay
36
38
  # find the correct method to deal with the output of the user's
37
39
  # command.
38
40
  #
41
+ # rubocop:disable Metrics/MethodLength
39
42
  def run(method)
40
43
  if method == 'do_list'
41
44
  run_list
@@ -49,6 +52,7 @@ module WavefrontDisplay
49
52
  long_output
50
53
  end
51
54
  end
55
+ # rubocop:enable Metrics/MethodLength
52
56
 
53
57
  # Choose the correct list handler. The user can specifiy a long
54
58
  # listing with the --long options.
@@ -91,6 +95,7 @@ module WavefrontDisplay
91
95
  #
92
96
  def run_error(method)
93
97
  return unless respond_to?(method)
98
+
94
99
  send(method)
95
100
  exit 1
96
101
  end
@@ -104,6 +109,7 @@ module WavefrontDisplay
104
109
 
105
110
  def prioritize_keys(data, keys)
106
111
  return _prioritize_keys(data, keys) unless data.is_a?(Array)
112
+
107
113
  data.map { |e| _prioritize_keys(e, keys) }
108
114
  end
109
115
 
@@ -115,6 +121,7 @@ module WavefrontDisplay
115
121
  def _prioritize_keys(data, keys)
116
122
  keys.each.with_object(data.is_a?(Map) ? Map.new : {}) do |k, a|
117
123
  next unless data.key?(k)
124
+
118
125
  a[k] = data[k]
119
126
  data.delete(k)
120
127
  end.merge(data)
@@ -150,17 +157,22 @@ module WavefrontDisplay
150
157
 
151
158
  # if this is a section of a larger dataset, say so
152
159
  #
153
- # rubocop:disable Metrics/AbcSize
154
160
  def pagination_line
155
161
  return unless raw.respond_to?(:moreItems) && raw.moreItems == true
156
162
 
157
- enditem = raw.limit.positive? ? raw.offset + raw.limit - 1 : 0
158
- puts format('List shows items %d to %d. Use -o and -L for more.',
159
- raw.offset, enditem)
163
+ puts format('List shows items %<first>d to %<last>d. ' \
164
+ 'Use -o and -L for more.',
165
+ first: raw.offset,
166
+ last: index_of_final_item)
160
167
  rescue StandardError
161
168
  puts 'List shows paginated output. Use -o and -L for more.'
162
169
  end
163
- # rubocop:enable Metrics/AbcSize
170
+
171
+ # Return the offset of the final item in view.
172
+ #
173
+ def index_of_final_item
174
+ raw.limit.positive? ? raw.offset + raw.limit - 1 : 0
175
+ end
164
176
 
165
177
  # Give it a key-value hash, and it will return the size of the first
166
178
  # column to use when formatting that data.
@@ -171,6 +183,7 @@ module WavefrontDisplay
171
183
  #
172
184
  def key_width(hash = {}, pad = 2)
173
185
  return 0 if hash.keys.empty?
186
+
174
187
  hash.keys.map(&:size).max + pad
175
188
  end
176
189
 
@@ -254,11 +267,18 @@ module WavefrontDisplay
254
267
  end
255
268
 
256
269
  def search_display_keys
257
- ([:id] + options[:'<condition>'].map do |c|
270
+ ([search_identifier_key] + options[:'<condition>'].map do |c|
258
271
  c.split(SEARCH_SPLIT, 2).first.to_sym
259
272
  end).uniq
260
273
  end
261
274
 
275
+ # Most objects refer to themselves by 'id'. Some, like accounts, don't.
276
+ # Override here.
277
+ #
278
+ def search_identifier_key
279
+ :id
280
+ end
281
+
262
282
  def do_search
263
283
  if data.empty?
264
284
  puts 'No matches.'
@@ -360,9 +380,8 @@ module WavefrontDisplay
360
380
 
361
381
  str = time.to_s
362
382
  fmt, out_fmt = time_formats(str)
363
- # rubocop:disable Style/DateTime
364
383
  ret = DateTime.strptime(str, fmt).to_time
365
- # rubocop:enable Style/DateTime
384
+
366
385
  ret = force_utc ? ret.utc : ret.localtime
367
386
  ret.strftime(out_fmt)
368
387
  end
@@ -380,5 +399,12 @@ module WavefrontDisplay
380
399
  raise ArgumentError
381
400
  end
382
401
  end
402
+
403
+ # @param things [Array[String]]
404
+ # @return [String] all "things", strong-quoted and comma-separated
405
+ #
406
+ def quoted(things)
407
+ things.map { |item| "'#{item}'" }.join(', ')
408
+ end
383
409
  end
384
410
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -16,11 +18,11 @@ module WavefrontDisplay
16
18
  end
17
19
 
18
20
  def do_enable
19
- puts "Enabled #{options[:'<id>']}."
21
+ puts "Enabled '#{options[:'<id>']}'."
20
22
  end
21
23
 
22
24
  def do_disable
23
- puts "Disabled #{options[:'<id>']}."
25
+ puts "Disabled '#{options[:'<id>']}'."
24
26
  end
25
27
  end
26
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'write'
2
4
 
3
5
  module WavefrontDisplay
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'date'
2
4
  require_relative 'base'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -10,7 +12,7 @@ module WavefrontDisplay
10
12
  end
11
13
 
12
14
  def do_read
13
- abort 'Message not found.' if data.empty?
15
+ abort 'Message not found.' unless data && !data.empty?
14
16
 
15
17
  puts message_title, data.content.fold(TW, 0), message_sender
16
18
  end
@@ -18,11 +20,13 @@ module WavefrontDisplay
18
20
  private
19
21
 
20
22
  def message_title
21
- format("\n%s\n%s\n", data.title, '-' * data.title.length)
23
+ format("\n%<title>s\n%<underline>s\n",
24
+ title: data.title,
25
+ underline: '-' * data.title.length)
22
26
  end
23
27
 
24
28
  def message_sender
25
- format("\n%#{TW - 2}s\n", data.source)
29
+ format("\n%#{TW - 2}<sender>s\n", sender: data.source)
26
30
  end
27
31
  end
28
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -22,7 +24,7 @@ module WavefrontDisplay
22
24
  end
23
25
 
24
26
  def bail_out
25
- puts 'No matches.'
27
+ puts "Did not find metric '#{options[:'<metric>']}'."
26
28
  exit
27
29
  end
28
30
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  module WavefrontDisplay
@@ -15,7 +17,7 @@ module WavefrontDisplay
15
17
  end
16
18
 
17
19
  def do_test
18
- puts 'Testing notification.'
20
+ puts "Testing notificant '#{options[:'<id>']}'."
19
21
  end
20
22
  end
21
23
  end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../stdlib/string'
4
+
1
5
  module WavefrontDisplayPrinter
2
6
  #
3
7
  # Print the long indented descriptions of things
@@ -26,10 +30,10 @@ module WavefrontDisplayPrinter
26
30
  # initializer options hash.
27
31
  #
28
32
  def default_opts
29
- { indent: 2,
30
- padding: 2,
33
+ { indent: 2,
34
+ padding: 2,
31
35
  separator: true,
32
- none: true }
36
+ none: true }
33
37
  end
34
38
 
35
39
  # @param data [Hash] raw data
@@ -39,6 +43,7 @@ module WavefrontDisplayPrinter
39
43
  #
40
44
  def preened_data(data, fields = nil)
41
45
  return data if fields.nil?
46
+
42
47
  data.map { |d| d.select { |k| fields.include?(k.to_sym) }.to_h }
43
48
  end
44
49
 
@@ -49,6 +54,7 @@ module WavefrontDisplayPrinter
49
54
  #
50
55
  def preened_value(value)
51
56
  return value unless value.is_a?(String) && value =~ /<.*>/
57
+
52
58
  value.gsub(%r{<\/?[^>]*>}, '').delete("\n")
53
59
  end
54
60
 
@@ -94,16 +100,22 @@ module WavefrontDisplayPrinter
94
100
  # Turn the list made by #make_list into user output
95
101
  # @return [String]
96
102
  #
97
- # rubocop:disable Metrics/AbcSize
98
103
  def to_s
99
- list.map do |e|
100
- indent = ' ' * opts[:indent] * e.last
101
- key_str = (indent + e.first.to_s + ' ' * kw)[0..kw]
102
- val = e[1] == :separator ? '-' * (TW - key_str.length) : e[1]
103
- line(key_str, val)
104
+ list.map do |item|
105
+ stringify_line(item)
104
106
  end.join("\n")
105
107
  end
106
- # rubocop:enable Metrics/AbcSize
108
+
109
+ # @param item [Array] of the form [key, value, indent_level]
110
+ #
111
+ def stringify_line(item)
112
+ key_str = format('%<indent>s%<key>s%<gutter>s',
113
+ indent: padding(item[2]),
114
+ key: item[0].to_s,
115
+ gutter: ' ' * kw)[0..kw]
116
+ line(key_str,
117
+ item[1] == :separator ? separator_line(key_str.size) : item[1])
118
+ end
107
119
 
108
120
  def line(key, val)
109
121
  line_length = key.to_s.size + val.to_s.size
@@ -112,11 +124,24 @@ module WavefrontDisplayPrinter
112
124
  val = val.value_fold(key.to_s.size)
113
125
  end
114
126
 
115
- format('%s%s', key, val).rstrip
127
+ format('%<padded_key>s%<value>s', padded_key: key, value: val).rstrip
116
128
  end
117
129
 
118
130
  private
119
131
 
132
+ # @return [String] correctly sized separator line
133
+ #
134
+ def separator_line(width)
135
+ '-' * (TW - width)
136
+ end
137
+
138
+ # @return [String] the correct amount of whitespace indentation for the
139
+ # given indent level
140
+ #
141
+ def padding(level)
142
+ ' ' * opts[:indent] * level
143
+ end
144
+
120
145
  # Part of the #make_list recursion. Deals with a hash.
121
146
  #
122
147
  # @param data [Hash]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The Unicode characters which we use to make a sparkline
2
4
  #
3
5
  BLOCKS = [' ', "\u2581", "\u2582", "\u2583", "\u2585", "\u2586",
@@ -35,6 +37,7 @@ class WavefrontSparkline
35
37
  #
36
38
  def make_fit(vals)
37
39
  return vals if vals.size < SPARK_WIDTH
40
+
38
41
  vals.<< vals.last if vals.size.odd?
39
42
  ret = vals.each_slice(2).with_object([]) { |s, a| a.<< s.inject(:+) / 2 }
40
43
  make_fit(ret)
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'map'
1
4
  require_relative '../../stdlib/array'
2
5
 
3
6
  module WavefrontDisplayPrinter
@@ -34,7 +37,9 @@ module WavefrontDisplayPrinter
34
37
  end
35
38
 
36
39
  def map_to_string(value)
37
- format('%s=%s', value.keys[0], value.values.join(','))
40
+ format('%<key>s=%<value>s',
41
+ key: value.keys[0],
42
+ value: value.values.join(','))
38
43
  end
39
44
 
40
45
  def to_s