Module: Arborist::CLI::Tree

Extended by:
Subcommand
Defined in:
lib/arborist/command/tree.rb

Overview

Command to dump the node tree of a running Arborist manager

Class Method Summary collapse

Methods included from Subcommand

display_table, error_string, exit_now!, extended, headline_string, help_now!, highlight_string, hl, prompt, skips_around, skips_post, skips_pre, success_string, unless_dryrun, visible_chars

Class Method Details

.ack_description(node) ⇒ Object

Return a description of the acknowledgement from the node.



215
216
217
218
219
220
221
222
223
224
# File 'lib/arborist/command/tree.rb', line 215

def ack_description( node )
  ack = node['ack'] or return '(no ack)'

  return " Acked by %s at %s%s: %s" % [
    ack['sender'],
    ack['time'],
    ack['via'] ? ' via ' + ack['via'] : '',
    ack['message']
  ]
end

.build_path(nodes, root) ⇒ Object

Given a sorted array of nodes, reorganize it for TTY::Tree.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/arborist/command/tree.rb', line 113

def build_path( nodes, root )
  children = []
  parent_node = nodes.shift
  return children unless parent_node

  if parent_node == root
    children << { node_description(parent_node) => build_tree(parent_node) }
  else
    children << { node_description(parent_node) => build_path(nodes, root) }
  end
  return children
end

.build_tree(node) ⇒ Object

Reorganize the node data to format used by TTY::Tree.



101
102
103
104
105
106
107
108
109
# File 'lib/arborist/command/tree.rb', line 101

def build_tree( node )
  return [] if node[ 'children' ].empty?

  children = []
  node[ 'children' ].each_value do |child|
    children << { node_description(child) => build_tree(child) }
  end
  return children
end

.errors_description(node) ⇒ Object

Return the errors from the specified node in a single line.



189
190
191
192
193
194
# File 'lib/arborist/command/tree.rb', line 189

def errors_description( node )
  errors = node['errors'] or return ''
  return '  ' + errors.map do |monid, error|
    "%s: %s" % [ monid, error ]
  end.join( '; ' )
end

.fetch_parents(start_node) ⇒ Object

Given a starting node, walk upwards through the tree until reaching the Arborist root node. Returns an array of nodes, sorted root down.



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/arborist/command/tree.rb', line 130

def fetch_parents( start_node )
  client = Arborist::Client.instance
  path = [ start_node ]
  parent = start_node[ 'parent' ]
  while parent
    parent_node = client.fetch_node( parent )
    path << parent_node
    parent = parent_node[ 'parent' ]
  end
  return path.reverse
end

.node_description(node) ⇒ Object

Return a description of the specified node.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/arborist/command/tree.rb', line 144

def node_description( node )
  desc = ""

  case node['type']
  when 'root'
    desc << "%s" % [ hl.bold.bright_blue(node['type']) ]
  else
    desc << highlight_string( node['identifier'] )
    desc << " %s" % [ hl.dark.white(node['type']) ]
  end

  desc << " [%s]" % [ node['description'] ] unless
    !node['description'] || node['description'].empty?
  desc << " (%s)" % [ status_description(node) ]

  child_count = node[ 'children' ].length
  desc << " [%d child node%s" % [
    child_count, child_count == 1 ? ']' : 's]'
  ] unless child_count.zero?

  case node['status']
  when 'down'
    desc << errors_description( node )
  when 'warn'
    desc << warnings_description( node )
  when 'quieted'
    desc << quieted_reasons_description( node )
  when 'acked'
    desc << ack_description( node )
    desc << "; was: "
    desc << errors_description( node )
  end

  return desc
end

.quieted_reasons_description(node) ⇒ Object

Return the quieted reasons from the specified node in a single line.



206
207
208
209
210
211
# File 'lib/arborist/command/tree.rb', line 206

def quieted_reasons_description( node )
  reasons = node['quieted_reasons'] or return ''
  return '  ' + reasons.map do |depname, reason|
    "%s: %s" % [ depname, reason ]
  end.join( '; ' )
end

.status_description(node) ⇒ Object

Return a more colorful description of the status of the given node.



182
183
184
185
# File 'lib/arborist/command/tree.rb', line 182

def status_description( node )
  status = node['status'] or return '-'
  return hl.decorate( status, status.to_sym ) rescue status
end

.warnings_description(node) ⇒ Object

Return the warnings from the specified node in a single line.



197
198
199
200
201
202
# File 'lib/arborist/command/tree.rb', line 197

def warnings_description( node )
  warnings = node['warnings'] or return ''
  return '  ' + warnings.map do |monid, error|
    "%s: %s" % [ monid, error ]
  end.join( '; ' )
end