Class: Coque::Sh

Inherits:
Cmd
  • Object
show all
Defined in:
lib/coque/sh.rb

Instance Attribute Summary collapse

Attributes included from Redirectable

#stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from Cmd

#get_default_fds, #pipe, #|

Methods included from Runnable

#log_start, #run, #run!, #success?, #to_a, #to_a!

Methods included from Redirectable

#<, #>, #>=, #err, #getio, #in, #out

Constructor Details

#initialize(context, args, stdin = nil, stdout = nil, stderr = nil) ⇒ Sh

Returns a new instance of Sh.



4
5
6
7
8
9
10
# File 'lib/coque/sh.rb', line 4

def initialize(context, args, stdin = nil, stdout = nil, stderr = nil)
  @context = context
  @args = args
  self.stdin = stdin if stdin
  self.stdout = stdout if stdout
  self.stderr = stderr if stderr
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/coque/sh.rb', line 3

def args
  @args
end

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/coque/sh.rb', line 3

def context
  @context
end

Instance Method Details

#[](*new_args) ⇒ Object



24
25
26
# File 'lib/coque/sh.rb', line 24

def [](*new_args)
  self.class.new(self.context, self.args + new_args)
end

#cloneObject



12
13
14
# File 'lib/coque/sh.rb', line 12

def clone
  self.class.new(context, args, stdin, stdout, stderr)
end

#get_resultObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coque/sh.rb', line 28

def get_result
  stdin, stdoutr, stdoutw = get_default_fds
  opts = {in: stdin, stdin.fileno => stdin.fileno,
          out: stdoutw, stdoutw.fileno => stdoutw.fileno,
          chdir: context.dir, unsetenv_others: context.disinherits_env?}

  # Redirect err to out: (e.g. for 2>&1)
  # {err: [:child, :out]}
  err_opts = if stderr
               {err: stderr, stderr.fileno => stderr.fileno}
             else
               {}
             end

  pid = spawn(context.env, args.join(" "), opts.merge(err_opts))

  stdoutw.close
  Result.new(pid, stdoutr)
end

#inspectObject



20
21
22
# File 'lib/coque/sh.rb', line 20

def inspect
  to_s
end

#to_sObject



16
17
18
# File 'lib/coque/sh.rb', line 16

def to_s
  "<Coque::Sh #{args.join(" ")}>"
end