@@ -51,6 +51,9 @@ type API struct {
51
51
52
52
// DoController indicates whether to scaffold controller files or not
53
53
DoController bool
54
+
55
+ // Force indicates that the resource should be created even if it already exists.
56
+ Force bool
54
57
}
55
58
56
59
// Validate validates whether API scaffold has correct bits to generate
@@ -63,6 +66,10 @@ func (api *API) Validate() error {
63
66
return err
64
67
}
65
68
69
+ if api .resourceExists () && ! api .Force {
70
+ return fmt .Errorf ("API resource already exists" )
71
+ }
72
+
66
73
return nil
67
74
}
68
75
@@ -205,12 +212,14 @@ func (api *API) scaffoldV2() error {
205
212
return fmt .Errorf ("error updating kustomization.yaml: %v" , err )
206
213
}
207
214
208
- // update scaffolded resource in project file
209
- api .project .Resources = append (api .project .Resources ,
210
- input.Resource {Group : r .Group , Version : r .Version , Kind : r .Kind })
211
- err = saveProjectFile ("PROJECT" , api .project )
212
- if err != nil {
213
- fmt .Printf ("error updating project file with resource information : %v \n " , err )
215
+ if ! api .resourceExists () {
216
+ // update scaffolded resource in project file
217
+ api .project .Resources = append (api .project .Resources ,
218
+ input.Resource {Group : r .Group , Version : r .Version , Kind : r .Kind })
219
+ err = saveProjectFile ("PROJECT" , api .project )
220
+ if err != nil {
221
+ fmt .Printf ("error updating project file with resource information : %v \n " , err )
222
+ }
214
223
}
215
224
216
225
} else {
@@ -270,3 +279,17 @@ func (api *API) validateResourceGroup(r *resource.Resource) error {
270
279
}
271
280
return nil
272
281
}
282
+
283
+ // resourceExists returns true if API resource is already tracked by the PROJECT file.
284
+ // Note that this works only for v2, since in v1 resources are not tracked by the PROJECT file.
285
+ func (api * API ) resourceExists () bool {
286
+ for _ , resource := range api .project .Resources {
287
+ if resource .Group == api .Resource .Group &&
288
+ resource .Version == api .Resource .Version &&
289
+ resource .Kind == api .Resource .Kind {
290
+ return true
291
+ }
292
+ }
293
+
294
+ return false
295
+ }
0 commit comments