Module: ForemanOpentofu::HclFormat
- Included in:
- AppWrapper, Concerns::BaseTemplateScopeExtensions, NicHelpers
- Defined in:
- app/lib/foreman_opentofu/hcl_format.rb
Instance Method Summary collapse
- #array_to_hcl(hsh, opts) ⇒ Object
-
#block_to_hcl(names, content = nil, opts = {}) ⇒ Object
output a hcl-block: hello “how” “are” “you” { … } the above would be created by: block_to_hcl([‘hello,’how’, ‘are’, ‘you’], { .. }).
- #default_opts(opts = {}) ⇒ Object
- #format_value(val, type) ⇒ Object
- #hash_to_hcl(hsh, opts) ⇒ Object
- #prefix_hcl(opts) ⇒ Object
-
#to_hcl(var, opts = {}) ⇒ Object
possible ‘opts`: `indent` : number of whitespace to indent; default 2 `depth` : start value of depth (for indentation); default: 0 `snippet`: if `true` and var is a `Hash` string will not be framed with `{}`.
Instance Method Details
#array_to_hcl(hsh, opts) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/lib/foreman_opentofu/hcl_format.rb', line 67 def array_to_hcl(hsh, opts) opts = default_opts(opts) hcl = '[' new_opts = opts.merge( depth: opts[:depth] + 1 ) close_block_on_newline = false hsh.each do |value| hcl << prefix_hcl(new_opts) hcl << to_hcl(value, new_opts) hcl << ',' close_block_on_newline = true end hcl.chomp!(',') hcl << prefix_hcl(opts) if close_block_on_newline hcl << ']' end |
#block_to_hcl(names, content = nil, opts = {}) ⇒ Object
output a hcl-block: hello “how” “are” “you” { … } the above would be created by: block_to_hcl([‘hello,’how’, ‘are’, ‘you’], { .. })
33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/lib/foreman_opentofu/hcl_format.rb', line 33 def block_to_hcl(names, content = nil, opts = {}) opts = default_opts(opts) hcl = prefix_hcl(opts) hcl << names[0] hcl << ' ' # sub-block-names are quoted hcl << names[1..].map(&:to_s).map(&:inspect).join(' ') hcl << ' ' if hcl[-1] != ' ' hcl << to_hcl(content, opts) end |
#default_opts(opts = {}) ⇒ Object
3 4 5 6 7 8 9 |
# File 'app/lib/foreman_opentofu/hcl_format.rb', line 3 def default_opts(opts = {}) { indent: 2, depth: 0, snippet: false, }.merge opts end |
#format_value(val, type) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'app/lib/foreman_opentofu/hcl_format.rb', line 86 def format_value(val, type) case type when 'string', 'select' then val when 'bool' then Foreman::Cast.to_bool(val) when 'number' then val.to_i else val end end |
#hash_to_hcl(hsh, opts) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/lib/foreman_opentofu/hcl_format.rb', line 45 def hash_to_hcl(hsh, opts) opts = default_opts(opts) hcl = '' new_opts = opts.merge(snippet: false) unless opts[:snippet] hcl << '{' new_opts[:depth] = opts[:depth] + 1 end close_block_on_newline = false hsh.each do |key, value| hcl << prefix_hcl(new_opts) hcl << "#{key} = #{to_hcl(value, new_opts)}" close_block_on_newline = true end hcl << prefix_hcl(opts) if close_block_on_newline hcl << '}' unless opts[:snippet] hcl end |
#prefix_hcl(opts) ⇒ Object
26 27 28 |
# File 'app/lib/foreman_opentofu/hcl_format.rb', line 26 def prefix_hcl(opts) "\n#{' ' * opts[:indent] * opts[:depth]}" end |
#to_hcl(var, opts = {}) ⇒ Object
possible ‘opts`:
`indent` : number of whitespace to indent; default 2
`depth` : start value of depth (for indentation); default: 0
`snippet`: if `true` and var is a `Hash` string will not be framed with `{}`
15 16 17 18 19 20 21 22 23 24 |
# File 'app/lib/foreman_opentofu/hcl_format.rb', line 15 def to_hcl(var, opts = {}) opts = default_opts.merge(opts) case var when Hash then hash_to_hcl(var, opts) when Array then array_to_hcl(var, opts) when String then var.inspect else var.to_s end end |