@@ -20,6 +20,7 @@ type valueRow struct {
20
20
Default string
21
21
AutoDescription string
22
22
Description string
23
+ Section string
23
24
Column int
24
25
LineNumber int
25
26
Dependency string
@@ -30,17 +31,52 @@ type chartTemplateData struct {
30
31
helm.ChartDocumentationInfo
31
32
HelmDocsVersion string
32
33
Values []valueRow
34
+ Sections []section
33
35
Files files
34
36
}
35
37
36
- func sortValueRows (valueRows []valueRow ) {
38
+ type section struct {
39
+ SectionName string
40
+ SectionItems []valueRow
41
+ }
42
+
43
+ func sortValueRows (valueRows []valueRow ) ([]valueRow , []section ) {
37
44
sortOrder := viper .GetString ("sort-values-order" )
38
45
39
46
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
40
47
log .Warnf ("Invalid sort order provided %s, defaulting to %s" , sortOrder , AlphaNumSortOrder )
41
48
sortOrder = AlphaNumSortOrder
42
49
}
43
50
51
+ var valueRowsSectionSorted []section
52
+ valueRowsSectionSorted = append (valueRowsSectionSorted , section {
53
+ SectionName : "General" ,
54
+ SectionItems : []valueRow {},
55
+ })
56
+
57
+ for _ , row := range valueRows {
58
+ if row .Section == "" {
59
+ valueRowsSectionSorted [0 ].SectionItems = append (valueRowsSectionSorted [0 ].SectionItems , row )
60
+ continue
61
+ }
62
+
63
+ containsSection := false
64
+ for i , section := range valueRowsSectionSorted {
65
+ if section .SectionName == row .Section {
66
+ containsSection = true
67
+ valueRowsSectionSorted [i ].SectionItems = append (valueRowsSectionSorted [i ].SectionItems , row )
68
+ break
69
+ }
70
+ }
71
+
72
+ if ! containsSection {
73
+ valueRowsSectionSorted = append (valueRowsSectionSorted , section {
74
+ SectionName : row .Section ,
75
+ SectionItems : []valueRow {row },
76
+ })
77
+ }
78
+ }
79
+
44
80
sort .Slice (valueRows , func (i , j int ) bool {
45
81
// Globals sort above non-globals.
46
82
if valueRows [i ].IsGlobal != valueRows [j ].IsGlobal {
@@ -73,6 +109,42 @@ func sortValueRows(valueRows []valueRow) {
73
109
panic ("cannot get here" )
74
110
}
75
111
})
112
+
113
+ for _ , section := range valueRowsSectionSorted {
114
+ sort .Slice (section .SectionItems , func (i , j int ) bool {
115
+ // Globals sort above non-globals.
116
+ if section .SectionItems [i ].IsGlobal != section .SectionItems [j ].IsGlobal {
117
+ return section .SectionItems [i ].IsGlobal
118
+ }
119
+
120
+ // Group by dependency for non-globals.
121
+ if ! section .SectionItems [i ].IsGlobal && ! section .SectionItems [j ].IsGlobal {
122
+ // Values for the main chart sort above values for dependencies.
123
+ if (section .SectionItems [i ].Dependency == "" ) != (section .SectionItems [j ].Dependency == "" ) {
124
+ return section .SectionItems [i ].Dependency == ""
125
+ }
126
+
127
+ // Group dependency values together.
128
+ if section .SectionItems [i ].Dependency != section .SectionItems [j ].Dependency {
129
+ return section .SectionItems [i ].Dependency < section .SectionItems [j ].Dependency
130
+ }
131
+ }
132
+
133
+ // Sort the remaining values within the same section.SectionItems using the configured sort order.
134
+ switch sortOrder {
135
+ case FileSortOrder :
136
+ if section .SectionItems [i ].LineNumber == section .SectionItems [j ].LineNumber {
137
+ return section .SectionItems [i ].Column < section .SectionItems [j ].Column
138
+ }
139
+ return section .SectionItems [i ].LineNumber < section .SectionItems [j ].LineNumber
140
+ case AlphaNumSortOrder :
141
+ return section .SectionItems [i ].Key < section .SectionItems [j ].Key
142
+ default :
143
+ panic ("cannot get here" )
144
+ }
145
+ })
146
+ }
147
+ return valueRows , valueRowsSectionSorted
76
148
}
77
149
78
150
func getUnsortedValueRows (document * yaml.Node , descriptions map [string ]helm.ChartValueDescription ) ([]valueRow , error ) {
@@ -134,7 +206,7 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
134
206
}
135
207
}
136
208
137
- sortValueRows (valuesTableRows )
209
+ valueRowsSorted , valueRowsSectionSorted := sortValueRows (valuesTableRows )
138
210
139
211
files , err := getFiles (info .ChartDirectory )
140
212
if err != nil {
@@ -144,7 +216,8 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
144
216
return chartTemplateData {
145
217
ChartDocumentationInfo : info ,
146
218
HelmDocsVersion : helmDocsVersion ,
147
- Values : valuesTableRows ,
219
+ Values : valueRowsSorted ,
220
+ Sections : valueRowsSectionSorted ,
148
221
Files : files ,
149
222
}, nil
150
223
}
0 commit comments