forked from norwoodj/helm-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
154 lines (129 loc) · 4.23 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
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
Column int
LineNumber int
Dependency string
IsGlobal bool
}
type chartTemplateData struct {
helm.ChartDocumentationInfo
HelmDocsVersion string
Values []valueRow
}
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
}
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[i].LineNumber
case AlphaNumSortOrder:
return valueRows[i].Key < valueRows[j].Key
default:
panic("cannot get here")
}
})
}
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 getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion string, dependencyValues []DependencyValues) (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)
return chartTemplateData{
ChartDocumentationInfo: info,
HelmDocsVersion: helmDocsVersion,
Values: valuesTableRows,
}, 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
}