containers 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/exe/ctrs +5 -0
  3. data/lib/containers/cli.rb +32 -115
  4. data/lib/containers/commands/attach.rb +11 -0
  5. data/lib/containers/commands/bash.rb +11 -0
  6. data/lib/containers/commands/bin.rb +11 -0
  7. data/lib/containers/commands/down.rb +8 -0
  8. data/lib/containers/commands/exec.rb +14 -0
  9. data/lib/containers/commands/inspect.rb +11 -0
  10. data/lib/containers/commands/list.rb +23 -0
  11. data/lib/containers/commands/rails/rails.rb +11 -0
  12. data/lib/containers/commands/restart.rb +12 -0
  13. data/lib/containers/commands/ruby/bundle.rb +11 -0
  14. data/lib/containers/commands/ruby/rake.rb +11 -0
  15. data/lib/containers/commands/start.rb +12 -0
  16. data/lib/containers/commands/stop.rb +12 -0
  17. data/lib/containers/commands/tail.rb +11 -0
  18. data/lib/containers/commands/up.rb +8 -0
  19. data/lib/containers/commands/yarn/yarn.rb +11 -0
  20. data/lib/containers/concerns/commandable.rb +19 -0
  21. data/lib/containers/concerns/configurable.rb +62 -0
  22. data/lib/containers/generator/cli.rb +43 -0
  23. data/lib/containers/generator/commands/compose.rb +51 -0
  24. data/lib/containers/generator/commands/config.rb +43 -0
  25. data/lib/containers/generator/commands/dockerfile.rb +46 -0
  26. data/lib/containers/generator/templates/containers.yml.erb +15 -0
  27. data/lib/containers/{templates → generator/templates}/docker-compose.yml.erb +21 -38
  28. data/lib/containers/version.rb +1 -1
  29. data/lib/containers.rb +0 -4
  30. metadata +106 -32
  31. data/Gemfile +0 -9
  32. data/Gemfile.lock +0 -106
  33. data/LICENSE.txt +0 -21
  34. data/README.md +0 -24
  35. data/Rakefile +0 -12
  36. data/containers.gemspec +0 -42
  37. data/lib/containers/generator.rb +0 -40
  38. /data/lib/containers/{templates → generator/templates}/Dockerfile.erb +0 -0
  39. /data/lib/containers/{templates/redis-cache.conf → generator/templates/redis/cache.conf} +0 -0
  40. /data/lib/containers/{templates/redis-queue.conf → generator/templates/redis/queue.conf} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 10eb220c07fba010dcc7f06bfbece1feef00f33f7521ea6c64422e0158d5f4b2
4
- data.tar.gz: fc61199216e1aa3fe368d5a26b98b775a31cf3ac0a6723fae29c25d0fe5f9912
3
+ metadata.gz: cd15c1e87342149b417b3b8671d6a435badd75d2bed6c32d4ca81d5cdce6e5b8
4
+ data.tar.gz: 9066bc4c1a4890a9eea31c5d22d92ed812090ebdb3bc45dad76f130ae686ad7f
5
5
  SHA512:
6
- metadata.gz: 9d597a36e082a8d692de221f41e60d07a38bb509cf08e56d47123178ee680bd46af86f8981b57aef772772a4da587976c5894da36f943127ec5b259d50cd6d07
7
- data.tar.gz: 50dc68c00349412c3f74a799a0c47b054dea7cdd8d078a9c87987a3d79b2f84d42fbd6540414ccb00b5757b6bfcea09ccce1dd09b688b8769e473fbca4d1f040
6
+ metadata.gz: 2f6d65ae82057568913304f231b84fa46671418d2e4119469b0e6691cc5937663b33cd06fc4787b5c7fabaffd4f364774fc49b4313a2a8aee71a38d7b4b1d9d0
7
+ data.tar.gz: 184d0fe60b715ec9e90c39520b54eba1045e13c2247c35ec849d885aa371d7cfcbd510a4c98d1ffaa4f011d3fd7b661b8225c71431f3a1e735218b38cde19840
data/exe/ctrs ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "containers"
4
+
5
+ Containers::CLI.start(ARGV)
@@ -1,141 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pry-byebug"
3
4
  require "thor"
4
5
  require "rainbow"
5
- require_relative "./generator"
6
+ require "active_support/all"
7
+ require_relative "concerns/commandable"
8
+ require_relative "concerns/configurable"
9
+ require_relative "generator/cli"
6
10
 
7
11
  module Containers
8
12
  class CLI < Thor
9
- COMMAND_PREFIX = "▶"
13
+ include Commandable
14
+ include Configurable
10
15
 
11
- alias_method :execute, :exec
16
+ Dir["commands/**/*.rb", base: __dir__].each { |f| require_relative f }
12
17
 
13
18
  desc "generate", "Commands used to generate files for the project"
14
- subcommand "generate", Generator
19
+ subcommand "generate", Generator::CLI
15
20
 
16
- desc "up", "Brings up the environment defined in docker-compose.yml"
17
- def up(*args)
18
- execute_command "docker compose up -d #{args.join " "}"
19
- end
20
-
21
- desc "down", "Tears down the environment defined in docker-compose.yml"
22
- def down(*args)
23
- execute_command "docker compose down #{args.join " "}"
24
- end
25
-
26
- desc "list", "Lists all containers for this project"
27
- method_option :detailed, type: :boolean, aliases: "-d", desc: "List detailed container information"
28
- method_option :service, type: :boolean, aliases: "-s", desc: "List container service names"
29
- def list(*args)
30
- return execute_command "docker ps -a | grep #{project_name}" if options[:detailed]
31
-
32
- if options[:service]
33
- command = "containers list -f"
34
- puts_command "#{command} | #{Rainbow("(strip project name)").green.faint}"
35
- list = `#{command}`.split("\n").reject { |item| item.strip == "" || item.include?(COMMAND_PREFIX) }
36
- puts list.map { |item| item.gsub(/\A#{project_name}-|\s/, "") }.sort.join("\n")
37
- return
38
- end
39
-
40
- command = "containers list -d"
41
- puts_command "#{command} | #{Rainbow("(strip docker details)").green.faint}"
42
- list = `#{command}`.split("\n").reject { |item| item.strip == "" || item.include?(COMMAND_PREFIX) }
43
- puts list.map { |item| item.split(" ").last.strip }.sort.join("\n")
44
- end
45
-
46
- desc "inspect", "Inspects a container"
47
- method_option :container, type: :string, aliases: "-c", required: true, desc: "The short name for the container"
48
- def inspect(*args)
49
- execute_command "docker inspect #{args.join " "} #{project_name}-#{options[:container]}"
50
- end
51
-
52
- desc "attach", "Attaches to a running container"
53
- method_option :container, type: :string, aliases: "-c", required: true, desc: "The short name for the container"
54
- def attach(*args)
55
- execute_command "docker attach #{project_name}-#{options[:container]} #{args.join " "}"
56
- end
57
-
58
- desc "exec", "Executes a command in a container"
59
- method_option :container, type: :string, aliases: "-c", default: "shell", desc: "The short name for the container"
60
- def exec(*args)
61
- options[:container] = "shell" if options[:container].to_s.strip == ""
62
- execute_command "docker exec -it #{project_name}-#{options[:container]} #{args.join " "}"
63
- end
64
-
65
- desc "tail", "Tails container logs"
66
- method_option :container, type: :array, aliases: "-c", required: true, desc: "The short name for the container (also supports a list of names)"
67
- def tail(*args)
68
- args << "--since 5m" unless args.include?("--since")
69
- execute_command "docker compose logs #{args.join " "} -f #{options[:container].join " "}"
70
- end
21
+ protected
71
22
 
72
- desc "start", "Starts container(s)"
73
- method_option :container, type: :string, aliases: "-c", desc: "The short name for the container"
74
- def start(*args)
75
- return execute_command "docker start #{args.join " "} #{project_name}-#{options[:container]}" if options[:container]
76
- container_names.each { |c| execute_command "docker start #{args.join " "} #{c}", replace_current_process: false }
23
+ def container_name(service_name)
24
+ service_name ||= docker_default_service
25
+ return nil unless service_name
26
+ "#{app_name}-#{service_name}"
77
27
  end
78
28
 
79
- desc "stop", "Stops container(s)"
80
- method_option :container, type: :string, aliases: "-c", desc: "The short name for the container"
81
- def stop(*args)
82
- return execute_command "docker stop #{args.join " "} #{project_name}-#{options[:container]}" if options[:container]
83
- container_names.each { |c| execute_command "docker stop #{args.join " "} #{c}", replace_current_process: false }
29
+ def service_name(container_name)
30
+ return nil unless container_name
31
+ container_name.sub(/\A#{app_name}-/, "")
84
32
  end
85
33
 
86
- desc "restart", "Restarts container(s)"
87
- method_option :container, type: :array, aliases: "-c", desc: "A list of container short names"
88
- def restart(*args)
89
- containers = options[:container] || container_names
90
- containers.each { |c| execute_command "docker restart #{args.join " "} #{c}", replace_current_process: false }
91
- end
92
-
93
- desc "bash", "Starts a bash shell"
94
- method_option :container, type: :string, aliases: "-c", default: "shell", desc: "The short name for the container"
95
- def bash(*args)
96
- execute_command "containers exec -c #{options[:container]} bash #{args.join " "}"
97
- end
98
-
99
- desc "bundle", "Runs the bundle command"
100
- method_option :container, type: :string, aliases: "-c", default: "shell", desc: "The short name for the container"
101
- def bundle(*args)
102
- execute_command "containers exec -c #{options[:container]} bundle #{args.join " "}"
103
- end
104
-
105
- desc "yarn", "Runs the yarn command"
106
- method_option :container, type: :string, aliases: "-c", default: "shell", desc: "The short name for the container"
107
- def yarn(*args)
108
- execute_command "containers exec -c #{options[:container]} yarn #{args.join " "}"
109
- end
110
-
111
- desc "rails", "Runs the Rails command"
112
- method_option :container, type: :string, aliases: "-c", default: "shell", desc: "The short name for the container"
113
- def rails(*args)
114
- execute_command "containers bundle -c #{options[:container]} exec rails #{args.join " "}"
34
+ def service_names
35
+ `containers list -s`.split("\n").reject do |line|
36
+ line.nil? || line.empty? || line.include?(Containers::Commandable::PREFIX)
37
+ end
115
38
  end
116
39
 
117
- private
118
-
119
40
  def container_names
120
- `containers list -f`.split("\n")
121
- end
122
-
123
- def project_name
124
- squish(File.basename(Dir.pwd)).gsub(/\s|_/, "-")
125
- end
126
-
127
- def squish(string)
128
- string.gsub(/\s+/, " ").strip
41
+ `containers list`.split("\n").reject do |line|
42
+ line.nil? || line.empty? || line.include?(Containers::Commandable::PREFIX)
43
+ end
129
44
  end
130
45
 
131
- def execute_command(command, replace_current_process: true)
132
- command = squish(command)
133
- puts_command command
134
- replace_current_process ? execute(command) : puts(`#{command}`)
46
+ def requested_container_names
47
+ return options[:container].compact if options[:container]
48
+ return requested_service_names.map { |name| container_name name }.compact if options[:service]
49
+ container_names
135
50
  end
136
51
 
137
- def puts_command(command)
138
- puts "#{Rainbow(COMMAND_PREFIX).green.faint} #{Rainbow(command).green.bright}"
52
+ def requested_service_names
53
+ return options[:service].compact if options[:service]
54
+ return requested_container_names(options).map { |name| service_name name }.compact if options[:container]
55
+ service_names
139
56
  end
140
57
  end
141
58
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "attach", "Attaches to a running container"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def attach(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "docker attach #{container} #{args.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "bash", "Starts a bash shell"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def bash(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "containers exec -c #{container} bash #{args.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "bin", "Runs executables in the ./bin directory"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def bin(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "containers exec -c #{container} bin/#{args.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "down", "Tears down the environment defined in docker-compose.yml"
5
+ def down(*args)
6
+ execute_command "docker compose #{docker_compose_files.map { |f| "-f #{f}" }.join " "} down #{args.join " "}"
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "exec", "Executes a command in a container"
5
+ long_desc "Also aliased as `x` for convenience"
6
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
7
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
8
+ def exec(*args)
9
+ container = options[:container] || container_name(options[:service])
10
+ execute_command "docker exec -it #{container} #{args.join " "}"
11
+ end
12
+
13
+ map x: :exec
14
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "inspect", "Inspects a container"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def inspect(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "docker inspect #{args.join " "} #{container}"
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "list", "Lists all containers for this project"
5
+ method_option :detailed, type: :boolean, aliases: "-d", desc: "List detailed container information"
6
+ method_option :service, type: :boolean, aliases: "-s", desc: "List container service names"
7
+ def list(*args)
8
+ return execute_command "docker ps -a | grep #{app_name}" if options[:detailed]
9
+
10
+ if options[:service]
11
+ command = "containers list"
12
+ puts_command "#{command} | #{Rainbow("(strip app_name)").green.faint}"
13
+ list = `#{command}`.split("\n").reject { |item| item.strip == "" || item.include?(PREFIX) }
14
+ puts list.map { |item| item.gsub(/\A#{app_name}-|\s/, "") }.sort.join("\n")
15
+ return
16
+ end
17
+
18
+ command = "containers list -d"
19
+ puts_command "#{command} | #{Rainbow("(strip docker details)").green.faint}"
20
+ list = `#{command}`.split("\n").reject { |item| item.strip == "" || item.include?(PREFIX) }
21
+ puts list.map { |item| item.split(" ").last.strip }.sort.join("\n")
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "rails", "Runs the Rails command"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def rails(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "containers bundle -c #{container} exec rails #{args.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "restart", "Restarts container(s)"
5
+ method_option :container, type: :array, aliases: "-c", desc: "A list of container names (space delimited)"
6
+ method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
7
+ def restart(*args)
8
+ requested_container_names.each do |container_name|
9
+ execute_command "docker restart #{args.join " "} #{container_name}", replace_current_process: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "bundle", "Runs the bundle command"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def bundle(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "containers exec -c #{container} bundle #{args.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "rake", "Runs ruby's rake command"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def rake(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "containers exec -c #{container} rake #{args.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "start", "Starts container(s)"
5
+ method_option :container, type: :array, aliases: "-c", desc: "A list of container names (space delimited)"
6
+ method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
7
+ def start(*args)
8
+ requested_container_names.each do |container_name|
9
+ execute_command "docker start #{args.join " "} #{container_name}", replace_current_process: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "stop", "Stops container(s)"
5
+ method_option :container, type: :array, aliases: "-c", desc: "A list of container names (space delimited)"
6
+ method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
7
+ def stop(*args)
8
+ requested_container_names.each do |container_name|
9
+ execute_command "docker stop #{args.join " "} #{container_name}", replace_current_process: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "tail", "Tails container logs"
5
+ method_option :container, type: :array, aliases: "-c", desc: "A list of container names (space delimited)"
6
+ method_option :service, type: :array, aliases: "-s", desc: "A list of service names (space delimited)"
7
+ def tail(*args)
8
+ args << "--since 5m" unless args.include?("--since")
9
+ execute_command "docker compose #{docker_compose_files.map { |f| "-f #{f}" }.join " "} logs #{args.join " "} -f #{requested_service_names.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "up", "Brings up the environment defined in docker-compose.yml"
5
+ def up(*args)
6
+ execute_command "docker compose #{docker_compose_files.map { |f| "-f #{f}" }.join " "} up -d #{args.join " "}"
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Containers::CLI < Thor
4
+ desc "yarn", "Runs the yarn command"
5
+ method_option :container, type: :string, aliases: "-c", desc: "The container name"
6
+ method_option :service, type: :string, aliases: "-s", desc: "The service name"
7
+ def yarn(*args)
8
+ container = options[:container] || container_name(options[:service])
9
+ execute_command "containers exec -c #{container} yarn #{args.join " "}"
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Containers::Commandable
4
+ extend ActiveSupport::Concern
5
+
6
+ PREFIX = "▶"
7
+
8
+ alias_method :execute, :exec
9
+
10
+ def puts_command(command)
11
+ puts "#{Rainbow(PREFIX).green.faint} #{Rainbow(command).green.bright}"
12
+ end
13
+
14
+ def execute_command(command, replace_current_process: true)
15
+ command = command.squish
16
+ puts_command command
17
+ replace_current_process ? execute(command) : puts(`#{command}`)
18
+ end
19
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Containers::Configurable
4
+ extend ActiveSupport::Concern
5
+
6
+ def self.gitname
7
+ email = begin
8
+ `git config --get user.email`.strip
9
+ rescue
10
+ nil
11
+ end
12
+ email&.split("@")&.first || "example"
13
+ end
14
+
15
+ DEFAULT_CONFIGURATION = {
16
+ "organization" => {
17
+ "name" => gitname
18
+ },
19
+ "app" => {
20
+ "name" => File.basename(Dir.pwd).parameterize,
21
+ "directory" => "."
22
+ },
23
+ "docker" => {
24
+ "directory" => ".",
25
+ "default_service" => nil,
26
+ "compose_files" => ["docker-compose.yml"]
27
+ }
28
+ }.freeze
29
+
30
+ def organization_name
31
+ configuration["organization"]["name"]
32
+ end
33
+
34
+ def app_name
35
+ configuration["app"]["name"]
36
+ end
37
+
38
+ def app_directory
39
+ configuration["app"]["directory"]
40
+ end
41
+
42
+ def docker_directory
43
+ configuration["docker"]["directory"]
44
+ end
45
+
46
+ def docker_default_service
47
+ configuration["docker"]["default_service"]
48
+ end
49
+
50
+ def docker_compose_files
51
+ configuration["docker"]["compose_files"]
52
+ end
53
+
54
+ def configuration
55
+ @configuration ||=
56
+ if File.exist?(".containers.yml")
57
+ YAML.load_file(".containers.yml")
58
+ else
59
+ DEFAULT_CONFIGURATION
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+ require "net/http"
5
+ require "uri"
6
+
7
+ module Containers::Generator
8
+ class CLI < Thor
9
+ include Containers::Commandable
10
+ Dir["commands/**/*.rb", base: __dir__].each { |f| require_relative f }
11
+
12
+ protected
13
+
14
+ def template_path(template_name)
15
+ Pathname.new(__dir__).join "templates/#{template_name}.erb"
16
+ end
17
+
18
+ def template_string(template_name)
19
+ File.read template_path(template_name)
20
+ end
21
+
22
+ def template(template_name)
23
+ ERB.new template_string(template_name), trim_mode: "-"
24
+ end
25
+
26
+ def render_template(template_name, vars = {})
27
+ view = Struct.new(*vars.keys).new(*vars.values).instance_eval { binding }
28
+ template(template_name).result view
29
+ end
30
+
31
+ def render_external_template(template, vars = {})
32
+ view = Struct.new(*vars.keys).new(*vars.values).instance_eval { binding }
33
+
34
+ template_string = if template.start_with?("http")
35
+ Net::HTTP.get URI.parse(template)
36
+ else
37
+ File.read template
38
+ end
39
+
40
+ ERB.new(template_string).result view
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require_relative "../../concerns/configurable"
5
+
6
+ class Containers::Generator::CLI < Thor
7
+ include ::Containers::Configurable
8
+
9
+ desc "compose", "Creates a docker-compose.yml file for the project"
10
+ method_option :template, type: :string, aliases: "-t", desc: "The docker-compose.yml template to use (can be a local file or a URL)"
11
+ def compose
12
+ FileUtils.mkdir_p docker_directory
13
+ path = File.expand_path("#{docker_directory}/docker-compose.yml")
14
+ exists = File.exist?(path)
15
+ original = File.read(path) if exists
16
+
17
+ continue = if exists
18
+ ask("#{Rainbow("docker-compose.yml already exists").red} Overwrite?", default: "n").to_s.upcase == "Y"
19
+ else
20
+ true
21
+ end
22
+
23
+ return unless continue
24
+
25
+ FileUtils.rm_f path
26
+
27
+ vars = {
28
+ organization: {name: ask("What is the organization name?", default: organization_name).to_s},
29
+ app: {
30
+ name: ask("What is the app name?", default: app_name).to_s.parameterize,
31
+ directory: File.expand_path(ask("What is the path to the app directory? ", default: app_directory).to_s)
32
+ }
33
+ }
34
+
35
+ contents = if options[:template]
36
+ render_external_template options[:template], vars
37
+ else
38
+ render_template "docker-compose.yml", vars
39
+ end
40
+
41
+ puts_command Rainbow("(Create #{path})").green.faint
42
+ File.write path, contents
43
+ puts Rainbow("docker-compose.yml created successfully").green.bright
44
+ rescue => error
45
+ puts Rainbow("Unexpected error! #{error.message}").red.bright
46
+ if exists && original
47
+ puts Rainbow("Restoring the original file.").green.faint
48
+ File.write path, original
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../concerns/configurable"
4
+
5
+ class Containers::Generator::CLI < Thor
6
+ include ::Containers::Configurable
7
+
8
+ desc "config", "Creates a .containers.yml config file for the project"
9
+ def config
10
+ path = File.expand_path(".containers.yml")
11
+ exists = File.exist?(path)
12
+ original = File.read(path) if exists
13
+
14
+ continue = if exists
15
+ ask("#{Rainbow(".containers.yml already exists").red} Overwrite?", default: "n").to_s.upcase == "Y"
16
+ else
17
+ true
18
+ end
19
+
20
+ return unless continue
21
+
22
+ FileUtils.rm_f path
23
+
24
+ vars = {
25
+ organization_name: ask("What is the organization name?", default: organization_name).to_s.parameterize,
26
+ app_name: ask("What is the app name?", default: app_name).to_s.parameterize,
27
+ app_directory: File.expand_path(ask("What is the path to the app directory? ", default: app_directory).to_s),
28
+ docker: {
29
+ directory: File.expand_path(ask("What is the path to the Docker directory? ", default: docker_directory).to_s),
30
+ default_service: ask("What is the default Docker service?", default: docker_default_service).to_s,
31
+ compose_files: docker_compose_files
32
+ }
33
+ }
34
+
35
+ File.write path, render_template("containers.yml", vars)
36
+ rescue => error
37
+ puts Rainbow("Unexpected error! #{error.message}").red.bright
38
+ if exists && original
39
+ puts Rainbow("Restoring the original file.").green.faint
40
+ File.write path, original
41
+ end
42
+ end
43
+ end