-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathmodel.go
230 lines (192 loc) · 6.34 KB
/
model.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
package document
import (
"fmt"
"sort"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"github.com/norwoodj/helm-docs/pkg/helm"
)
type valueRow struct {
Key string
Type string
NotationType string
AutoDefault string
Default string
AutoDescription string
Description string
Section string
Column int
LineNumber int
Dependency string
IsGlobal bool
}
type chartTemplateData struct {
helm.ChartDocumentationInfo
HelmDocsVersion string
Values []valueRow
Sections sections
Files files
SkipVersionFooter bool
}
type sections struct {
DefaultSection section
Sections []section
}
type section struct {
SectionName string
SectionItems []valueRow
}
func sortValueRowsByOrder(valueRows []valueRow, sortOrder string) {
sort.Slice(valueRows, func(i, j int) bool {
// Globals sort above non-globals.
if valueRows[i].IsGlobal != valueRows[j].IsGlobal {
return valueRows[i].IsGlobal
}
// Group by dependency for non-globals.
if !valueRows[i].IsGlobal && !valueRows[j].IsGlobal {
// Values for the main chart sort above values for dependencies.
if (valueRows[i].Dependency == "") != (valueRows[j].Dependency == "") {
return valueRows[i].Dependency == ""
}
// Group dependency values together.
if valueRows[i].Dependency != valueRows[j].Dependency {
return valueRows[i].Dependency < valueRows[j].Dependency
}
}
// Sort the remaining values within the same section using the configured sort order.
switch sortOrder {
case FileSortOrder:
if valueRows[i].LineNumber == valueRows[j].LineNumber {
return valueRows[i].Column < valueRows[j].Column
}
return valueRows[i].LineNumber < valueRows[j].LineNumber
case AlphaNumSortOrder:
return valueRows[i].Key < valueRows[j].Key
default:
panic("cannot get here")
}
})
}
func sortValueRows(valueRows []valueRow) {
sortOrder := viper.GetString("sort-values-order")
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
sortOrder = AlphaNumSortOrder
}
sortValueRowsByOrder(valueRows, sortOrder)
}
func sortSectionedValueRows(sectionedValueRows sections) {
sortOrder := viper.GetString("sort-values-order")
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
sortOrder = AlphaNumSortOrder
}
sortValueRowsByOrder(sectionedValueRows.DefaultSection.SectionItems, sortOrder)
for _, section := range sectionedValueRows.Sections {
sortValueRowsByOrder(section.SectionItems, sortOrder)
}
}
func getUnsortedValueRows(document *yaml.Node, descriptions map[string]helm.ChartValueDescription) ([]valueRow, error) {
// Handle empty values file case.
if document.Kind == 0 {
return nil, nil
}
if document.Kind != yaml.DocumentNode {
return nil, fmt.Errorf("invalid node kind supplied: %d", document.Kind)
}
if document.Content[0].Kind != yaml.MappingNode {
return nil, fmt.Errorf("values file must resolve to a map (was %d)", document.Content[0].Kind)
}
return createValueRowsFromField("", nil, document.Content[0], descriptions, true)
}
func getSectionedValueRows(valueRows []valueRow) sections {
var valueRowsSectionSorted sections
valueRowsSectionSorted.DefaultSection = section{
SectionName: "Other Values",
SectionItems: []valueRow{},
}
for _, row := range valueRows {
if row.Section == "" {
valueRowsSectionSorted.DefaultSection.SectionItems = append(valueRowsSectionSorted.DefaultSection.SectionItems, row)
continue
}
containsSection := false
for i, section := range valueRowsSectionSorted.Sections {
if section.SectionName == row.Section {
containsSection = true
valueRowsSectionSorted.Sections[i].SectionItems = append(valueRowsSectionSorted.Sections[i].SectionItems, row)
break
}
}
if !containsSection {
valueRowsSectionSorted.Sections = append(valueRowsSectionSorted.Sections, section{
SectionName: row.Section,
SectionItems: []valueRow{row},
})
}
}
return valueRowsSectionSorted
}
func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion string, dependencyValues []DependencyValues, skipVersionFooter bool) (chartTemplateData, error) {
valuesTableRows, err := getUnsortedValueRows(info.ChartValues, info.ChartValuesDescriptions)
if err != nil {
return chartTemplateData{}, err
}
if viper.GetBool("ignore-non-descriptions") {
valuesTableRows = removeRowsWithoutDescription(valuesTableRows)
}
if len(dependencyValues) > 0 {
seenGlobalKeys := make(map[string]bool)
for i, row := range valuesTableRows {
if strings.HasPrefix(row.Key, "global.") {
valuesTableRows[i].IsGlobal = true
seenGlobalKeys[row.Key] = true
}
}
for _, dep := range dependencyValues {
depValuesTableRows, err := getUnsortedValueRows(dep.ChartValues, dep.ChartValuesDescriptions)
if err != nil {
return chartTemplateData{}, err
}
for _, row := range depValuesTableRows {
if row.Key == "global" || strings.HasPrefix(row.Key, "global.") {
if seenGlobalKeys[row.Key] {
continue
}
row.IsGlobal = true
seenGlobalKeys[row.Key] = true
} else {
row.Key = dep.Prefix + "." + row.Key
}
row.Dependency = dep.Prefix
valuesTableRows = append(valuesTableRows, row)
}
}
}
sortValueRows(valuesTableRows)
valueRowsSectionSorted := getSectionedValueRows(valuesTableRows)
sortSectionedValueRows(valueRowsSectionSorted)
files, err := getFiles(info.ChartDirectory)
if err != nil {
return chartTemplateData{}, err
}
return chartTemplateData{
ChartDocumentationInfo: info,
HelmDocsVersion: helmDocsVersion,
Values: valuesTableRows,
Sections: valueRowsSectionSorted,
Files: files,
SkipVersionFooter: skipVersionFooter,
}, nil
}
func removeRowsWithoutDescription(valuesTableRows []valueRow) []valueRow {
var valuesTableRowsWithoutDescription []valueRow
for i := range valuesTableRows {
if valuesTableRows[i].AutoDescription != "" || valuesTableRows[i].Description != "" {
valuesTableRowsWithoutDescription = append(valuesTableRowsWithoutDescription, valuesTableRows[i])
}
}
return valuesTableRowsWithoutDescription
}