Class: Containers::Generator::CLI

Inherits:
Thor
  • Object
show all
Includes:
Commandable, Configurable
Defined in:
lib/containers/generator/cli.rb,
lib/containers/generator/commands/config.rb,
lib/containers/generator/commands/compose.rb,
lib/containers/generator/commands/dockerfile.rb

Constant Summary

Constants included from Configurable

Configurable::DEFAULT_CONFIGURATION

Constants included from Commandable

Commandable::PREFIX

Instance Method Summary collapse

Methods included from Configurable

#app_directory, #app_name, #configuration, #docker_compose_files, #docker_default_service, #docker_directory, gitname, #organization_name

Methods included from Commandable

#execute_command, #puts_command

Instance Method Details

#composeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/containers/generator/commands/compose.rb', line 11

def compose
  FileUtils.mkdir_p docker_directory
  path = File.expand_path("#{docker_directory}/docker-compose.yml")
  exists = File.exist?(path)
  original = File.read(path) if exists

  continue = if exists
    ask("#{Rainbow("docker-compose.yml already exists").red} Overwrite?", default: "n").to_s.upcase == "Y"
  else
    true
  end

  return unless continue

  FileUtils.rm_f path

  vars = {
    organization: {name: ask("What is the organization name?", default: organization_name).to_s},
    app: {
      name: ask("What is the app name?", default: app_name).to_s.parameterize,
      directory: File.expand_path(ask("What is the path to the app directory? ", default: app_directory).to_s)
    }
  }

  contents = if options[:template]
    render_external_template options[:template], vars
  else
    render_template "docker-compose.yml", vars
  end

  puts_command Rainbow("(Create #{path})").green.faint
  File.write path, contents
  puts Rainbow("docker-compose.yml created successfully").green.bright
rescue => error
  puts Rainbow("Unexpected error! #{error.message}").red.bright
  if exists && original
    puts Rainbow("Restoring the original file.").green.faint
    File.write path, original
  end
end

#configObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/containers/generator/commands/config.rb', line 9

def config
  path = File.expand_path(".containers.yml")
  exists = File.exist?(path)
  original = File.read(path) if exists

  continue = if exists
    ask("#{Rainbow(".containers.yml already exists").red} Overwrite?", default: "n").to_s.upcase == "Y"
  else
    true
  end

  return unless continue

  FileUtils.rm_f path

  vars = {
    organization_name: ask("What is the organization name?", default: organization_name).to_s.parameterize,
    app_name: ask("What is the app name?", default: app_name).to_s.parameterize,
    app_directory: File.expand_path(ask("What is the path to the app directory? ", default: app_directory).to_s),
    docker: {
      directory: File.expand_path(ask("What is the path to the Docker directory? ", default: docker_directory).to_s),
      default_service: ask("What is the default Docker service?", default: docker_default_service).to_s,
      compose_files: docker_compose_files
    }
  }

  File.write path, render_template("containers.yml", vars)
rescue => error
  puts Rainbow("Unexpected error! #{error.message}").red.bright
  if exists && original
    puts Rainbow("Restoring the original file.").green.faint
    File.write path, original
  end
end

#dockerfileObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/containers/generator/commands/dockerfile.rb', line 11

def dockerfile
  FileUtils.mkdir_p docker_directory
  path = File.expand_path("#{docker_directory}/Dockerfile")
  exists = File.exist?(path)
  original = File.read(path) if exists

  continue = if exists
    ask("#{Rainbow("Dockerfile already exists").red} Overwrite?", default: "n").to_s.upcase == "Y"
  else
    true
  end

  return unless continue

  FileUtils.rm_f path

  ruby_version = ask("What Ruby version does this project use?", default: "3.1.2").to_s
  vars = {ruby_version: ruby_version}

  contents = if options[:template]
    render_external_template options[:template], vars
  else
    render_template "Dockerfile", vars
  end

  puts_command Rainbow("(Create #{path})").green.faint
  File.write path, contents
  puts Rainbow("Dockerfile created successfully").green.bright
rescue => error
  puts Rainbow("Unexpected error! #{error.message}").red.bright
  if exists && original
    puts Rainbow("Restoring the original file.").green.faint
    File.write path, original
  end
end