Module: Execution

Instance Method Summary collapse

Instance Method Details

#forkexec(command, prefix = nil, color = nil) ⇒ Object

unison doesn’t work when ran in a new thread this functions creates a full new process instead



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/docker-sync/execution.rb', line 39

def forkexec(command, prefix = nil, color = nil)

  if prefix.nil?
    # TODO: probably pick the command name without args
    prefix = 'unknown'
  end

  if color.nil?
    color = :cyan
  end

  Process.fork  {
    `#{command}` || raise(command + ' failed')
  }

end

#threadexec(command, prefix = nil, color = nil) ⇒ Object



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
# File 'lib/docker-sync/execution.rb', line 8

def threadexec(command, prefix = nil, color = nil)

  if prefix.nil?
    # TODO: probably pick the command name without args
    prefix = 'unknown'
  end

  if color.nil?
    color = :cyan
  end

  Thread.new {
    Open3.popen3(command) do |_, stdout, stderr, _|

      # noinspection RubyAssignmentExpressionInConditionalInspection
      while line_out = stdout.gets
        say_status prefix, line_out, color
      end

      # noinspection RubyAssignmentExpressionInConditionalInspection
      while line_err = stderr.gets
        say_status prefix, line_err, :red
      end

    end
  }

end