Class: Ufo::DockerfileUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/dockerfile_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(full_image_name, options = {}) ⇒ DockerfileUpdater

Returns a new instance of DockerfileUpdater.



3
4
5
6
7
# File 'lib/ufo/dockerfile_updater.rb', line 3

def initialize(full_image_name, options={})
  @full_image_name = full_image_name
  @options = options
  @project_root = options[:project_root] || '.'
end

Instance Method Details

#current_dockerfileObject



13
14
15
# File 'lib/ufo/dockerfile_updater.rb', line 13

def current_dockerfile
  @current_dockerfile ||= IO.read(dockerfile_path)
end

#dockerfile_pathObject



17
18
19
# File 'lib/ufo/dockerfile_updater.rb', line 17

def dockerfile_path
  "#{@project_root}/Dockerfile"
end

#new_dockerfileObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ufo/dockerfile_updater.rb', line 21

def new_dockerfile
  lines = current_dockerfile.split("\n")
  # replace FROM line
  new_lines = lines.map do |line|
                if line =~ /^FROM /
                  "FROM #{@full_image_name}"
                else
                  line
                end
              end
  new_lines.join("\n") + "\n"
end

#updateObject



9
10
11
# File 'lib/ufo/dockerfile_updater.rb', line 9

def update
  write_new_dockerfile
end

#write_new_dockerfileObject



34
35
36
37
38
39
40
# File 'lib/ufo/dockerfile_updater.rb', line 34

def write_new_dockerfile
  IO.write(dockerfile_path, new_dockerfile)
  unless @options[:mute]
    puts "The Dockerfile FROM statement has been updated with the latest base image:".green
    puts "  #{@full_image_name}".green
  end
end