Class: Kamaze::DockerImage::Command

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

Overview

Describe a command

Command is able to run itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, config = {}, extra = nil) ⇒ Command

Returns a new instance of Command.

Parameters:

  • command (Array<String>)
  • config (Hash) (defaults to: {})
  • extra (String|nil) (defaults to: nil)


23
24
25
26
27
28
29
30
31
32
# File 'lib/kamaze/docker_image/command.rb', line 23

def initialize(command, config = {}, extra = nil)
  command.clone.to_a.tap do |c|
    extras = Shellwords.split(extra.to_s).compact
    @parts = c.push(*extras).freeze
  end

  config.clone.to_h.tap do |c|
    @config = c.freeze
  end
end

Instance Attribute Details

#configHash (readonly)

Returns:

  • (Hash)


18
19
20
# File 'lib/kamaze/docker_image/command.rb', line 18

def config
  @config
end

#partsArray<String> (readonly, protected)

Returns:

  • (Array<String>)


58
59
60
# File 'lib/kamaze/docker_image/command.rb', line 58

def parts
  @parts
end

Instance Method Details

#executeBoolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/kamaze/docker_image/command.rb', line 51

def execute
  system(*self.to_a)
end

#run(&block) ⇒ Object Also known as: call



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

def run(&block)
  sh(*self.to_a, &block)
end

#sh(*cmd, &block) ⇒ Object (protected)



73
74
75
76
77
78
# File 'lib/kamaze/docker_image/command.rb', line 73

def sh(*cmd, &block)
  options = cmd.last.is_a?(Hash) ? cmd.pop : {}
  options[:verbose] = config[:verbose] unless options.key?(:verbose)

  utils.sh(*cmd.map(&:to_s).push(options), &block)
end

#to_aArray<String>

Returns:

  • (Array<String>)


35
36
37
# File 'lib/kamaze/docker_image/command.rb', line 35

def to_a
  parts.clone
end

#to_sString

Returns:

  • (String)


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

def to_s
  Shellwords.join(self.to_a)
end

#utilsObject (protected)

Get utils.

return [Class]



63
64
65
66
67
68
69
70
# File 'lib/kamaze/docker_image/command.rb', line 63

def utils
  if Gem::Specification.find_all_by_name('rake')
    require 'rake'
    require 'rake/file_utils'
  end

  Class.new { include FileUtils }.new
end