Class: Belt::CLI::TerraformCommand
- Inherits:
-
Object
- Object
- Belt::CLI::TerraformCommand
- Defined in:
- lib/belt/cli/terraform_command.rb
Constant Summary collapse
- ACTIONS =
%w[init plan apply destroy output].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(action, env, extra_args) ⇒ TerraformCommand
constructor
A new instance of TerraformCommand.
- #run ⇒ Object
Constructor Details
#initialize(action, env, extra_args) ⇒ TerraformCommand
Returns a new instance of TerraformCommand.
46 47 48 49 50 51 |
# File 'lib/belt/cli/terraform_command.rb', line 46 def initialize(action, env, extra_args) @action = action @env = env @extra_args = extra_args @infra_dir = self.class.find_infrastructure_dir end |
Class Method Details
.find_infrastructure_dir ⇒ Object
39 40 41 42 43 44 |
# File 'lib/belt/cli/terraform_command.rb', line 39 def self.find_infrastructure_dir %w[infrastructure infra].each do |dir| return dir if Dir.exist?(dir) end nil end |
.list_environments ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/belt/cli/terraform_command.rb', line 29 def self.list_environments infra_dir = find_infrastructure_dir return [] unless infra_dir Dir.children(infra_dir) .select { |d| File.directory?(File.join(infra_dir, d)) } .reject { |d| d.start_with?('.') || d == 'modules' } .sort end |
.run(action, args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/belt/cli/terraform_command.rb', line 10 def self.run(action, args) env = EnvResolver.resolve(args) if env.nil? puts "Usage: belt #{action} <environment> [terraform flags...]" puts "\nYou can also set BELT_ENV to skip the environment argument." puts "\nExamples:" puts " belt #{action} wups" puts " belt #{action} dev01" puts " BELT_ENV=wups belt #{action}" puts ' belt plan staging -target=module.lambda' puts "\nAvailable environments:" list_environments.each { |e| puts " #{e}" } exit 1 end new(action, env, args).run end |
Instance Method Details
#run ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/belt/cli/terraform_command.rb', line 53 def run validate! env_dir = File.join(@infra_dir, @env) args = ['terraform', @action, *@extra_args] puts "belt → #{args.join(' ')} (in #{env_dir}/)" Dir.chdir(env_dir) { exec(*args) } end |