Class: Pkgr::Command

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

Defined Under Namespace

Classes: CommandFailed

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil, log_tag = nil) ⇒ Command

Returns a new instance of Command.



15
16
17
18
# File 'lib/pkgr/command.rb', line 15

def initialize(logger = nil, log_tag = nil)
  @logger = logger || Logger.new(STDOUT)
  @log_tag = log_tag
end

Class Attribute Details

.default_timeoutObject

Returns the value of attribute default_timeout.



11
12
13
# File 'lib/pkgr/command.rb', line 11

def default_timeout
  @default_timeout
end

Instance Attribute Details

#log_tagObject

Returns the value of attribute log_tag.



8
9
10
# File 'lib/pkgr/command.rb', line 8

def log_tag
  @log_tag
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/pkgr/command.rb', line 8

def logger
  @logger
end

Instance Method Details

#_run(command, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pkgr/command.rb', line 68

def _run(command, opts = {})
  env = opts[:env] || {}
  live_stream = opts[:live_stream]
  fail = !! opts[:fail]

  cmd = Mixlib::ShellOut.new(
    command,
    environment: env,
    timeout: self.class.default_timeout
  )

  # live stream is buggy, using IO.popen instead
  # cmd.live_stream = logger if live_stream
  cmd.logger = logger
  cmd.log_level = :debug
  cmd.log_tag = log_tag if log_tag
  cmd.run_command

  cmd.error! if fail

  cmd.stdout.chomp
rescue RuntimeError, Errno::ENOENT => e
  logger.error "Command failed: #{e.message}"

  msg = ["Command failed"]
  cmd.stdout.split("\n").each do |line|
    msg.push "STDOUT -- #{line}"
  end
  cmd.stderr.split("\n").each do |line|
    msg.push "STDERR -- #{line}"
  end
  raise CommandFailed, msg.join("\n")
end

#_stream(command, opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pkgr/command.rb', line 48

def _stream(command, opts = {})
  env = opts[:env] || {}
  fail = !! opts[:fail]
  env_string = env.map{|(k,v)| [k,"\"#{v}\""].join("=")}.join(" ")

  logger.debug "sh(#{command})"

  IO.popen("#{env_string} #{command}") do |io|
    until io.eof?
      data = io.gets
      puts data
    end
  end

  raise CommandFailed, "Failure during packaging" if fail && ! $?.exitstatus.zero?
# raised when file does not exist
rescue Errno::ENOENT, RuntimeError => e
  raise CommandFailed, e.message
end

#capture(command, env = {}) ⇒ Object



34
35
36
37
38
# File 'lib/pkgr/command.rb', line 34

def capture(command, env = {})
  value = _run(command, {env: env})
  value = yield(value) if block_given?
  value
end

#capture!(command, error_message, env = {}) ⇒ Object



28
29
30
31
32
# File 'lib/pkgr/command.rb', line 28

def capture!(command, error_message, env = {})
  value = _run(command, {env: env, fail: true})
  value = yield(value) if block_given?
  value
end

#run(command, env = {}) ⇒ Object



40
41
42
# File 'lib/pkgr/command.rb', line 40

def run(command, env = {})
  _run(command, {env: env})
end

#run!(command, env = {}) ⇒ Object



44
45
46
# File 'lib/pkgr/command.rb', line 44

def run!(command, env = {})
  _run(command, {env: env, fail: true})
end

#stream(command, env = {}) ⇒ Object



24
25
26
# File 'lib/pkgr/command.rb', line 24

def stream(command, env = {})
  _stream(command, {env: env, live_stream: logger})
end

#stream!(command, env = {}) ⇒ Object



20
21
22
# File 'lib/pkgr/command.rb', line 20

def stream!(command, env = {})
  _stream(command, {env: env, fail: true, live_stream: logger})
end