Class: Dash::Dockerfile::Instruction

Inherits:
Object
  • Object
show all
Defined in:
lib/dash/dockerfile/instruction.rb

Overview

One logical Dockerfile instruction: the keyword, its flags, and everything else on the line — continuations joined, heredoc bodies appended, comments dropped.

line is the first physical line the instruction started on, because that is the line an operator opens their editor at when advice names it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, args:, flags: {}, line: 1) ⇒ Instruction

Returns a new instance of Instruction.



12
13
14
15
16
17
# File 'lib/dash/dockerfile/instruction.rb', line 12

def initialize(name:, args:, flags: {}, line: 1)
  @name = name
  @args = args
  @flags = flags
  @line = line
end

Instance Attribute Details

#args ⇒ Object (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/dash/dockerfile/instruction.rb', line 9

def args
  @args
end

#flags ⇒ Object (readonly)

Returns the value of attribute flags.



9
10
11
# File 'lib/dash/dockerfile/instruction.rb', line 9

def flags
  @flags
end

#line ⇒ Object (readonly)

Returns the value of attribute line.



9
10
11
# File 'lib/dash/dockerfile/instruction.rb', line 9

def line
  @line
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/dash/dockerfile/instruction.rb', line 9

def name
  @name
end

#stage ⇒ Object

Returns the value of attribute stage.



10
11
12
# File 'lib/dash/dockerfile/instruction.rb', line 10

def stage
  @stage
end

Instance Method Details

#argv ⇒ Object

The exec form's arguments, or nil when this is the shell form (or malformed JSON, which BuildKit would reject but which must not take the analyzer down with it).



35
36
37
38
39
40
41
42
43
44
# File 'lib/dash/dockerfile/instruction.rb', line 35

def argv
  return @argv if defined?(@argv)

  @argv = begin
    parsed = JSON.parse(args) if args.start_with?("[")
    parsed if parsed.is_a?(Array) && parsed.all?(String)
  rescue JSON::ParserError
    nil
  end
end

#flag(key) ⇒ Object

The first value of a flag. Repeated flags (several --mounts on one RUN) keep every value in flags; callers that only care whether one is present ask for the first.



21
22
23
# File 'lib/dash/dockerfile/instruction.rb', line 21

def flag(key)
  Array(flags[key]).first
end

#flag?(key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dash/dockerfile/instruction.rb', line 25

def flag?(key)
  flags.key?(key)
end

#json? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/dash/dockerfile/instruction.rb', line 29

def json?
  args.start_with?("[") && !argv.nil?
end

#shell_command ⇒ Object

What the instruction runs, as one string, whichever form it was written in — the rules match against shell text and should not have to care.



48
49
50
# File 'lib/dash/dockerfile/instruction.rb', line 48

def shell_command
  json? ? argv.join(" ") : args
end

#to_s ⇒ Object

Collapsed to a single line so it can be compared with what buildx printed for the matching vertex, and so advice can name it without wrapping the terminal.



54
55
56
57
# File 'lib/dash/dockerfile/instruction.rb', line 54

def to_s
  [ name, *flags.flat_map { |key, values| values.map { |value| "--#{key}=#{value}" } }, args ]
    .reject(&:blank?).join(" ").gsub(/\s+/, " ")
end