ruby-terraform 0.65.0.pre.4 → 0.65.0.pre.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89f188a94d3b8b59c9a65a87d151a239bc96c3b3c7612718991d84ee482eede4
4
- data.tar.gz: dfdb9d0fac5a258cd823a441c8b70fbf05715129c6d4bd32ddeb128d5447f1e8
3
+ metadata.gz: fc71a524e8870cabf47b5d3d88f474119bb38ed849afc036af51f95475335c37
4
+ data.tar.gz: 11bf8ae65abe06ff4e0cec9f347a82f613976cbd7378c62fc22fb97d336e05f4
5
5
  SHA512:
6
- metadata.gz: d0bd171c22777b63821ba3bab4d2a4d600e2fd3352c92c8c31e7758dd8732a7d89c060459012f4d63361373cce9e1d0ff0ab7092c6d2710fa52287790289c262
7
- data.tar.gz: bfbd26e7b628847187de85f8ef2753d2f84ebeaaad739f1c9b129a4db1f201bc09fb7c854d8b338a48df0d7288188a3ea510bb124abe5366cbb311671d538b9a
6
+ metadata.gz: a6cd2f1ac3bc0b91d270925ddea294c6c30fe3385061437467fc78c1964bea141ac410212f0358a091e483898bf2d06a4b27931783c444e5176bb184b5ede8c9
7
+ data.tar.gz: 8a863584cdf4d4bc90ebe62cb289b893fd6082f704f828ab00ba21b67228841ceb774b7d16a03332feebcdd4174364a340124b2796ca6e5acc2b2383050a2b83
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-terraform (0.65.0.pre.4)
4
+ ruby-terraform (0.65.0.pre.5)
5
5
  lino (>= 1.5)
6
6
 
7
7
  GEM
@@ -0,0 +1,55 @@
1
+ require 'lino'
2
+
3
+ module RubyTerraform
4
+ module CommandLine
5
+ class Builder
6
+ def initialize(binary:, sub_commands:, options:, arguments:)
7
+ @builder = instantiate_builder(binary)
8
+ @sub_commands = array_of(sub_commands)
9
+ @options = array_of(options)
10
+ @arguments = array_of(arguments)
11
+ end
12
+
13
+ def build
14
+ configure_builder
15
+ builder.build
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :builder, :sub_commands, :options, :arguments
21
+
22
+ def configure_builder
23
+ add_subcommands_and_options
24
+ add_arguments
25
+ end
26
+
27
+ def add_subcommands_and_options
28
+ sub_commands[0...-1].each do |command|
29
+ @builder = builder.with_subcommand(command)
30
+ end
31
+ @builder = builder.with_subcommand(sub_commands.last) do |sub|
32
+ options.inject(sub) do |sub_command, option|
33
+ option.add_to_subcommand(sub_command)
34
+ end
35
+ end
36
+ end
37
+
38
+ def add_arguments
39
+ @builder = builder.with_arguments(arguments)
40
+ end
41
+
42
+ def instantiate_builder(binary)
43
+ Lino::CommandLineBuilder
44
+ .for_command(binary)
45
+ .with_option_separator('=')
46
+ end
47
+
48
+ def array_of(value)
49
+ return value if value.respond_to?(:each)
50
+
51
+ value.nil? ? [] : [value]
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ module RubyTerraform
2
+ module CommandLine
3
+ module Option
4
+ class Base
5
+ def initialize(switch, value)
6
+ @switch = switch
7
+ coerce_value(value)
8
+ end
9
+
10
+ def add_to_subcommand(_sub)
11
+ raise 'not implemented'
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :switch, :value
17
+
18
+ def coerce_value(value)
19
+ @value = value
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'boolean_value'
2
+ require_relative 'base'
3
+
4
+ module RubyTerraform
5
+ module CommandLine
6
+ module Option
7
+ class Boolean < Base
8
+ include BooleanValue
9
+
10
+ def add_to_subcommand(sub)
11
+ sub.with_option(switch, value)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ module RubyTerraform
2
+ module CommandLine
3
+ module Option
4
+ module BooleanValue
5
+ def coerce_value(value)
6
+ @value = boolean_val(value)
7
+ end
8
+
9
+ private
10
+
11
+ def boolean_val(value)
12
+ return nil if value.nil?
13
+ return value if a_boolean?(value)
14
+ return true if true_as_string?(value)
15
+
16
+ false
17
+ end
18
+
19
+ def a_boolean?(value)
20
+ value.is_a?(TrueClass) || value.is_a?(FalseClass)
21
+ end
22
+
23
+ def true_as_string?(value)
24
+ value.respond_to?(:downcase) && value.downcase == 'true'
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'boolean_value'
2
+ require_relative 'base'
3
+
4
+ module RubyTerraform
5
+ module CommandLine
6
+ module Option
7
+ class Flag < Base
8
+ include BooleanValue
9
+
10
+ def add_to_subcommand(sub)
11
+ value ? sub.with_flag(switch) : sub
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+ require_relative 'base'
3
+
4
+ module RubyTerraform
5
+ module CommandLine
6
+ module Option
7
+ class Standard < Base
8
+ def add_to_subcommand(sub)
9
+ if value.respond_to?(:keys)
10
+ add_hash_to_subcommand(sub)
11
+ elsif value.respond_to?(:each)
12
+ add_array_to_subcommand(sub)
13
+ else
14
+ sub.with_option(switch, value)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def add_hash_to_subcommand(sub)
21
+ sub.with_repeated_option(
22
+ switch,
23
+ value.map do |hash_key, hash_value|
24
+ "'#{hash_key}=#{as_string(hash_value)}'"
25
+ end,
26
+ separator: ' '
27
+ )
28
+ end
29
+
30
+ def add_array_to_subcommand(sub)
31
+ sub.with_repeated_option(switch, value)
32
+ end
33
+
34
+ def as_string(value)
35
+ value.is_a?(String) ? value : JSON.generate(value)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+ module RubyTerraform
2
+ module CommandLine
3
+ module Option
4
+ class Switch
5
+ def initialize(switch)
6
+ @switch_without_prefix = switch[0] == '-' ? switch[1..] : switch
7
+ end
8
+
9
+ def to_s
10
+ "-#{switch_without_prefix}"
11
+ end
12
+
13
+ def as_key
14
+ snake_case.to_sym
15
+ end
16
+
17
+ def as_plural_key
18
+ "#{snake_case}s".to_sym
19
+ end
20
+
21
+ def ==(other)
22
+ to_s == other
23
+ end
24
+
25
+ def eql?(other)
26
+ to_s == other
27
+ end
28
+
29
+ def hash
30
+ to_s.hash
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :switch_without_prefix
36
+
37
+ def snake_case
38
+ switch_without_prefix.gsub('-', '_')
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,79 @@
1
+ require_relative 'option/standard'
2
+ require_relative 'option/flag'
3
+ require_relative 'option/boolean'
4
+ require_relative 'option/switch'
5
+
6
+ module RubyTerraform
7
+ module CommandLine
8
+ class OptionsFactory
9
+ PLURAL_SWITCHES = Set.new(
10
+ %w[-var -target -var-file]
11
+ ).freeze
12
+ BOOLEAN_SWITCHES = Set.new(
13
+ %w[-auto-approve -backend -get -get-plugins -input -list -lock
14
+ -refresh -upgrade -verify-plugins -write]
15
+ ).freeze
16
+ FLAG_SWITCHES = Set.new(
17
+ %w[-allow-missing -allow-missing-config -check -compact-warnings
18
+ -destroy -detailed-exitcode -diff -draw-cycles -force -force-copy
19
+ -ignore-remote-version -json -no-color -raw -reconfigure -recursive
20
+ -update]
21
+ ).freeze
22
+ OVERRIDE_SWITCHES = {
23
+ config: :directory,
24
+ out: :plan
25
+ }.freeze
26
+
27
+ def self.from(values, switches)
28
+ new(values, switches).from
29
+ end
30
+
31
+ private_class_method :new
32
+
33
+ def initialize(values, switches)
34
+ @switches = switches.map { |switch| Option::Switch.new(switch) }
35
+ @values = values
36
+ end
37
+
38
+ def from
39
+ switches.each_with_object([]) do |switch, options|
40
+ options.append(*options_from_switch(switch))
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :switches, :values
47
+
48
+ def options_from_switch(switch)
49
+ return plural_options(switch) if PLURAL_SWITCHES.include?(switch)
50
+ return boolean_option(switch) if BOOLEAN_SWITCHES.include?(switch)
51
+ return flag_option(switch) if FLAG_SWITCHES.include?(switch)
52
+ return override_option(switch) if OVERRIDE_SWITCHES.key?(switch.as_key)
53
+
54
+ standard_option(switch, switch.as_key)
55
+ end
56
+
57
+ def boolean_option(switch)
58
+ [Option::Boolean.new(switch.to_s, values[switch.as_key])]
59
+ end
60
+
61
+ def flag_option(switch)
62
+ [Option::Flag.new(switch.to_s, values[switch.as_key])]
63
+ end
64
+
65
+ def standard_option(switch, hash_key)
66
+ [Option::Standard.new(switch.to_s, values[hash_key])]
67
+ end
68
+
69
+ def override_option(switch)
70
+ standard_option(switch, OVERRIDE_SWITCHES[switch.as_key])
71
+ end
72
+
73
+ def plural_options(switch)
74
+ standard_option(switch.to_s, switch.as_key) +
75
+ standard_option(switch.to_s, switch.as_plural_key)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -1,49 +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
+ '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
- )
29
- end
30
- sub = sub.with_option('-var-file', var_file) if var_file
31
- var_files.each do |file|
32
- sub = sub.with_option('-var-file', file)
33
- end
34
- sub = sub.with_option('-target', target) if target
35
- targets.each do |file|
36
- sub = sub.with_option('-target', file)
37
- end
38
- sub = sub.with_option('-state', state) if state
39
- sub = sub.with_option('-input', input) if input
40
- sub = sub.with_option('-auto-approve', auto_approve) unless
41
- auto_approve.nil?
42
- sub = sub.with_option('-backup', backup) if backup
43
- sub = sub.with_flag('-no-color') if no_color
44
- sub
45
- end
46
- .with_argument(plan || directory)
23
+ def option_override_values(opts)
24
+ { backup: opts[:no_backup] ? '-' : opts[:backup] }
47
25
  end
48
26
  end
49
27
  end
@@ -1,5 +1,6 @@
1
- require 'lino'
2
1
  require_relative '../errors'
2
+ require_relative '../command_line/builder'
3
+ require_relative '../command_line/options_factory'
3
4
 
4
5
  module RubyTerraform
5
6
  module Commands
@@ -12,20 +13,12 @@ module RubyTerraform
12
13
  @stdin = stdin || RubyTerraform.configuration.stdin
13
14
  @stdout = stdout || RubyTerraform.configuration.stdout
14
15
  @stderr = stderr || RubyTerraform.configuration.stderr
16
+ initialize_command
15
17
  end
16
18
 
17
19
  def execute(opts = {})
18
- builder = instantiate_builder
19
-
20
20
  do_before(opts)
21
- command = configure_command(builder, opts).build
22
- logger.debug("Running '#{command}'.")
23
-
24
- command.execute(
25
- stdin: stdin,
26
- stdout: stdout,
27
- stderr: stderr
28
- )
21
+ build_and_execute_command(opts)
29
22
  do_after(opts)
30
23
  rescue Open4::SpawnError
31
24
  message = "Failed while running '#{command_name}'."
@@ -37,21 +30,66 @@ module RubyTerraform
37
30
 
38
31
  attr_reader :binary, :logger, :stdin, :stdout, :stderr
39
32
 
33
+ def build_and_execute_command(opts)
34
+ command = build_command(opts)
35
+ logger.debug("Running '#{command}'.")
36
+ command.execute(
37
+ stdin: stdin,
38
+ stdout: stdout,
39
+ stderr: stderr
40
+ )
41
+ end
42
+
40
43
  def command_name
41
44
  self.class.to_s.split('::')[-1].downcase
42
45
  end
43
46
 
44
- def instantiate_builder
45
- Lino::CommandLineBuilder
46
- .for_command(binary)
47
- .with_option_separator('=')
47
+ def do_before(_opts); end
48
+
49
+ def do_after(_opts); end
50
+
51
+ private
52
+
53
+ def initialize_command; end
54
+
55
+ def build_command(opts)
56
+ values = apply_option_defaults_and_overrides(opts)
57
+ RubyTerraform::CommandLine::Builder.new(
58
+ binary: @binary,
59
+ sub_commands: sub_commands(values),
60
+ options: options(values),
61
+ arguments: arguments(values)
62
+ ).build
48
63
  end
49
64
 
50
- def do_before(opts); end
65
+ def apply_option_defaults_and_overrides(opts)
66
+ option_default_values(opts).merge(opts)
67
+ .merge(option_override_values(opts))
68
+ end
69
+
70
+ def option_default_values(_values)
71
+ {}
72
+ end
51
73
 
52
- def configure_command(builder, opts); end
74
+ def option_override_values(_values)
75
+ {}
76
+ end
53
77
 
54
- def do_after(opts); end
78
+ def sub_commands(_values)
79
+ []
80
+ end
81
+
82
+ def options(values)
83
+ RubyTerraform::CommandLine::OptionsFactory.from(values, switches)
84
+ end
85
+
86
+ def switches
87
+ []
88
+ end
89
+
90
+ def arguments(_values)
91
+ []
92
+ end
55
93
  end
56
94
  end
57
95
  end
@@ -3,45 +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
+ '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
- )
27
- end
28
- sub = sub.with_option('-var-file', var_file) if var_file
29
- var_files.each do |file|
30
- sub = sub.with_option('-var-file', file)
31
- end
32
- sub = sub.with_option('-target', target) if target
33
- targets.each do |target_name|
34
- sub = sub.with_option('-target', target_name)
35
- end
36
- sub = sub.with_option('-state', state) if state
37
- sub = sub.with_option('-auto-approve', auto_approve) unless
38
- auto_approve.nil?
39
- sub = sub.with_option('-backup', backup) if backup
40
- sub = sub.with_flag('-no-color') if no_color
41
- sub = sub.with_flag('-force') if force
42
- sub
43
- end
44
- .with_argument(directory)
22
+ def option_override_values(opts)
23
+ { backup: opts[:no_backup] ? '-' : opts[:backup] }
45
24
  end
46
25
  end
47
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
+ '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,14 +3,16 @@ 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
+ 'get'
12
+ end
13
+
14
+ def arguments(values)
15
+ values[:directory]
14
16
  end
15
17
  end
16
18
  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
+ '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,43 +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
- unless force_copy.nil?
20
- sub = sub.with_option('-force-copy',
21
- force_copy)
22
- end
23
- sub = sub.with_option('-get', get) unless get.nil?
24
- sub = sub.with_option('-from-module', source) if source
25
- sub = sub.with_flag('-no-color') if no_color
26
- unless plugin_dir.nil?
27
- sub = sub.with_option('-plugin-dir',
28
- plugin_dir)
29
- end
30
- backend_config.each do |key, value|
31
- sub = sub.with_option(
32
- '-backend-config',
33
- "'#{key}=#{value}'",
34
- separator: ' '
35
- )
36
- end
37
- sub
38
- end
11
+ def sub_commands(_values)
12
+ 'init'
13
+ end
39
14
 
40
- builder = builder.with_argument(path) if path
15
+ def arguments(values)
16
+ values[:path]
17
+ end
41
18
 
42
- builder
19
+ def option_default_values(_opts)
20
+ { backend_config: {} }
43
21
  end
44
22
  end
45
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
+ '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)
@@ -1,46 +1,22 @@
1
- require 'json'
2
1
  require_relative 'base'
3
2
 
4
3
  module RubyTerraform
5
4
  module Commands
6
5
  class Plan < Base
7
- def configure_command(builder, opts)
8
- directory = opts[:directory]
9
- vars = opts[:vars] || {}
10
- var_file = opts[:var_file]
11
- var_files = opts[:var_files] || []
12
- target = opts[:target]
13
- targets = opts[:targets] || []
14
- state = opts[:state]
15
- plan = opts[:plan]
16
- input = opts[:input]
17
- destroy = opts[:destroy]
18
- no_color = opts[:no_color]
6
+ def switches
7
+ %w[-destroy -input -no-color -out -state -target -var -var-file]
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ 'plan'
12
+ end
13
+
14
+ def option_default_values(_opts)
15
+ { vars: {}, var_files: [], targets: [] }
16
+ end
19
17
 
20
- builder
21
- .with_subcommand('plan') 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
- )
27
- end
28
- sub = sub.with_option('-var-file', var_file) if var_file
29
- var_files.each do |file|
30
- sub = sub.with_option('-var-file', file)
31
- end
32
- sub = sub.with_option('-target', target) if target
33
- targets.each do |file|
34
- sub = sub.with_option('-target', file)
35
- end
36
- sub = sub.with_option('-state', state) if state
37
- sub = sub.with_option('-out', plan) if plan
38
- sub = sub.with_option('-input', input) if input
39
- sub = sub.with_flag('-destroy') if destroy
40
- sub = sub.with_flag('-no-color') if no_color
41
- sub
42
- end
43
- .with_argument(directory)
18
+ def arguments(values)
19
+ values[:directory]
44
20
  end
45
21
  end
46
22
  end
@@ -1,42 +1,22 @@
1
- require 'json'
2
1
  require_relative 'base'
3
2
 
4
3
  module RubyTerraform
5
4
  module Commands
6
5
  class Refresh < Base
7
- def configure_command(builder, opts)
8
- directory = opts[:directory]
9
- vars = opts[:vars] || {}
10
- var_file = opts[:var_file]
11
- var_files = opts[:var_files] || []
12
- state = opts[:state]
13
- input = opts[:input]
14
- target = opts[:target]
15
- targets = opts[:targets] || []
16
- no_color = opts[:no_color]
6
+ def switches
7
+ %w[-input -no-color -state -target -var -var-file] + super
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ 'refresh'
12
+ end
13
+
14
+ def arguments(values)
15
+ values[:directory]
16
+ end
17
17
 
18
- builder
19
- .with_subcommand('refresh') do |sub|
20
- vars.each do |key, value|
21
- var_value = value.is_a?(String) ? value : JSON.generate(value)
22
- sub = sub.with_option(
23
- '-var', "'#{key}=#{var_value}'", separator: ' '
24
- )
25
- end
26
- sub = sub.with_option('-var-file', var_file) if var_file
27
- var_files.each do |file|
28
- sub = sub.with_option('-var-file', file)
29
- end
30
- sub = sub.with_option('-state', state) if state
31
- sub = sub.with_option('-input', input) if input
32
- sub = sub.with_option('-target', target) if target
33
- targets.each do |target_name|
34
- sub = sub.with_option('-target', target_name)
35
- end
36
- sub = sub.with_flag('-no-color') if no_color
37
- sub
38
- end
39
- .with_argument(directory)
18
+ def option_default_values(_opts)
19
+ { vars: {}, var_files: [], targets: [] }
40
20
  end
41
21
  end
42
22
  end
@@ -3,24 +3,16 @@ require_relative 'base'
3
3
  module RubyTerraform
4
4
  module Commands
5
5
  class RemoteConfig < Base
6
- def configure_command(builder, opts)
7
- backend = opts[:backend]
8
- no_color = opts[:no_color]
9
- backend_config = opts[:backend_config] || {}
6
+ def switches
7
+ %w[-backend -backend-config -no-color]
8
+ end
10
9
 
11
- builder
12
- .with_subcommand('remote')
13
- .with_subcommand('config') do |sub|
14
- sub = sub.with_option('-backend', backend) if backend
15
- backend_config.each do |key, value|
16
- sub = sub.with_option(
17
- '-backend-config', "'#{key}=#{value}'", separator: ' '
18
- )
19
- end
10
+ def sub_commands(_values)
11
+ %w[remote config]
12
+ end
20
13
 
21
- sub = sub.with_flag('-no-color') if no_color
22
- sub
23
- end
14
+ def option_default_values(_opts)
15
+ { backend_config: {} }
24
16
  end
25
17
  end
26
18
  end
@@ -1,24 +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 Show < Base
8
- def configure_command(builder, opts)
9
- path = opts[:path] || opts[:directory]
10
- json_format = opts[:json]
11
- no_color = opts[:no_color]
12
- module_depth = opts[:module_depth]
6
+ def switches
7
+ %w[-json -no-color -module-depth] + super
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ 'show'
12
+ end
13
13
 
14
- builder
15
- .with_subcommand('show') do |sub|
16
- sub = sub.with_option('-module-depth', module_depth) if module_depth
17
- sub = sub.with_flag('-no-color') if no_color
18
- sub = sub.with_flag('-json') if json_format
19
- sub
20
- end
21
- .with_argument(path)
14
+ def arguments(values)
15
+ values[:path] || values[:directory]
22
16
  end
23
17
  end
24
18
  end
@@ -1,39 +1,22 @@
1
- require 'json'
2
1
  require_relative 'base'
3
2
 
4
3
  module RubyTerraform
5
4
  module Commands
6
5
  class Validate < Base
7
- def configure_command(builder, opts)
8
- directory = opts[:directory]
9
- vars = opts[:vars] || {}
10
- var_file = opts[:var_file]
11
- var_files = opts[:var_files] || []
12
- state = opts[:state]
13
- check_variables = opts[:check_variables]
14
- no_color = opts[:no_color]
15
- json_format = opts[:json]
6
+ def switches
7
+ %w[-json -no-color -var -var-file -state -check-variables] + super
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ 'validate'
12
+ end
13
+
14
+ def arguments(values)
15
+ values[:directory]
16
+ end
16
17
 
17
- builder
18
- .with_subcommand('validate') do |sub|
19
- vars.each do |key, value|
20
- var_value = value.is_a?(String) ? value : JSON.generate(value)
21
- sub = sub.with_option(
22
- '-var', "'#{key}=#{var_value}'", separator: ' '
23
- )
24
- end
25
- sub = sub.with_option('-var-file', var_file) if var_file
26
- var_files.each do |file|
27
- sub = sub.with_option('-var-file', file)
28
- end
29
- sub = sub.with_option('-state', state) if state
30
- sub = sub.with_option('-check-variables', check_variables) unless
31
- check_variables.nil?
32
- sub = sub.with_flag('-no-color') if no_color
33
- sub = sub.with_flag('-json') if json_format
34
- sub
35
- end
36
- .with_argument(directory)
18
+ def option_default_values(_opts)
19
+ { vars: {}, var_files: [] }
37
20
  end
38
21
  end
39
22
  end
@@ -3,18 +3,21 @@ require_relative 'base'
3
3
  module RubyTerraform
4
4
  module Commands
5
5
  class Workspace < Base
6
- def configure_command(builder, opts)
7
- directory = opts[:directory] || nil
8
- operation = opts[:operation] || 'list'
9
- workspace = opts[:workspace] || nil
6
+ def sub_commands(values)
7
+ commands = ['workspace', values[:operation]]
8
+ if values[:workspace] && values[:operation] != 'list'
9
+ commands << values[:workspace]
10
+ else
11
+ commands
12
+ end
13
+ end
10
14
 
11
- builder = builder
12
- .with_subcommand('workspace')
13
- .with_subcommand(operation)
15
+ def arguments(values)
16
+ values[:directory]
17
+ end
14
18
 
15
- builder = builder.with_subcommand(workspace) if
16
- workspace && operation != 'list'
17
- builder.with_argument(directory)
19
+ def option_default_values(_opts)
20
+ { directory: nil, operation: 'list', workspace: nil }
18
21
  end
19
22
  end
20
23
  end
@@ -1,20 +1,23 @@
1
1
  module RubyTerraform
2
2
  class Output
3
- def self.for(opts)
4
- name = opts[:name]
5
- backend_config = opts[:backend_config]
6
-
7
- source_directory = opts[:source_directory]
8
- work_directory = opts[:work_directory]
3
+ class << self
4
+ def for(opts)
5
+ Dir.chdir(create_config_directory(opts)) do
6
+ RubyTerraform.init(backend_config: opts[:backend_config])
7
+ RubyTerraform.output(name: opts[:name])
8
+ end
9
+ end
9
10
 
10
- configuration_directory = File.join(work_directory, source_directory)
11
+ private
11
12
 
12
- FileUtils.mkdir_p File.dirname(configuration_directory)
13
- FileUtils.cp_r source_directory, configuration_directory
13
+ def create_config_directory(opts)
14
+ source_directory = opts[:source_directory]
15
+ work_directory = opts[:work_directory]
14
16
 
15
- Dir.chdir(configuration_directory) do
16
- RubyTerraform.init(backend_config: backend_config)
17
- RubyTerraform.output(name: name)
17
+ configuration_directory = File.join(work_directory, source_directory)
18
+ FileUtils.mkdir_p File.dirname(configuration_directory)
19
+ FileUtils.cp_r source_directory, configuration_directory
20
+ configuration_directory
18
21
  end
19
22
  end
20
23
  end
@@ -1,3 +1,3 @@
1
1
  module RubyTerraform
2
- VERSION = '0.65.0.pre.4'.freeze
2
+ VERSION = '0.65.0.pre.5'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-terraform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.65.0.pre.4
4
+ version: 0.65.0.pre.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby Clemson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-03 00:00:00.000000000 Z
11
+ date: 2021-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lino
@@ -238,6 +238,14 @@ files:
238
238
  - bin/setup
239
239
  - lib/ruby-terraform.rb
240
240
  - lib/ruby_terraform.rb
241
+ - lib/ruby_terraform/command_line/builder.rb
242
+ - lib/ruby_terraform/command_line/option/base.rb
243
+ - lib/ruby_terraform/command_line/option/boolean.rb
244
+ - lib/ruby_terraform/command_line/option/boolean_value.rb
245
+ - lib/ruby_terraform/command_line/option/flag.rb
246
+ - lib/ruby_terraform/command_line/option/standard.rb
247
+ - lib/ruby_terraform/command_line/option/switch.rb
248
+ - lib/ruby_terraform/command_line/options_factory.rb
241
249
  - lib/ruby_terraform/commands.rb
242
250
  - lib/ruby_terraform/commands/apply.rb
243
251
  - lib/ruby_terraform/commands/base.rb