Class: Joumae::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/joumae/command.rb

Constant Summary collapse

EXIT_STATUS_INTERRUPT =
130

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, resource_name:, client:) ⇒ Command

Returns a new instance of Command.



12
13
14
15
16
# File 'lib/joumae/command.rb', line 12

def initialize(cmd, resource_name:, client:)
  @cmd = cmd
  @resource_name = resource_name
  @client = client
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



10
11
12
# File 'lib/joumae/command.rb', line 10

def cmd
  @cmd
end

Instance Method Details

#loggerObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/joumae/command.rb', line 18

def logger
  @logger ||= Logger.new(STDOUT).tap do |logger|
    log_level_from_env = ENV['JOUMAE_LOG_LEVEL'] || 'INFO'
    logger.level = Logger.const_get(log_level_from_env)
    logger.formatter = proc do |severity, datetime, progname, msg|
      date_format = datetime.strftime("%Y-%m-%d %H:%M:%S")
      "#{cmd} (#{severity}): #{msg}\n"
    end
  end
end

#run!Object



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

def run!
  status = Joumae::Transaction.run!(resource_name: @resource_name, client: @client) do
    Open3.popen3(cmd) do |i, o, e, w|
      unless STDIN.tty?
        redirect(STDIN => i)
      end
      i.close

      redirect(o => STDOUT, e => STDERR)

      debug w.value

      w.value.exitstatus
    end
  end
  raise Joumae::CommandFailedError.new("Exit status(=#{status}) is non-zero.", status) if status != 0
end