Class: PackRb::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_rb/packer.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Packer

Returns a new instance of Packer.



25
26
27
28
29
30
# File 'lib/pack_rb/packer.rb', line 25

def initialize(opts = {})
  @machine_readable = opts[:machine_readable]
  @bin_path         = opts[:bin_path]
  @tpl              = opts[:tpl]
  @stream_output    = opts.fetch(:stream_output, false)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pack_rb/packer.rb', line 62

def method_missing(name, *args, &block)
  return super unless commander.respond_to?(name)

  opts = {
    base_cmd: command,
    tpl: template,
    args: args.first
  }

  commander.send(name, opts)
end

Instance Method Details

#binObject

Raises:

  • (RuntimeError)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pack_rb/packer.rb', line 40

def bin
  return bin_path if bin_path
  # some distros, such as Arch Linux, package the Packer
  # binary as ``packer-io``
  ['packer', 'packer-io'].each do |bin_name|
    p = find_executable(bin_name)
    return p if p
  end
  raise RuntimeError, "Could not find packer or packer-io binary on path." \
                      " Please specify the full binary path with the " \
                      "'bin_path' option."
end

#commandObject



32
33
34
35
36
37
38
# File 'lib/pack_rb/packer.rb', line 32

def command
  if machine_readable
    "#{bin} -machine-readable"
  else
    bin
  end
end

#commanderObject



58
59
60
# File 'lib/pack_rb/packer.rb', line 58

def commander
  @commander ||= PackRb::SubCommands.new(stream_output: @stream_output)
end

#templateObject



53
54
55
56
# File 'lib/pack_rb/packer.rb', line 53

def template
  obj = @tpl
  json?(obj) || path?(obj) || hash?(obj)
end