Module: Proteus::Generators::Context

Included in:
Proteus::Generate
Defined in:
lib/proteus/generators/context.rb

Class Method Summary collapse

Class Method Details

.included(thor_class) ⇒ Object



4
5
6
7
8
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
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/proteus/generators/context.rb', line 4

def self.included(thor_class)
  thor_class.class_eval do

    desc "context CONTEXT", "Generate a new context"
    def context(context)

      if context !~ /^([a-z0-9])+(_{1}[a-z0-9]+)*$/
        say "The name of your context has to be valid snake case. For example: 'foo_bar'", :red
        exit 1
      end

      context_directory = File.join(context_path(context))
      empty_directory(context_directory)
      empty_directory(File.join(context_directory, 'environments'))
      empty_directory(File.join(context_directory, 'modules'))

      template_binding = Proteus::Templates::TemplateBinding.new(
        context: context,
        environment: nil,
        module_name: nil
      )

      ['main', 'variables'].each do |template_name|
        template(
          "context/#{template_name}.tf.erb",
          File.join(
            context_directory,
            "#{template_name}.tf"
          ),
          context: template_binding.get_binding
        )
      end

      gitignore = File.join(destination_root, ".gitignore")

      if File.file?(gitignore)
        matches = File.readlines(gitignore).select do |line|
          line.match(/\/contexts\/#{context}\/backend.tf/)
        end
      end

      if !File.file?(gitignore) || matches.none?
        File.open(gitignore, 'a') do |file|
          file.puts "/contexts/#{context}/backend.tf"
        end
      end

      say "Context #{context} created. Go ahead and create environments and modules.", :green
    end
  end
end