Skip to content

Commit 960afc0

Browse files
committed
add linter
Signed-off-by: Tim Ramlot <[email protected]>
1 parent b020c3a commit 960afc0

File tree

14 files changed

+904
-19
lines changed

14 files changed

+904
-19
lines changed

go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,34 @@ module github.com/cert-manager/helm-docgen
33
go 1.21
44

55
require (
6+
github.com/Masterminds/sprig/v3 v3.2.3
67
github.com/spf13/cobra v1.8.0
8+
github.com/stretchr/testify v1.8.1
79
gopkg.in/yaml.v3 v3.0.1
810
k8s.io/kube-openapi v0.0.0-20240105020646-a37d4de58910
911
)
1012

1113
require (
14+
github.com/Masterminds/goutils v1.1.1 // indirect
15+
github.com/Masterminds/semver/v3 v3.2.0 // indirect
16+
github.com/davecgh/go-spew v1.1.1 // indirect
1217
github.com/go-openapi/jsonpointer v0.19.6 // indirect
1318
github.com/go-openapi/jsonreference v0.20.1 // indirect
1419
github.com/go-openapi/swag v0.22.3 // indirect
1520
github.com/golang/protobuf v1.5.2 // indirect
1621
github.com/google/gnostic-models v0.6.8 // indirect
22+
github.com/google/uuid v1.1.2 // indirect
23+
github.com/huandu/xstrings v1.3.3 // indirect
24+
github.com/imdario/mergo v0.3.11 // indirect
1725
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1826
github.com/josharian/intern v1.0.0 // indirect
1927
github.com/mailru/easyjson v0.7.7 // indirect
28+
github.com/mitchellh/copystructure v1.0.0 // indirect
29+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
30+
github.com/pmezard/go-difflib v1.0.0 // indirect
31+
github.com/shopspring/decimal v1.2.0 // indirect
32+
github.com/spf13/cast v1.3.1 // indirect
2033
github.com/spf13/pflag v1.0.5 // indirect
34+
golang.org/x/crypto v0.3.0 // indirect
2135
google.golang.org/protobuf v1.27.1 // indirect
2236
)

go.sum

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
2+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
3+
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
4+
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
5+
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
6+
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
17
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
28
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
39
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -18,6 +24,13 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
1824
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1925
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
2026
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
27+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
28+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
29+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
30+
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
31+
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
32+
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
33+
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
2134
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
2235
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
2336
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
@@ -30,20 +43,59 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3043
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
3144
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
3245
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
46+
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
47+
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
48+
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
49+
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
3350
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3451
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3552
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
53+
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
54+
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
55+
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
56+
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
3657
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
3758
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
3859
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
3960
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
4061
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4162
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
4263
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
64+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
65+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
4366
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4467
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4568
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
4669
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
70+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
71+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
72+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
73+
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
74+
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
75+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
76+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
77+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
78+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
79+
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
80+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
81+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
82+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
83+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
84+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
85+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
86+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
87+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
88+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
89+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
90+
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
91+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
92+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
93+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
94+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
95+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
96+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
97+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
98+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4799
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
48100
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
49101
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
@@ -54,6 +106,10 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
54106
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
55107
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
56108
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
109+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
110+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
111+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
112+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
57113
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
58114
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
59115
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

linter/diff.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package linter
2+
3+
import (
4+
"sort"
5+
"strings"
6+
)
7+
8+
func DiffPaths(pathsA []string, pathsB []string) ([]string, []string) {
9+
sort.Strings(pathsA)
10+
sort.Strings(pathsB)
11+
12+
missingA := []string{}
13+
missingB := []string{}
14+
15+
prefix := "<NOT A PREFIX>"
16+
var i, j int
17+
for i < len(pathsA) && j < len(pathsB) {
18+
pathA := pathsA[i]
19+
pathB := pathsB[j]
20+
21+
pathAHasPrefix := strings.HasPrefix(pathA, prefix)
22+
pathBHasPrefix := strings.HasPrefix(pathB, prefix)
23+
24+
if pathA == pathB {
25+
prefix = pathA
26+
i++
27+
j++
28+
continue
29+
}
30+
31+
if pathBHasPrefix && pathAHasPrefix {
32+
prefix = "<NOT A PREFIX>"
33+
continue
34+
}
35+
36+
if pathA < pathB {
37+
if !pathAHasPrefix {
38+
missingB = append(missingB, pathA)
39+
}
40+
i++
41+
} else {
42+
if !pathBHasPrefix {
43+
missingA = append(missingA, pathB)
44+
}
45+
j++
46+
}
47+
}
48+
49+
for i < len(pathsA) {
50+
pathA := pathsA[i]
51+
52+
pathAHasPrefix := strings.HasPrefix(pathA, prefix)
53+
if !pathAHasPrefix {
54+
missingB = append(missingB, pathA)
55+
}
56+
57+
i++
58+
}
59+
60+
for j < len(pathsB) {
61+
pathB := pathsB[j]
62+
63+
pathBHasPrefix := strings.HasPrefix(pathB, prefix)
64+
if !pathBHasPrefix {
65+
missingA = append(missingA, pathB)
66+
}
67+
68+
j++
69+
}
70+
71+
return missingA, missingB
72+
}

linter/diff_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package linter
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestDiffPaths(t *testing.T) {
10+
type testcase struct {
11+
a []string
12+
b []string
13+
wantMissingA []string
14+
wantMissingB []string
15+
}
16+
17+
testcases := []testcase{
18+
{
19+
a: []string{".$", ".$.a", ".$.b", ".$.c"},
20+
b: []string{".$", ".$.a", ".$.b", ".$.c"},
21+
wantMissingA: []string{},
22+
wantMissingB: []string{},
23+
},
24+
{
25+
a: []string{".$", ".$.a", ".$.b", ".$.c"},
26+
b: []string{".$", ".$.a", ".$.b"},
27+
wantMissingA: []string{},
28+
wantMissingB: []string{".$.c"},
29+
},
30+
{
31+
a: []string{".$", ".$.a", ".$.b"},
32+
b: []string{".$", ".$.a", ".$.b", ".$.c"},
33+
wantMissingA: []string{".$.c"},
34+
wantMissingB: []string{},
35+
},
36+
{
37+
a: []string{".$", ".$.a", ".$.a.b"},
38+
b: []string{".$", ".$.a"},
39+
wantMissingA: []string{},
40+
wantMissingB: []string{},
41+
},
42+
{
43+
a: []string{".$", ".$.a", ".$.a.b"},
44+
b: []string{".$", ".$.a", ".$.a.c"},
45+
wantMissingA: []string{".$.a.c"},
46+
wantMissingB: []string{".$.a.b"},
47+
},
48+
{
49+
a: []string{".$", ".$.a", ".$.a.b.d"},
50+
b: []string{".$", ".$.a", ".$.a.c"},
51+
wantMissingA: []string{".$.a.c"},
52+
wantMissingB: []string{".$.a.b.d"},
53+
},
54+
{
55+
a: []string{".$", ".$.a", ".$.a.b.d"},
56+
b: []string{".$", ".$.b", ".$.c"},
57+
wantMissingA: []string{".$.b", ".$.c"},
58+
wantMissingB: []string{".$.a", ".$.a.b.d"},
59+
},
60+
{
61+
a: []string{".$", ".$.b", ".$.d.e.f"},
62+
b: []string{".$", ".$.a", ".$.b", ".$.b.c", ".$.d.g"},
63+
wantMissingA: []string{".$.a", ".$.d.g"},
64+
wantMissingB: []string{".$.d.e.f"},
65+
},
66+
{
67+
a: []string{".$", ".$.a", ".$.a.b", ".$.a.b.c3"},
68+
b: []string{".$", ".$.a", ".$.a.b", ".$.a.b.c1", ".$.a.b.c2", ".$.a.b.c3", ".$.a.b.c4"},
69+
wantMissingA: []string{".$.a.b.c1", ".$.a.b.c2", ".$.a.b.c4"},
70+
wantMissingB: []string{},
71+
},
72+
}
73+
74+
for _, tc := range testcases {
75+
diffA, diffB := DiffPaths(tc.a, tc.b)
76+
77+
require.EqualValues(t, tc.wantMissingA, diffA)
78+
require.EqualValues(t, tc.wantMissingB, diffB)
79+
}
80+
}

linter/linter.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package linter
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"slices"
7+
"strings"
8+
9+
"github.com/cert-manager/helm-docgen/linter/parsetemplates"
10+
"github.com/cert-manager/helm-docgen/parser"
11+
)
12+
13+
func Lint(
14+
templatesFolder string,
15+
exceptionsPath string,
16+
document *parser.Document,
17+
) error {
18+
templatePaths, err := parsetemplates.ListTemplatePaths(templatesFolder)
19+
if err != nil {
20+
return err
21+
}
22+
23+
valuePathsDict := map[string]struct{}{}
24+
for _, section := range document.Sections {
25+
for _, property := range section.Properties {
26+
valuePathsDict[property.Path.String()] = struct{}{}
27+
}
28+
}
29+
valuePathsDict = parsetemplates.MakeUniform(valuePathsDict)
30+
valuePaths := []string{}
31+
for path := range valuePathsDict {
32+
valuePaths = append(valuePaths, path)
33+
}
34+
35+
exceptionStrings := []string{}
36+
if exceptionsPath != "" {
37+
exceptionsPathsRaw, err := os.ReadFile(exceptionsPath)
38+
if err != nil {
39+
return err
40+
}
41+
42+
exceptionStrings = strings.Split(string(exceptionsPathsRaw), "\n")
43+
}
44+
45+
missingValues, missingTemplates := DiffPaths(valuePaths, templatePaths)
46+
47+
succeeded := true
48+
for _, missingValue := range missingValues {
49+
exceptionString := fmt.Sprintf("value missing from values.yaml: %s", missingValue)
50+
51+
if !slices.Contains(exceptionStrings, exceptionString) {
52+
fmt.Println(exceptionString)
53+
succeeded = false
54+
}
55+
}
56+
57+
for _, missingTemplate := range missingTemplates {
58+
exceptionString := fmt.Sprintf("value missing from templates: %s", missingTemplate)
59+
60+
if !slices.Contains(exceptionStrings, exceptionString) {
61+
fmt.Println(exceptionString)
62+
succeeded = false
63+
}
64+
}
65+
66+
if !succeeded {
67+
return fmt.Errorf("values.yaml and templates are not in sync")
68+
}
69+
70+
return nil
71+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package funcs_serdes
2+
3+
import (
4+
"text/template"
5+
6+
"github.com/Masterminds/sprig/v3"
7+
)
8+
9+
func FuncMap() template.FuncMap {
10+
f := sprig.TxtFuncMap()
11+
delete(f, "env")
12+
delete(f, "expandenv")
13+
14+
fmap := template.FuncMap{
15+
"toToml": func(v interface{}) string { return "" },
16+
"toYaml": func(v interface{}) string { return "" },
17+
"fromYaml": func(str string) map[string]interface{} { return map[string]interface{}{} },
18+
"fromYamlArray": func(str string) []interface{} { return nil },
19+
"toJson": func(v interface{}) string { return "" },
20+
"fromJson": func(str string) map[string]interface{} { return map[string]interface{}{} },
21+
"fromJsonArray": func(str string) []interface{} { return nil },
22+
"lookup": func(string, string, string, string) (map[string]interface{}, error) {
23+
return map[string]interface{}{}, nil
24+
},
25+
"include": func(name string, data interface{}) (string, error) { return "", nil },
26+
"required": func(warn string, val interface{}) (interface{}, error) { return nil, nil },
27+
"fail": func(msg string) (string, error) { return "", nil },
28+
"tpl": func(tpl string, parentContext interface{}) (string, error) { return "", nil },
29+
}
30+
31+
for key, value := range fmap {
32+
f[key] = value
33+
}
34+
35+
return f
36+
}

0 commit comments

Comments
 (0)