forked from terraform-docs/terraform-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
576 lines (495 loc) · 15.3 KB
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
Copyright 2021 The terraform-docs Authors.
Licensed under the MIT license (the "License"); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at the LICENSE file in
the root directory of this source tree.
*/
package terraform
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/hashicorp/hcl/v2/hclsimple"
"github.com/rquadling/terraform-config-inspect/tfconfig"
"github.com/terraform-docs/terraform-docs/internal/reader"
"github.com/terraform-docs/terraform-docs/internal/types"
"github.com/terraform-docs/terraform-docs/print"
)
// LoadWithOptions returns new instance of Module with all the inputs and
// outputs discovered from provided 'path' containing Terraform config
func LoadWithOptions(config *print.Config) (*Module, error) {
tfmodule, err := loadModule(config.ModuleRoot)
if err != nil {
return nil, err
}
module, err := loadModuleItems(tfmodule, config)
if err != nil {
return nil, err
}
sortItems(module, config)
return module, nil
}
func loadModule(path string) (*tfconfig.Module, error) {
module, diag := tfconfig.LoadModule(path)
if diag != nil && diag.HasErrors() {
return nil, diag
}
return module, nil
}
func loadModuleItems(tfmodule *tfconfig.Module, config *print.Config) (*Module, error) {
header, err := loadHeader(config)
if err != nil {
return nil, err
}
footer, err := loadFooter(config)
if err != nil {
return nil, err
}
inputs, required, optional := loadInputs(tfmodule, config)
modulecalls := loadModulecalls(tfmodule, config)
outputs, err := loadOutputs(tfmodule, config)
if err != nil {
return nil, err
}
providers := loadProviders(tfmodule, config)
requirements := loadRequirements(tfmodule)
resources := loadResources(tfmodule, config)
return &Module{
Header: header,
Footer: footer,
Inputs: inputs,
ModuleCalls: modulecalls,
Outputs: outputs,
Providers: providers,
Requirements: requirements,
Resources: resources,
RequiredInputs: required,
OptionalInputs: optional,
}, nil
}
func getFileFormat(filename string) string {
if filename == "" {
return ""
}
last := strings.LastIndex(filename, ".")
if last == -1 {
return ""
}
return filename[last:]
}
func isFileFormatSupported(filename string, section string) (bool, error) {
if section == "" {
return false, errors.New("section is missing")
}
if filename == "" {
return false, fmt.Errorf("--%s-from value is missing", section)
}
switch getFileFormat(filename) {
case ".adoc", ".md", ".tf", ".txt":
return true, nil
}
return false, fmt.Errorf("only .adoc, .md, .tf, and .txt formats are supported to read %s from", section)
}
func loadHeader(config *print.Config) (string, error) {
if !config.Sections.Header {
return "", nil
}
return loadSection(config, config.HeaderFrom, "header")
}
func loadFooter(config *print.Config) (string, error) {
if !config.Sections.Footer {
return "", nil
}
return loadSection(config, config.FooterFrom, "footer")
}
func loadSection(config *print.Config, file string, section string) (string, error) { //nolint:gocyclo
// NOTE(khos2ow): this function is over our cyclomatic complexity goal.
// Be wary when adding branches, and look for functionality that could
// be reasonably moved into an injected dependency.
if section == "" {
return "", errors.New("section is missing")
}
filename := filepath.Join(config.ModuleRoot, file)
if ok, err := isFileFormatSupported(file, section); !ok {
return "", err
}
if info, err := os.Stat(filename); os.IsNotExist(err) || info.IsDir() {
if section == "header" && file == "main.tf" {
return "", nil // absorb the error to not break workflow for default value of header and missing 'main.tf'
}
return "", err // user explicitly asked for a file which doesn't exist
}
if getFileFormat(file) != ".tf" {
content, err := os.ReadFile(filepath.Clean(filename))
if err != nil {
return "", err
}
return string(content), nil
}
lines := reader.Lines{
FileName: filename,
LineNum: -1,
Condition: func(line string) bool {
line = strings.TrimSpace(line)
return strings.HasPrefix(line, "/*") || strings.HasPrefix(line, "*") || strings.HasPrefix(line, "*/")
},
Parser: func(line string) (string, bool) {
tmp := strings.TrimSpace(line)
if strings.HasPrefix(tmp, "/*") || strings.HasPrefix(tmp, "*/") {
return "", false
}
if tmp == "*" {
return "", true
}
line = strings.TrimLeft(line, " ")
line = strings.TrimRight(line, "\r\n")
line = strings.TrimPrefix(line, "* ")
return line, true
},
}
sectionText, err := lines.Extract()
if err != nil {
return "", err
}
return strings.Join(sectionText, "\n"), nil
}
func loadInputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Input, []*Input, []*Input) {
var inputs = make([]*Input, 0, len(tfmodule.Variables))
var required = make([]*Input, 0, len(tfmodule.Variables))
var optional = make([]*Input, 0, len(tfmodule.Variables))
for _, input := range tfmodule.Variables {
comments := loadComments(input.Pos.Filename, input.Pos.Line)
// skip over inputs that are marked as being ignored
if strings.Contains(comments, "terraform-docs-ignore") {
continue
}
// convert CRLF to LF early on (https://github.com/terraform-docs/terraform-docs/issues/305)
inputDescription := strings.ReplaceAll(input.Description, "\r\n", "\n")
if inputDescription == "" && config.Settings.ReadComments {
inputDescription = comments
}
i := &Input{
Name: input.Name,
Type: types.TypeOf(input.Type, input.Default),
Description: types.String(inputDescription),
Default: types.ValueOf(input.Default),
Required: input.Required,
Position: Position{
Filename: input.Pos.Filename,
Line: input.Pos.Line,
},
}
for _, validation := range input.Validation {
i.Validation = append(i.Validation, types.String(validation))
}
inputs = append(inputs, i)
if i.HasDefault() {
optional = append(optional, i)
} else {
required = append(required, i)
}
}
return inputs, required, optional
}
func formatSource(s, v string) (source, version string) {
substr := "?ref="
if v != "" {
return s, v
}
pos := strings.LastIndex(s, substr)
if pos == -1 {
return s, version
}
adjustedPos := pos + len(substr)
if adjustedPos >= len(s) {
return s, version
}
source = s[0:pos]
version = s[adjustedPos:]
return source, version
}
func loadModulecalls(tfmodule *tfconfig.Module, config *print.Config) []*ModuleCall {
var modules = make([]*ModuleCall, 0)
var source, version string
for _, m := range tfmodule.ModuleCalls {
comments := loadComments(m.Pos.Filename, m.Pos.Line)
// skip over modules that are marked as being ignored
if strings.Contains(comments, "terraform-docs-ignore") {
continue
}
description := ""
if config.Settings.ReadComments {
description = comments
}
source, version = formatSource(m.Source, m.Version)
modules = append(modules, &ModuleCall{
Name: m.Name,
Source: source,
Version: version,
Description: types.String(description),
Position: Position{
Filename: m.Pos.Filename,
Line: m.Pos.Line,
},
})
}
return modules
}
func loadOutputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Output, error) {
outputs := make([]*Output, 0, len(tfmodule.Outputs))
values := make(map[string]*output)
if config.OutputValues.Enabled {
var err error
values, err = loadOutputValues(config)
if err != nil {
return nil, err
}
}
for _, o := range tfmodule.Outputs {
comments := loadComments(o.Pos.Filename, o.Pos.Line)
// skip over outputs that are marked as being ignored
if strings.Contains(comments, "terraform-docs-ignore") {
continue
}
// convert CRLF to LF early on (https://github.com/terraform-docs/terraform-docs/issues/584)
description := strings.ReplaceAll(o.Description, "\r\n", "\n")
if description == "" && config.Settings.ReadComments {
description = comments
}
output := &Output{
Name: o.Name,
Description: types.String(description),
Position: Position{
Filename: o.Pos.Filename,
Line: o.Pos.Line,
},
ShowValue: config.OutputValues.Enabled,
}
if config.OutputValues.Enabled {
if value, ok := values[output.Name]; ok {
output.Sensitive = value.Sensitive
output.Value = types.ValueOf(value.Value)
} else {
output.Value = types.ValueOf("null")
}
if output.Sensitive {
output.Value = types.ValueOf(`<sensitive>`)
}
}
outputs = append(outputs, output)
}
return outputs, nil
}
func loadOutputValues(config *print.Config) (map[string]*output, error) {
var out []byte
var err error
if config.OutputValues.From == "" {
cmd := exec.Command("terraform", "output", "-json")
cmd.Dir = config.ModuleRoot
if out, err = cmd.Output(); err != nil {
return nil, fmt.Errorf("caught error while reading the terraform outputs: %w", err)
}
} else if out, err = os.ReadFile(config.OutputValues.From); err != nil {
return nil, fmt.Errorf("caught error while reading the terraform outputs file at %s: %w", config.OutputValues.From, err)
}
var terraformOutputs map[string]*output
err = json.Unmarshal(out, &terraformOutputs)
if err != nil {
return nil, err
}
return terraformOutputs, err
}
func loadProviders(tfmodule *tfconfig.Module, config *print.Config) []*Provider { //nolint:gocyclo
// NOTE(khos2ow): this function is over our cyclomatic complexity goal.
// Be wary when adding branches, and look for functionality that could
// be reasonably moved into an injected dependency.
type provider struct {
Name string `hcl:"name,label"`
Version string `hcl:"version"`
Constraints *string `hcl:"constraints"`
Hashes []string `hcl:"hashes"`
}
type lockfile struct {
Provider []provider `hcl:"provider,block"`
}
lock := make(map[string]provider)
if config.Settings.LockFile {
var lf lockfile
filename := filepath.Join(config.ModuleRoot, ".terraform.lock.hcl")
if err := hclsimple.DecodeFile(filename, nil, &lf); err == nil {
for i := range lf.Provider {
segments := strings.Split(lf.Provider[i].Name, "/")
name := segments[len(segments)-1]
lock[name] = lf.Provider[i]
}
}
}
resources := []map[string]*tfconfig.Resource{tfmodule.ManagedResources, tfmodule.DataResources}
discovered := make(map[string]*Provider)
for _, resource := range resources {
for _, r := range resource {
comments := loadComments(r.Pos.Filename, r.Pos.Line)
// skip over resources that are marked as being ignored
if strings.Contains(comments, "terraform-docs-ignore") {
continue
}
var version = ""
if l, ok := lock[r.Provider.Name]; ok {
version = l.Version
} else if rv, ok := tfmodule.RequiredProviders[r.Provider.Name]; ok && len(rv.VersionConstraints) > 0 {
version = strings.Join(rv.VersionConstraints, " ")
}
key := fmt.Sprintf("%s.%s", r.Provider.Name, r.Provider.Alias)
if _, ok := discovered[key]; ok {
continue
}
discovered[key] = &Provider{
Name: r.Provider.Name,
Alias: types.String(r.Provider.Alias),
Version: types.String(version),
Position: Position{
Filename: r.Pos.Filename,
Line: r.Pos.Line,
},
}
}
}
providers := make([]*Provider, 0, len(discovered))
for _, provider := range discovered {
providers = append(providers, provider)
}
return providers
}
func loadRequirements(tfmodule *tfconfig.Module) []*Requirement {
var requirements = make([]*Requirement, 0)
for _, core := range tfmodule.RequiredCore {
requirements = append(requirements, &Requirement{
Name: "terraform",
Version: types.String(core),
})
}
names := make([]string, 0, len(tfmodule.RequiredProviders))
for n := range tfmodule.RequiredProviders {
names = append(names, n)
}
sort.Strings(names)
for _, name := range names {
for _, version := range tfmodule.RequiredProviders[name].VersionConstraints {
requirements = append(requirements, &Requirement{
Name: name,
Version: types.String(version),
})
}
}
return requirements
}
func loadResources(tfmodule *tfconfig.Module, config *print.Config) []*Resource {
allResources := []map[string]*tfconfig.Resource{tfmodule.ManagedResources, tfmodule.DataResources}
discovered := make(map[string]*Resource)
for _, resource := range allResources {
for _, r := range resource {
comments := loadComments(r.Pos.Filename, r.Pos.Line)
// skip over resources that are marked as being ignored
if strings.Contains(comments, "terraform-docs-ignore") {
continue
}
var version string
if rv, ok := tfmodule.RequiredProviders[r.Provider.Name]; ok {
version = resourceVersion(rv.VersionConstraints)
}
var source string
if len(tfmodule.RequiredProviders[r.Provider.Name].Source) > 0 {
source = tfmodule.RequiredProviders[r.Provider.Name].Source
} else {
source = fmt.Sprintf("%s/%s", "hashicorp", r.Provider.Name)
}
rType := strings.TrimPrefix(r.Type, r.Provider.Name+"_")
key := fmt.Sprintf("%s.%s.%s.%s", r.Provider.Name, r.Mode, rType, r.Name)
description := ""
if config.Settings.ReadComments {
description = comments
}
discovered[key] = &Resource{
Type: rType,
Name: r.Name,
Mode: r.Mode.String(),
ProviderName: r.Provider.Name,
ProviderSource: source,
Version: types.String(version),
Description: types.String(description),
Position: Position{
Filename: r.Pos.Filename,
Line: r.Pos.Line,
},
}
}
}
resources := make([]*Resource, 0, len(discovered))
for _, resource := range discovered {
resources = append(resources, resource)
}
return resources
}
func resourceVersion(constraints []string) string {
if len(constraints) == 0 {
return "latest"
}
versionParts := strings.Split(constraints[len(constraints)-1], " ")
switch len(versionParts) {
case 1:
if _, err := strconv.Atoi(versionParts[0][0:1]); err != nil {
if versionParts[0][0:1] == "=" {
return versionParts[0][1:]
}
return "latest"
}
return versionParts[0]
case 2:
if versionParts[0] == "=" {
return versionParts[1]
}
}
return "latest"
}
func loadComments(filename string, lineNum int) string {
lines := reader.Lines{
FileName: filename,
LineNum: lineNum,
Condition: func(line string) bool {
return strings.HasPrefix(line, "#") || strings.HasPrefix(line, "//")
},
Parser: func(line string) (string, bool) {
line = strings.TrimSpace(line)
line = strings.TrimPrefix(line, "#")
line = strings.TrimPrefix(line, "//")
line = strings.TrimSpace(line)
return line, true
},
}
comment, err := lines.Extract()
if err != nil {
return "" // absorb the error, we don't need to bubble it up or break the execution
}
return strings.Join(comment, " ")
}
func sortItems(tfmodule *Module, config *print.Config) {
// inputs
inputs(tfmodule.Inputs).sort(config.Sort.Enabled, config.Sort.By)
inputs(tfmodule.RequiredInputs).sort(config.Sort.Enabled, config.Sort.By)
inputs(tfmodule.OptionalInputs).sort(config.Sort.Enabled, config.Sort.By)
// outputs
outputs(tfmodule.Outputs).sort(config.Sort.Enabled, config.Sort.By)
// providers
providers(tfmodule.Providers).sort(config.Sort.Enabled, config.Sort.By)
// resources
resources(tfmodule.Resources).sort(config.Sort.Enabled, config.Sort.By)
// modules
modulecalls(tfmodule.ModuleCalls).sort(config.Sort.Enabled, config.Sort.By)
}