terradoc 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c668a9992a63992cab4d5fde32f12de35e7e23b928c661f07d50c50836660911
4
- data.tar.gz: 5cdefd3f75f4be030e0c2e705a3d6b11dbeacfdcd17eba1f6a26f20ad8ae2963
3
+ metadata.gz: 0deada70047ce96aaebd65a8ba1b59deed1a73e2de26fb52ef555cdc1bd45ab4
4
+ data.tar.gz: a969849a9d998faffdc452ede71654ebc25b22d69202421da7ae59a2367cdd38
5
5
  SHA512:
6
- metadata.gz: ccee360f632a4fd11c305bde0355db05ff46973beebe34cdaff6757032e9fc855fa36973b2f51c841044aea02d6dad53b3506df974866d556fe41b232346b079
7
- data.tar.gz: 3f05794e964a3937e5a5e34f558eb44117ddabb35181752999b3f13db82fe207b1f745e0f461a669a6e26f1dec623cab468e525eb4687efd3739ffcd08b2ad75
6
+ metadata.gz: 0bb6e91cfb9999706f47393e06448e224406685d9e521979f63c269bd1bc2e459b78360e7e760b95cdbc11673816af35f71d073ee7a5307aa00366b2c4dbde62
7
+ data.tar.gz: cdd4007eccd3a48170d35a8ad11688d5f42ee26d9b08a61244e41f5108eb0b0f17ab09784941a5e659c114df9852a702a180fe30ab6a0e5ad7c8398fbd6e86c6
data/.rubocop.yml CHANGED
@@ -67,5 +67,12 @@ Style/RedundantBegin:
67
67
  Style/RedundantReturn:
68
68
  Enabled: false
69
69
 
70
+ Style/GuardClause:
71
+ Enabled: false
72
+
73
+ Style/SafeNavigation:
74
+ Enabled: false
75
+
70
76
  Style/SpecialGlobalVars:
71
77
  Enabled: false
78
+
data/CHANGELOG.md CHANGED
@@ -5,11 +5,19 @@ All notable changes to this project will be documented in this file.
5
5
 
6
6
  This changelog was automatically generated using [Caretaker](https://github.com/TerraformToolbox/terradoc) by [Wolf Software](https://github.com/WolfSoftware)
7
7
 
8
+ ### [v0.1.5](https://github.com/TerraformToolbox/terradoc/compare/v0.1.4...v0.1.5)
9
+
10
+ > Released on March, 18th 2020
11
+
12
+ - Update the readme ready for release [`[830805a]`](https://github.com/TerraformToolbox/terradoc/commit/830805a455387532a39abb52bec7b634eb83ab67) [`[TGWolf]`](https://github.com/TGWolf)
13
+
14
+ - Fairly big internal review to make use of hcl2json, and additional outputs [`[b528f93]`](https://github.com/TerraformToolbox/terradoc/commit/b528f9385eb98ee4756f7feb9c2a9b481e302ff8) [`[TGWolf]`](https://github.com/TGWolf)
15
+
8
16
  ### [v0.1.4](https://github.com/TerraformToolbox/terradoc/compare/v0.1.3...v0.1.4)
9
17
 
10
18
  > Released on March, 18th 2020
11
19
 
12
- - Remember to remove the debugging output [`[498e435]`](https://github.com/TerraformToolbox/terradoc/commit/498e435a526888867657af27467932bc20323759) [`[TGWolf]`](https://github.com/TGWolf)
20
+ - Remember to remove the debugging output [`[b962a62]`](https://github.com/TerraformToolbox/terradoc/commit/b962a6269494fe5aaee758110356c359a5481240) [`[TGWolf]`](https://github.com/TGWolf)
13
21
 
14
22
  ### [v0.1.3](https://github.com/TerraformToolbox/terradoc/compare/v0.1.2...v0.1.3)
15
23
 
data/EXAMPLE.md CHANGED
@@ -1,24 +1,25 @@
1
1
  # Some example README
2
2
 
3
- ## Terraform Data Sources
3
+ ## Data Sources
4
4
  <!--Terradoc-data-sources-start-->
5
5
  <!--Terradoc-data-sources-end-->
6
6
 
7
- ## Terraform Modules
7
+ ## Modules
8
8
  <!--Terradoc-modules-start-->
9
9
  <!--Terradoc-modules-end-->
10
10
 
11
- ## The Outputs
11
+ ## Outputs
12
12
  <!--Terradoc-outputs-start-->
13
13
  <!--Terradoc-outputs-end-->
14
14
 
15
- ## The resources we create
15
+ ## Providers
16
+ <!--Terradoc-providers-start-->
17
+ <!--Terradoc-providers-end-->
18
+
19
+ ## Resources
16
20
  <!--Terradoc-resources-start-->
17
21
  <!--Terradoc-resources-end-->
18
22
 
19
- ## The inputs / variables
23
+ ## Variables
20
24
  <!--Terradoc-variables-start-->
21
25
  <!--Terradoc-variables-end-->
22
-
23
-
24
- # END
data/README.md CHANGED
@@ -49,7 +49,38 @@ Usage: terradoc
49
49
 
50
50
  ```
51
51
 
52
- Once you have installed Terradoc you can simply run ```terradoc``` and it will generate the output.
52
+ ### Adding Tags
53
+
54
+ There are a number tags that you need to add to your README in order for terradoc to know where to insert the data.
55
+
56
+ ```
57
+ # Data Sources
58
+ <!--Terradoc-data-sources-start-->
59
+ <!--Terradoc-data-sources-end-->
60
+
61
+ # Modules
62
+ <!--Terradoc-modules-start-->
63
+ <!--Terradoc-modules-end-->
64
+
65
+ # Outputs
66
+ <!--Terradoc-outputs-start-->
67
+ <!--Terradoc-outputs-end-->
68
+
69
+ # Providers
70
+ <!--Terradoc-providers-start-->
71
+ <!--Terradoc-providers-end-->
72
+
73
+ # Resources
74
+ <!--Terradoc-resources-start-->
75
+ <!--Terradoc-resources-end-->
76
+
77
+ # Variables
78
+ <!--Terradoc-variables-start-->
79
+ <!--Terradoc-variables-end-->
80
+
81
+ ```
82
+
83
+ Now you can simply run ```terradoc``` and it will generate the output.
53
84
 
54
85
  ## Contributing to Terradoc
55
86
 
@@ -77,3 +108,4 @@ For further information please refer to the [contributing](https://github.com/Te
77
108
 
78
109
  <p align="right"><img src="https://img.shields.io/badge/Created%20By-Wolf-black?style=for-the-badge" /></p>
79
110
 
111
+
data/VERSION.txt CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -1,3 +1,3 @@
1
1
  class Terradoc
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.1.5'.freeze
3
3
  end
data/lib/terradoc.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'terradoc/version'
2
2
 
3
+ require 'json'
4
+ require 'open3'
3
5
  require 'tty-spinner'
4
6
 
5
7
  # -------------------------------------------------------------------------------- #
@@ -9,6 +11,10 @@ class Terradoc
9
11
  # -------------------------------------------------------------------------------- #
10
12
  # -------------------------------------------------------------------------------- #
11
13
  def initialize(options = {})
14
+ @command = 'hcl2json'
15
+
16
+ raise StandardError.new("Failed to locate #{@command} please install (pip install #{@command})") unless which @command
17
+
12
18
  @base_path = '.'
13
19
  @base_path = options[:path] unless options[:path].nil?
14
20
  @base_path.chomp!('/')
@@ -17,15 +23,12 @@ class Terradoc
17
23
  @output_file = 'README.md'
18
24
  @output_file = options[:output] unless options[:output].nil?
19
25
 
26
+ @raw_results = {}
20
27
  @files = []
21
28
  @pattern = "#{@base_path}/*.tf"
22
29
 
23
30
  @config = {
24
31
  'data-sources' => {
25
- 'include' => 'data',
26
- 'match' => /^(?!data).*/,
27
- 'regex' => /^data\s*"(?<name>.*?)"\s*".*/,
28
- 'split' => /(?=data\s*")/,
29
32
  'start' => '<!--Terradoc-data-sources-start-->',
30
33
  'end' => '<!--Terradoc-data-sources-end-->',
31
34
  'raw_results' => [],
@@ -33,10 +36,6 @@ class Terradoc
33
36
  'data_table' => []
34
37
  },
35
38
  'modules' => {
36
- 'include' => 'module',
37
- 'match' => /^(?!module).*/,
38
- 'regex' => /^module\s*".*"\s*{\s*source\s*=\s*"(?<name>.*?)"/,
39
- 'split' => /(?=module\s*")/,
40
39
  'start' => '<!--Terradoc-modules-start-->',
41
40
  'end' => '<!--Terradoc-modules-end-->',
42
41
  'raw_results' => [],
@@ -44,11 +43,6 @@ class Terradoc
44
43
  'data_table' => []
45
44
  },
46
45
  'outputs' => {
47
- 'include' => 'output',
48
- 'match' => /^(?!output).*/,
49
- 'regex' => /^output\s*"(?<name>.*)"\s*{\s*(?<details>.*)\s*}/,
50
- 'regex_stage_2' => /description\s*=\s*"(?<description>.*?)".*/,
51
- 'split' => /(?=output\s*")/,
52
46
  'start' => '<!--Terradoc-outputs-start-->',
53
47
  'end' => '<!--Terradoc-outputs-end-->',
54
48
  'raw_results' => [],
@@ -56,22 +50,20 @@ class Terradoc
56
50
  'data_table' => []
57
51
  },
58
52
  'resources' => {
59
- 'include' => 'resource',
60
- 'match' => /^(?!resource).*/,
61
- 'regex' => /^resource\s*"(?<name>.*?)"\s*".*/,
62
- 'split' => /(?=resource\s*")/,
63
53
  'start' => '<!--Terradoc-resources-start-->',
64
54
  'end' => '<!--Terradoc-resources-end-->',
65
55
  'raw_results' => [],
66
56
  'sorted_results' => [],
67
57
  'data_table' => []
68
58
  },
59
+ 'providers' => {
60
+ 'start' => '<!--Terradoc-providers-start-->',
61
+ 'end' => '<!--Terradoc-providers-end-->',
62
+ 'raw_results' => [],
63
+ 'sorted_results' => [],
64
+ 'data_table' => []
65
+ },
69
66
  'variables' => {
70
- 'include' => 'variable',
71
- 'match' => /^(?!variable).*/,
72
- 'regex' => /^variable\s*"(?<name>.*)"\s*{\s*(?<details>.*)\s*}/,
73
- 'regex_stage_2' => /(description|type|default)\s*=\s*(.*?(?=description|type|default\s*=\s*|$))/,
74
- 'split' => /(?=variable\s*")/,
75
67
  'start' => '<!--Terradoc-variables-start-->',
76
68
  'end' => '<!--Terradoc-variables-end-->',
77
69
  'raw_results' => [],
@@ -83,6 +75,19 @@ class Terradoc
83
75
  terradoc_main
84
76
  end
85
77
 
78
+ # -------------------------------------------------------------------------------- #
79
+ # -------------------------------------------------------------------------------- #
80
+ def which(cmd)
81
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
82
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
83
+ exts.each do |ext|
84
+ exe = File.join(path, "#{cmd}#{ext}")
85
+ return exe if File.executable?(exe) && !File.directory?(exe)
86
+ end
87
+ end
88
+ return nil
89
+ end
90
+
86
91
  # -------------------------------------------------------------------------------- #
87
92
  # -------------------------------------------------------------------------------- #
88
93
  def terradoc_main
@@ -90,15 +95,17 @@ class Terradoc
90
95
 
91
96
  sp1 = spinners.register '[:spinner] Locating files'
92
97
  sp2 = spinners.register '[:spinner] Processing files'
93
- sp3 = spinners.register '[:spinner] Sorting the results'
94
- sp4 = spinners.register '[:spinner] Generating data tables'
95
- sp5 = spinners.register '[:spinner] Writing output'
98
+ sp3 = spinners.register '[:spinner] Processing raw results'
99
+ sp4 = spinners.register '[:spinner] Sorting the results'
100
+ sp5 = spinners.register '[:spinner] Generating data tables'
101
+ sp6 = spinners.register '[:spinner] Writing output'
96
102
 
97
103
  sp1.auto_spin
98
104
  sp2.auto_spin
99
105
  sp3.auto_spin
100
106
  sp4.auto_spin
101
107
  sp5.auto_spin
108
+ sp6.auto_spin
102
109
 
103
110
  generate_file_list
104
111
  sp1.success
@@ -106,14 +113,17 @@ class Terradoc
106
113
  process_files
107
114
  sp2.success
108
115
 
109
- sort_details
116
+ process_raw_results
110
117
  sp3.success
111
118
 
112
- generate_data_tables
119
+ sort_details
113
120
  sp4.success
114
121
 
115
- write_output
122
+ generate_data_tables
116
123
  sp5.success
124
+
125
+ write_output
126
+ sp6.success
117
127
  end
118
128
 
119
129
  # -------------------------------------------------------------------------------- #
@@ -128,11 +138,64 @@ class Terradoc
128
138
  # -------------------------------------------------------------------------------- #
129
139
  def process_files
130
140
  @files.each do |file|
131
- lines = File.readlines(file)
132
- lines = lines.reject { |x| x.start_with? '#' }
133
- data = lines.join(' ').gsub("\r\n", ' ').gsub("\n", ' ').gsub("\r", ' ').squeeze(' ').strip
141
+ Open3.popen3("hcl2json #{file}") do |_stdin, stdout, _stderr, _wait_thr|
142
+ stdout_str = stdout.read
143
+
144
+ json = JSON.parse(stdout_str)
145
+ @raw_results = @raw_results.merge(json)
146
+ end
147
+ end
148
+ end
149
+
150
+ # -------------------------------------------------------------------------------- #
151
+ # -------------------------------------------------------------------------------- #
152
+ def process_raw_results
153
+ unless @raw_results['data'].nil?
154
+ @raw_results['data'].each do |key, _value|
155
+ name = key.gsub('_', '\_')
156
+ @config['data-sources']['raw_results'] << { :name => name }
157
+ end
158
+ end
159
+
160
+ unless @raw_results['module'].nil?
161
+ @raw_results['module'].each do |key, value|
162
+ name = key.gsub('_', '\_')
163
+ @config['modules']['raw_results'] << { :name => name, :source => value['source'], :version => value['version'] }
164
+ end
165
+ end
166
+
167
+ unless @raw_results['output'].nil?
168
+ @raw_results['output'].each do |key, value|
169
+ name = key.gsub('_', '\_')
170
+ @config['outputs']['raw_results'] << { :name => name, :description => value['description'] }
171
+ end
172
+ end
134
173
 
135
- process_file(data)
174
+ unless @raw_results['provider'].nil?
175
+ @raw_results['provider'].each do |key, _value|
176
+ name = key.gsub('_', '\_')
177
+ @config['providers']['raw_results'] << { :name => name }
178
+ end
179
+ end
180
+
181
+ unless @raw_results['resource'].nil?
182
+ @raw_results['resource'].each do |key, _value|
183
+ name = key.gsub('_', '\_')
184
+ @config['resources']['raw_results'] << { :name => name }
185
+ end
186
+ end
187
+
188
+ unless @raw_results['variable'].nil?
189
+ @raw_results['variable'].each do |key, value|
190
+ name = key.gsub('_', '\_')
191
+ default = value['default'].to_s.gsub('_', '\_')
192
+ required = if value.key?(:default)
193
+ 'No'
194
+ else
195
+ 'Yes'
196
+ end
197
+ @config['variables']['raw_results'] << { :name => name, :description => value['description'], :type => value['type'], :default => default, :required => required }
198
+ end
136
199
  end
137
200
  end
138
201
 
@@ -141,7 +204,6 @@ class Terradoc
141
204
  def cleanup_array(array)
142
205
  array = array.uniq { |k| k[:name] } if array.count.positive?
143
206
  array = array.sort_by { |k| k[:name] } if array.count.positive?
144
-
145
207
  return array
146
208
  end
147
209
 
@@ -200,7 +262,7 @@ class Terradoc
200
262
 
201
263
  if @config['variables']['sorted_results'].size.positive?
202
264
  @config['variables']['data_table'] << '| Name | Description | Type | Default | Required? |'
203
- @config['variables']['data_table'] << '| ---- | ----------- | ---- |:-------:|:---------:|'
265
+ @config['variables']['data_table'] << '| ---- | ----------- |:----:|:-------:|:---------:|'
204
266
  @config['variables']['sorted_results'].each do |item|
205
267
  @config['variables']['data_table'] << "| #{item[:name]} | #{item[:description]} | #{item[:type]} | #{item[:default]} | #{item[:required]} |"
206
268
  end
@@ -209,63 +271,6 @@ class Terradoc
209
271
  end
210
272
  end
211
273
 
212
- # -------------------------------------------------------------------------------- #
213
- # -------------------------------------------------------------------------------- #
214
- def clean_string(str)
215
- return str if str.empty?
216
-
217
- return str.strip.gsub(/\"/, '').chomp('.').strip
218
- end
219
-
220
- # -------------------------------------------------------------------------------- #
221
- # -------------------------------------------------------------------------------- #
222
- def process_file(data)
223
- @config.each do |key, value|
224
- next unless data.include?(value['include'])
225
-
226
- items = data.split(value['split'])
227
- items.each do |item|
228
- result = {}
229
-
230
- item.strip!
231
- next if item.match(value['match'])
232
-
233
- matches = item.match(value['regex'])
234
-
235
- next unless matches.size.positive?
236
- next unless matches.names.include?('name')
237
-
238
- result[:name] = matches[:name].gsub('_', '\_')
239
-
240
- if matches.names.include?('details')
241
- if key == 'outputs'
242
- description = '-'
243
-
244
- parts = item.match(value['regex_stage_2'])
245
- description = clean_string(parts[:description]) if parts.size.positive?
246
-
247
- result[:description] = description
248
- else
249
- matches[:details].scan(value['regex_stage_2']).each do |key2, value2|
250
- result[key2.to_sym] = if key2 == 'default'
251
- value2
252
- else
253
- clean_string(value2)
254
- end
255
- end
256
-
257
- result[:required] = if result.key?(:default)
258
- 'No'
259
- else
260
- 'Yes'
261
- end
262
- end
263
- end
264
- value['raw_results'] << result
265
- end
266
- end
267
- end
268
-
269
274
  # -------------------------------------------------------------------------------- #
270
275
  # -------------------------------------------------------------------------------- #
271
276
  def load_file(filename)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terradoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Gurney aka Wolf