Method: Constancy::Diff#print_report

Defined in:
lib/constancy/diff.rb


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/constancy/diff.rb', line 110

def print_report
  puts '='*85
  puts @target.description(@mode)

  puts "  Keys scanned: #{@diff.count}"
  if Constancy.config.verbose?
    puts "  Keys ignored: #{self.items_to_ignore.count}"
    puts "  Keys in sync: #{self.items_to_noop.count}"
  end

  puts if self.any_changes?

  from_content_key, to_content_key, to_path_key, to_type_display_name =
    case @mode
    when :push then [:local_content, :remote_content, :consul_key, "Keys"]
    when :pull
      case @target.type
      when :dir then [:remote_content, :local_content, :display_filename, "Files"]
      when :file then [:remote_content, :local_content, :display_filename, "File entries"]
      end
    end

  @diff.each do |item|
    case item.op
    when :create
      puts "CREATE".bold.green + " #{item[to_path_key]}"
      puts '-'*85
      # simulate diff but without complaints about line endings
      item[from_content_key].each_line do |line|
        puts "+#{line.chomp}".green
      end
      puts '-'*85

    when :update
      puts "UPDATE".bold + " #{item[to_path_key]}"
      puts '-'*85
      puts Diffy::Diff.new(item[to_content_key], item[from_content_key]).to_s(:color)
      puts '-'*85

    when :delete
      if @target.delete?
        puts "DELETE".bold.red + " #{item[to_path_key]}"
        puts '-'*85
        # simulate diff but without complaints about line endings
        item[to_content_key].each_line do |line|
          puts "-#{line.chomp}".red
        end
        puts '-'*85
      else
        if Constancy.config.verbose?
          puts "IGNORE".bold + " #{item[to_path_key]}"
        end
      end

    when :ignore
      if Constancy.config.verbose?
        puts "IGNORE".bold + " #{item[to_path_key]}"
      end

    when :noop
      if Constancy.config.verbose?
        puts "NO-OP!".bold + " #{item[to_path_key]}"
      end

    else
      if Constancy.config.verbose?
        STDERR.puts "WARNING: unexpected operation '#{item.op}' for #{item[to_path_key]}"
      end

    end
  end

  if self.items_to_create.count > 0
    puts
    puts "#{to_type_display_name} to create: #{self.items_to_create.count}".bold
    self.items_to_create.each do |item|
      puts "+ #{item[to_path_key]}".green
    end
  end

  if self.items_to_update.count > 0
    puts
    puts "#{to_type_display_name} to update: #{self.items_to_update.count}".bold
    self.items_to_update.each do |item|
      puts "~ #{item[to_path_key]}".blue
    end
  end

  if @target.delete?
    if self.items_to_delete.count > 0
      puts
      puts "#{to_type_display_name} to delete: #{self.items_to_delete.count}".bold
      self.items_to_delete.each do |item|
        puts "- #{item[to_path_key]}".red
      end
    end
  end
end