hcl2-rb 0.9.1 → 0.9.2
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 +4 -4
- data/lib/hcl2-rb/formatter.rb +29 -11
- data/lib/hcl2-rb/helpers.rb +1 -1
- data/spec/formatter_spec.rb +122 -35
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da8df68511ccd65a048c216406ece161d869e2b8180a3ad3ac52e77b7526bcd4
|
4
|
+
data.tar.gz: 7b247239df05a67799097c6bf254f6ad15bd4afdfd36ac7fbb2d965786db8eff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00cba379c4e4c17690c5112b5a86c2fdd2057040c5266b06b4f96dc018c2784744280d99a09ecb5536bfd2d56efaa4f69d7304bf2e589c75d6de1b79e132d21b
|
7
|
+
data.tar.gz: ba89c043c9a91503e8716aeb8280f76e1889d9727ec7ae4ac8405e9f3f08e8ca18f49a2992b520f9fce7f1ed3c4bd2b47fe0396e64fb9dc2d952361be3f6b6ec
|
data/lib/hcl2-rb/formatter.rb
CHANGED
@@ -9,25 +9,33 @@ class Hash
|
|
9
9
|
true
|
10
10
|
end
|
11
11
|
|
12
|
-
def to_hcl2(
|
12
|
+
def to_hcl2(spaces: 2, tab: 0)
|
13
13
|
hcl2 = []
|
14
14
|
|
15
15
|
each do |k, v|
|
16
16
|
case v
|
17
|
-
when Numeric, String, TrueClass, FalseClass
|
17
|
+
when Numeric, String, TrueClass, FalseClass
|
18
18
|
hcl2.push("#{k} = #{v.to_hcl2}\n")
|
19
|
+
when Array
|
20
|
+
if v.has_child?
|
21
|
+
v.each do |child|
|
22
|
+
hcl2.push({ "#{k}": child }.to_hcl2)
|
23
|
+
end
|
24
|
+
else
|
25
|
+
hcl2.push("#{k} = #{v.to_hcl2}")
|
26
|
+
end
|
19
27
|
when Hash
|
20
28
|
labels = HCL2::Helpers.dfs(v)
|
21
29
|
labels.each do |label|
|
22
30
|
# It creates a label string and we shouldn't
|
23
31
|
# forget about addition space before this one
|
24
|
-
n = label.empty? ? nil : label.map { |l| l&.to_hcl2 }.join('').prepend(' ') # It creates label string
|
32
|
+
n = label.empty? ? nil : label.map { |l| l&.to_hcl2 }.join(' ').prepend(' ') # It creates label string
|
25
33
|
|
26
34
|
# We should go deeper to format child attributes
|
27
35
|
c = label.empty? ? v : v.dig(*label)
|
28
36
|
|
29
37
|
hcl2.push("#{k}#{n} {")
|
30
|
-
hcl2.push(c.to_hcl2(
|
38
|
+
hcl2.push(c.to_hcl2(spaces: spaces, tab: 1))
|
31
39
|
hcl2.push("}\n")
|
32
40
|
end
|
33
41
|
else
|
@@ -35,7 +43,23 @@ class Hash
|
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
38
|
-
hcl2.map { |v| v.prepend(' ' *
|
46
|
+
hcl2.map { |v| v.prepend(' ' * spaces * tab) }.join("\n")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Array
|
51
|
+
def has_child?
|
52
|
+
return false if empty?
|
53
|
+
|
54
|
+
each do |v|
|
55
|
+
return false unless v.is_a?(Hash)
|
56
|
+
end
|
57
|
+
|
58
|
+
true
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_hcl2
|
62
|
+
to_s
|
39
63
|
end
|
40
64
|
end
|
41
65
|
|
@@ -68,9 +92,3 @@ class FalseClass
|
|
68
92
|
to_s
|
69
93
|
end
|
70
94
|
end
|
71
|
-
|
72
|
-
class Array
|
73
|
-
def to_hcl2
|
74
|
-
to_s
|
75
|
-
end
|
76
|
-
end
|
data/lib/hcl2-rb/helpers.rb
CHANGED
data/spec/formatter_spec.rb
CHANGED
@@ -1,100 +1,187 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Numeric do
|
4
|
-
it 'format
|
4
|
+
it 'format to hcl2' do
|
5
5
|
expect(12_345.to_hcl2).to eq('12345')
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
RSpec.describe String do
|
10
|
-
it 'format
|
10
|
+
it 'format to hcl2' do
|
11
11
|
expect('string'.to_hcl2).to eq('"string"')
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
RSpec.describe Symbol do
|
16
|
-
it 'format
|
16
|
+
it 'format to hcl2' do
|
17
17
|
expect(:symbol.to_hcl2).to eq('"symbol"')
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
RSpec.describe TrueClass do
|
22
|
-
it 'format
|
22
|
+
it 'format to hcl2' do
|
23
23
|
expect(true.to_hcl2).to eq('true')
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
RSpec.describe FalseClass do
|
28
|
-
it 'format
|
28
|
+
it 'format to hcl2' do
|
29
29
|
expect(false.to_hcl2).to eq('false')
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
RSpec.describe Array do
|
34
|
-
it 'format
|
34
|
+
it 'format to hcl2' do
|
35
35
|
expect([1, 2, 'three'].to_hcl2).to eq('[1, 2, "three"]')
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
RSpec.describe Hash do
|
40
|
-
|
40
|
+
fixture = {
|
41
41
|
ui: true,
|
42
42
|
listener: {
|
43
43
|
tcp: {
|
44
|
-
address: '
|
44
|
+
address: '0.0.0.0:8200'
|
45
45
|
},
|
46
46
|
unix: {
|
47
47
|
address: '/run/vault.sock'
|
48
48
|
}
|
49
|
-
},
|
50
|
-
telemetry: {
|
51
|
-
statsite_address: 'statsite.company.local:8125'
|
52
|
-
},
|
53
|
-
advanced: {
|
54
|
-
foo: {
|
55
|
-
bar: {
|
56
|
-
bim: 'bam'
|
57
|
-
}
|
58
|
-
},
|
59
|
-
it: 'works?'
|
60
49
|
}
|
61
50
|
}
|
62
51
|
|
63
|
-
|
64
|
-
|
65
|
-
|
52
|
+
expectation = <<~EXP
|
53
|
+
ui = true
|
54
|
+
|
55
|
+
listener "tcp" {
|
56
|
+
address = "0.0.0.0:8200"
|
57
|
+
|
58
|
+
}
|
59
|
+
|
60
|
+
listener "unix" {
|
61
|
+
address = "/run/vault.sock"
|
62
|
+
|
63
|
+
}
|
64
|
+
EXP
|
65
|
+
|
66
|
+
it 'check childs' do
|
67
|
+
expect(fixture[:listener].has_child?).to eq(true)
|
68
|
+
expect(fixture[:listener][:tcp].has_child?).to eq(false)
|
66
69
|
end
|
67
70
|
|
68
|
-
it 'format
|
69
|
-
|
70
|
-
|
71
|
+
it 'format to hcl2' do
|
72
|
+
expect(fixture.to_hcl2).to eq(expectation)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
RSpec.describe 'Advanced cases' do
|
77
|
+
it 'check array objects formating' do
|
78
|
+
fixture = {
|
79
|
+
listener: [{
|
80
|
+
tcp: {
|
81
|
+
address: '0.0.0.0:8200'
|
82
|
+
}
|
83
|
+
}, {
|
84
|
+
tcp: {
|
85
|
+
address: '127.0.0.1:8201'
|
86
|
+
}
|
87
|
+
}]
|
88
|
+
}
|
71
89
|
|
90
|
+
expectation = <<~EXP
|
72
91
|
listener "tcp" {
|
73
|
-
address = "
|
92
|
+
address = "0.0.0.0:8200"
|
74
93
|
|
75
94
|
}
|
76
95
|
|
77
|
-
listener "
|
78
|
-
address = "
|
96
|
+
listener "tcp" {
|
97
|
+
address = "127.0.0.1:8201"
|
79
98
|
|
80
99
|
}
|
100
|
+
EXP
|
81
101
|
|
82
|
-
|
83
|
-
|
102
|
+
expect(fixture.to_hcl2).to eq(expectation)
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'check nested object' do
|
106
|
+
fixture = {
|
107
|
+
resource: {
|
108
|
+
openstack_compute_flavor_v2: {
|
109
|
+
this: {
|
110
|
+
name: 'this',
|
111
|
+
region: 'region_a',
|
112
|
+
ram: 4,
|
113
|
+
vcpus: 2
|
114
|
+
}
|
115
|
+
},
|
116
|
+
openstack_networking_port_v2: {
|
117
|
+
this: {
|
118
|
+
name: 'this',
|
119
|
+
region: 'region_a'
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
expectation = <<~EXP
|
126
|
+
resource "openstack_compute_flavor_v2" "this" {
|
127
|
+
name = "this"
|
128
|
+
|
129
|
+
region = "region_a"
|
130
|
+
|
131
|
+
ram = 4
|
132
|
+
|
133
|
+
vcpus = 2
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
resource "openstack_networking_port_v2" "this" {
|
138
|
+
name = "this"
|
139
|
+
|
140
|
+
region = "region_a"
|
84
141
|
|
85
142
|
}
|
143
|
+
EXP
|
86
144
|
|
87
|
-
|
88
|
-
|
89
|
-
|
145
|
+
expect(fixture.to_hcl2).to eq(expectation)
|
146
|
+
end
|
147
|
+
end
|
90
148
|
|
149
|
+
RSpec.describe 'HashiCorp examples' do
|
150
|
+
it 'Vault' do
|
151
|
+
fixture = {
|
152
|
+
ui: true,
|
153
|
+
listener: {
|
154
|
+
tcp: {
|
155
|
+
address: '127.0.0.1:8200'
|
156
|
+
},
|
157
|
+
unix: {
|
158
|
+
address: '/run/vault.sock'
|
91
159
|
}
|
160
|
+
},
|
161
|
+
telemetry: {
|
162
|
+
statsite_address: 'statsite.company.local:8125'
|
163
|
+
}
|
164
|
+
}
|
92
165
|
|
93
|
-
|
166
|
+
expectation = <<~EXP
|
167
|
+
ui = true
|
168
|
+
|
169
|
+
listener "tcp" {
|
170
|
+
address = "127.0.0.1:8200"
|
171
|
+
|
172
|
+
}
|
173
|
+
|
174
|
+
listener "unix" {
|
175
|
+
address = "/run/vault.sock"
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
telemetry {
|
180
|
+
statsite_address = "statsite.company.local:8125"
|
94
181
|
|
95
182
|
}
|
96
183
|
EXP
|
97
184
|
|
98
|
-
expect(
|
185
|
+
expect(fixture.to_hcl2).to eq(expectation)
|
99
186
|
end
|
100
187
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hcl2-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- iveahugeship
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
Ruby common data types formatter to HCL2.
|