Class: JackAndTheElasticBeanstalk::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/jack_and_the_elastic_beanstalk/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin:, stdout:, stderr:, logger:) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 8

def initialize(stdin:, stdout:, stderr:, logger:)
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @paths = [Pathname.pwd]
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 6

def logger
  @logger
end

#stderrObject (readonly)

Returns the value of attribute stderr.



5
6
7
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 5

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



3
4
5
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 3

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



4
5
6
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 4

def stdout
  @stdout
end

Instance Method Details

#capture3(*commands, options: {}, env: {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 47

def capture3(*commands, options: {}, env: {})
  logger.debug("jeb") { commands.inspect }

  Open3.capture3(env, *commands, { chdir: pwd.to_s }.merge(options)).tap do |out, err, status|
    logger.debug("jeb") { status.inspect }

    each_line(out, prefix: "stdout") do |line|
      logger.debug("jeb") { line }
    end

    each_line(err, prefix: "stderr") do |line|
      logger.debug("jeb") { line }
    end
  end
end

#capture3!(*commands, options: {}, env: {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 63

def capture3!(*commands, options: {}, env: {})
  out, err, status = capture3(*commands, options: options, env: env)

  unless status.success?
    raise "Faiiled to execute command: #{commands.inspect}"
  end

  [out, err]
end

#chdir(dir) ⇒ Object



26
27
28
29
30
31
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 26

def chdir(dir)
  paths.push dir
  yield
ensure
  paths.pop
end

#each_line(string, prefix: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 37

def each_line(string, prefix: nil)
  Array(string).flat_map {|s| s.split(/\n/) }.each do |line|
    if prefix
      yield "#{prefix}: #{line}"
    else
      yield line
    end
  end
end

#pathsObject



16
17
18
19
20
21
22
23
24
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 16

def paths
  id = "#{inspect}:paths".to_sym

  unless Thread.current[id]
    Thread.current[id] = @paths.dup
  end

  Thread.current[id]
end

#pwdObject



33
34
35
# File 'lib/jack_and_the_elastic_beanstalk/runner.rb', line 33

def pwd
  paths.last
end