Class: Foruiman::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/foruiman/process.rb

Overview

Adapted from Foreman::Process. Expansion belongs to the shell, not Ruby.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = {}) ⇒ Process

Returns a new instance of Process.



7
8
9
10
11
# File 'lib/foruiman/process.rb', line 7

def initialize(command, options = {})
  @command = command.dup.freeze
  @env = (options[:env] || ENV.to_h).dup.freeze
  @cwd = File.expand_path(options[:cwd] || Dir.pwd)
end

Instance Attribute Details

#command ⇒ Object (readonly)

Returns the value of attribute command.



5
6
7
# File 'lib/foruiman/process.rb', line 5

def command
  @command
end

#cwd ⇒ Object (readonly)

Returns the value of attribute cwd.



5
6
7
# File 'lib/foruiman/process.rb', line 5

def cwd
  @cwd
end

#env ⇒ Object (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/foruiman/process.rb', line 5

def env
  @env
end

Instance Method Details

#run(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/foruiman/process.rb', line 13

def run(options = {})
  environment = env.merge(options.fetch(:env, {}))
  redirects = { chdir: cwd, in: options.fetch(:input, $stdin), out: options.fetch(:output, $stdout),
                err: options.fetch(:error, $stderr), unsetenv_others: true, close_others: true }
  return spawn_session(environment, redirects) if options[:new_session]

  ::Process.spawn(environment, "/bin/sh", "-c", shell_command, **redirects, pgroup: true)
end