Class: Kdeploy::OutputFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/output_formatter.rb

Overview

Formats and displays execution results

Instance Method Summary collapse

Constructor Details

#initializeOutputFormatter

Returns a new instance of OutputFormatter.



9
10
11
# File 'lib/kdeploy/output_formatter.rb', line 9

def initialize
  @pastel = Pastel.new
end

Instance Method Details

#build_summary_line(host, counts, max_host_len) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/kdeploy/output_formatter.rb', line 94

def build_summary_line(host, counts, max_host_len)
  ok_w = 7
  changed_w = 11
  failed_w = 10

  ok_str = @pastel.green("ok=#{counts[:ok].to_s.ljust(ok_w - 3)}")
  changed_str = @pastel.yellow("changed=#{counts[:changed].to_s.ljust(changed_w - 8)}")
  failed_str = @pastel.red("failed=#{counts[:failed].to_s.ljust(failed_w - 7)}")
  "#{host.ljust(max_host_len)} : #{ok_str}  #{changed_str}  #{failed_str}"
end

#calculate_summary_counts(result) ⇒ Object



87
88
89
90
91
92
# File 'lib/kdeploy/output_formatter.rb', line 87

def calculate_summary_counts(result)
  ok = %i[success changed].include?(result[:status]) ? result[:output].size : 0
  failed = result[:status] == :failed ? 1 : 0
  changed = result[:status] == :changed ? result[:output].size : 0
  { ok: ok, failed: failed, changed: changed }
end

#colorize_summary_line(line, counts) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/kdeploy/output_formatter.rb', line 105

def colorize_summary_line(line, counts)
  if counts[:failed].positive?
    @pastel.red(line)
  elsif counts[:ok].positive? && counts[:failed].zero?
    @pastel.green(line)
  else
    line
  end
end

#format_command_for_dry_run(command) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/kdeploy/output_formatter.rb', line 139

def format_command_for_dry_run(command)
  case command[:type]
  when :run
    "#{@pastel.green('>')} #{command[:command]}"
  when :upload
    "#{@pastel.blue('>')} Upload: #{command[:source]} -> #{command[:destination]}"
  when :upload_template
    "#{@pastel.blue('>')} Template: #{command[:source]} -> #{command[:destination]}"
  else
    "#{@pastel.blue('>')} #{command[:type]}: #{command}"
  end
end

#format_dry_run_box(title, content) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/kdeploy/output_formatter.rb', line 115

def format_dry_run_box(title, content)
  TTY::Box.frame(
    content,
    title: { top_left: " #{title} " },
    style: {
      border: {
        fg: :yellow
      }
    }
  )
end

#format_dry_run_headerObject



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kdeploy/output_formatter.rb', line 127

def format_dry_run_header
  TTY::Box.frame(
    'Showing what would be done without executing any commands',
    title: { top_left: ' Dry Run Mode ', bottom_right: ' Kdeploy ' },
    style: {
      border: {
        fg: :blue
      }
    }
  )
end

#format_error(error_message) ⇒ Object



73
74
75
# File 'lib/kdeploy/output_formatter.rb', line 73

def format_error(error_message)
  @pastel.red("  ERROR: #{error_message}")
end

#format_file_step(step, type, prefix) ⇒ Object



45
46
47
48
49
# File 'lib/kdeploy/output_formatter.rb', line 45

def format_file_step(step, type, prefix)
  duration_str = format_duration(step[:duration])
  label = type == :upload ? '[upload]' : '[template]'
  color("    #{label} #{step[:command].sub(prefix, '')}#{duration_str}")
end

#format_file_steps(steps, shown, type, header, prefix) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/kdeploy/output_formatter.rb', line 34

def format_file_steps(steps, shown, type, header, prefix)
  output = [header]
  steps.each do |step|
    next if step_already_shown?(step, type, shown)

    mark_step_as_shown(step, type, shown)
    output << format_file_step(step, type, prefix)
  end
  output
end

#format_host_status(host, status) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/kdeploy/output_formatter.rb', line 17

def format_host_status(host, status)
  status_str = case status
               when :success then @pastel.green('ok')
               when :changed then @pastel.yellow('changed')
               else @pastel.red('failed')
               end
  @pastel.bright_white("\n#{host.ljust(24)} : #{status_str}")
end

#format_run_steps(steps, shown) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kdeploy/output_formatter.rb', line 51

def format_run_steps(steps, shown)
  output = []
  output << @pastel.cyan('  === Run ===')
  steps.each do |step|
    next if step_already_shown?(step, :run, shown)

    mark_step_as_shown(step, :run, shown)
    output.concat(format_single_run_step(step))
  end
  output
end

#format_single_run_step(step) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/kdeploy/output_formatter.rb', line 63

def format_single_run_step(step)
  output = []
  duration_str = format_duration(step[:duration])
  command_line = step[:command].to_s.lines.first.strip
  output << @pastel.cyan("    [run]    #{command_line}#{duration_str}")
  output.concat(format_multiline_command(step[:command]))
  output.concat(format_command_output(step[:output]))
  output
end

#format_summary_headerObject



77
78
79
# File 'lib/kdeploy/output_formatter.rb', line 77

def format_summary_header
  @pastel.cyan("\nPLAY RECAP #{'*' * 64}")
end

#format_summary_line(host, result, max_host_len) ⇒ Object



81
82
83
84
85
# File 'lib/kdeploy/output_formatter.rb', line 81

def format_summary_line(host, result, max_host_len)
  counts = calculate_summary_counts(result)
  line = build_summary_line(host, counts, max_host_len)
  colorize_summary_line(line, counts)
end

#format_task_header(task_name) ⇒ Object



13
14
15
# File 'lib/kdeploy/output_formatter.rb', line 13

def format_task_header(task_name)
  @pastel.cyan("\nPLAY [#{task_name}] " + ('*' * 64))
end

#format_template_steps(steps, shown) ⇒ Object



30
31
32
# File 'lib/kdeploy/output_formatter.rb', line 30

def format_template_steps(steps, shown)
  format_file_steps(steps, shown, :upload_template, @pastel.yellow('  === Template ==='), 'upload_template: ')
end

#format_upload_steps(steps, shown) ⇒ Object



26
27
28
# File 'lib/kdeploy/output_formatter.rb', line 26

def format_upload_steps(steps, shown)
  format_file_steps(steps, shown, :upload, @pastel.green('  === Upload ==='), 'upload: ')
end