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,45 +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
+ %w[plan]
12
+ end
13
+
14
+ def arguments(values)
15
+ [values[:directory]]
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
- 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 |file|
33
- sub = sub.with_option('-target', file)
34
- end
35
- sub = sub.with_option('-state', state) if state
36
- sub = sub.with_option('-out', plan) if plan
37
- sub = sub.with_option('-input', input) if input
38
- sub = sub.with_flag('-destroy') if destroy
39
- sub = sub.with_flag('-no-color') if no_color
40
- sub
41
- end
42
- .with_argument(directory)
18
+ def option_default_values(_opts)
19
+ { vars: {}, var_files: [], targets: [] }
43
20
  end
44
21
  end
45
22
  end
@@ -1,41 +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]
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ %w[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
- 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('-input', input) if input
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_flag('-no-color') if no_color
36
- sub
37
- end
38
- .with_argument(directory)
18
+ def option_default_values(_opts)
19
+ { vars: {}, var_files: [], targets: [] }
39
20
  end
40
21
  end
41
22
  end
@@ -3,24 +3,17 @@ 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
- end
10
+ def sub_commands(_values)
11
+ %w[remote config]
12
+ end
19
13
 
20
- sub = sub.with_flag('-no-color') if no_color
21
- sub
22
- end
14
+ def option_default_values(_opts)
15
+ { backend_config: {} }
23
16
  end
24
17
  end
25
18
  end
26
- end
19
+ 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]
8
+ end
9
+
10
+ def sub_commands(_values)
11
+ %w[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,38 +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
+ %w[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
- end
24
- sub = sub.with_option('-var-file', var_file) if var_file
25
- var_files.each do |file|
26
- sub = sub.with_option('-var-file', file)
27
- end
28
- sub = sub.with_option('-state', state) if state
29
- sub = sub.with_option('-check-variables', check_variables) unless
30
- check_variables.nil?
31
- sub = sub.with_flag('-no-color') if no_color
32
- sub = sub.with_flag('-json') if json_format
33
- sub
34
- end
35
- .with_argument(directory)
18
+ def option_default_values(_opts)
19
+ { vars: {}, var_files: [] }
36
20
  end
37
21
  end
38
22
  end
@@ -3,21 +3,22 @@ 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
10
-
11
- builder = builder
12
- .with_subcommand('workspace')
13
- .with_subcommand(operation)
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
14
14
 
15
- builder = builder.with_subcommand(workspace) if
16
- workspace && operation != 'list'
17
- builder = builder.with_argument(directory)
15
+ def arguments(values)
16
+ [values[:directory]]
17
+ end
18
18
 
19
- builder
19
+ def option_default_values(_opts)
20
+ { directory: nil, operation: 'list', workspace: nil }
20
21
  end
21
22
  end
22
23
  end
23
- end
24
+ end
@@ -3,4 +3,4 @@ module RubyTerraform
3
3
  class ExecutionError < StandardError
4
4
  end
5
5
  end
6
- end
6
+ end
@@ -0,0 +1,22 @@
1
+ module RubyTerraform
2
+ module Options
3
+ class Base
4
+ def initialize(switch, value)
5
+ @switch = switch
6
+ coerce_value(value)
7
+ end
8
+
9
+ def apply(_builder)
10
+ raise 'not implemented'
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :switch, :value
16
+
17
+ def coerce_value(value)
18
+ @value = value
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'base'
2
+ require_relative 'values/boolean'
3
+
4
+ module RubyTerraform
5
+ module Options
6
+ class Boolean < Base
7
+ include Values::Boolean
8
+
9
+ def apply(builder)
10
+ builder.with_option(switch, value)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,116 @@
1
+ require_relative 'switch'
2
+ require_relative 'boolean'
3
+ require_relative 'flag'
4
+ require_relative 'standard'
5
+
6
+ module RubyTerraform
7
+ module Options
8
+ class Factory
9
+ PLURAL_SWITCHES =
10
+ Set.new(
11
+ %w[
12
+ -var
13
+ -target
14
+ -var-file
15
+ ]
16
+ ).freeze
17
+
18
+ BOOLEAN_SWITCHES =
19
+ Set.new(
20
+ %w[
21
+ -auto-approve
22
+ -backend
23
+ -get
24
+ -get-plugins
25
+ -input
26
+ -list
27
+ -lock
28
+ -refresh
29
+ -upgrade
30
+ -verify-plugins
31
+ -write
32
+ ]
33
+ ).freeze
34
+
35
+ FLAG_SWITCHES =
36
+ Set.new(
37
+ %w[
38
+ -allow-missing
39
+ -allow-missing-config
40
+ -check
41
+ -compact-warnings
42
+ -destroy
43
+ -detailed-exitcode
44
+ -diff
45
+ -draw-cycles
46
+ -force
47
+ -force-copy
48
+ -ignore-remote-version
49
+ -json
50
+ -no-color
51
+ -raw
52
+ -reconfigure
53
+ -recursive
54
+ -update
55
+ ]
56
+ ).freeze
57
+
58
+ OVERRIDE_SWITCHES =
59
+ {
60
+ config: :directory,
61
+ out: :plan
62
+ }.freeze
63
+
64
+ def self.from(values, switches)
65
+ new(values, switches).from
66
+ end
67
+
68
+ private_class_method :new
69
+
70
+ def initialize(values, switches)
71
+ @switches = switches.map { |switch| Switch.new(switch) }
72
+ @values = values
73
+ end
74
+
75
+ def from
76
+ switches.each_with_object([]) do |switch, options|
77
+ options.append(*options_from_switch(switch))
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ attr_reader :switches, :values
84
+
85
+ def options_from_switch(switch)
86
+ return plural_options(switch) if PLURAL_SWITCHES.include?(switch)
87
+ return boolean_option(switch) if BOOLEAN_SWITCHES.include?(switch)
88
+ return flag_option(switch) if FLAG_SWITCHES.include?(switch)
89
+ return override_option(switch) if OVERRIDE_SWITCHES.key?(switch.as_key)
90
+
91
+ standard_option(switch, switch.as_key)
92
+ end
93
+
94
+ def boolean_option(switch)
95
+ [Boolean.new(switch.to_s, values[switch.as_key])]
96
+ end
97
+
98
+ def flag_option(switch)
99
+ [Flag.new(switch.to_s, values[switch.as_key])]
100
+ end
101
+
102
+ def standard_option(switch, hash_key)
103
+ [Standard.new(switch.to_s, values[hash_key])]
104
+ end
105
+
106
+ def override_option(switch)
107
+ standard_option(switch, OVERRIDE_SWITCHES[switch.as_key])
108
+ end
109
+
110
+ def plural_options(switch)
111
+ standard_option(switch.to_s, switch.as_key) +
112
+ standard_option(switch.to_s, switch.as_plural_key)
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'base'
2
+ require_relative 'values/boolean'
3
+
4
+ module RubyTerraform
5
+ module Options
6
+ class Flag < Base
7
+ include Values::Boolean
8
+
9
+ def apply(builder)
10
+ value ? builder.with_flag(switch) : builder
11
+ end
12
+ end
13
+ end
14
+ end