File: output_redirection.rb

package info (click to toggle)
ruby-whenever 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 328 kB
  • sloc: ruby: 2,722; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,405 bytes parent folder | download | duplicates (2)
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
module Whenever
  module Output
    class Redirection
      def initialize(output)
        @output = output
      end
      
      def to_s
        return '' unless defined?(@output)
        case @output
          when String   then redirect_from_string
          when Hash     then redirect_from_hash
          when NilClass then ">> /dev/null 2>&1"
          when Proc     then @output.call
          else ''
        end 
      end
      
    protected
      
      def stdout
        return unless @output.has_key?(:standard)
        @output[:standard].nil? ? '/dev/null' : @output[:standard]
      end

      def stderr
        return unless @output.has_key?(:error)
        @output[:error].nil? ? '/dev/null' : @output[:error]
      end

      def redirect_from_hash
        case
          when stdout == '/dev/null' && stderr == '/dev/null'
            "> /dev/null 2>&1"
          when stdout && stderr == '/dev/null'
            ">> #{stdout} 2> /dev/null"
          when stdout && stderr
            ">> #{stdout} 2>> #{stderr}"
          when stderr == '/dev/null'
            "2> /dev/null"
          when stderr
            "2>> #{stderr}"
          when stdout == '/dev/null'
            "> /dev/null"
          when stdout
            ">> #{stdout}"
          else
            ''
        end
      end

      def redirect_from_string
        ">> #{@output} 2>&1"
      end
    end
  end
end