Class: ConfigmonkeyCli::Application::ManifestAction::Invoke

Inherits:
Base
  • Object
show all
Defined in:
lib/configmonkey_cli/application/manifest_actions/invoke.rb

Instance Attribute Summary

Attributes inherited from Base

#app, #args, #manifest, #opts, #thor

Instance Method Summary collapse

Methods inherited from Base

#args_and_opts, #exists?, #expand_dst, #expand_src, #initialize, #prepare, #rel, #to_s

Constructor Details

This class inherits a constructor from ConfigmonkeyCli::Application::ManifestAction::Base

Instance Method Details

#destructiveObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/configmonkey_cli/application/manifest_actions/invoke.rb', line 18

def destructive
  status :invoke, :yellow, @args[0]
  code, res = exec(@args[0], @opts[:chomp])

  if opts[:echo]
    lines = res.split("\n")
    if code.exitstatus.zero?
      say padded("#{c "[OK]", :green} #{lines[0]}", :black)
      lines[1..-1].each{|l| say padded("     #{l}") } if lines.length > 1
    else
      say padded("[#{code.exitstatus}] #{lines[0]}", :red)
      lines[1..-1].each{|l| say padded("     #{l}") } if lines.length > 1
      raise "Invoked process exited with status #{code.exitstatus}: #{res}" if opts[:fail]
    end
  end
end

#exec(cmd, chomp = true) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/configmonkey_cli/application/manifest_actions/invoke.rb', line 35

def exec cmd, chomp = true
  app.debug "§invoking:#{cmd}"
  _stdin, _stdouterr, _thread = Open3.popen2e(cmd)
  _thread.join
  res = _stdouterr.read
  [_thread.value, chomp ? res.chomp : res]
ensure
  _stdin.close rescue false
  _stdouterr.close rescue false
end

#init(command = nil, opts = {}, &command_builder) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/configmonkey_cli/application/manifest_actions/invoke.rb', line 5

def init command = nil, opts = {}, &command_builder
  if command_builder
    command = "#{command}" # reference copy
    command = command_builder.call(command, opts)
  end
  @args = [command]
  @opts = opts.reverse_merge(chomp: true, echo: true, fail: true)
end

#simulateObject



14
15
16
# File 'lib/configmonkey_cli/application/manifest_actions/invoke.rb', line 14

def simulate
  status :invoke, :yellow, @args[0]
end