-
Notifications
You must be signed in to change notification settings - Fork 534
/
pretty.go
118 lines (99 loc) · 2.42 KB
/
pretty.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
package pretty
import (
"bytes"
"fmt"
"strings"
"github.com/segmentio/terraform-docs/internal/pkg/print"
"github.com/segmentio/terraform-docs/internal/pkg/tfconf"
)
// Print prints a pretty document.
func Print(module *tfconf.Module, settings *print.Settings) (string, error) {
var buffer bytes.Buffer
module.Sort(settings)
if settings.ShowProviders {
printProviders(&buffer, module.Providers, settings)
}
if settings.ShowInputs {
printInputs(&buffer, module.Inputs, settings)
}
if settings.ShowOutputs {
printOutputs(&buffer, module.Outputs, settings)
}
return buffer.String(), nil
}
func getProviderVersion(provider *tfconf.Provider) string {
var result = ""
if provider.Version != "" {
result = fmt.Sprintf(" (%s)", provider.Version)
}
return result
}
func getInputDefaultValue(input *tfconf.Input, settings *print.Settings) string {
var result = "required"
if input.HasDefault() {
result = input.Default
}
return result
}
func getDescription(description string) string {
var result = "n/a"
if description != "" {
result = strings.TrimSuffix(description, "\n")
}
return result
}
func printProviders(buffer *bytes.Buffer, providers []*tfconf.Provider, settings *print.Settings) {
buffer.WriteString("\n\n")
for _, provider := range providers {
var format string
if settings.ShowColor {
format = "\033[36mprovider.%s\033[0m%s\n\n"
} else {
format = "provider.%s%s\n\n"
}
buffer.WriteString(
fmt.Sprintf(
format,
provider.GetName(),
getProviderVersion(provider),
),
)
}
}
func printInputs(buffer *bytes.Buffer, inputs []*tfconf.Input, settings *print.Settings) {
buffer.WriteString("\n\n")
for _, input := range inputs {
var format string
if settings.ShowColor {
format = "\033[36minput.%s\033[0m (%s)\n\033[90m%s\033[0m\n\n"
} else {
format = "input.%s (%s)\n%s\n\n"
}
buffer.WriteString(
fmt.Sprintf(
format,
input.Name,
getInputDefaultValue(input, settings),
getDescription(input.Description),
),
)
}
}
func printOutputs(buffer *bytes.Buffer, outputs []*tfconf.Output, settings *print.Settings) {
buffer.WriteString("\n\n")
for _, output := range outputs {
var format string
if settings.ShowColor {
format = "\033[36moutput.%s\033[0m\n\033[90m%s\033[0m\n\n"
} else {
format = "output.%s\n%s\n\n"
}
buffer.WriteString(
fmt.Sprintf(
format,
output.Name,
getDescription(output.Description),
),
)
}
}