Skip to content

Commit b391ac4

Browse files
authored
pkg/scaffold/resource.go: robust validators and avoid panics (#815)
1 parent 8714eb0 commit b391ac4

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

pkg/scaffold/resource.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,17 @@ func (r *Resource) checkAndSetKinds() error {
116116
}
117117

118118
func (r *Resource) checkAndSetGroups() error {
119-
r.FullGroup = strings.Split(r.APIVersion, "/")[0]
120-
r.Group = strings.Split(r.FullGroup, ".")[0]
121-
122-
if len(r.Group) == 0 {
119+
fg := strings.Split(r.APIVersion, "/")
120+
if len(fg) < 2 || len(fg[0]) == 0 {
121+
return errors.New("full group cannot be empty")
122+
}
123+
g := strings.Split(fg[0], ".")
124+
if len(g) < 2 || len(g[0]) == 0 {
123125
return errors.New("group cannot be empty")
124126
}
127+
r.FullGroup = fg[0]
128+
r.Group = g[0]
129+
125130
if !ResourceGroupRegexp.MatchString(r.Group) {
126131
return errors.New("group should consist of lowercase alphabetical characters")
127132
}
@@ -130,7 +135,7 @@ func (r *Resource) checkAndSetGroups() error {
130135

131136
func (r *Resource) checkAndSetVersion() error {
132137
api := strings.Split(r.APIVersion, "/")
133-
if len(api) < 2 {
138+
if len(api) < 2 || len(api[1]) == 0 {
134139
return errors.New("version cannot be empty")
135140
}
136141
r.Version = api[1]

0 commit comments

Comments
 (0)