Class: CemAcpt::Bolt::Cmd::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/cem_acpt/bolt/cmd/output.rb

Overview

Wraps the output of a Bolt command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_output, strict: true, **item_defaults) ⇒ Output

Returns a new instance of Output.



12
13
14
15
16
17
18
19
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 12

def initialize(cmd_output, strict: true, **item_defaults)
  @original_cmd_output = cmd_output
  @strict = strict
  @item_defaults = item_defaults.transform_keys(&:to_s)
  init_cmd_output_and_error_obj(cmd_output)
  @target_count = @cmd_output['target_count'] || 0
  @elapsed_time = @cmd_output['elapsed_time'] || 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 97

def method_missing(method, *args, **kwargs, &block)
  if @cmd_output.respond_to?(method)
    @cmd_output.send(method, *args, **kwargs, &block)
  elsif error.respond_to?(method)
    error.send(method, *args, **kwargs, &block)
  else
    super
  end
end

Instance Attribute Details

#cmd_outputObject

Returns the value of attribute cmd_output.



10
11
12
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 10

def cmd_output
  @cmd_output
end

#elapsed_timeObject

Returns the value of attribute elapsed_time.



10
11
12
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 10

def elapsed_time
  @elapsed_time
end

#error_objObject

Returns the value of attribute error_obj.



10
11
12
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 10

def error_obj
  @error_obj
end

#itemsObject

Returns the value of attribute items.



10
11
12
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 10

def items
  @items
end

#target_countObject

Returns the value of attribute target_count.



10
11
12
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 10

def target_count
  @target_count
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



82
83
84
85
86
87
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 82

def ==(other)
  return false unless other.is_a?(self.class)
  return false unless to_h == other.to_h

  items.zip(other.items).all? { |a, b| a == b }
end

#actionObject



21
22
23
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 21

def action
  items&.map(&:action)&.uniq || [@item_defaults['action']] || ['unknown']
end

#copy_with_new_items(new_items = []) ⇒ Object

Exists solely for Bolt tests



91
92
93
94
95
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 91

def copy_with_new_items(new_items = [])
  new_self = self.class.new(original_cmd_output, strict: @strict, **@item_defaults)
  new_self.items = new_items
  new_self
end

#errorObject



25
26
27
28
29
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 25

def error
  return nil unless error?

  items.find(&:error?)&.error || error_obj
end

#error?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 31

def error?
  !error_obj.nil? || items.any?(&:error?)
end

#inspectObject



60
61
62
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 60

def inspect
  "#<#{self.class}:#{object_id} #{self}>"
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 107

def respond_to_missing?(method, include_private = false)
  @cmd_output.respond_to?(method, include_private) || error.respond_to?(method, include_private) || super
end

#resultsObject



68
69
70
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 68

def results
  @results ||= new_results
end

#results?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 72

def results?
  !results.empty?
end

#statusObject



39
40
41
42
43
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 39

def status
  return 0 if success?

  1
end

#success?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 35

def success?
  !error?
end

#summaryObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 45

def summary
  @summary ||= [
    "status: #{success? ? 'passed' : 'failed'}",
    "items total: #{items.length}",
    "items succeeded: #{items.count(&:success?)}",
    "items failed: #{items.count(&:error?)}",
    "target count: #{target_count}",
    "elapsed time: #{elapsed_time}",
  ].join(', ')
end

#summary?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 56

def summary?
  true
end

#to_hObject



64
65
66
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 64

def to_h
  @cmd_output
end

#to_sObject



76
77
78
79
80
# File 'lib/cem_acpt/bolt/cmd/output.rb', line 76

def to_s
  return error.to_s if error?

  JSON.pretty_generate(@cmd_output)
end