aboutsummaryrefslogtreecommitdiff
path: root/vendor/supply_drop/lib/supply_drop/writer/colorful_streaming.rb
blob: e88e552aadc875a4e65beec04db03aa58be05dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
begin
  require 'paint'
rescue
end

module SupplyDrop
  module Writer
    class Streaming
      def initialize(logger)
        @mode = Capistrano::Logger::DEBUG
        @logger = logger
      end

      def collect_output(host, data)
        if data =~ /^(notice|err|warning):/
          @mode = $1

          # make deprecation warnings like notices
          if data =~ /^warning: .*is deprecated.*$/
            @mode = 'notice'
          end

          # make variable scope warnings like notices
          if data =~ /^warning: Scope*$/
            @mode = 'notice'
          end

          # force the printing of 'finished catalog run' if there have not been any errors
          if @mode == 'notice' && !@error_encountered && data =~ /Finished catalog run/
            @mode = 'forced_notice'
          elsif @mode == 'err'
            @error_encountered = true
          end
        end

        # log each line, colorizing the hostname
        data.lines.each do |line|
          if line =~ /\w/
            @logger.log log_level, line.sub(/\n$/,''), colorize(host)
          end
        end
      end

      def log_level
        case @mode
          when 'err'     then Capistrano::Logger::IMPORTANT
          when 'warning' then Capistrano::Logger::INFO
          when 'notice'  then Capistrano::Logger::DEBUG
          else Capistrano::Logger::IMPORTANT
        end
      end

      def colorize(str)
        if defined? Paint
          color = case @mode
            when 'err'     then :red
            when 'warning' then :yellow
            when 'notice'  then :cyan
            when 'forced_notice' then :cyan
            else :clear
          end
          Paint[str, color, :bold]
        else
          str
        end
      end

      def all_output_collected
      end
    end
  end
end