ruby-terraform 0.65.0.pre.1 → 0.65.0.pre.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +1,27 @@
1
- require 'json'
2
1
  require_relative 'base'
3
2
 
4
3
  module RubyTerraform
5
4
  module Commands
6
5
  class Apply < Base
7
- def configure_command(builder, opts)
8
- directory = opts[:directory]
9
- plan = opts[:plan]
10
- vars = opts[:vars] || {}
11
- var_file = opts[:var_file]
12
- var_files = opts[:var_files] || []
13
- target = opts[:target]
14
- targets = opts[:targets] || []
15
- state = opts[:state]
16
- input = opts[:input]
17
- auto_approve = opts[:auto_approve]
18
- no_backup = opts[:no_backup]
19
- backup = no_backup ? '-' : opts[:backup]
20
- no_color = opts[:no_color]
6
+ def sub_commands(_values)
7
+ %w[apply]
8
+ end
9
+
10
+ def switches
11
+ %w[-backup -lock -lock-timeout -input -auto-approve -no-color -state
12
+ -target -var -var-file]
13
+ end
14
+
15
+ def arguments(values)
16
+ [values[:plan] || values[:directory]]
17
+ end
18
+
19
+ def option_default_values(_opts)
20
+ { vars: {}, var_files: [], targets: [] }
21
+ end
21
22
 
22
- builder
23
- .with_subcommand('apply') do |sub|
24
- vars.each do |key, value|
25
- var_value = value.is_a?(String) ? value : JSON.generate(value)
26
- sub = sub.with_option(
27
- '-var', "'#{key}=#{var_value}'", separator: ' ')
28
- end
29
- sub = sub.with_option('-var-file', var_file) if var_file
30
- var_files.each do |file|
31
- sub = sub.with_option('-var-file', file)
32
- end
33
- sub = sub.with_option('-target', target) if target
34
- targets.each do |file|
35
- sub = sub.with_option('-target', file)
36
- end
37
- sub = sub.with_option('-state', state) if state
38
- sub = sub.with_option('-input', input) if input
39
- sub = sub.with_option('-auto-approve', auto_approve) unless
40
- auto_approve.nil?
41
- sub = sub.with_option('-backup', backup) if backup
42
- sub = sub.with_flag('-no-color') if no_color
43
- sub
44
- end
45
- .with_argument(plan || directory)
23
+ def option_override_values(opts)
24
+ { backup: opts[:no_backup] ? '-' : opts[:backup] }
46
25
  end
47
26
  end
48
27
  end
@@ -1,30 +1,25 @@
1
1
  require 'lino'
2
+
2
3
  require_relative '../errors'
4
+ require_relative '../options/factory'
3
5
 
4
6
  module RubyTerraform
5
7
  module Commands
6
8
  class Base
7
9
  def initialize(
8
- binary: nil, logger: nil, stdin: nil, stdout: nil, stderr: nil)
10
+ binary: nil, logger: nil, stdin: nil, stdout: nil, stderr: nil
11
+ )
9
12
  @binary = binary || RubyTerraform.configuration.binary
10
13
  @logger = logger || RubyTerraform.configuration.logger
11
14
  @stdin = stdin || RubyTerraform.configuration.stdin
12
15
  @stdout = stdout || RubyTerraform.configuration.stdout
13
16
  @stderr = stderr || RubyTerraform.configuration.stderr
17
+ initialize_command
14
18
  end
15
19
 
16
20
  def execute(opts = {})
17
- builder = instantiate_builder
18
-
19
21
  do_before(opts)
20
- command = configure_command(builder, opts).build
21
- logger.debug("Running '#{command.to_s}'.")
22
-
23
- command.execute(
24
- stdin: stdin,
25
- stdout: stdout,
26
- stderr: stderr
27
- )
22
+ build_and_execute_command(opts)
28
23
  do_after(opts)
29
24
  rescue Open4::SpawnError
30
25
  message = "Failed while running '#{command_name}'."
@@ -36,23 +31,69 @@ module RubyTerraform
36
31
 
37
32
  attr_reader :binary, :logger, :stdin, :stdout, :stderr
38
33
 
34
+ def build_and_execute_command(opts)
35
+ command = build_command(opts)
36
+ logger.debug("Running '#{command}'.")
37
+ command.execute(
38
+ stdin: stdin,
39
+ stdout: stdout,
40
+ stderr: stderr
41
+ )
42
+ end
43
+
39
44
  def command_name
40
- self.class.to_s.split("::")[-1].downcase
45
+ self.class.to_s.split('::')[-1].downcase
41
46
  end
42
47
 
43
- def instantiate_builder
48
+ def do_before(_opts); end
49
+
50
+ def do_after(_opts); end
51
+
52
+ private
53
+
54
+ def initialize_command; end
55
+
56
+ def build_command(opts)
57
+ values = apply_option_defaults_and_overrides(opts)
58
+
44
59
  Lino::CommandLineBuilder
45
- .for_command(binary)
46
- .with_option_separator('=')
60
+ .for_command(@binary)
61
+ .with_options_after_subcommands
62
+ .with_option_separator('=')
63
+ .with_appliables(options(values))
64
+ .with_subcommands(sub_commands(values))
65
+ .with_arguments(arguments(values))
66
+ .build
67
+ end
68
+
69
+ def apply_option_defaults_and_overrides(opts)
70
+ option_default_values(opts)
71
+ .merge(opts)
72
+ .merge(option_override_values(opts))
73
+ end
74
+
75
+ def option_default_values(_values)
76
+ {}
77
+ end
78
+
79
+ def option_override_values(_values)
80
+ {}
81
+ end
82
+
83
+ def sub_commands(_values)
84
+ []
47
85
  end
48
86
 
49
- def do_before(opts)
87
+ def options(values)
88
+ RubyTerraform::Options::Factory.from(values, switches)
50
89
  end
51
90
 
52
- def configure_command(builder, opts)
91
+ def switches
92
+ []
53
93
  end
54
94
 
55
- def do_after(opts)
95
+ def arguments(_values)
96
+ []
56
97
  end
57
98
  end
58
99
  end
@@ -6,7 +6,7 @@ module RubyTerraform
6
6
  attr_reader :logger
7
7
 
8
8
  def initialize(directory: nil, logger: nil)
9
- @directory = directory ? directory : '.terraform'
9
+ @directory = directory || '.terraform'
10
10
  @logger = logger || RubyTerraform.configuration.logger
11
11
  end
12
12
 
@@ -14,7 +14,7 @@ module RubyTerraform
14
14
  directory = opts[:directory] || @directory
15
15
  begin
16
16
  logger.info "Cleaning terraform directory '#{directory}'."
17
- FileUtils.rm_r(directory, :secure => true)
17
+ FileUtils.rm_r(directory, secure: true)
18
18
  rescue Errno::ENOENT => e
19
19
  logger.error "Couldn't clean '#{directory}': #{e.message}"
20
20
  end
@@ -3,44 +3,24 @@ require_relative 'base'
3
3
  module RubyTerraform
4
4
  module Commands
5
5
  class Destroy < Base
6
- def configure_command(builder, opts)
7
- directory = opts[:directory]
8
- vars = opts[:vars] || {}
9
- var_file = opts[:var_file]
10
- var_files = opts[:var_files] || []
11
- target = opts[:target]
12
- targets = opts[:targets] || []
13
- state = opts[:state]
14
- force = opts[:force]
15
- no_backup = opts[:no_backup]
16
- backup = no_backup ? '-' : opts[:backup]
17
- no_color = opts[:no_color]
18
- auto_approve = opts[:auto_approve]
6
+ def switches
7
+ %w[-backup -auto-approve -force -no-color -state -target -var -var-file]
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ %w[destroy]
12
+ end
13
+
14
+ def arguments(values)
15
+ [values[:directory]]
16
+ end
17
+
18
+ def option_default_values(_opts)
19
+ { vars: {}, var_files: [], targets: [] }
20
+ end
19
21
 
20
- builder
21
- .with_subcommand('destroy') do |sub|
22
- vars.each do |key, value|
23
- var_value = value.is_a?(String) ? value : JSON.generate(value)
24
- sub = sub.with_option(
25
- '-var', "'#{key}=#{var_value}'", separator: ' ')
26
- end
27
- sub = sub.with_option('-var-file', var_file) if var_file
28
- var_files.each do |file|
29
- sub = sub.with_option('-var-file', file)
30
- end
31
- sub = sub.with_option('-target', target) if target
32
- targets.each do |target_name|
33
- sub = sub.with_option('-target', target_name)
34
- end
35
- sub = sub.with_option('-state', state) if state
36
- sub = sub.with_option('-auto-approve', auto_approve) unless
37
- auto_approve.nil?
38
- sub = sub.with_option('-backup', backup) if backup
39
- sub = sub.with_flag('-no-color') if no_color
40
- sub = sub.with_flag('-force') if force
41
- sub
42
- end
43
- .with_argument(directory)
22
+ def option_override_values(opts)
23
+ { backup: opts[:no_backup] ? '-' : opts[:backup] }
44
24
  end
45
25
  end
46
26
  end
@@ -1,29 +1,18 @@
1
- # frozen_string_literal: true
2
-
3
1
  require_relative 'base'
4
2
 
5
3
  module RubyTerraform
6
4
  module Commands
7
5
  class Format < Base
8
- def configure_command(builder, opts)
9
- directory = opts[:directory]
10
- check = opts[:check]
11
- diff = opts[:diff]
12
- list = opts[:list]
13
- no_color = opts[:no_color]
14
- recursive = opts[:recursive]
15
- write = opts[:write]
6
+ def switches
7
+ %w[-list -write -diff -check -recursive -no-color]
8
+ end
16
9
 
17
- builder.with_subcommand('fmt') do |sub|
18
- sub = sub.with_option('-list', list) if list
19
- sub = sub.with_option('-write', write) if write
10
+ def sub_commands(_values)
11
+ %w[fmt]
12
+ end
20
13
 
21
- sub = sub.with_flag('-check') if check
22
- sub = sub.with_flag('-diff') if diff
23
- sub = sub.with_flag('-no-color') if no_color
24
- sub = sub.with_flag('-recursive') if recursive
25
- sub
26
- end.with_argument(directory)
14
+ def arguments(values)
15
+ [values[:directory]]
27
16
  end
28
17
  end
29
18
  end
@@ -3,15 +3,17 @@ require_relative 'base'
3
3
  module RubyTerraform
4
4
  module Commands
5
5
  class Get < Base
6
- def configure_command(builder, opts)
7
- builder
8
- .with_subcommand('get') do |sub|
9
- sub = sub.with_option('-update', true) if opts[:update]
10
- sub = sub.with_flag('-no-color') if opts[:no_color]
11
- sub
12
- end
13
- .with_argument(opts[:directory])
6
+ def switches
7
+ %w[-update -no-color]
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ %w[get]
12
+ end
13
+
14
+ def arguments(values)
15
+ [values[:directory]]
14
16
  end
15
17
  end
16
18
  end
17
- end
19
+ end
@@ -1,45 +1,26 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
1
  require_relative 'base'
5
2
 
6
3
  module RubyTerraform
7
4
  module Commands
8
5
  class Import < Base
9
- def configure_command(builder, opts)
10
- directory = opts[:directory]
11
- vars = opts[:vars] || {}
12
- var_file = opts[:var_file]
13
- var_files = opts[:var_files] || []
14
- no_color = opts[:no_color]
15
- no_backup = opts[:no_backup]
16
- backup = no_backup ? '-' : opts[:backup]
17
- state = opts[:state]
18
- input = opts[:input]
19
- address = opts[:address]
20
- id = opts[:id]
6
+ def switches
7
+ %w[-config -backup -input -no-color -state -var -var-file]
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ %w[import]
12
+ end
13
+
14
+ def arguments(values)
15
+ [values[:address], values[:id]]
16
+ end
17
+
18
+ def option_default_values(_opts)
19
+ { vars: {}, var_files: [] }
20
+ end
21
21
 
22
- builder
23
- .with_subcommand('import') do |sub|
24
- sub = sub.with_option('-config', directory)
25
- vars.each do |key, value|
26
- var_value = value.is_a?(String) ? value : JSON.generate(value)
27
- sub = sub.with_option(
28
- '-var', "'#{key}=#{var_value}'", separator: ' '
29
- )
30
- end
31
- sub = sub.with_option('-var-file', var_file) if var_file
32
- var_files.each do |file|
33
- sub = sub.with_option('-var-file', file)
34
- end
35
- sub = sub.with_option('-state', state) if state
36
- sub = sub.with_option('-input', input) if input
37
- sub = sub.with_option('-backup', backup) if backup
38
- sub = sub.with_flag('-no-color') if no_color
39
- sub
40
- end
41
- .with_argument(address)
42
- .with_argument(id)
22
+ def option_override_values(opts)
23
+ { backup: opts[:no_backup] ? '-' : opts[:backup] }
43
24
  end
44
25
  end
45
26
  end
@@ -3,36 +3,21 @@ require_relative 'base'
3
3
  module RubyTerraform
4
4
  module Commands
5
5
  class Init < Base
6
- def configure_command(builder, opts)
7
- no_color = opts[:no_color]
8
- backend = opts[:backend]
9
- get = opts[:get]
10
- backend_config = opts[:backend_config] || {}
11
- source = opts[:from_module]
12
- path = opts[:path]
13
- plugin_dir = opts[:plugin_dir]
14
- force_copy = opts[:force_copy]
6
+ def switches
7
+ %w[-backend -backend-config -from-module -get -no-color -plugin-dir
8
+ -force-copy]
9
+ end
15
10
 
16
- builder = builder
17
- .with_subcommand('init') do |sub|
18
- sub = sub.with_option('-backend', backend) unless backend.nil?
19
- sub = sub.with_option('-force-copy', force_copy) unless force_copy.nil?
20
- sub = sub.with_option('-get', get) unless get.nil?
21
- sub = sub.with_option('-from-module', source) if source
22
- sub = sub.with_flag('-no-color') if no_color
23
- sub = sub.with_option('-plugin-dir', plugin_dir) unless plugin_dir.nil?
24
- backend_config.each do |key, value|
25
- sub = sub.with_option(
26
- '-backend-config',
27
- "'#{key}=#{value}'",
28
- separator: ' ')
29
- end
30
- sub
31
- end
11
+ def sub_commands(_values)
12
+ %w[init]
13
+ end
32
14
 
33
- builder = builder.with_argument(path) if path
15
+ def arguments(values)
16
+ [values[:path]]
17
+ end
34
18
 
35
- builder
19
+ def option_default_values(_opts)
20
+ { backend_config: {} }
36
21
  end
37
22
  end
38
23
  end
@@ -4,29 +4,22 @@ require_relative 'base'
4
4
  module RubyTerraform
5
5
  module Commands
6
6
  class Output < Base
7
- def initialize(**kwargs)
8
- super(**kwargs)
9
- @stdout = StringIO.new unless
10
- defined?(@stdout) && @stdout.respond_to?(:string)
7
+ def initialize_command
8
+ return if defined?(@stdout) && @stdout.respond_to?(:string)
9
+
10
+ @stdout = StringIO.new
11
+ end
12
+
13
+ def switches
14
+ %w[-json -raw -no-color -state -module]
11
15
  end
12
16
 
13
- def configure_command(builder, opts)
14
- name = opts[:name]
15
- state = opts[:state]
16
- no_color = opts[:no_color]
17
- json = opts[:json]
18
- mod = opts[:module]
17
+ def sub_commands(_values)
18
+ %w[output]
19
+ end
19
20
 
20
- builder = builder
21
- .with_subcommand('output') do |sub|
22
- sub = sub.with_flag('-no-color') if no_color
23
- sub = sub.with_flag('-json') if json
24
- sub = sub.with_option('-state', state) if state
25
- sub = sub.with_option('-module', mod) if mod
26
- sub
27
- end
28
- builder = builder.with_argument(name) if name
29
- builder
21
+ def arguments(values)
22
+ [values[:name]]
30
23
  end
31
24
 
32
25
  def do_after(opts)